Compare commits
2 Commits
7e7124ee11
...
d7ca392e49
| Author | SHA1 | Date | |
|---|---|---|---|
| d7ca392e49 | |||
| 55c038fad8 |
1
.idea/dictionaries/project.xml
generated
1
.idea/dictionaries/project.xml
generated
@@ -2,6 +2,7 @@
|
|||||||
<dictionary name="project">
|
<dictionary name="project">
|
||||||
<words>
|
<words>
|
||||||
<w>mintime</w>
|
<w>mintime</w>
|
||||||
|
<w>wsock</w>
|
||||||
</words>
|
</words>
|
||||||
</dictionary>
|
</dictionary>
|
||||||
</component>
|
</component>
|
||||||
@@ -26,5 +26,9 @@ target_link_libraries(server01 PRIVATE wsock32 ws2_32 json-c::json-c)
|
|||||||
|
|
||||||
add_executable(server02 server02.c server02.h ${COMMON_SOURCES})
|
add_executable(server02 server02.c server02.h ${COMMON_SOURCES})
|
||||||
target_link_libraries(server02 wsock32 ws2_32)
|
target_link_libraries(server02 wsock32 ws2_32)
|
||||||
|
|
||||||
add_executable(server03 server03.c server03.h ${COMMON_SOURCES})
|
add_executable(server03 server03.c server03.h ${COMMON_SOURCES})
|
||||||
target_link_libraries(server03 wsock32 ws2_32)
|
target_link_libraries(server03 wsock32 ws2_32)
|
||||||
|
|
||||||
|
add_executable(server04 server04.c server04.h ${COMMON_SOURCES})
|
||||||
|
target_link_libraries(server04 wsock32 ws2_32)
|
||||||
18
data.c
18
data.c
@@ -24,6 +24,24 @@ SOCKET get_listen_socket() {
|
|||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SOCKET get_listen_socket_udp() {
|
||||||
|
WSADATA WSAData;
|
||||||
|
|
||||||
|
SOCKADDR_IN serverAddr;
|
||||||
|
|
||||||
|
WSAStartup(MAKEWORD(2,2), &WSAData);
|
||||||
|
SOCKET server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
|
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
|
||||||
|
listen(server, 0);
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
char_array_t *char_array_create(int capacity) {
|
char_array_t *char_array_create(int capacity) {
|
||||||
char_array_t *array = malloc(sizeof(char_array_t));
|
char_array_t *array = malloc(sizeof(char_array_t));
|
||||||
array->capacity = capacity;
|
array->capacity = capacity;
|
||||||
|
|||||||
1
data.h
1
data.h
@@ -36,3 +36,4 @@ void byte_array_shift_bytes(byte_array_t *array, size_t length);
|
|||||||
|
|
||||||
|
|
||||||
SOCKET get_listen_socket();
|
SOCKET get_listen_socket();
|
||||||
|
SOCKET get_listen_socket_udp();
|
||||||
125
server04.c
Normal file
125
server04.c
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
//
|
||||||
|
// Created by PeterDwyer on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "server04.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "data.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <inaddr.h>
|
||||||
|
|
||||||
|
#define BUF_SIZE 1024
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const SOCKET server = get_listen_socket_udp();
|
||||||
|
SOCKADDR_IN clientAddr;
|
||||||
|
SOCKET client;
|
||||||
|
int clientAddrSize = sizeof(clientAddr);
|
||||||
|
int connection_number = 1;
|
||||||
|
char *request = malloc(BUF_SIZE);
|
||||||
|
char buffer[BUF_SIZE] = {0};
|
||||||
|
int bytesReceived;
|
||||||
|
data_map_t *data = data_map_create(1024);
|
||||||
|
printf("Listening for incoming connections...\n");
|
||||||
|
while ((bytesReceived = recvfrom(server, buffer, sizeof(buffer), 0, (SOCKADDR *) &clientAddr, &clientAddrSize)) > 0) {
|
||||||
|
const char *equals = "=";
|
||||||
|
size_t pos;
|
||||||
|
// IN_ADDR addr = clientAddr.sin_addr;
|
||||||
|
// printf("{%d} Client ip: |%d.%d.%d.%d| \n", connection_number, addr.S_un.S_un_b.s_b1, addr.S_un.S_un_b.s_b2, addr.S_un.S_un_b.s_b3, addr.S_un.S_un_b.s_b4);
|
||||||
|
// printf("{%d} Client port: |%d| \n", connection_number, clientAddr.sin_port);
|
||||||
|
if ((pos = strcspn(buffer, equals)) >= 0) {
|
||||||
|
key_value_t *kv = malloc(sizeof(key_value_t));
|
||||||
|
strncpy_s(kv->key, BUF_SIZE, buffer, pos);
|
||||||
|
strncpy_s(kv->value, BUF_SIZE, buffer + sizeof(char)*(pos+1), sizeof(buffer) - pos - 1);
|
||||||
|
data_map_insert_kv(data, kv);
|
||||||
|
printf("{%d} Key: |%s| Value: |%s| \n", connection_number, kv->key, kv->value);
|
||||||
|
} else {
|
||||||
|
printf("{%d} Client sent: |%s| \n", connection_number, buffer);
|
||||||
|
}
|
||||||
|
sendto(server, buffer, bytesReceived, 0, (SOCKADDR *) &clientAddr, clientAddrSize);
|
||||||
|
strncpy_s(request, BUF_SIZE, buffer, bytesReceived);
|
||||||
|
printf("{%d} Client sent: |%s| \n", connection_number, request);
|
||||||
|
connection_number++;
|
||||||
|
}
|
||||||
|
data_map_free(data);
|
||||||
|
free(request);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
data_map_t *data_map_create(const int capacity) {
|
||||||
|
data_map_t *map = malloc(sizeof(data_map_t));
|
||||||
|
map->capacity = capacity;
|
||||||
|
map->count = 0;
|
||||||
|
map->data = calloc(capacity, sizeof(key_value_t));
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
void data_map_free(data_map_t *map) {
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
free(map->data[i].key);
|
||||||
|
free(map->data[i].value);
|
||||||
|
}
|
||||||
|
free(map->data);
|
||||||
|
free(map);
|
||||||
|
}
|
||||||
|
void data_map_insert_kv(data_map_t *map, key_value_t *kv) {
|
||||||
|
if (map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, kv->key) == 0) {
|
||||||
|
map->data[i].value = kv->value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const size_t new_size = map->count + 1;
|
||||||
|
if (new_size > map->capacity) {
|
||||||
|
map->capacity = map->capacity+1024;
|
||||||
|
key_value_t *new_array = realloc(map->data, map->capacity * sizeof(key_value_t));
|
||||||
|
map->data = new_array;
|
||||||
|
}
|
||||||
|
map->data[map->count].key = kv->key;
|
||||||
|
map->data[map->count].value = kv->value;
|
||||||
|
map->count = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void data_map_append(data_map_t *map, char *key, char *value) {
|
||||||
|
if (map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
const size_t new_size = map->count + 1;
|
||||||
|
if (new_size > map->capacity) {
|
||||||
|
map->capacity = map->capacity+1024;
|
||||||
|
key_value_t *new_array = realloc(map->data, map->capacity * sizeof(key_value_t));
|
||||||
|
map->data = new_array;
|
||||||
|
if (map->data == NULL) {
|
||||||
|
printf("Failed to allocate memory for array\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map->data[map->count].key = key;
|
||||||
|
map->data[map->count].value = value;
|
||||||
|
map->count = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void data_map_insert(data_map_t *map, char *key, char *value) {
|
||||||
|
if (map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, key) == 0) {
|
||||||
|
map->data[i].value = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data_map_append(map, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *data_map_get(data_map_t *map, const char *key) {
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, key) == 0) {
|
||||||
|
return map->data[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
28
server04.h
Normal file
28
server04.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by PeterDwyer on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SERVER04_H
|
||||||
|
#define SERVER04_H
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#endif //SERVER04_H
|
||||||
|
|
||||||
|
typedef struct KeyValue {
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
}key_value_t;
|
||||||
|
|
||||||
|
typedef struct DataMap {
|
||||||
|
size_t capacity;
|
||||||
|
size_t count;
|
||||||
|
key_value_t *data;
|
||||||
|
} data_map_t;
|
||||||
|
|
||||||
|
|
||||||
|
data_map_t *data_map_create(int capacity);
|
||||||
|
void data_map_free(data_map_t *map);
|
||||||
|
void data_map_append(data_map_t *map, char *key, char *value);
|
||||||
|
void data_map_insert(data_map_t *map, char *key, char *value);
|
||||||
|
void data_map_insert_kv(data_map_t *map, key_value_t *kv);
|
||||||
|
char *data_map_get(data_map_t *map, const char *key);
|
||||||
203
server04_database_client_test_suite.py
Normal file
203
server04_database_client_test_suite.py
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
from typing import Optional, Dict, List, Tuple
|
||||||
|
|
||||||
|
class DatabaseTestClient:
|
||||||
|
def __init__(self, host: str = 'localhost', server_port: int = 40000):
|
||||||
|
self.host = host
|
||||||
|
self.server_port = server_port
|
||||||
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
# Bind to any available interface (empty string) on an ephemeral port (0)
|
||||||
|
self.sock.bind(('', 0))
|
||||||
|
# Get the actual port assigned
|
||||||
|
self.client_port = self.sock.getsockname()[1]
|
||||||
|
self.sock.settimeout(1.0) # 1 second timeout for receives
|
||||||
|
print(f"Client bound to port {self.client_port}")
|
||||||
|
|
||||||
|
def send_insert(self, key: str, value: str) -> None:
|
||||||
|
"""Send an insert request."""
|
||||||
|
message = f"{key}={value}"
|
||||||
|
if len(message) >= 1000:
|
||||||
|
raise ValueError("Message too long")
|
||||||
|
self.sock.sendto(message.encode(), (self.host, self.server_port))
|
||||||
|
|
||||||
|
def send_retrieve(self, key: str) -> Optional[str]:
|
||||||
|
"""Send a retrieve request and return the response."""
|
||||||
|
if len(key) >= 1000:
|
||||||
|
raise ValueError("Key too long")
|
||||||
|
self.sock.sendto(key.encode(), (self.host, self.server_port))
|
||||||
|
|
||||||
|
try:
|
||||||
|
data, addr = self.sock.recvfrom(1000) # Changed from recvmsg to recvfrom
|
||||||
|
return data.decode()
|
||||||
|
except socket.timeout:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.sock.close()
|
||||||
|
|
||||||
|
def run_basic_tests(client: DatabaseTestClient) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run basic functionality tests."""
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# Test 1: Basic insert and retrieve
|
||||||
|
client.send_insert("test1", "value1")
|
||||||
|
time.sleep(0.1) # Give server time to process
|
||||||
|
response = client.send_retrieve("test1")
|
||||||
|
results.append(("Basic insert/retrieve",
|
||||||
|
response == "test1=value1",
|
||||||
|
f"Expected 'test1=value1', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 2: Update existing key
|
||||||
|
client.send_insert("test1", "value2")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("test1")
|
||||||
|
results.append(("Update existing key",
|
||||||
|
response == "test1=value2",
|
||||||
|
f"Expected 'test1=value2', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 3: Empty key
|
||||||
|
client.send_insert("", "empty_key_value")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("")
|
||||||
|
results.append(("Empty key",
|
||||||
|
response == "=empty_key_value",
|
||||||
|
f"Expected '=empty_key_value', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 4: Empty value
|
||||||
|
client.send_insert("empty_value", "")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("empty_value")
|
||||||
|
results.append(("Empty value",
|
||||||
|
response == "empty_value=",
|
||||||
|
f"Expected 'empty_value=', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 5: Version check
|
||||||
|
response = client.send_retrieve("version")
|
||||||
|
results.append(("Version check",
|
||||||
|
response is not None and response.startswith("version=") and len(response) > 8,
|
||||||
|
f"Version response invalid: '{response}'"))
|
||||||
|
|
||||||
|
# Test 6: Version modification attempt
|
||||||
|
client.send_insert("version", "HACKED")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("version")
|
||||||
|
results.append(("Version modification prevention",
|
||||||
|
response is not None and not response.endswith("HACKED"),
|
||||||
|
f"Version should not be modifiable, got: '{response}'"))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def run_edge_case_tests(client: DatabaseTestClient) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run edge case tests."""
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# Test 1: Key with multiple equals signs
|
||||||
|
client.send_insert("multi=equals", "value=with=equals")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("multi=equals")
|
||||||
|
results.append(("Multiple equals in key",
|
||||||
|
response is None or not response.startswith("multi=equals"),
|
||||||
|
"Key with equals sign should not be stored"))
|
||||||
|
|
||||||
|
# Test 2: Value with equals signs
|
||||||
|
client.send_insert("test2", "value=with=equals")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("test2")
|
||||||
|
results.append(("Equals in value",
|
||||||
|
response == "test2=value=with=equals",
|
||||||
|
f"Expected 'test2=value=with=equals', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 3: Non-existent key
|
||||||
|
response = client.send_retrieve("nonexistent")
|
||||||
|
results.append(("Non-existent key",
|
||||||
|
response is None or response == "nonexistent=",
|
||||||
|
f"Expected None or 'nonexistent=', got '{response}'"))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def run_concurrent_tests(num_clients: int = 5) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run concurrent access tests."""
|
||||||
|
results = []
|
||||||
|
clients = [DatabaseTestClient() for _ in range(num_clients)]
|
||||||
|
|
||||||
|
def concurrent_ops(client_id: int):
|
||||||
|
client = clients[client_id]
|
||||||
|
key = f"concurrent_{client_id}"
|
||||||
|
for i in range(10):
|
||||||
|
value = f"value_{i}"
|
||||||
|
client.send_insert(key, value)
|
||||||
|
time.sleep(0.05)
|
||||||
|
response = client.send_retrieve(key)
|
||||||
|
if response != f"{key}={value}":
|
||||||
|
results.append((f"Concurrent client {client_id}",
|
||||||
|
False,
|
||||||
|
f"Expected '{key}={value}', got '{response}'"))
|
||||||
|
return
|
||||||
|
results.append((f"Concurrent client {client_id}",
|
||||||
|
True,
|
||||||
|
"All operations successful"))
|
||||||
|
|
||||||
|
threads = [threading.Thread(target=concurrent_ops, args=(i,))
|
||||||
|
for i in range(num_clients)]
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
for client in clients:
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# MESSAGE = "Hello, UDP!"
|
||||||
|
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
|
||||||
|
# sock.sendto(bytes(MESSAGE, "utf-8"), ("localhost", 40000))
|
||||||
|
# quit()
|
||||||
|
# Allow command line arguments for host and port
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser(description='Test UDP Database Server')
|
||||||
|
parser.add_argument('--host', default='localhost', help='Server host')
|
||||||
|
parser.add_argument('--port', type=int, default=40000, help='Server port')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print(f"Starting Unusual Database Program Tests on {args.host}:{args.port}\n")
|
||||||
|
|
||||||
|
client = DatabaseTestClient(args.host, args.port)
|
||||||
|
# ... rest of the main function remains the same ...
|
||||||
|
|
||||||
|
print("Running basic functionality tests...")
|
||||||
|
basic_results = run_basic_tests(client)
|
||||||
|
|
||||||
|
print("\nRunning edge case tests...")
|
||||||
|
edge_results = run_edge_case_tests(client)
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
print("\nRunning concurrent access tests...")
|
||||||
|
concurrent_results = run_concurrent_tests()
|
||||||
|
|
||||||
|
# Print results
|
||||||
|
all_results = basic_results + edge_results + concurrent_results
|
||||||
|
passed = sum(1 for _, success, _ in all_results if success)
|
||||||
|
total = len(all_results)
|
||||||
|
|
||||||
|
print("\nTest Results:")
|
||||||
|
print("=" * 60)
|
||||||
|
for test_name, success, message in all_results:
|
||||||
|
status = "✓" if success else "✗"
|
||||||
|
print(f"{status} {test_name}")
|
||||||
|
if not success:
|
||||||
|
print(f" {message}")
|
||||||
|
|
||||||
|
print("\nSummary:")
|
||||||
|
print(f"Passed: {passed}/{total} tests")
|
||||||
|
print(f"Success rate: {(passed/total)*100:.1f}%")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user