#include #include "CppUTest/TestHarness.h" #include "CppUTest/CommandLineTestRunner.h" #include "bluetooth_data_types.h" #include "bluetooth_gatt.h" #include "ble/mesh/adv_bearer.h" #include "ble/mesh/mesh_crypto.h" #include "ble/mesh/mesh_network.h" #include "mesh_transport.h" #include "btstack_util.h" #include "provisioning.h" #include "btstack_memory.h" extern "C" int mock_process_hci_cmd(void); static mesh_network_pdu_t * received_network_pdu; static mesh_network_pdu_t * received_proxy_pdu; static uint8_t sent_network_pdu_data[29]; static uint8_t sent_network_pdu_len; static uint8_t recv_upper_transport_pdu_data[100]; static uint16_t recv_upper_transport_pdu_len; static btstack_packet_handler_t mesh_packet_handler; void adv_bearer_register_for_mesh_message(btstack_packet_handler_t packet_handler){ mesh_packet_handler = packet_handler; } void adv_bearer_request_can_send_now_for_mesh_message(void){ printf("adv_bearer_request_can_send_now_for_mesh_message\n"); // simulate can send now uint8_t event[3]; event[0] = HCI_EVENT_MESH_META; event[1] = 1; event[2] = MESH_SUBEVENT_CAN_SEND_NOW; (*mesh_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); } void adv_bearer_send_mesh_message(const uint8_t * network_pdu, uint16_t size){ printf("adv_bearer_send_mesh_message: \n"); printf_hexdump(network_pdu, size); memcpy(sent_network_pdu_data, network_pdu, size); sent_network_pdu_len = size; } void adv_bearer_emit_sent(void){ printf("adv_bearer_emit_sent\n"); uint8_t event[3]; event[0] = HCI_EVENT_MESH_META; event[1] = 1; event[2] = MESH_SUBEVENT_CAN_SEND_NOW; (*mesh_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); } void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){ int i; for (i=0; ilen; memcpy(recv_upper_transport_pdu_data, transport_pdu->data, recv_upper_transport_pdu_len); } TEST_GROUP(MessageTest){ void setup(void){ btstack_memory_init(); btstack_crypto_init(); load_provisioning_data_test_message(); mesh_network_init(); mesh_network_set_higher_layer_handler(&test_lower_transport_callback_handler); mesh_network_set_proxy_message_handler(&test_proxy_server_callback_handler); mesh_upper_transport_register_unsegemented_message_handler(&test_upper_transport_unsegmented_callback_handler); mesh_upper_transport_register_segemented_message_handler(&test_upper_transport_segmented_callback_handler); mesh_seq_auth_reset(); received_network_pdu = NULL; recv_upper_transport_pdu_len =0; sent_network_pdu_len = 0; } void teardown(void){ // printf("-- teardown start --\n\n"); while (!btstack_crypto_idle()){ mock_process_hci_cmd(); } // mesh_network_reset(); // mesh_transport_reset(); // mesh_network_dump(); // mesh_transport_dump(); printf("-- teardown complete --\n\n"); } }; static uint8_t transport_pdu_data[64]; static uint16_t transport_pdu_len; static uint8_t test_network_pdu_len; static uint8_t test_network_pdu_data[29]; void test_receive_network_pdus(int count, char ** network_pdus, char ** lower_transport_pdus, char * access_pdu){ int i; for (i=0;idata; uint8_t proxy_pdu_len = received_proxy_pdu->len; int i = 0; char ** network_pdus = proxy_config_pdus; transport_pdu_len = strlen(network_pdus[i]) / 2; btstack_parse_hex(network_pdus[i], transport_pdu_len, transport_pdu_data); CHECK_EQUAL( transport_pdu_len-1, proxy_pdu_len); CHECK_EQUAL_ARRAY(transport_pdu_data+1, proxy_pdu_data, transport_pdu_len-1); received_proxy_pdu = NULL; } static btstack_crypto_aes128_t crypto_request_aes128; static uint8_t plaintext[16]; static uint8_t identity_key[16]; static uint8_t hash[16]; static uint8_t random_value[8]; static void mesh_proxy_handle_get_aes128(void * arg){ UNUSED(arg); uint8_t expected_hash[8]; uint8_t expected_random_value[8]; btstack_parse_hex("00861765aefcc57b", 8, expected_hash); CHECK_EQUAL_ARRAY(&hash[8], expected_hash, 8); btstack_parse_hex("34ae608fbbc1f2c6", 8, expected_random_value); CHECK_EQUAL_ARRAY(random_value, expected_random_value, 8); } TEST(MessageTest, ServiceDataUsingNodeIdentityTest){ btstack_parse_hex("34ae608fbbc1f2c6", 8, random_value); memset(plaintext, 0, sizeof(plaintext)); memcpy(&plaintext[6] , random_value, 8); big_endian_store_16(plaintext, 14, 0x1201); // 84396c435ac48560b5965385253e210c btstack_parse_hex("84396c435ac48560b5965385253e210c", 16, identity_key); btstack_crypto_aes128_encrypt(&crypto_request_aes128, identity_key, plaintext, hash, mesh_proxy_handle_get_aes128, NULL); } // Mesh v1.0, 8.2.1 static btstack_crypto_aes128_cmac_t aes_cmac_request; static uint8_t k4_result[1]; static void handle_k4_result(void *arg){ printf("ApplicationkeyIDTest: %02x\n", k4_result[0]); CHECK_EQUAL( 0x26, k4_result[0]); } TEST(MessageTest, ApplicationkeyIDTest){ static uint8_t application_key[16]; btstack_parse_hex("63964771734fbd76e3b40519d1d94a48", 16, application_key); mesh_k4(&aes_cmac_request, application_key, &k4_result[0], &handle_k4_result, NULL); } int main (int argc, const char * argv[]){ return CommandLineTestRunner::RunAllTests(argc, argv); }