-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
129 lines (110 loc) · 4.57 KB
/
test_server.py
File metadata and controls
129 lines (110 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from server import (
get_config_path,
load_algorithms,
read_config,
check_algorithm_string,
search_in_file,
start_server,
handle_client
)
import sys
import pytest
import os
import socket
import ssl
import json
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__),
'..')))
# Constants for tests
# Example valid algorithm;
# ensure it exists in your algorithm list
VALID_ALGORITHM = "default"
# Example invalid algorithm for testing purposes
INVALID_ALGORITHM = "invalid_algo"
# Fixture to point to the real test_200k.txt file
@pytest.fixture
def test_200k_file():
"""Fixture to provide the path to the actual test_200k.txt file."""
test_file_path = os.path.join(os.path.dirname(__file__),
'test_200k.txt')
# Ensure the test file exists
assert os.path.exists(test_file_path), f"File not found: {test_file_path}"
return test_file_path
@pytest.fixture
def config_file(tmp_path, test_200k_file):
"""Fixture to create a temporary config file."""
config_file_path = tmp_path / 'config.ini'
config_file_path.write_text(
f"[default]\n"
f"linuxpath={test_200k_file}\n" # Path to the test file
f"use_ssl=true\n" # Enable SSL for testing
f"ssl_certfile=/path/to/certfile\n" # Mock SSL cert file path
f"ssl_keyfile=/path/to/keyfile\n" # Mock SSL key file path
# Mock PSK key file path
f"ssl_psk_keyfile=/path/to/pskfile\n"
# Configuration for rereading on query
f"reread_on_query_config=true\n"
# Path for metrics JSON
f"metrics_json_path=/path/to/metrics.json\n"
# Path for algorithms list JSON
f"algorithms_list_json=/path/to/algorithms.json\n"
)
return str(config_file_path)
def test_get_config_path():
"""Test whether the configuration file path is constructed correctly."""
filename = 'config.ini'
config_path = get_config_path(filename) # Get the path for the config file
# Check if it ends with the correct filename
assert config_path.endswith(filename)
assert os.path.isabs(config_path) # Ensure the path is absolute
def test_load_algorithms(test_200k_file, tmp_path):
"""Test loading algorithms from the configuration."""
config_file = tmp_path / 'config.ini'
config_file.write_text(
# Set algorithms list JSON
f"[Settings]\nalgorithms_list_json=algorithms.json\n"
)
# Create a mock algorithms file
algorithms_file = tmp_path / 'algorithms.json'
# Write valid algorithms to JSON
algorithms_file.write_text(json.dumps({"algorithms": [VALID_ALGORITHM]}))
# Load algorithms from the mock file
algorithms = load_algorithms()
# Ensure the valid algorithm is recognized
assert VALID_ALGORITHM in algorithms
# Ensure the invalid algorithm is not recognized
assert INVALID_ALGORITHM not in algorithms
def test_check_algorithm_string():
"""Test if the provided algorithm string
is recognized as valid or invalid."""
# Ensure algorithms are loaded for testing
load_algorithms() # Ensure this updates the ALGORITHMS_LIST
assert check_algorithm_string(
VALID_ALGORITHM) is True # Check valid algorithm
assert check_algorithm_string(
INVALID_ALGORITHM) is False # Check invalid algorithm
def test_read_config(config_file, test_200k_file):
"""Test the function that reads from the config file
and returns settings as a dictionary."""
settings = read_config(config_file) # Read settings from the config file
# Check the file path in settings
assert settings['file_path'] == test_200k_file
assert settings['use_ssl'] is True # Verify SSL usage is enabled
def test_search_in_file_found(test_200k_file):
"""Test the search_in_file function
to check if it finds an existing string."""
query = {'query_string': '9;0;1;11;0;8;5;0;',
'algorithm': VALID_ALGORITHM} # Define the search query
search_result = search_in_file(test_200k_file, query) # Perform the search
# Check if the result matches the expected output
assert search_result
def test_search_in_file_not_found(test_200k_file):
"""Test the search_in_file function
to ensure it returns False for a missing string.
"""
query = {'query_string': 'nonexistent_string',
'algorithm': VALID_ALGORITHM} # Define a non-existing query
result = search_in_file(test_200k_file, query) # Perform the search
assert result is False # Ensure the search result is False