Skip to content

Commit

Permalink
Added failing serialize send peer list tests
Browse files Browse the repository at this point in the history
Signed-off-by: leonard.kosta <[email protected]>
  • Loading branch information
kostaleonard committed Dec 21, 2024
1 parent c7b6ccf commit c652fc2
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include <stdint.h>
#include "include/return_codes.h"

#define COMMAND_PREFIX "LEO:"
#define COMMAND_PREFIX "LEO\0"
#define COMMAND_PREFIX_LEN 4
#define COMMAND_ERROR_MESSAGE_LEN 256
#define COMMAND_HEADER_INITIALIZER {{'L', 'E', 'O', ':'}, 0, 0}
#define COMMAND_HEADER_INITIALIZER {{'L', 'E', 'O', '\0'}, 0, 0}
// TODO remove COMMAND_REGISTER_PEER_LEN? We can't tell the length for all of the commands in advance, so this could cause confusion
#define COMMAND_REGISTER_PEER_LEN (sizeof(command_register_peer_t) - sizeof(command_header_t))

Expand Down Expand Up @@ -86,7 +86,9 @@ return_code_t command_header_serialize(
* @return return_code_t A return code indicating success or failure.
*/
return_code_t command_header_deserialize(
command_header_t *command_header, unsigned char *buffer, uint64_t buffer_size);
command_header_t *command_header,
unsigned char *buffer,
uint64_t buffer_size);

// TODO docstring
// TODO note that these serialization functions will set the command_len field in the struct because it may not be known in advance
Expand Down
3 changes: 3 additions & 0 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ return_code_t command_register_peer_deserialize(
return_code = FAILURE_BUFFER_TOO_SMALL;
goto end;
}
// TODO check for invalid deserialized command?
deserialized_command_register_peer.sin6_family = ntohs(
*(uint16_t *)next_spot_in_buffer);
next_spot_in_buffer += sizeof(uint16_t);
Expand Down Expand Up @@ -218,6 +219,8 @@ return_code_t command_send_peer_list_serialize(
command_send_peer_list_t *command_send_peer_list,
unsigned char **buffer,
uint64_t *buffer_size) {
// TODO serialize list
// TODO once we know the size, set command_send_peer_list->header->command_len to the right length, serialize the header, and combine the buffers
return FAILURE_INVALID_INPUT;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ int main(int argc, char **argv) {
test_command_register_peer_deserialize_fails_on_invalid_prefix),
cmocka_unit_test(
test_command_register_peer_deserialize_fails_on_invalid_input),
cmocka_unit_test(
test_command_send_peer_list_serialize_fails_on_invalid_input),
cmocka_unit_test(
test_command_send_peer_list_serialize_fails_on_invalid_prefix),
cmocka_unit_test(
test_command_send_peer_list_serialize_fails_on_invalid_command),
cmocka_unit_test(
test_command_send_peer_list_serialize_creates_nonempty_buffer),
cmocka_unit_test(
test_command_send_peer_list_deserialize_reconstructs_command),
cmocka_unit_test(
test_command_send_peer_list_deserialize_fails_on_read_past_buffer),
cmocka_unit_test(
test_command_send_peer_list_deserialize_fails_on_invalid_prefix),
cmocka_unit_test(
test_command_send_peer_list_deserialize_fails_on_invalid_input),
};
return_code = cmocka_run_group_tests(tests, NULL, NULL);
end:
Expand Down
221 changes: 221 additions & 0 deletions tests/test_networking.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdbool.h>
#include "include/linked_list.h"
#include "include/networking.h"
#include "include/peer_discovery.h"
#include "tests/test_networking.h"

