From 4555cdfdf1c335bf2cf42295ea3f005b8d7da916 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 20 Feb 2017 09:31:59 -0500 Subject: [PATCH] Fixed bugs around libp2p_message --- record/message.c | 26 +++++++++++----- test/test_record.h | 73 ++++++++++++++++++++++++++++++++++++++++----- test/testit.c | 6 ++-- utils/linked_list.c | 8 +++-- 4 files changed, 94 insertions(+), 19 deletions(-) diff --git a/record/message.c b/record/message.c index 65e4438..59c4146 100644 --- a/record/message.c +++ b/record/message.c @@ -24,12 +24,13 @@ struct Libp2pPeer* libp2p_message_peer_new() { void libp2p_message_peer_free(struct Libp2pPeer* in) { if (in != NULL) { if (in->id != NULL) - free(in); + free(in->id); // free the memory in the linked list struct Libp2pLinkedList* current = in->addr_head; while (current != NULL) { struct Libp2pLinkedList* temp = current->next; multiaddress_free((struct MultiAddress*)current->item); + free(current); current = temp; } free(in); @@ -113,10 +114,9 @@ int libp2p_message_peer_protobuf_decode(unsigned char* in, size_t in_size, struc goto exit; pos += bytes_read; // now turn it into multiaddress - struct Libp2pLinkedList* current = (struct Libp2pLinkedList*)malloc(sizeof(struct Libp2pLinkedList)); + struct Libp2pLinkedList* current = libp2p_utils_linked_list_new(); if (current == NULL) goto exit; - current->next = NULL; current->item = (void*)multiaddress_new_from_bytes(buffer, buffer_size); free(buffer); buffer = NULL; @@ -169,10 +169,14 @@ struct Libp2pMessage* libp2p_message_new() { } void libp2p_message_free(struct Libp2pMessage* in) { + // a linked list of peer structs struct Libp2pLinkedList* current = in->closer_peer_head; while (current != NULL) { struct Libp2pLinkedList* next = current->next; - libp2p_message_peer_free((struct Libp2pPeer*)next->item); + struct Libp2pPeer* temp = (struct Libp2pPeer*)current->item; + libp2p_message_peer_free(temp); + current->item = NULL; + libp2p_utils_linked_list_free(current); current = next; } if (in->key != NULL) @@ -285,20 +289,24 @@ int libp2p_message_protobuf_decode(unsigned char* in, size_t in_size, struct Lib size_t pos = 0; int retVal = 0; size_t buffer_size = 0; + size_t bytes_read = 0; + int field_no = 0; + enum WireType field_type = 0; unsigned char* buffer = NULL; struct Libp2pLinkedList* current_item = NULL; struct Libp2pLinkedList* last_closer = NULL; struct Libp2pLinkedList* last_provider = NULL; + struct Libp2pMessage* ptr = NULL; if ( (*out = (struct Libp2pMessage*)malloc(sizeof(struct Libp2pMessage))) == NULL) goto exit; - struct Libp2pMessage* ptr = *out; + ptr = *out; while(pos < in_size) { - size_t bytes_read = 0; - int field_no; - enum WireType field_type; + bytes_read = 0; + field_no = 0; + field_type = 0; if (protobuf_decode_field_and_type(&in[pos], in_size, &field_no, &field_type, &bytes_read) == 0) { goto exit; } @@ -323,6 +331,8 @@ int libp2p_message_protobuf_decode(unsigned char* in, size_t in_size, struct Lib buffer = NULL; goto exit; } + free(buffer); + buffer = NULL; pos += bytes_read; break; case (8): // closer peers diff --git a/test/test_record.h b/test/test_record.h index f4466bc..68c5c61 100644 --- a/test/test_record.h +++ b/test/test_record.h @@ -14,19 +14,25 @@ int setval(char** result, size_t* result_size, char* in) { return 1; } -int test_record_protobuf() { +struct Libp2pRecord* test_record_create() { struct Libp2pRecord* record = libp2p_record_new(); + if (record != NULL) { + setval(&record->key, &record->key_size, "Key"); + setval((char**)&record->value, &record->value_size, "Value"); + setval(&record->author, &record->author_size, "Author"); + setval((char**)&record->signature, &record->signature_size, "Signature"); + setval(&record->time_received, &record->time_received_size, "Time_Received"); + } + return record; +} + +int test_record_protobuf() { + struct Libp2pRecord* record = test_record_create(); struct Libp2pRecord* results = NULL; size_t protobuf_size = 0; char* protobuf = NULL; int retVal = 0; - setval(&record->key, &record->key_size, "Key"); - setval((char**)&record->value, &record->value_size, "Value"); - setval(&record->author, &record->author_size, "Author"); - setval((char**)&record->signature, &record->signature_size, "Signature"); - setval(&record->time_received, &record->time_received_size, "Time_Received"); - // protobuf, unprotobuf protobuf_size = libp2p_record_protobuf_encode_size(record); protobuf = (unsigned char*)malloc(protobuf_size); @@ -193,3 +199,56 @@ int test_record_peer_protobuf() { libp2p_message_peer_free(result); return retVal; } + +int test_record_message_protobuf() { + int retVal = 0; + struct Libp2pPeer* closer_peer = NULL; + struct Libp2pMessage* message = NULL; + struct Libp2pMessage* result = NULL; + char* buffer = NULL; + size_t buffer_len = 0; + + // construct message + closer_peer = libp2p_message_peer_new(); + closer_peer->connection_type = CONNECTION_TYPE_CAN_CONNECT; + closer_peer->id = malloc(7); + strcpy(closer_peer->id, "ABC123"); + closer_peer->id_size = strlen(closer_peer->id); + closer_peer->addr_head = libp2p_utils_linked_list_new(); + closer_peer->addr_head->item = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/4001"); + + message = libp2p_message_new(); + message->closer_peer_head = libp2p_utils_linked_list_new(); + message->closer_peer_head->item = closer_peer; + message->cluster_level_raw = 1; + message->key = malloc(7); + strcpy(message->key, "ABC123"); + message->key_size = 6; + message->message_type = MESSAGE_TYPE_ADD_PROVIDER; + message->record = test_record_create(); + + // protobuf + buffer_len = libp2p_message_protobuf_encode_size(message); + buffer = malloc(buffer_len); + if (!libp2p_message_protobuf_encode(message, buffer, buffer_len, &buffer_len)) + goto exit; + + // decode + if (!libp2p_message_protobuf_decode(buffer, buffer_len, &result)) + goto exit; + + // check results + if (result->cluster_level_raw != 1) + goto exit; + + // cleanup + retVal = 1; + exit: + if (message != NULL) + libp2p_message_free(message); + if (buffer != NULL) + free(buffer); + if (result != NULL) + libp2p_message_free(result); + return retVal; +} diff --git a/test/testit.c b/test/testit.c index bc9dda6..4b19020 100644 --- a/test/testit.c +++ b/test/testit.c @@ -41,7 +41,8 @@ const char* names[] = { "test_dialer_dial", "test_record_protobuf", "test_record_make_put_record", - "test_record_peer_protobuf" + "test_record_peer_protobuf", + "test_record_message_protobuf" }; int (*funcs[])(void) = { @@ -74,7 +75,8 @@ int (*funcs[])(void) = { test_dialer_dial, test_record_protobuf, test_record_make_put_record, - test_record_peer_protobuf + test_record_peer_protobuf, + test_record_message_protobuf }; int testit(const char* name, int (*func)(void)) { diff --git a/utils/linked_list.c b/utils/linked_list.c index 295c3be..1f75321 100644 --- a/utils/linked_list.c +++ b/utils/linked_list.c @@ -13,8 +13,12 @@ struct Libp2pLinkedList* libp2p_utils_linked_list_new() { void libp2p_utils_linked_list_free(struct Libp2pLinkedList* head) { struct Libp2pLinkedList* current = head; + struct Libp2pLinkedList* next = NULL; while (current != NULL) { - free(current->item); - current = current->next; + next = current->next; + if (current->item != NULL) + free(current->item); + free(current); + current = next; } }