static void test_array_get_size(void **state) { (void)state; srand(time(NULL)); ASSERT_EQUAL(kaa_array_get_size(NULL, NULL), 0); ASSERT_EQUAL(kaa_array_get_size(NULL, (get_size_fn)kaa_null_get_size), avro_long_get_size(0)); const char *plain_str = "data"; kaa_string_t *reference_kaa_str = kaa_string_copy_create(plain_str); size_t element_size = kaa_string_get_size(reference_kaa_str); kaa_string_destroy(reference_kaa_str); size_t array_size = 1 + rand() % 10; kaa_list_t *avro_array = kaa_list_create(); size_t i = 0; for (i = 0; i < array_size; ++i) { kaa_list_push_back(avro_array, kaa_string_copy_create(plain_str)); } size_t expected_size = avro_long_get_size(array_size) + array_size * element_size + avro_long_get_size(0); ASSERT_EQUAL(kaa_array_get_size(avro_array, &kaa_string_get_size), expected_size); kaa_list_destroy(avro_array, kaa_string_destroy); }
static void kaa_device_info_request(void *context , kaa_remote_control_ecf_device_info_request_t *event , kaa_endpoint_id_p source) { (void)context; (void)source; demo_printf("Device info request received\r\n"); kaa_remote_control_ecf_device_info_response_t *response = kaa_remote_control_ecf_device_info_response_create(); response->device_name = kaa_string_copy_create(TARGET_DEVICE_NAME); response->model = kaa_string_copy_create(TARGET_MODEL_NAME); response->gpio_status = kaa_list_create(); for (int i = 0; i < target_gpio_led_get_count(); ++i) { gpio_port_t *gpio_led = target_get_gpio_port( i ); if (gpio_led) { kaa_remote_control_ecf_gpio_status_t *gio_status = kaa_remote_control_ecf_gpio_status_create(); gio_status->id = i; gio_status->status = gpio_led->state; gio_status->type = kaa_string_copy_create( gpio_led->id ); kaa_list_push_back(response->gpio_status, (void*)gio_status); } } kaa_error_t err = kaa_event_manager_send_kaa_remote_control_ecf_device_info_response(kaa_client_get_context(kaa_client)->event_manager, response, NULL); response->destroy(response); // Destroying event that was successfully sent event->destroy(event); }
static void kaa_demo_add_log_record(void *context) { static size_t log_record_counter = 0; float humidity = 0.0; float temperature = 0.0; if (dht11_read_val(DHT11_PIN, &humidity, &temperature) != 0) { printf("Failed to read data from sensor\n"); return; } ++log_record_counter; kaa_user_log_record_t *log_record = kaa_logging_sensor_data_create(); if (!log_record) { printf("Failed to create log record, error code %d\n", KAA_ERR_NOMEM); return; } log_record->sensor_id = kaa_string_copy_create("Sensor 1"); log_record->region = kaa_string_copy_create("Region 1"); log_record->model = kaa_string_copy_create("DHT11"); log_record->value = temperature; printf("Going to add %zuth log record: { id: '%s', region: '%s', model: '%s', val: %g }\n" , log_record_counter, log_record->sensor_id->data, log_record->region->data, log_record->model->data, log_record->value); kaa_error_t error_code = kaa_logging_add_record(kaa_client_get_context((kaa_client_t *)context)->log_collector, log_record, NULL); if (error_code) { printf("Failed to add log record, error code %d\n", error_code); } log_record->destroy(log_record); }
void kaa_demo_add_log_record() { ++log_record_counter; printf("Going to add %zuth log record\n", log_record_counter); kaa_user_log_record_t *log_record = kaa_logging_log_data_create(); if (!log_record) { KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_NOT_INITIALIZED, "Failed to allocate log record"); return; } log_record->level = ENUM_LEVEL_INFO; log_record->tag = kaa_string_move_create(KAA_DEMO_LOG_TAG, NULL); size_t log_message_buffer_size = strlen(KAA_DEMO_LOG_MESSAGE) + sizeof(log_record_counter); char log_message_buffer[log_message_buffer_size]; snprintf(log_message_buffer, log_message_buffer_size, "%s%zu", KAA_DEMO_LOG_MESSAGE, log_record_counter); log_record->message = kaa_string_copy_create(log_message_buffer); kaa_error_t error_code = kaa_logging_add_record(kaa_context_->log_collector, log_record); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to add log record"); log_record->destroy(log_record); }
static void test_string_deserialize(void **state) { (void)state; const char *plain_test_str1 = "test"; kaa_string_t *kaa_str1 = kaa_string_copy_create(plain_test_str1); ASSERT_NOT_NULL(kaa_str1); size_t expected_size = kaa_string_get_size(kaa_str1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_string_serialize(avro_writer, kaa_str1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); kaa_string_t *kaa_str2 = kaa_string_deserialize(avro_reader); ASSERT_NOT_NULL(kaa_str2); ASSERT_EQUAL(strcmp(kaa_str2->data, plain_test_str1), 0); ASSERT_EQUAL(strcmp(kaa_str2->data, kaa_str1->data), 0); kaa_string_destroy(kaa_str2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); kaa_string_destroy(kaa_str1); }
static void kaa_demo_add_log_record(void *context) { if (log_record_counter++ >= KAA_DEMO_LOGS_TO_SEND) { kaa_client_stop((kaa_client_t *)context); return; } printf("Going to add %zuth log record\n", log_record_counter); kaa_user_log_record_t *log_record = kaa_logging_log_data_create(); if (!log_record) { printf("Failed to create log record, error code %d\n", KAA_ERR_NOMEM); return; } log_record->level = ENUM_LEVEL_KAA_INFO; log_record->tag = kaa_string_move_create(KAA_DEMO_LOG_TAG, NULL); char log_message_buffer[32]; snprintf(log_message_buffer, 32, KAA_DEMO_LOG_MESSAGE"%zu", log_record_counter); log_record->message = kaa_string_copy_create(log_message_buffer); kaa_error_t error_code = kaa_logging_add_record(kaa_client_get_context(kaa_client)->log_collector, log_record); if (error_code) { printf("Failed to add log record, error code %d\n", error_code); } log_record->destroy(log_record); }
static void test_string_copy_create(void **state) { (void)state; const char *plain_test_str1 = "test"; ASSERT_NULL(kaa_string_copy_create(NULL)); kaa_string_t *kaa_str1 = kaa_string_copy_create(plain_test_str1); ASSERT_NOT_NULL(kaa_str1); ASSERT_NOT_NULL(kaa_str1->data); ASSERT_EQUAL(kaa_str1->destroy, &kaa_data_destroy); ASSERT_EQUAL(strcmp(kaa_str1->data, plain_test_str1), 0); kaa_string_destroy(kaa_str1); }
static void test_array_serialize(void **state) { (void)state; const char *plain_str = "data"; kaa_string_t *reference_kaa_str = kaa_string_copy_create(plain_str); size_t element_size = kaa_string_get_size(reference_kaa_str); kaa_string_destroy(reference_kaa_str); size_t array_size = 1 + rand() % 10; size_t expected_size = avro_long_get_size(array_size) + array_size * element_size + avro_long_get_size(0); char manual_buffer[expected_size]; avro_writer_t manual_avro_writer = avro_writer_memory(manual_buffer, expected_size); avro_binary_encoding.write_long(manual_avro_writer, array_size); kaa_list_t *avro_array = kaa_list_create(); size_t i = 0; for (i = 0; i < array_size; ++i) { kaa_string_t *array_data = kaa_string_copy_create(plain_str); kaa_list_push_back(avro_array, array_data); avro_binary_encoding.write_string(manual_avro_writer, array_data->data); } avro_binary_encoding.write_long(manual_avro_writer, 0); size_t actual_size = kaa_array_get_size(avro_array, kaa_string_get_size); ASSERT_EQUAL(actual_size, expected_size); char auto_buffer[actual_size]; avro_writer_t auto_avro_writer = avro_writer_memory(auto_buffer, actual_size); kaa_array_serialize(auto_avro_writer, avro_array, kaa_string_serialize); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); kaa_list_destroy(avro_array, kaa_string_destroy); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
void test_profile_sync_get_size(void) { KAA_TRACE_IN(logger); kaa_error_t error_code = KAA_ERR_NONE; kaa_profile_t *profile = kaa_profile_basic_endpoint_profile_test_create(); profile->profile_body = kaa_string_copy_create("dummy"); size_t serialized_profile_size = profile->get_size(profile); char *serialized_profile = (char *) KAA_MALLOC(serialized_profile_size * sizeof(char)); avro_writer_t writer = avro_writer_memory(serialized_profile, serialized_profile_size); profile->serialize(writer, profile); size_t expected_size = KAA_EXTENSION_HEADER_SIZE + sizeof(uint32_t) // profile size + kaa_aligned_size_get(serialized_profile_size); size_t profile_sync_size = 0; error_code = kaa_profile_manager_update_profile(profile_manager, profile); ASSERT_EQUAL(error_code, KAA_ERR_NONE); status->is_registered = true; error_code = kaa_profile_request_get_size(profile_manager, &profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(expected_size, profile_sync_size); status->is_registered = false; expected_size += sizeof(uint32_t) + TEST_PUB_KEY_SIZE; error_code = kaa_profile_request_get_size(profile_manager, &profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(expected_size, profile_sync_size); const char *access_token = "access token"; error_code = kaa_profile_manager_set_endpoint_access_token(profile_manager, access_token); expected_size += sizeof(uint32_t) + strlen(access_token); error_code = kaa_profile_request_get_size(profile_manager, &profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(expected_size, profile_sync_size); avro_writer_free(writer); KAA_FREE(serialized_profile); profile->destroy(profile); KAA_TRACE_OUT(logger); }
void kaa_on_device_info_request(void *context , kaa_device_event_class_family_device_info_request_t *event , kaa_endpoint_id_p source) { log_info("DeviceInfoRequest event received!"); kaa_device_event_class_family_device_info_response_t *response = kaa_device_event_class_family_device_info_response_create(); kaa_device_event_class_family_device_info_t *info = kaa_device_event_class_family_device_info_create(); info->name = kaa_string_copy_create(device_name); info->model = kaa_string_copy_create(device_model); response->device_info = info; kaa_event_manager_send_kaa_device_event_class_family_device_info_response(kaa_client_get_context(kaa_client)->event_manager, response, source); log_info("DeviceInfoResponse sent!"); response->destroy(response); event->destroy(event); }
void test_profile_update(void) { KAA_TRACE_IN(logger); kaa_profile_t *profile1 = kaa_profile_basic_endpoint_profile_test_create(); profile1->profile_body = kaa_string_copy_create("dummy"); kaa_error_t error = kaa_profile_manager_update_profile(profile_manager, profile1); ASSERT_EQUAL(error, KAA_ERR_NONE); bool need_resync = false; error = kaa_profile_need_profile_resync(profile_manager, &need_resync); ASSERT_EQUAL(error, KAA_ERR_NONE); ASSERT_TRUE(need_resync); error = kaa_profile_manager_update_profile(profile_manager, profile1); ASSERT_EQUAL(error, KAA_ERR_NONE); error = kaa_profile_need_profile_resync(profile_manager, &need_resync); ASSERT_EQUAL(error, KAA_ERR_NONE); ASSERT_FALSE(need_resync); profile1->destroy(profile1); kaa_profile_t *profile2 = kaa_profile_basic_endpoint_profile_test_create(); profile2->profile_body = kaa_string_copy_create("new_dummy"); error = kaa_profile_manager_update_profile(profile_manager, profile2); ASSERT_EQUAL(error, KAA_ERR_NONE); error = kaa_profile_need_profile_resync(profile_manager, &need_resync); ASSERT_EQUAL(error, KAA_ERR_NONE); ASSERT_TRUE(need_resync); profile2->destroy(profile2); KAA_TRACE_OUT(logger); }
static void test_string_get_size(void **state) { (void)state; ASSERT_EQUAL(kaa_string_get_size(NULL), 0); const char *plain_test_str1 = "test"; kaa_string_t *kaa_str1 = kaa_string_copy_create(plain_test_str1); ASSERT_NOT_NULL(kaa_str1); size_t plain_test_str1_len = strlen(plain_test_str1); ASSERT_EQUAL(kaa_string_get_size(kaa_str1), avro_long_get_size(plain_test_str1_len) + plain_test_str1_len); kaa_string_destroy(kaa_str1); }
static void test_string_serialize(void **state) { (void)state; const char *plain_test_str1 = "test"; kaa_string_t *kaa_str1 = kaa_string_copy_create(plain_test_str1); ASSERT_NOT_NULL(kaa_str1); size_t expected_size = kaa_string_get_size(kaa_str1); char auto_buffer[expected_size]; avro_writer_t auto_avro_writer = avro_writer_memory(auto_buffer, expected_size); char manual_buffer[expected_size]; avro_writer_t manual_avro_writer = avro_writer_memory(manual_buffer, expected_size); ASSERT_EQUAL(auto_avro_writer->buf, auto_buffer); ASSERT_EQUAL(auto_avro_writer->written, 0); ASSERT_EQUAL((size_t)auto_avro_writer->len, expected_size); kaa_string_serialize(auto_avro_writer, NULL); ASSERT_EQUAL(auto_avro_writer->buf, auto_buffer); ASSERT_EQUAL(auto_avro_writer->written, 0); ASSERT_EQUAL((size_t)auto_avro_writer->len, expected_size); kaa_string_t fake_kaa_str = { NULL, NULL}; kaa_string_serialize(auto_avro_writer, &fake_kaa_str); ASSERT_EQUAL(auto_avro_writer->buf, auto_buffer); ASSERT_EQUAL(auto_avro_writer->written, 0); ASSERT_EQUAL((size_t)auto_avro_writer->len, expected_size); /* * REAL DATA */ kaa_string_serialize(auto_avro_writer, kaa_str1); avro_binary_encoding.write_string(manual_avro_writer, plain_test_str1); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); kaa_string_destroy(kaa_str1); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
void test_profile_is_set(void) { KAA_TRACE_IN(logger); #if KAA_PROFILE_SCHEMA_VERSION > 0 ASSERT_FALSE(kaa_profile_manager_is_profile_set(profile_manager)); kaa_profile_t *profile = kaa_profile_basic_endpoint_profile_test_create(); profile->profile_body = kaa_string_copy_create("test"); kaa_error_t error = kaa_profile_manager_update_profile(profile_manager, profile); profile->destroy(profile); ASSERT_EQUAL(error, KAA_ERR_NONE); ASSERT_TRUE(kaa_profile_manager_is_profile_set(profile_manager)); #else ASSERT_TRUE(kaa_profile_manager_is_profile_set(profile_manager)); #endif KAA_TRACE_OUT(logger); }
static void kaa_demo_add_log_record(void *context) { (void) context; if (log_record_counter >= KAA_DEMO_LOGS_TO_SEND) { printf("All logs are sent, waiting for responce\n"); if (log_successfully_sent_counter == KAA_DEMO_LOGS_TO_SEND) { printf("All logs successfully sent, stopping demo...\n"); kaa_client_stop(context); } return; } printf("Going to add %zuth log record\n", log_record_counter); kaa_user_log_record_t *log_record = kaa_logging_log_data_create(); if (!log_record) { printf("Failed to create log record, error code %d\n", KAA_ERR_NOMEM); return; } log_record->level = ENUM_LEVEL_KAA_INFO; log_record->tag = kaa_string_move_create(KAA_DEMO_LOG_TAG, NULL); char log_message_buffer[KAA_DEMO_LOG_BUF_SZ]; snprintf(log_message_buffer, KAA_DEMO_LOG_BUF_SZ, KAA_DEMO_LOG_MESSAGE"%zu", log_record_counter); log_record->message = kaa_string_copy_create(log_message_buffer); kaa_log_record_info_t log_info; kaa_error_t error_code = kaa_logging_add_record(kaa_client_get_context(kaa_client)->log_collector, log_record, &log_info); if (error_code) { printf("Failed to add log record, error code %d\n", error_code); } else { printf("Log record: %u added to bucket %u\n", log_info.log_id, log_info.bucket_id); } log_record->destroy(log_record); log_record_counter++; }
void test_profile_sync_serialize(void) { KAA_TRACE_IN(logger); kaa_error_t error_code; kaa_platform_message_writer_t *manual_writer; kaa_platform_message_writer_t *auto_writer; const char *access_token = "access token"; const size_t access_token_size = strlen(access_token); kaa_profile_t *profile = kaa_profile_basic_endpoint_profile_test_create(); profile->profile_body = kaa_string_copy_create("dummy"); size_t serialized_profile_size = profile->get_size(profile); char *serialized_profile = (char *) KAA_MALLOC(serialized_profile_size * sizeof(char)); avro_writer_t avro_writer = avro_writer_memory(serialized_profile, serialized_profile_size); profile->serialize(avro_writer, profile); error_code = kaa_profile_manager_update_profile(profile_manager, profile); ASSERT_EQUAL(error_code, KAA_ERR_NONE); status->is_registered = false; error_code = kaa_profile_manager_set_endpoint_access_token(profile_manager, access_token); ASSERT_EQUAL(error_code, KAA_ERR_NONE); size_t profile_sync_size; error_code = kaa_profile_request_get_size(profile_manager, &profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); char buffer[profile_sync_size]; error_code = kaa_platform_message_writer_create(&manual_writer, buffer, profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); uint32_t network_order_32; error_code = kaa_platform_message_write_extension_header(manual_writer , KAA_PROFILE_EXTENSION_TYPE , 0 , profile_sync_size - KAA_EXTENSION_HEADER_SIZE); ASSERT_EQUAL(error_code, KAA_ERR_NONE); bool need_resync = true; ASSERT_EQUAL(kaa_profile_need_profile_resync(profile_manager, &need_resync), KAA_ERR_NONE); network_order_32 = KAA_HTONL(0); if (need_resync) network_order_32 = KAA_HTONL(serialized_profile_size); error_code = kaa_platform_message_write(manual_writer, &network_order_32, sizeof(uint32_t)); ASSERT_EQUAL(error_code, KAA_ERR_NONE); if (need_resync) { error_code = kaa_platform_message_write_aligned(manual_writer, serialized_profile, serialized_profile_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); } network_order_32 = KAA_HTONS(TEST_PUB_KEY_SIZE) << 16 | PUB_KEY_VALUE; error_code = kaa_platform_message_write(manual_writer, &network_order_32, sizeof(uint32_t)); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_platform_message_write_aligned(manual_writer, test_ep_key, TEST_PUB_KEY_SIZE); ASSERT_EQUAL(error_code, KAA_ERR_NONE); network_order_32 = KAA_HTONS(access_token_size) << 16 | ACCESS_TOKEN_VALUE; error_code = kaa_platform_message_write(manual_writer, &network_order_32, sizeof(uint32_t)); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_platform_message_write_aligned(manual_writer, access_token, access_token_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); char buffer2[profile_sync_size]; error_code = kaa_platform_message_writer_create(&auto_writer, buffer2, profile_sync_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_profile_request_serialize(profile_manager, auto_writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = (memcmp(buffer, buffer2, profile_sync_size) == 0 ? KAA_ERR_NONE : KAA_ERR_BADDATA); ASSERT_EQUAL(error_code, KAA_ERR_NONE); KAA_FREE(serialized_profile); avro_writer_free(avro_writer); profile->destroy(profile); kaa_platform_message_writer_destroy(auto_writer); kaa_platform_message_writer_destroy(manual_writer); KAA_TRACE_OUT(logger); }
void test_create_request(void **state) { (void)state; kaa_user_log_record_t *test_log_record = kaa_test_log_record_create(); test_log_record->data = kaa_string_copy_create(TEST_LOG_BUFFER); size_t test_log_record_size = test_log_record->get_size(test_log_record); kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); strategy.decision = NOOP; strategy.max_parallel_uploads = UINT32_MAX; kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 2 * test_log_record_size, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, create_mock_storage(), &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); size_t expected_size = 0; error_code = kaa_logging_request_get_size(log_collector, &expected_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); uint8_t buffer[expected_size]; kaa_platform_message_writer_t *writer = NULL; error_code = kaa_platform_message_writer_create(&writer, buffer, expected_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_NOT_NULL(writer); error_code = kaa_logging_request_serialize(log_collector, writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); kaa_platform_message_writer_destroy(writer); uint8_t *buf_cursor = buffer; ASSERT_EQUAL(KAA_EXTENSION_LOGGING, KAA_HTONS(*(uint16_t *)buf_cursor)); buf_cursor += sizeof(uint16_t); uint8_t options[] = { 0x00, 0x01 }; ASSERT_EQUAL(memcmp(buf_cursor, options, 2), 0); buf_cursor += 2; ASSERT_EQUAL(*(uint32_t *) buf_cursor, KAA_HTONL(20)); buf_cursor += sizeof(uint32_t); uint8_t request_id_records_count[] = { 0x00, 0x01, 0x00, 0x01 }; ASSERT_EQUAL(memcmp(buf_cursor, request_id_records_count, 4), 0); buf_cursor += 4; uint8_t record_buf[test_log_record_size]; avro_writer_t avro_writer = avro_writer_memory((char *)record_buf, test_log_record_size); test_log_record->serialize(avro_writer, test_log_record); avro_writer_free(avro_writer); ASSERT_EQUAL(*(uint32_t *) buf_cursor, KAA_HTONL(test_log_record_size)); buf_cursor += sizeof(uint32_t); ASSERT_EQUAL(memcmp(buf_cursor, record_buf, test_log_record_size), 0); kaa_log_collector_destroy(log_collector); test_log_record->destroy(test_log_record); } void test_response(void **state) { (void)state; srand(time(NULL)); kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); mock_storage_context_t *storage = create_mock_storage(); kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 1024, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, storage, &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); uint32_t response_count = 2; size_t response_buffer_size = sizeof(uint32_t) + sizeof(uint32_t) * response_count; uint8_t response_buffer[response_buffer_size]; uint8_t *response = response_buffer; *((uint32_t *)response) = KAA_HTONL(response_count); response += sizeof(uint32_t); /* First response */ *((uint16_t *)response) = KAA_HTONS(rand()); response += sizeof(uint16_t); *((uint8_t *)response) = 0x0; // SUCCESS response += sizeof(uint8_t); *((uint8_t *)response) = 0; response += sizeof(uint8_t); /* Second response */ *((uint16_t *)response) = KAA_HTONS(rand()); response += sizeof(uint16_t); *((uint8_t *)response) = 0x1; // FAILURE response += sizeof(uint8_t); *((uint8_t *)response) = rand() % 4; response += sizeof(uint8_t); kaa_platform_message_reader_t *reader = NULL; error_code = kaa_platform_message_reader_create(&reader, response_buffer, response_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_NOT_NULL(reader); error_code = kaa_logging_handle_server_sync(log_collector, reader, 0, response_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_TRUE(strategy.on_failure_count); ASSERT_TRUE(storage->on_remove_by_id_count); ASSERT_TRUE(storage->on_unmark_by_id_count); kaa_platform_message_reader_destroy(reader); kaa_log_collector_destroy(log_collector); } void test_timeout(void **state) { (void)state; kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); kaa_user_log_record_t *test_log_record = kaa_test_log_record_create(); test_log_record->data = kaa_string_copy_create(TEST_LOG_BUFFER); size_t test_log_record_size = test_log_record->get_size(test_log_record); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); strategy.timeout = TEST_TIMEOUT; strategy.decision = NOOP; strategy.max_parallel_uploads = UINT32_MAX; kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 2 * test_log_record_size, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, create_mock_storage(), &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); size_t request_buffer_size = 256; uint8_t request_buffer[request_buffer_size]; kaa_platform_message_writer_t *writer = NULL; error_code = kaa_platform_message_writer_create(&writer, request_buffer, request_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_request_serialize(log_collector, writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); sleep(TEST_TIMEOUT + 1); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_TRUE(strategy.on_timeout_count); test_log_record->destroy(test_log_record); kaa_platform_message_writer_destroy(writer); kaa_log_collector_destroy(log_collector); } void test_decline_timeout(void **state) { (void)state; kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); kaa_user_log_record_t *test_log_record = kaa_test_log_record_create(); test_log_record->data = kaa_string_copy_create(TEST_LOG_BUFFER); size_t test_log_record_size = test_log_record->get_size(test_log_record); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); strategy.timeout = TEST_TIMEOUT; strategy.decision = NOOP; strategy.max_parallel_uploads = UINT32_MAX; mock_storage_context_t *storage = create_mock_storage(); ASSERT_NOT_NULL(storage); kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 2 * test_log_record_size, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, storage, &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); size_t request_buffer_size = 256; uint8_t request_buffer[request_buffer_size]; kaa_platform_message_writer_t *writer = NULL; error_code = kaa_platform_message_writer_create(&writer, request_buffer, request_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_request_serialize(log_collector, writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); sleep(TEST_TIMEOUT + 1); uint16_t bucket_id = *((uint16_t *)(request_buffer + KAA_EXTENSION_HEADER_SIZE)); bucket_id = KAA_NTOHS(bucket_id); uint32_t response_count = 1; size_t response_buffer_size = sizeof(uint32_t) + sizeof(uint32_t) * response_count; uint8_t response_buffer[response_buffer_size]; uint8_t *response = response_buffer; *((uint32_t *)response) = KAA_HTONL(response_count); response += sizeof(uint32_t); /* First response */ *((uint16_t *)response) = KAA_HTONS(bucket_id); response += sizeof(uint16_t); *((uint8_t *)response) = 0x0; // SUCCESS response += sizeof(uint8_t); *((uint8_t *)response) = 0; response += sizeof(uint8_t); kaa_platform_message_reader_t *reader = NULL; error_code = kaa_platform_message_reader_create(&reader, response_buffer, response_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_NOT_NULL(reader); error_code = kaa_logging_handle_server_sync(log_collector, reader, 0, response_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_TRUE(storage->on_remove_by_id_count); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_FALSE(strategy.on_timeout_count); test_log_record->destroy(test_log_record); kaa_platform_message_writer_destroy(writer); kaa_platform_message_reader_destroy(reader); kaa_log_collector_destroy(log_collector); } void test_max_parallel_uploads_with_log_sync(void **state) { (void)state; uint32_t channel_id = 0; kaa_transport_channel_interface_t transport_context; test_kaa_channel_create(&transport_context); kaa_channel_manager_add_transport_channel(channel_manager, &transport_context, &channel_id); kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); kaa_user_log_record_t *test_log_record = kaa_test_log_record_create(); test_log_record->data = kaa_string_copy_create(TEST_LOG_BUFFER); size_t test_log_size = test_log_record->get_size(test_log_record); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); strategy.timeout = INT16_MAX; strategy.decision = UPLOAD; mock_storage_context_t *storage = create_mock_storage(); ASSERT_NOT_NULL(storage); kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 2 * test_log_size, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, storage, &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); /* * Ensure the log delivery is forbidden at all. */ strategy.max_parallel_uploads = 0; error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(((mock_transport_channel_context_t *)transport_context.context)->on_sync_count, 0); /* * Ensure the first request is allowed. */ strategy.max_parallel_uploads = 1; error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); ASSERT_EQUAL(((mock_transport_channel_context_t *)transport_context.context)->on_sync_count, 1); /* * Do the first request to remember the delivery timeout of the log batch. */ size_t request_buffer_size = 256; uint8_t request_buffer[request_buffer_size]; kaa_platform_message_writer_t *writer = NULL; error_code = kaa_platform_message_writer_create(&writer, request_buffer, request_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_request_serialize(log_collector, writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); /* * Ensure the second request is forbidden. */ ASSERT_EQUAL(((mock_transport_channel_context_t *)transport_context.context)->on_sync_count, 1); /* * Clean up. */ error_code = kaa_channel_manager_remove_transport_channel(channel_manager, channel_id); ASSERT_EQUAL(error_code, KAA_ERR_NONE); test_log_record->destroy(test_log_record); kaa_log_collector_destroy(log_collector); } void test_max_parallel_uploads_with_sync_all(void **state) { (void)state; uint32_t channel_id = 0; kaa_transport_channel_interface_t transport_context; test_kaa_channel_create(&transport_context); kaa_channel_manager_add_transport_channel(channel_manager, &transport_context, &channel_id); kaa_log_collector_t *log_collector = NULL; kaa_error_t error_code = kaa_log_collector_create(&log_collector, status, channel_manager, logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); kaa_user_log_record_t *test_log_record = kaa_test_log_record_create(); test_log_record->data = kaa_string_copy_create(TEST_LOG_BUFFER); size_t test_log_size = test_log_record->get_size(test_log_record); mock_strategy_context_t strategy; memset(&strategy, 0, sizeof(mock_strategy_context_t)); strategy.timeout = INT16_MAX; strategy.decision = UPLOAD; mock_storage_context_t *storage = create_mock_storage(); ASSERT_NOT_NULL(storage); kaa_log_bucket_constraints_t constraints = { .max_bucket_size = 2 * test_log_size, .max_bucket_log_count = UINT32_MAX, }; error_code = kaa_logging_init(log_collector, storage, &strategy, &constraints); ASSERT_EQUAL(error_code, KAA_ERR_NONE); /* * Ensure the log delivery is forbidden at all. */ strategy.max_parallel_uploads = 0; error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); size_t expected_size = 0; error_code = kaa_logging_request_get_size(log_collector, &expected_size); assert_int_equal(KAA_ERR_NONE, error_code); ASSERT_FALSE(expected_size); /* * Ensure the first request is allowed. */ strategy.max_parallel_uploads = 1; error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); /* * Do the first request to remember the delivery timeout of the log batch. */ error_code = kaa_logging_request_get_size(log_collector, &expected_size); assert_int_equal(KAA_ERR_NONE, error_code); ASSERT_TRUE(expected_size); size_t request_buffer_size = 256; uint8_t request_buffer[request_buffer_size]; kaa_platform_message_writer_t *writer = NULL; error_code = kaa_platform_message_writer_create(&writer, request_buffer, request_buffer_size); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_request_serialize(log_collector, writer); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_add_record(log_collector, test_log_record, NULL); ASSERT_EQUAL(error_code, KAA_ERR_NONE); /* * Ensure the second request is forbidden. */ error_code = kaa_logging_request_get_size(log_collector, &expected_size); assert_int_equal(KAA_ERR_NONE, error_code); ASSERT_FALSE(expected_size); /* * Clean up. */ error_code = kaa_channel_manager_remove_transport_channel(channel_manager, channel_id); ASSERT_EQUAL(error_code, KAA_ERR_NONE); test_log_record->destroy(test_log_record); kaa_log_collector_destroy(log_collector); } /* ---------------------------------------------------------------------------*/ /* Log delivery tests */ /* ---------------------------------------------------------------------------*/ /* Server chunk, managed by a corresponding reader object. * Perfectly packed. Packed attribute is intentionally avoided. */ struct response_chunk { uint8_t bucket_id[2]; /* 16 bits for bucket ID */ uint8_t resp_code; /* 8 bits for response code. 0 == SUCCESS, 1 == FAILURE */ // cppcheck-suppress unusedStructMember uint8_t reserved; /* Should be 0 */ }; struct response_packet { uint8_t resp_cnt[4]; /* 32 bits for amount of responces in buffer */ struct response_chunk resps[]; /* Responses itself */ }; #define RESP_PACKETS 2 /* Amount of response packets */ #define RESP_SUCCESS_IDX 0 /* Index of successfull response */ #define RESP_FAILURE_IDX 1 /* Index of failed response */ #define TEST_BUFFER_SIZE 1024 #define TEST_EXT_OP 0 /* Simple stub */ static mock_strategy_context_t test_strategy1; static mock_strategy_context_t test_strategy2; static mock_storage_context_t *test_storage1; static mock_storage_context_t *test_storage2; static kaa_log_collector_t *log_collector; static size_t test_log_record_size = TEST_BUFFER_SIZE; /* Will contain response_packet. Thus required to be aligned. */ static uint32_t test_reader_buffer[TEST_BUFFER_SIZE / 4]; static uint32_t test_writer_buffer[TEST_BUFFER_SIZE / 4]; /* Portion of the test buffer filled with valid data */ static size_t test_filled_size; static kaa_platform_message_reader_t *test_reader; static kaa_platform_message_writer_t *test_writer; static kaa_user_log_record_t *test_log_record; /* Values to be checked inside mock event function */ static void *expected_ctx; static int check_bucket; static uint16_t expected_bucked_id; /* Required to trace generic mock function calls */ static int call_is_expected; static int call_completed; /* Required to trace on fail mock function calls */ static int failed_call_is_expected; static int failed_call_completed; /* Required to trace on success mock function calls */ static int success_call_is_expected; static int success_call_completed; /* Required to trace on timeout mock function calls */ static int timeout_call_is_expected; static int timeout_call_completed; /* Mock event functions */ static void mock_log_event_generic_fn(void *ctx, const kaa_log_bucket_info_t *bucket) { ASSERT_TRUE(call_is_expected); ASSERT_NOT_NULL(bucket); /* Shouldn't be NULL no matter what */ if (check_bucket) { ASSERT_EQUAL(expected_bucked_id, bucket->bucket_id); } ASSERT_EQUAL(expected_ctx, ctx); call_completed++; }
void test_deserializing(void) { KAA_TRACE_IN(context->logger); kaa_notification_t *notification = kaa_notification_notification_create(); const char *message = "Hello World!!!\n"; notification->message = kaa_string_copy_create(message); size = (sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) /*that was header*/+ sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t) /*extension options and payload length*/ + sizeof(uint32_t) + sizeof(uint32_t) /*state sqn and delta status*/ + sizeof(uint16_t) + sizeof(uint16_t) /*field id and topic count*/ + sizeof(uint64_t) /*topic ID*/ + sizeof(uint16_t) + sizeof(uint16_t) /*subscriptions type + topic name length*/ + sizeof(uint32_t) /*topic name + padding */ + sizeof(uint16_t) + sizeof(uint16_t) /*field id and notifications count*/ + sizeof(uint32_t) /* Notification sqn */ + sizeof(uint16_t) + sizeof(uint16_t) /* Notification type + uid length */+ sizeof (uint32_t) /* notification body size*/ + sizeof (uint64_t) /*Topic Id*/ + /*Not unicast notifications*/ + kaa_aligned_size_get(notification->get_size(notification))); char *unserialized_buffer = (char *)KAA_MALLOC(size); ASSERT_NOT_NULL(unserialized_buffer); buffer_pointer = unserialized_buffer; memset(unserialized_buffer, 0, size); *(uint32_t *)unserialized_buffer = KAA_HTONL((uint32_t) KAA_PLATFORM_PROTOCOL_ID); //KAA_HTONL(KAA_PLATFORM_PROTOCOL_ID); unserialized_buffer += sizeof(uint32_t); *(uint16_t *)unserialized_buffer = KAA_HTONS((uint16_t)1); unserialized_buffer += sizeof(uint16_t); *(uint16_t *)unserialized_buffer = KAA_HTONS((uint16_t)1); // extension count unserialized_buffer += sizeof(uint16_t); *(uint8_t *)unserialized_buffer = (uint8_t)KAA_NOTIFICATION_EXTENSION_TYPE; unserialized_buffer += sizeof(uint8_t); unserialized_buffer += sizeof(uint8_t) + sizeof(uint16_t); // pass by extension options uint32_t payload_info = sizeof(uint32_t) + sizeof(uint32_t) /*state sqn and delta status*/ + sizeof(uint16_t) + sizeof(uint16_t) /*field id and topic count*/ + sizeof(uint64_t) /*topic ID*/ + sizeof(uint16_t) + sizeof(uint16_t) /*subscriptions type + topic name length*/ + sizeof(uint32_t) /*topic name + padding */ + sizeof(uint16_t) + sizeof(uint16_t) /*field id and notifications count*/ + sizeof(uint32_t) /* Notification sqn */ + sizeof(uint16_t) + sizeof(uint16_t) /* Notification type + uid length */+ sizeof (uint32_t) /* notification body size*/ + sizeof (uint64_t) /*Topic Id*/ + kaa_aligned_size_get(notification->get_size(notification)); *(uint32_t *)unserialized_buffer = KAA_HTONL((uint32_t) payload_info); unserialized_buffer += sizeof(uint32_t); *(uint32_t *)unserialized_buffer = KAA_HTONL((uint32_t)455); //Notification sqn unserialized_buffer += sizeof(uint32_t); *(uint32_t *)unserialized_buffer = KAA_HTONL((uint32_t)2); // Delta status unserialized_buffer += sizeof(uint32_t); //TOPICS *(uint8_t *)unserialized_buffer = (uint8_t)0; unserialized_buffer += sizeof(uint16_t); *(uint16_t *)unserialized_buffer = KAA_HTONS ((uint16_t)1); // topics count unserialized_buffer += sizeof(uint16_t); *(uint64_t *)unserialized_buffer = KAA_HTONLL((uint64_t)22); //topic id unserialized_buffer += sizeof(uint64_t); *(uint8_t *)unserialized_buffer = (uint8_t)OPTIONAL_SUBSCRIPTION; unserialized_buffer += sizeof(uint16_t); *(uint16_t *)unserialized_buffer = KAA_HTONS((uint16_t)4); //KAA unserialized_buffer += sizeof(uint16_t); *unserialized_buffer++ = 'K';*unserialized_buffer++ = 'A'; // topic name + padding *unserialized_buffer++ = 'A';*unserialized_buffer++ = 'A'; unserialized_buffer += (4 - kaa_aligned_size_get(4)); //----------------------------------------------------------------------- *(uint8_t *)unserialized_buffer = (uint8_t)1; //Notification field ID unserialized_buffer += sizeof(uint16_t); *(uint16_t *)unserialized_buffer = KAA_HTONS ((uint16_t)1); // notitifications count unserialized_buffer += sizeof(uint16_t); *(uint32_t *)unserialized_buffer = KAA_HTONL((uint32_t)99); //sqn pointer_to_sqn = unserialized_buffer; // To have the possibility to change sqn. unserialized_buffer += sizeof(uint32_t); *(uint8_t *)unserialized_buffer = (uint8_t)0x1; //notification type unserialized_buffer += sizeof(uint16_t); *(uint16_t *)unserialized_buffer = KAA_HTONS ((uint16_t) 0); //uid length unserialized_buffer += sizeof(uint16_t); *(uint32_t *)unserialized_buffer = KAA_HTONL ((uint32_t)notification->get_size(notification)); unserialized_buffer += sizeof(uint32_t); *(uint64_t *)unserialized_buffer = KAA_HTONLL((uint64_t)22); unserialized_buffer += sizeof(uint64_t); avro_writer_t avro_writer = avro_writer_memory(unserialized_buffer, notification->get_size(notification)); notification->serialize(avro_writer, notification); err = kaa_platform_protocol_process_server_sync(context->platform_protocol, buffer_pointer, size); avro_writer_free(avro_writer); ASSERT_EQUAL(err, KAA_ERR_NONE); notification->destroy(notification); KAA_TRACE_OUT(context->logger); }