void test_command_header_serialize_fails_on_invalid_input() {
Expand Down Expand Up @@ -305,3 +307,222 @@ void test_command_register_peer_deserialize_fails_on_invalid_input() {
assert_true(FAILURE_INVALID_INPUT == return_code);
free(buffer);
}

void test_command_send_peer_list_serialize_fails_on_invalid_input() {
command_header_t command_header = COMMAND_HEADER_INITIALIZER;
command_header.command = COMMAND_SEND_PEER_LIST;
command_header.command_len = 0;
command_send_peer_list_t command_send_peer_list = {0};
command_send_peer_list.header = command_header;
linked_list_t *peer_info_list = NULL;
return_code_t return_code = linked_list_create(
&peer_info_list, free, compare_peer_info_t);
assert_true(SUCCESS == return_code);
peer_info_t *peer1 = calloc(1, sizeof(peer_info_t));
peer1->listen_addr.sin6_family = AF_INET6;
peer1->listen_addr.sin6_port = 12345;
peer1->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer1->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer1->listen_addr.sin6_scope_id = 0;
peer1->last_connected = 100;
peer_info_t *peer2 = calloc(1, sizeof(peer_info_t));
peer2->listen_addr.sin6_family = AF_INET6;
peer2->listen_addr.sin6_port = 23456;
peer2->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[0] = 0xfe;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[1] = 0x80;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer2->listen_addr.sin6_scope_id = 0;
peer2->last_connected = 200;
return_code = linked_list_prepend(peer_info_list, peer2);
assert_true(SUCCESS == return_code);
return_code = linked_list_prepend(peer_info_list, peer1);
assert_true(SUCCESS == return_code);
unsigned char *peer_list_buffer = NULL;
uint64_t peer_list_buffer_size = 0;
return_code = peer_info_list_serialize(
peer_info_list, &peer_list_buffer, &peer_list_buffer_size);
assert_true(SUCCESS == return_code);
command_send_peer_list.peer_list_data = peer_list_buffer;
command_send_peer_list.peer_list_data_len = peer_list_buffer_size;
unsigned char *buffer = NULL;
uint64_t buffer_size = 0;
return_code = command_send_peer_list_serialize(
NULL, &buffer, &buffer_size);
assert_true(FAILURE_INVALID_INPUT == return_code);
return_code = command_send_peer_list_serialize(
&command_send_peer_list, NULL, &buffer_size);
assert_true(FAILURE_INVALID_INPUT == return_code);
return_code = command_send_peer_list_serialize(
&command_send_peer_list, &buffer, NULL);
assert_true(FAILURE_INVALID_INPUT == return_code);
free(peer_list_buffer);
linked_list_destroy(peer_info_list);
}

void test_command_send_peer_list_serialize_fails_on_invalid_prefix() {
command_header_t command_header = COMMAND_HEADER_INITIALIZER;
command_header.command_prefix[2] = 'A';
command_header.command = COMMAND_SEND_PEER_LIST;
command_header.command_len = 0;
command_send_peer_list_t command_send_peer_list = {0};
command_send_peer_list.header = command_header;
linked_list_t *peer_info_list = NULL;
return_code_t return_code = linked_list_create(
&peer_info_list, free, compare_peer_info_t);
assert_true(SUCCESS == return_code);
peer_info_t *peer1 = calloc(1, sizeof(peer_info_t));
peer1->listen_addr.sin6_family = AF_INET6;
peer1->listen_addr.sin6_port = 12345;
peer1->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer1->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer1->listen_addr.sin6_scope_id = 0;
peer1->last_connected = 100;
peer_info_t *peer2 = calloc(1, sizeof(peer_info_t));
peer2->listen_addr.sin6_family = AF_INET6;
peer2->listen_addr.sin6_port = 23456;
peer2->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[0] = 0xfe;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[1] = 0x80;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer2->listen_addr.sin6_scope_id = 0;
peer2->last_connected = 200;
return_code = linked_list_prepend(peer_info_list, peer2);
assert_true(SUCCESS == return_code);
return_code = linked_list_prepend(peer_info_list, peer1);
assert_true(SUCCESS == return_code);
unsigned char *peer_list_buffer = NULL;
uint64_t peer_list_buffer_size = 0;
return_code = peer_info_list_serialize(
peer_info_list, &peer_list_buffer, &peer_list_buffer_size);
assert_true(SUCCESS == return_code);
command_send_peer_list.peer_list_data = peer_list_buffer;
command_send_peer_list.peer_list_data_len = peer_list_buffer_size;
unsigned char *buffer = NULL;
uint64_t buffer_size = 0;
return_code = command_send_peer_list_serialize(
&command_send_peer_list, &buffer, &buffer_size);
assert_true(FAILURE_INVALID_COMMAND_PREFIX == return_code);
free(buffer);
linked_list_destroy(peer_info_list);
}

void test_command_send_peer_list_serialize_fails_on_invalid_command() {
command_header_t command_header = COMMAND_HEADER_INITIALIZER;
command_header.command = COMMAND_OK;
command_header.command_len = 0;
command_send_peer_list_t command_send_peer_list = {0};
command_send_peer_list.header = command_header;
linked_list_t *peer_info_list = NULL;
return_code_t return_code = linked_list_create(
&peer_info_list, free, compare_peer_info_t);
assert_true(SUCCESS == return_code);
peer_info_t *peer1 = calloc(1, sizeof(peer_info_t));
peer1->listen_addr.sin6_family = AF_INET6;
peer1->listen_addr.sin6_port = 12345;
peer1->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer1->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer1->listen_addr.sin6_scope_id = 0;
peer1->last_connected = 100;
peer_info_t *peer2 = calloc(1, sizeof(peer_info_t));
peer2->listen_addr.sin6_family = AF_INET6;
peer2->listen_addr.sin6_port = 23456;
peer2->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[0] = 0xfe;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[1] = 0x80;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer2->listen_addr.sin6_scope_id = 0;
peer2->last_connected = 200;
return_code = linked_list_prepend(peer_info_list, peer2);
assert_true(SUCCESS == return_code);
return_code = linked_list_prepend(peer_info_list, peer1);
assert_true(SUCCESS == return_code);
unsigned char *peer_list_buffer = NULL;
uint64_t peer_list_buffer_size = 0;
return_code = peer_info_list_serialize(
peer_info_list, &peer_list_buffer, &peer_list_buffer_size);
assert_true(SUCCESS == return_code);
command_send_peer_list.peer_list_data = peer_list_buffer;
command_send_peer_list.peer_list_data_len = peer_list_buffer_size;
unsigned char *buffer = NULL;
uint64_t buffer_size = 0;
return_code = command_send_peer_list_serialize(
&command_send_peer_list, &buffer, &buffer_size);
assert_true(FAILURE_INVALID_COMMAND == return_code);
free(buffer);
linked_list_destroy(peer_info_list);
}

void test_command_send_peer_list_serialize_creates_nonempty_buffer() {
command_header_t command_header = COMMAND_HEADER_INITIALIZER;
command_header.command = COMMAND_SEND_PEER_LIST;
command_header.command_len = 0;
command_send_peer_list_t command_send_peer_list = {0};
command_send_peer_list.header = command_header;
linked_list_t *peer_info_list = NULL;
return_code_t return_code = linked_list_create(
&peer_info_list, free, compare_peer_info_t);
assert_true(SUCCESS == return_code);
peer_info_t *peer1 = calloc(1, sizeof(peer_info_t));
peer1->listen_addr.sin6_family = AF_INET6;
peer1->listen_addr.sin6_port = 12345;
peer1->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer1->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer1->listen_addr.sin6_scope_id = 0;
peer1->last_connected = 100;
peer_info_t *peer2 = calloc(1, sizeof(peer_info_t));
peer2->listen_addr.sin6_family = AF_INET6;
peer2->listen_addr.sin6_port = 23456;
peer2->listen_addr.sin6_flowinfo = 0;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[0] = 0xfe;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[1] = 0x80;
((unsigned char *)(&peer2->listen_addr.sin6_addr))[
sizeof(IN6_ADDR) - 1] = 1;
peer2->listen_addr.sin6_scope_id = 0;
peer2->last_connected = 200;
return_code = linked_list_prepend(peer_info_list, peer2);
assert_true(SUCCESS == return_code);
return_code = linked_list_prepend(peer_info_list, peer1);
assert_true(SUCCESS == return_code);
unsigned char *peer_list_buffer = NULL;
uint64_t peer_list_buffer_size = 0;
return_code = peer_info_list_serialize(
peer_info_list, &peer_list_buffer, &peer_list_buffer_size);
assert_true(SUCCESS == return_code);
command_send_peer_list.peer_list_data = peer_list_buffer;
command_send_peer_list.peer_list_data_len = peer_list_buffer_size;
unsigned char *buffer = NULL;
uint64_t buffer_size = 0;
return_code = command_send_peer_list_serialize(
&command_send_peer_list, &buffer, &buffer_size);
assert_true(SUCCESS == return_code);
unsigned char *empty_buffer = calloc(buffer_size, 1);
assert_true(0 != memcmp(buffer, empty_buffer, buffer_size));
free(buffer);
free(empty_buffer);
free(peer_list_buffer);
linked_list_destroy(peer_info_list);
}

void test_command_send_peer_list_deserialize_reconstructs_command() {
assert_true(false);
}

void test_command_send_peer_list_deserialize_fails_on_read_past_buffer() {
assert_true(false);
}

void test_command_send_peer_list_deserialize_fails_on_invalid_prefix() {
assert_true(false);
}

void test_command_send_peer_list_deserialize_fails_on_invalid_input() {
assert_true(false);
}
16 changes: 16 additions & 0 deletions tests/test_networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ void test_command_register_peer_deserialize_fails_on_invalid_prefix();

void test_command_register_peer_deserialize_fails_on_invalid_input();

void test_command_send_peer_list_serialize_fails_on_invalid_input();

void test_command_send_peer_list_serialize_fails_on_invalid_prefix();

void test_command_send_peer_list_serialize_fails_on_invalid_command();

void test_command_send_peer_list_serialize_creates_nonempty_buffer();

void test_command_send_peer_list_deserialize_reconstructs_command();

void test_command_send_peer_list_deserialize_fails_on_read_past_buffer();

void test_command_send_peer_list_deserialize_fails_on_invalid_prefix();

void test_command_send_peer_list_deserialize_fails_on_invalid_input();

#endif // TESTS_TEST_NETWORKING_H_

0 comments on commit c652fc2

Please sign in to comment.