Loading src/lib/kinetic_allocator.c +17 −43 Original line number Diff line number Diff line Loading @@ -23,10 +23,6 @@ #include <stdlib.h> #include <pthread.h> //============================================================================== // Generic List Support (INTERNAL) //============================================================================== KineticConnection* KineticAllocator_NewConnection(void) { KineticConnection* connection = calloc(1, sizeof(KineticConnection)); Loading @@ -43,31 +39,6 @@ void KineticAllocator_FreeConnection(KineticConnection* connection) free(connection); } //============================================================================== // PDU List Support //============================================================================== KineticPDU* KineticAllocator_NewPDU(KineticConnection* connection) { assert(connection != NULL); LOGF3("Allocating new PDU on connection (0x%0llX)", connection); KineticPDU* newPDU = (KineticPDU*)calloc(1, sizeof(KineticPDU)); if (newPDU == NULL) { LOG0("Failed allocating new PDU!"); return NULL; } KineticPDU_Init(newPDU, connection); LOGF3("Allocated new PDU (0x%0llX) on connection", newPDU, connection); return newPDU; } void KineticAllocator_FreePDU(KineticPDU* pdu) { free(pdu); LOGF3("Freed PDU (0x%0llX)", pdu); } KineticResponse * KineticAllocator_NewKineticResponse(size_t const valueLength) { KineticResponse * response = calloc(1, sizeof(*response) + valueLength); Loading @@ -80,7 +51,8 @@ KineticResponse * KineticAllocator_NewKineticResponse(size_t const valueLength) void KineticAllocator_FreeKineticResponse(KineticResponse * response) { if (response != NULL) { assert(response != NULL); if (response->command != NULL) { protobuf_c_message_free_unpacked(&response->command->base, NULL); } Loading @@ -89,11 +61,6 @@ void KineticAllocator_FreeKineticResponse(KineticResponse * response) } free(response); } } //============================================================================== // Operation List Support //============================================================================== KineticOperation* KineticAllocator_NewOperation(KineticConnection* const connection) { Loading @@ -106,7 +73,13 @@ KineticOperation* KineticAllocator_NewOperation(KineticConnection* const connect return NULL; } KineticOperation_Init(newOperation, connection); newOperation->request = KineticAllocator_NewPDU(connection); LOGF3("Allocating new PDU on connection (0x%0llX)", connection); newOperation->request = (KineticPDU*)calloc(1, sizeof(KineticPDU)); if (newOperation->request == NULL) { LOG0("Failed allocating new PDU!"); free(newOperation); return NULL; } KineticPDU_InitWithCommand(newOperation->request, connection); LOGF3("Allocated new operation (0x%0llX) on connection (0x%0llX)", newOperation, connection); return newOperation; Loading @@ -120,7 +93,8 @@ void KineticAllocator_FreeOperation(KineticOperation* operation) if (operation->request != NULL) { LOGF3("Freeing request PDU (0x%0llX) from operation (0x%0llX) on connection (0x%0llX)", operation->request, operation, connection); KineticAllocator_FreePDU(operation->request); free(operation->request); LOGF3("Freed PDU (0x%0llX)", operation->request); operation->request = NULL; } if (operation->response != NULL) { Loading src/lib/kinetic_operation.c +20 −25 Original line number Diff line number Diff line Loading @@ -45,10 +45,13 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) KineticProto_Message* proto = &operation->request->message.message; // Pack the command, if available if (request->message.has_command) { size_t expectedLen = KineticProto_command__get_packed_size(&request->message.command); request->message.message.commandBytes.data = (uint8_t*)malloc(expectedLen); assert(request->message.message.commandBytes.data != NULL); if(request->message.message.commandBytes.data == NULL) { LOG0("Failed to allocate command bytes!"); return KINETIC_STATUS_MEMORY_ERROR; } size_t packedLen = KineticProto_command__pack( &request->message.command, request->message.message.commandBytes.data); Loading @@ -59,7 +62,6 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) .data = request->message.message.commandBytes.data, .len = request->message.message.commandBytes.len, }); } switch (proto->authType) { case KINETIC_PROTO_MESSAGE_AUTH_TYPE_PINAUTH: Loading Loading @@ -109,12 +111,9 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) size_t offset = 0; uint8_t * msg = malloc(PDU_HEADER_LEN + header.protobufLength + header.valueLength); if (msg == NULL) { if (request->message.has_command) { free(request->message.message.commandBytes.data); request->message.message.commandBytes.data = NULL; } LOG0("Failed to allocate outgoing message!"); return KINETIC_STATUS_MEMORY_ERROR; Loading @@ -132,11 +131,8 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) assert(len == header.protobufLength); offset += header.protobufLength; if (request->message.has_command) { free(request->message.message.commandBytes.data); request->message.message.commandBytes.data = NULL; } // Send the value/payload, if specified if (header.valueLength > 0) { Loading Loading @@ -720,7 +716,6 @@ static void KineticOperation_ValidateOperation(KineticOperation* operation) assert(operation != NULL); assert(operation->connection != NULL); assert(operation->request != NULL); assert(operation->request->message.has_command); assert(operation->request->command != NULL); assert(operation->request->command->header != NULL); assert(operation->request->command->header->has_sequence); Loading src/lib/kinetic_types_internal.c +0 −1 Original line number Diff line number Diff line Loading @@ -492,7 +492,6 @@ void KineticPDU_Init(KineticPDU* pdu, KineticConnection* con) void KineticPDU_InitWithCommand(KineticPDU* pdu, KineticConnection* con) { KineticPDU_Init(pdu, con); pdu->message.has_command = true; pdu->command = &pdu->message.command; pdu->command->header = &pdu->message.header; } src/lib/kinetic_types_internal.h +0 −1 Original line number Diff line number Diff line Loading @@ -114,7 +114,6 @@ typedef struct _KineticMessage { KineticProto_Message_PINauth pinAuth; uint8_t hmacData[KINETIC_HMAC_MAX_LEN]; bool has_command; // Set to `true` to enable command element KineticProto_Command command; KineticProto_Command_Header header; KineticProto_Command_Body body; Loading test/unit/test_kinetic_allocator.c +100 −12 Original line number Diff line number Diff line Loading @@ -23,7 +23,7 @@ #include "kinetic_logger.h" #include "kinetic_proto.h" #include "byte_array.h" #include "protobuf-c/protobuf-c.h" #include "mock_protobuf-c.h" #include "unity.h" #include "unity_helper.h" #include "mock_memory_stubs.h" Loading Loading @@ -58,25 +58,113 @@ void test_KineticAllocator_NewConnection_should_return_a_connection_with_connect TEST_ASSERT_FALSE(connection->connected); } void test_KineticAllocator_NewPDU_should_return_null_if_calloc_returns_null(void) void test_KineticAllocator_NewKineticResponse_should_return_null_if_calloc_return_null(void) { calloc_ExpectAndReturn(1, sizeof(KineticResponse) + 1234, NULL); KineticResponse * response = KineticAllocator_NewKineticResponse(1234); TEST_ASSERT_NULL(response); } void test_KineticAllocator_FreeKineticResponse_should_free_the_command_if_its_not_null(void) { KineticProto_Command command; KineticResponse rsp = { .command = &command}; protobuf_c_message_free_unpacked_Expect(&command.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_FreeKineticResponse_should_free_the_proto_if_its_not_null(void) { KineticProto_Message proto; KineticResponse rsp = { .proto = &proto}; protobuf_c_message_free_unpacked_Expect(&proto.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_FreeKineticResponse_should_free_the_proto_and_command_if_they_are_not_null(void) { KineticProto_Message proto; KineticProto_Command command; KineticResponse rsp = { .proto = &proto, .command = &command }; protobuf_c_message_free_unpacked_Expect(&command.base, NULL); protobuf_c_message_free_unpacked_Expect(&proto.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_NewOperation_should_return_null_if_calloc_returns_null_for_operation(void) { calloc_ExpectAndReturn(1, sizeof(KineticOperation), NULL); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NULL(operation); } void test_KineticAllocator_NewOperation_should_return_null_and_free_operation_if_calloc_returns_null_for_pdu(void) { KineticOperation op; calloc_ExpectAndReturn(1, sizeof(KineticOperation), &op); KineticOperation_Init_Expect(&op, &Connection); calloc_ExpectAndReturn(1, sizeof(KineticPDU), NULL); KineticPDU * pdu = KineticAllocator_NewPDU(&Connection); TEST_ASSERT_NULL(pdu); free_Expect(&op); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NULL(operation); } void test_KineticAllocator_NewPDU_should_initialize_pdu(void) void test_KineticAllocator_NewOperation_should_initialize_operation_and_pdu(void) { KineticOperation op; KineticPDU pdu; calloc_ExpectAndReturn(1, sizeof(KineticOperation), &op); KineticOperation_Init_Expect(&op, &Connection); calloc_ExpectAndReturn(1, sizeof(KineticPDU), &pdu); KineticPDU_Init_Expect(&pdu, &Connection); KineticPDU * pdu_pointer = KineticAllocator_NewPDU(&Connection); TEST_ASSERT_NOT_NULL(pdu_pointer); KineticPDU_InitWithCommand_Expect(&pdu, &Connection); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NOT_NULL(operation); } void test_KineticAllocator_NewKineticResponse_should_return_null_if_calloc_return_null(void) void test_KineticAllocator_FreeOperation_should_free_request_if_its_not_null(void) { calloc_ExpectAndReturn(1, sizeof(KineticResponse) + 1234, NULL); KineticResponse * response = KineticAllocator_NewKineticResponse(1234); TEST_ASSERT_NULL(response); KineticPDU pdu; memset(&pdu, 0x00, sizeof(pdu)); KineticOperation op = { .request = &pdu }; free_Expect(&pdu); free_Expect(&op); KineticAllocator_FreeOperation(&op); } void test_KineticAllocator_FreeOperation_should_free_response_if_its_not_null(void) { KineticResponse response; memset(&response, 0x00, sizeof(response)); KineticOperation op = { .response = &response }; free_Expect(&response); free_Expect(&op); KineticAllocator_FreeOperation(&op); } Loading
src/lib/kinetic_allocator.c +17 −43 Original line number Diff line number Diff line Loading @@ -23,10 +23,6 @@ #include <stdlib.h> #include <pthread.h> //============================================================================== // Generic List Support (INTERNAL) //============================================================================== KineticConnection* KineticAllocator_NewConnection(void) { KineticConnection* connection = calloc(1, sizeof(KineticConnection)); Loading @@ -43,31 +39,6 @@ void KineticAllocator_FreeConnection(KineticConnection* connection) free(connection); } //============================================================================== // PDU List Support //============================================================================== KineticPDU* KineticAllocator_NewPDU(KineticConnection* connection) { assert(connection != NULL); LOGF3("Allocating new PDU on connection (0x%0llX)", connection); KineticPDU* newPDU = (KineticPDU*)calloc(1, sizeof(KineticPDU)); if (newPDU == NULL) { LOG0("Failed allocating new PDU!"); return NULL; } KineticPDU_Init(newPDU, connection); LOGF3("Allocated new PDU (0x%0llX) on connection", newPDU, connection); return newPDU; } void KineticAllocator_FreePDU(KineticPDU* pdu) { free(pdu); LOGF3("Freed PDU (0x%0llX)", pdu); } KineticResponse * KineticAllocator_NewKineticResponse(size_t const valueLength) { KineticResponse * response = calloc(1, sizeof(*response) + valueLength); Loading @@ -80,7 +51,8 @@ KineticResponse * KineticAllocator_NewKineticResponse(size_t const valueLength) void KineticAllocator_FreeKineticResponse(KineticResponse * response) { if (response != NULL) { assert(response != NULL); if (response->command != NULL) { protobuf_c_message_free_unpacked(&response->command->base, NULL); } Loading @@ -89,11 +61,6 @@ void KineticAllocator_FreeKineticResponse(KineticResponse * response) } free(response); } } //============================================================================== // Operation List Support //============================================================================== KineticOperation* KineticAllocator_NewOperation(KineticConnection* const connection) { Loading @@ -106,7 +73,13 @@ KineticOperation* KineticAllocator_NewOperation(KineticConnection* const connect return NULL; } KineticOperation_Init(newOperation, connection); newOperation->request = KineticAllocator_NewPDU(connection); LOGF3("Allocating new PDU on connection (0x%0llX)", connection); newOperation->request = (KineticPDU*)calloc(1, sizeof(KineticPDU)); if (newOperation->request == NULL) { LOG0("Failed allocating new PDU!"); free(newOperation); return NULL; } KineticPDU_InitWithCommand(newOperation->request, connection); LOGF3("Allocated new operation (0x%0llX) on connection (0x%0llX)", newOperation, connection); return newOperation; Loading @@ -120,7 +93,8 @@ void KineticAllocator_FreeOperation(KineticOperation* operation) if (operation->request != NULL) { LOGF3("Freeing request PDU (0x%0llX) from operation (0x%0llX) on connection (0x%0llX)", operation->request, operation, connection); KineticAllocator_FreePDU(operation->request); free(operation->request); LOGF3("Freed PDU (0x%0llX)", operation->request); operation->request = NULL; } if (operation->response != NULL) { Loading
src/lib/kinetic_operation.c +20 −25 Original line number Diff line number Diff line Loading @@ -45,10 +45,13 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) KineticProto_Message* proto = &operation->request->message.message; // Pack the command, if available if (request->message.has_command) { size_t expectedLen = KineticProto_command__get_packed_size(&request->message.command); request->message.message.commandBytes.data = (uint8_t*)malloc(expectedLen); assert(request->message.message.commandBytes.data != NULL); if(request->message.message.commandBytes.data == NULL) { LOG0("Failed to allocate command bytes!"); return KINETIC_STATUS_MEMORY_ERROR; } size_t packedLen = KineticProto_command__pack( &request->message.command, request->message.message.commandBytes.data); Loading @@ -59,7 +62,6 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) .data = request->message.message.commandBytes.data, .len = request->message.message.commandBytes.len, }); } switch (proto->authType) { case KINETIC_PROTO_MESSAGE_AUTH_TYPE_PINAUTH: Loading Loading @@ -109,12 +111,9 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) size_t offset = 0; uint8_t * msg = malloc(PDU_HEADER_LEN + header.protobufLength + header.valueLength); if (msg == NULL) { if (request->message.has_command) { free(request->message.message.commandBytes.data); request->message.message.commandBytes.data = NULL; } LOG0("Failed to allocate outgoing message!"); return KINETIC_STATUS_MEMORY_ERROR; Loading @@ -132,11 +131,8 @@ KineticStatus KineticOperation_SendRequest(KineticOperation* const operation) assert(len == header.protobufLength); offset += header.protobufLength; if (request->message.has_command) { free(request->message.message.commandBytes.data); request->message.message.commandBytes.data = NULL; } // Send the value/payload, if specified if (header.valueLength > 0) { Loading Loading @@ -720,7 +716,6 @@ static void KineticOperation_ValidateOperation(KineticOperation* operation) assert(operation != NULL); assert(operation->connection != NULL); assert(operation->request != NULL); assert(operation->request->message.has_command); assert(operation->request->command != NULL); assert(operation->request->command->header != NULL); assert(operation->request->command->header->has_sequence); Loading
src/lib/kinetic_types_internal.c +0 −1 Original line number Diff line number Diff line Loading @@ -492,7 +492,6 @@ void KineticPDU_Init(KineticPDU* pdu, KineticConnection* con) void KineticPDU_InitWithCommand(KineticPDU* pdu, KineticConnection* con) { KineticPDU_Init(pdu, con); pdu->message.has_command = true; pdu->command = &pdu->message.command; pdu->command->header = &pdu->message.header; }
src/lib/kinetic_types_internal.h +0 −1 Original line number Diff line number Diff line Loading @@ -114,7 +114,6 @@ typedef struct _KineticMessage { KineticProto_Message_PINauth pinAuth; uint8_t hmacData[KINETIC_HMAC_MAX_LEN]; bool has_command; // Set to `true` to enable command element KineticProto_Command command; KineticProto_Command_Header header; KineticProto_Command_Body body; Loading
test/unit/test_kinetic_allocator.c +100 −12 Original line number Diff line number Diff line Loading @@ -23,7 +23,7 @@ #include "kinetic_logger.h" #include "kinetic_proto.h" #include "byte_array.h" #include "protobuf-c/protobuf-c.h" #include "mock_protobuf-c.h" #include "unity.h" #include "unity_helper.h" #include "mock_memory_stubs.h" Loading Loading @@ -58,25 +58,113 @@ void test_KineticAllocator_NewConnection_should_return_a_connection_with_connect TEST_ASSERT_FALSE(connection->connected); } void test_KineticAllocator_NewPDU_should_return_null_if_calloc_returns_null(void) void test_KineticAllocator_NewKineticResponse_should_return_null_if_calloc_return_null(void) { calloc_ExpectAndReturn(1, sizeof(KineticResponse) + 1234, NULL); KineticResponse * response = KineticAllocator_NewKineticResponse(1234); TEST_ASSERT_NULL(response); } void test_KineticAllocator_FreeKineticResponse_should_free_the_command_if_its_not_null(void) { KineticProto_Command command; KineticResponse rsp = { .command = &command}; protobuf_c_message_free_unpacked_Expect(&command.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_FreeKineticResponse_should_free_the_proto_if_its_not_null(void) { KineticProto_Message proto; KineticResponse rsp = { .proto = &proto}; protobuf_c_message_free_unpacked_Expect(&proto.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_FreeKineticResponse_should_free_the_proto_and_command_if_they_are_not_null(void) { KineticProto_Message proto; KineticProto_Command command; KineticResponse rsp = { .proto = &proto, .command = &command }; protobuf_c_message_free_unpacked_Expect(&command.base, NULL); protobuf_c_message_free_unpacked_Expect(&proto.base, NULL); free_Expect(&rsp); KineticAllocator_FreeKineticResponse(&rsp); } void test_KineticAllocator_NewOperation_should_return_null_if_calloc_returns_null_for_operation(void) { calloc_ExpectAndReturn(1, sizeof(KineticOperation), NULL); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NULL(operation); } void test_KineticAllocator_NewOperation_should_return_null_and_free_operation_if_calloc_returns_null_for_pdu(void) { KineticOperation op; calloc_ExpectAndReturn(1, sizeof(KineticOperation), &op); KineticOperation_Init_Expect(&op, &Connection); calloc_ExpectAndReturn(1, sizeof(KineticPDU), NULL); KineticPDU * pdu = KineticAllocator_NewPDU(&Connection); TEST_ASSERT_NULL(pdu); free_Expect(&op); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NULL(operation); } void test_KineticAllocator_NewPDU_should_initialize_pdu(void) void test_KineticAllocator_NewOperation_should_initialize_operation_and_pdu(void) { KineticOperation op; KineticPDU pdu; calloc_ExpectAndReturn(1, sizeof(KineticOperation), &op); KineticOperation_Init_Expect(&op, &Connection); calloc_ExpectAndReturn(1, sizeof(KineticPDU), &pdu); KineticPDU_Init_Expect(&pdu, &Connection); KineticPDU * pdu_pointer = KineticAllocator_NewPDU(&Connection); TEST_ASSERT_NOT_NULL(pdu_pointer); KineticPDU_InitWithCommand_Expect(&pdu, &Connection); KineticOperation * operation = KineticAllocator_NewOperation(&Connection); TEST_ASSERT_NOT_NULL(operation); } void test_KineticAllocator_NewKineticResponse_should_return_null_if_calloc_return_null(void) void test_KineticAllocator_FreeOperation_should_free_request_if_its_not_null(void) { calloc_ExpectAndReturn(1, sizeof(KineticResponse) + 1234, NULL); KineticResponse * response = KineticAllocator_NewKineticResponse(1234); TEST_ASSERT_NULL(response); KineticPDU pdu; memset(&pdu, 0x00, sizeof(pdu)); KineticOperation op = { .request = &pdu }; free_Expect(&pdu); free_Expect(&op); KineticAllocator_FreeOperation(&op); } void test_KineticAllocator_FreeOperation_should_free_response_if_its_not_null(void) { KineticResponse response; memset(&response, 0x00, sizeof(response)); KineticOperation op = { .response = &response }; free_Expect(&response); free_Expect(&op); KineticAllocator_FreeOperation(&op); }