New test from ISIS implemented - test_lookup_sent correct

This commit is contained in:
t
2025-01-10 21:41:44 +01:00
parent bb9c2d1d4c
commit 92581b3d4b
4 changed files with 49 additions and 24 deletions
+2 -2
View File
@@ -35,7 +35,7 @@ void extract_lookup_request(lookup_request* lr){
build_lookup_request(lr); build_lookup_request(lr);
} }
int call_network(lookup_request* lr, in_addr_t ip, int port){ int call_network(lookup_request* lr, in_addr_t ip, uint16_t port){
// int succ_sock; // int succ_sock;
struct addrinfo hints, *servinfo; // *p; struct addrinfo hints, *servinfo; // *p;
int rv; int rv;
@@ -66,7 +66,7 @@ int call_network(lookup_request* lr, in_addr_t ip, int port){
freeaddrinfo(servinfo); freeaddrinfo(servinfo);
lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr); lr->node_ip.s_addr = ntohl(addr.sin_addr.s_addr);
lr->node_port = ntohs(addr.sin_port); // lr->node_port = ntohs(addr.sin_port);
/* /*
for(p = servinfo; p != NULL; p = p->ai_next) { for(p = servinfo; p != NULL; p = p->ai_next) {
+1 -1
View File
@@ -21,7 +21,7 @@ void build_lookup_request(lookup_request*);
void extract_lookup_request(lookup_request*); void extract_lookup_request(lookup_request*);
int call_network(lookup_request*, in_addr_t, int); int call_network(lookup_request*, in_addr_t, uint16_t);
void clone_lookup_request(lookup_request* dest, lookup_request* source); void clone_lookup_request(lookup_request* dest, lookup_request* source);
+3 -3
View File
@@ -78,7 +78,7 @@ def test_listen(static_peer):
@pytest.mark.timeout(1) @pytest.mark.timeout(1)
@pytest.mark.parametrize("uri", ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) @pytest.mark.parametrize("uri", ['a', 'b', 'aa', 'ab', 'ac', 'ad', 'klm', 'xyz'])
def test_immediate_dht(static_peer, uri): def test_immediate_dht(static_peer, uri):
"""Test hashing of request (1.2) """Test hashing of request (1.2)
Run peer in minimal (non-trivial) DHT Run peer in minimal (non-trivial) DHT
@@ -121,7 +121,7 @@ def test_immediate_dht(static_peer, uri):
@pytest.mark.timeout(1) @pytest.mark.timeout(1)
@pytest.mark.parametrize("uri", ['a', 'b']) @pytest.mark.parametrize("uri", ['a', 'aa'])
def test_lookup_sent(static_peer, uri): def test_lookup_sent(static_peer, uri):
"""Test for lookup to correct peer (1.3) """Test for lookup to correct peer (1.3)
@@ -219,7 +219,7 @@ def test_lookup_forward(static_peer):
@pytest.mark.timeout(1) @pytest.mark.timeout(1)
@pytest.mark.parametrize("uri", ['a', 'b']) @pytest.mark.parametrize("uri", ['a', 'aa'])
def test_lookup_complete(static_peer, uri): def test_lookup_complete(static_peer, uri):
"""Test for correct lookup use (1.6) """Test for correct lookup use (1.6)
+43 -18
View File
@@ -54,14 +54,23 @@ neighbor check_neighborhood(uint16_t);
uint16_t hash_uri(const char *uri){ uint16_t hash_uri(const char *uri){
char *uri_ptr = (char*)uri; char *uri_ptr = (char*)uri;
char dynamic[] = "/dynamic/"; /*
char static_[] = "/static/"; char *it = (char *)uri;
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic); // shift uri_ptr to last '/'
elif(strstr(uri, static_)) uri_ptr += strlen(static_); while(*it)
elif(uri[0] == '/') uri_ptr++; {
if (*it == '/') uri_ptr = it++;
it++;
}
return pseudo_hash((unsigned char *)uri_ptr, strlen(uri_ptr)); // skip last '/'
if (*uri_ptr == '/') uri_ptr++;
if (!*uri_ptr) return -1;
*/
return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr));
} }
/** /**
@@ -87,6 +96,17 @@ void send_reply(int conn, struct request *request) {
// const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length); // const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
uint16_t resource_hash = hash_uri(request->uri); uint16_t resource_hash = hash_uri(request->uri);
fprintf(stderr, "Resource hash: %u\n", resource_hash);
if (resource_hash == -1)
{
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
goto direct_send;
}
neighbor responsible = check_neighborhood(resource_hash); neighbor responsible = check_neighborhood(resource_hash);
lookup_request *req = find_lookup(resource_hash); lookup_request *req = find_lookup(resource_hash);
@@ -108,25 +128,25 @@ void send_reply(int conn, struct request *request) {
} }
if (responsible == res) { if (responsible == res) {
sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); printf("Responsible - 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
printf("200\n");
} }
elif(responsible == res_303) elif(responsible == res_303)
{ {
int suc_id = _SUCC_ID; int suc_id = _SUCC_ID;
if (resource_hash <= suc_id || suc_id >= 0 && suc_id < _NODE_ID) if ((_NODE_ID < suc_id && resource_hash > _NODE_ID && resource_hash <= suc_id) || (_NODE_ID > suc_id && (resource_hash > _NODE_ID || resource_hash <= suc_id)))
{
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
else
{ {
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri); sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri);
offset = strlen(reply); offset = strlen(reply);
printf("303\n"); printf("303\n");
} }
else
{
printf("404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply);
}
} }
elif(responsible == not_res) elif(responsible == not_res)
{ {
@@ -167,6 +187,8 @@ void send_reply(int conn, struct request *request) {
offset = strlen(reply); offset = strlen(reply);
} }
direct_send: // ... do goto, yes you read that right ;)
// Send the reply back to the client // Send the reply back to the client
if (send(conn, reply, offset, 0) == -1) { if (send(conn, reply, offset, 0) == -1) {
perror("send"); perror("send");
@@ -455,10 +477,12 @@ static void handle_udp_request(int _sock){
{ {
const neighbor responsibility = check_neighborhood(lr.hash_id); const neighbor responsibility = check_neighborhood(lr.hash_id);
/* res is handles upfront by not res and suc responsibility check
if (responsibility == res) { if (responsibility == res) {
lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID); lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID);
} }
elif (responsibility == not_res) { */
if (responsibility == not_res) {
// is succ responsible // is succ responsible
if (lr.hash_id <= suc_id) if (lr.hash_id <= suc_id)
{ {
@@ -466,14 +490,15 @@ static void handle_udp_request(int _sock){
} }
else else
{ {
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID); // lookup_request_init(&backfire, 0, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
clone_lookup_request(&backfire, &lr);
callback_ip = &suc_ip_s.s_addr; callback_ip = &suc_ip_s.s_addr;
callback_port = _SUCC_PORT; callback_port = _SUCC_PORT;
} }
} }
} }
call_network(&backfire, *callback_ip, callback_port); //call_network(&backfire, *callback_ip, callback_port);
} }
elif (lr.message_type == 1) elif (lr.message_type == 1)
{ {