void test_KineticPDU_Send_should_send_the_PDU_and_return_true_upon_successful_transmission_of_full_PDU_with_no_value_payload(void) { LOG_LOCATION; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KineticEntry entry = {.value = BYTE_BUFFER_NONE}; KineticPDU_AttachEntry(&PDU, &entry); // Create NBO copy of header for sending PDU.headerNBO.versionPrefix = 'F'; PDU.headerNBO.protobufLength = KineticNBO_FromHostU32(KineticProto__get_packed_size(PDU.proto)); PDU.headerNBO.valueLength = 0; KineticHMAC_Init_Expect(&PDU.hmac, KINETIC_PROTO_SECURITY_ACL_HMACALGORITHM_HmacSHA1); KineticHMAC_Populate_Expect(&PDU.hmac, &PDU.protoData.message.proto, PDU.connection->session.hmacKey); KineticSocket_Write_ExpectAndReturn(Connection.socket, &headerNBO, KINETIC_STATUS_SUCCESS); KineticSocket_WriteProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); TEST_IGNORE_MESSAGE("Need to figure out how to handle address of PDU header"); KineticStatus status = KineticPDU_Send(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status); }
void test_KineticPDU_Receive_should_receive_a_message_with_no_value_payload_and_forward_status_upon_receipt_of_valid_PDU_with_non_successful_status(void) { LOG_LOCATION; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); PDU.protoData.message.status.code = KINETIC_PROTO_STATUS_STATUS_CODE_PERM_DATA_ERROR; PDU.protoData.message.status.has_code = true; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); // Fake value/payload length uint8_t data[1024]; ByteArray expectedValue = {.data = data, .len = sizeof(data)}; ByteArray_FillWithDummyData(expectedValue); KineticEntry entry = {.value = ByteBuffer_CreateWithArray(expectedValue)}; KineticPDU_AttachEntry(&PDU, &entry); PDU.headerNBO.valueLength = KineticNBO_FromHostU32(0); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, true); EnableAndSetPDUStatus(&PDU, KINETIC_PROTO_STATUS_STATUS_CODE_PERM_DATA_ERROR); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_DATA_ERROR, status); TEST_ASSERT_EQUAL( KINETIC_PROTO_STATUS_STATUS_CODE_PERM_DATA_ERROR, PDU.protoData.message.status.code); } void test_KineticPDU_Receive_should_receive_a_message_for_the_exchange_and_return_false_upon_failure_to_read_header(void) { LOG_LOCATION; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_CONNECTION_ERROR); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_CONNECTION_ERROR, status); }
void test_KineticPDU_Receive_should_receive_a_message_for_the_exchange_and_return_false_upon_failure_to_read_protobuf(void) { LOG_LOCATION; KineticPDU_Init(&PDU, &Connection); PDU.protoData.message.status.code = KINETIC_PROTO_STATUS_STATUS_CODE_PERM_DATA_ERROR; PDU.protoData.message.status.has_code = true; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); PDU.headerNBO = (KineticPDUHeader) { .versionPrefix = (uint8_t)'F', .protobufLength = KineticNBO_FromHostU32(12), .valueLength = 0 }; KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_DEVICE_BUSY); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_DEVICE_BUSY, status) } void test_KineticPDU_Receive_should_receive_a_message_for_the_exchange_and_return_false_upon_HMAC_validation_failure(void) { LOG_LOCATION; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, false); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_DATA_ERROR, status); }
void test_KineticNBO_FromHostU32_should_convert_uint32_from_host_to_network_byte_order(void) { uint32_t host = 0x12345678u; uint32_t net = KineticNBO_FromHostU32(host); TEST_ASSERT_EQUAL_HEX32(0x78563412u, net); }
void test_KineticPDU_Receive_should_receive_a_message_for_the_exchange_and_return_false_upon_value_field_receive_failure(void) { LOG_LOCATION; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); PDU.protoData.message.status.code = KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS; PDU.protoData.message.status.has_code = true; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); PDU.headerNBO = (KineticPDUHeader) { .versionPrefix = 'F', .protobufLength = KineticNBO_ToHostU32(17), .valueLength = KineticNBO_ToHostU32(124) }; // Fake value/payload length uint8_t data[124]; size_t bytesToRead = sizeof(data); ByteArray expectedValue = ByteArray_Create(data, sizeof(data)); ByteArray_FillWithDummyData(expectedValue); KineticEntry entry = {.value = ByteBuffer_CreateWithArray(expectedValue)}; KineticPDU_AttachEntry(&PDU, &entry); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, true); KineticSocket_Read_ExpectAndReturn(Connection.socket, &entry.value, bytesToRead, KINETIC_STATUS_SOCKET_ERROR); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SOCKET_ERROR, status); TEST_ASSERT_EQUAL(17, PDU.header.protobufLength); TEST_ASSERT_EQUAL(bytesToRead, expectedValue.len); TEST_ASSERT_EQUAL(expectedValue.len, PDU.header.valueLength); } void test_KineticPDU_Receive_should_receive_a_message_and_update_the_ConnectionID_for_the_connection_when_provided(void) { LOG_LOCATION; Connection.connectionID = 98765; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); PDU.protoData.message.status.code = KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS; PDU.protoData.message.status.has_code = true; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); // Fake value/payload length uint8_t data[124]; size_t bytesToRead = sizeof(data); ByteArray expectedValue = {.data = data, .len = sizeof(data)}; ByteArray_FillWithDummyData(expectedValue); KineticEntry entry = {.value = ByteBuffer_CreateWithArray(expectedValue)}; KineticPDU_AttachEntry(&PDU, &entry); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, true); KineticSocket_Read_ExpectAndReturn(Connection.socket, &entry.value, bytesToRead, KINETIC_STATUS_SUCCESS); PDU.headerNBO.valueLength = KineticNBO_FromHostU32(expectedValue.len); EnableAndSetPDUConnectionID(&PDU, 12345); EnableAndSetPDUStatus(&PDU, KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status); TEST_ASSERT_EQUAL(KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS, PDU.protoData.message.status.code); TEST_ASSERT_EQUAL(12345, PDU.proto->command->header->connectionID); TEST_ASSERT_EQUAL(12345, PDU.connection->connectionID); } void test_KineticPDU_Receive_should_receive_a_message_and_NOT_update_the_ConnectionID_for_the_connection_if_not_provided(void) { LOG_LOCATION; Connection.connectionID = 98765; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); PDU.protoData.message.status.code = KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS; PDU.protoData.message.status.has_code = true; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); // Fake value/payload length uint8_t data[124]; size_t bytesToRead = sizeof(data); ByteArray expectedValue = {.data = data, .len = sizeof(data)}; ByteArray_FillWithDummyData(expectedValue); KineticEntry entry = {.value = ByteBuffer_CreateWithArray(expectedValue)}; KineticPDU_AttachEntry(&PDU, &entry); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, true); KineticSocket_Read_ExpectAndReturn(Connection.socket, &entry.value, bytesToRead, KINETIC_STATUS_SUCCESS); PDU.headerNBO.valueLength = KineticNBO_FromHostU32(expectedValue.len); EnableAndSetPDUStatus(&PDU, KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status); TEST_ASSERT_EQUAL(KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS, PDU.protoData.message.status.code); TEST_ASSERT_EQUAL(98765, PDU.proto->command->header->connectionID); TEST_ASSERT_EQUAL(98765, PDU.connection->connectionID); } void test_KineticPDU_GetStatus_should_return_KINETIC_STATUS_INVALID_if_no_KineticProto_Status_StatusCode_in_response(void) { LOG_LOCATION; KineticStatus status; status = KineticPDU_GetStatus(NULL); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); PDU.proto = NULL; status = KineticPDU_GetStatus(&PDU); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); PDU.proto = &PDU.protoData.message.proto; PDU.proto->command = NULL; status = KineticPDU_GetStatus(&PDU); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); PDU.proto->command = &PDU.protoData.message.command; status = KineticPDU_GetStatus(&PDU); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); PDU.proto->command->status = &PDU.protoData.message.status; PDU.proto->command->status->has_code = false; status = KineticPDU_GetStatus(&PDU); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); PDU.proto->command->status->has_code = true; PDU.proto->command->status->code = KINETIC_PROTO_STATUS_STATUS_CODE_INVALID_STATUS_CODE; status = KineticPDU_GetStatus(&PDU); TEST_ASSERT_EQUAL(KINETIC_STATUS_INVALID, status); }
void test_KineticPDU_Send_should_send_the_specified_message_and_return_false_upon_failure_to_send_header(void) { LOG_LOCATION; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); char valueData[] = "Some arbitrary value"; KineticEntry entry = {.value = ByteBuffer_Create(valueData, strlen(valueData))}; KineticPDU_AttachEntry(&PDU, &entry); KineticHMAC_Init_Expect(&PDU.hmac, KINETIC_PROTO_SECURITY_ACL_HMACALGORITHM_HmacSHA1); KineticHMAC_Populate_Expect(&PDU.hmac, &PDU.protoData.message.proto, PDU.connection->session.hmacKey); KineticSocket_Write_ExpectAndReturn(Connection.socket, &headerNBO, KINETIC_STATUS_SOCKET_ERROR); TEST_IGNORE_MESSAGE("Need to figure out how to handle address of PDU header"); KineticStatus status = KineticPDU_Send(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SOCKET_ERROR, status); } void test_KineticPDU_Send_should_send_the_specified_message_and_return_false_upon_failure_to_send_protobuf(void) { LOG_LOCATION; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); char valueData[] = "Some arbitrary value"; KineticEntry entry = {.value = ByteBuffer_Create(valueData, strlen(valueData))}; KineticPDU_AttachEntry(&PDU, &entry); KineticHMAC_Init_Expect(&PDU.hmac, KINETIC_PROTO_SECURITY_ACL_HMACALGORITHM_HmacSHA1); KineticHMAC_Populate_Expect(&PDU.hmac, &PDU.protoData.message.proto, PDU.connection->session.hmacKey); KineticSocket_Write_ExpectAndReturn(Connection.socket, &headerNBO, KINETIC_STATUS_SUCCESS); KineticSocket_WriteProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SOCKET_TIMEOUT); TEST_IGNORE_MESSAGE("Need to figure out how to handle address of PDU header"); KineticStatus status = KineticPDU_Send(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SOCKET_TIMEOUT, status); } void test_KineticPDU_Send_should_send_the_specified_message_and_return_KineticStatus_if_value_write_fails(void) { LOG_LOCATION; ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); uint8_t valueData[128]; KineticEntry entry = {.value = ByteBuffer_Create(valueData, sizeof(valueData))}; ByteBuffer_AppendCString(&entry.value, "Some arbitrary value"); KineticPDU_AttachEntry(&PDU, &entry); KineticHMAC_Init_Expect(&PDU.hmac, KINETIC_PROTO_SECURITY_ACL_HMACALGORITHM_HmacSHA1); KineticHMAC_Populate_Expect(&PDU.hmac, &PDU.protoData.message.proto, PDU.connection->session.hmacKey); KineticSocket_Write_ExpectAndReturn(Connection.socket, &headerNBO, KINETIC_STATUS_SUCCESS); KineticSocket_WriteProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticSocket_Write_ExpectAndReturn(Connection.socket, &entry.value, KINETIC_STATUS_SOCKET_TIMEOUT); TEST_IGNORE_MESSAGE("Need to figure out how to handle address of PDU header"); KineticStatus status = KineticPDU_Send(&PDU); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SOCKET_TIMEOUT, status); } void test_KineticPDU_Receive_should_receive_a_message_with_value_payload_and_return_true_upon_receipt_of_valid_PDU(void) { LOG_LOCATION; Connection.connectionID = 98765; KINETIC_PDU_INIT_WITH_MESSAGE(&PDU, &Connection); ByteBuffer headerNBO = ByteBuffer_Create(&PDU.headerNBO, sizeof(KineticPDUHeader)); // Fake value/payload length uint8_t data[1024]; ByteArray expectedValue = ByteArray_Create(data, sizeof(data)); ByteArray_FillWithDummyData(expectedValue); KineticEntry entry = {.value = ByteBuffer_CreateWithArray(expectedValue)}; KineticPDU_AttachEntry(&PDU, &entry); KineticSocket_Read_ExpectAndReturn(Connection.socket, &headerNBO, sizeof(KineticPDUHeader), KINETIC_STATUS_SUCCESS); KineticSocket_ReadProtobuf_ExpectAndReturn(Connection.socket, &PDU, KINETIC_STATUS_SUCCESS); KineticHMAC_Validate_ExpectAndReturn(PDU.proto, PDU.connection->session.hmacKey, true); KineticSocket_Read_ExpectAndReturn(Connection.socket, &entry.value, expectedValue.len, KINETIC_STATUS_SUCCESS); PDU.headerNBO.valueLength = KineticNBO_FromHostU32(expectedValue.len); EnableAndSetPDUConnectionID(&PDU, 12345); EnableAndSetPDUStatus(&PDU, KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS); KineticStatus status = KineticPDU_Receive(&PDU); TEST_ASSERT_EQUAL(KINETIC_PROTO_STATUS_STATUS_CODE_SUCCESS, PDU.protoData.message.status.code); TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status); }