void ext_status_read(char **buffer, size_t *buffer_size, bool *needs_deallocation) { *buffer = NULL; *buffer_size = 0; *needs_deallocation = true; FILE* status_file = fopen(KAA_STATUS_STORAGE, "rb"); if (!status_file) { return; } fseek(status_file, 0, SEEK_END); *buffer_size = ftell(status_file); *buffer = (char *) KAA_MALLOC((*buffer_size) * sizeof(char)); if (*buffer == NULL) { *buffer_size = 0; fclose(status_file); return; } fseek(status_file, 0, SEEK_SET); if (fread(*buffer, *buffer_size, 1, status_file) == 0) { *buffer_size = 0; KAA_FREE(*buffer); } *needs_deallocation = true; fclose(status_file); }
void test_list_for_each() { KAA_TRACE_IN(logger); kaa_list_t *list = kaa_list_create(); ASSERT_NOT_NULL(list); int32_t *number1_ptr; int node_number = 4; for (int i = 0; i < node_number; ++i) { number1_ptr = (int32_t *)KAA_MALLOC(sizeof(int32_t *)); ASSERT_NOT_NULL(number1_ptr); *number1_ptr = rand(); kaa_list_push_back(list, number1_ptr); } int number2 = rand(); kaa_list_for_each(kaa_list_begin(list), kaa_list_back(list), (process_data)&test_process_data, &number2); kaa_list_node_t *it = kaa_list_begin(list); while (it) { ASSERT_EQUAL(*(int32_t *)kaa_list_get_data(kaa_list_begin(list)), number2); it = kaa_list_next(it); } kaa_list_destroy(list, NULL); KAA_TRACE_OUT(logger); }
void test_list_push_front() { KAA_TRACE_IN(logger); kaa_list_t *list = kaa_list_create(); ASSERT_NOT_NULL(list); int32_t *number; int node_number = 2; for (int i = 0; i < node_number; ++i) { number = (int32_t *)KAA_MALLOC(sizeof(int32_t *)); ASSERT_NOT_NULL(number); *number = rand(); kaa_list_push_front(list, number); } ASSERT_EQUAL(kaa_list_get_size(list), node_number); ASSERT_NOT_NULL(kaa_list_begin(list)); ASSERT_NULL(kaa_list_prev(kaa_list_begin(list))); ASSERT_EQUAL((*(int32_t *)kaa_list_get_data(kaa_list_begin(list))), *number); kaa_list_destroy(list, NULL); KAA_TRACE_OUT(logger); }
kaa_error_t kaa_platform_protocol_create(kaa_platform_protocol_t **platform_protocol_p, kaa_logger_t *logger, kaa_status_t *status) { if (!platform_protocol_p || !logger || !status) { return KAA_ERR_BADPARAM; } *platform_protocol_p = KAA_MALLOC(sizeof(**platform_protocol_p)); if (!*platform_protocol_p) { return KAA_ERR_NOMEM; } **platform_protocol_p = (kaa_platform_protocol_t){ .request_id = 0, .status = status, .logger = logger, }; return KAA_ERR_NONE; } void kaa_platform_protocol_destroy(kaa_platform_protocol_t *self) { KAA_FREE(self); }
/** @deprecated Use kaa_extension_profile_init(). */ kaa_error_t kaa_profile_manager_create(kaa_profile_manager_t **profile_manager_p, kaa_status_t *status , kaa_channel_manager_t *channel_manager, kaa_logger_t *logger) { if (!profile_manager_p || !channel_manager || !status) { return KAA_ERR_BADPARAM; } kaa_profile_manager_t *profile_manager = KAA_MALLOC(sizeof(kaa_profile_manager_t)); if (!profile_manager) { return KAA_ERR_NOMEM; } /** * KAA_CALLOC is really needed. */ profile_manager->extension_data = KAA_CALLOC(1, sizeof(kaa_profile_extension_data_t)); if (!profile_manager->extension_data) { KAA_FREE(profile_manager); return KAA_ERR_NOMEM; } profile_manager->need_resync = true; profile_manager->profile_body.size = 0; profile_manager->profile_body.buffer = NULL; profile_manager->channel_manager = channel_manager; profile_manager->status = status; profile_manager->logger = logger; ext_calculate_sha_hash(NULL, 0, profile_manager->profile_hash); ext_copy_sha_hash(profile_manager->status->profile_hash, profile_manager->profile_hash); *profile_manager_p = profile_manager; return KAA_ERR_NONE; }
static void test_list_sort() { kaa_list_t *list = kaa_list_create(); ASSERT_NOT_NULL(list); uint64_t node_number = 100; for (uint64_t i = 0; i < node_number; ++i) { test_list_node_t *node = KAA_MALLOC(sizeof(test_list_node_t)); ASSERT_NOT_NULL(node); node->id = (uint64_t) rand(); kaa_list_push_back(list, node); } ASSERT_EQUAL(kaa_list_get_size(list), node_number); kaa_list_sort(list,&test_kaa_list_predicate); kaa_list_node_t *it,*next; it = kaa_list_begin(list); next = kaa_list_next(it); while (it && next) { ASSERT_TRUE(((test_list_node_t*)kaa_list_get_data(it))->id <= ((test_list_node_t*)kaa_list_get_data(next))->id); it = next; next = kaa_list_next(it); } kaa_list_destroy(list, NULL); }
static void test_string_move_create(void **state) { (void)state; ASSERT_NULL(kaa_string_move_create(NULL, NULL)); const char *plain_test_str1 = "test"; kaa_string_t *kaa_str1 = kaa_string_move_create(plain_test_str1, NULL); ASSERT_NOT_NULL(kaa_str1); ASSERT_NOT_NULL(kaa_str1->data); ASSERT_NULL(kaa_str1->destroy); ASSERT_EQUAL(strcmp(kaa_str1->data, plain_test_str1), 0); kaa_string_destroy(kaa_str1); kaa_str1 = NULL; char *plain_test_str2 = (char *)KAA_MALLOC(strlen(plain_test_str1) + 1); ASSERT_NOT_NULL(plain_test_str2); strcpy(plain_test_str2, plain_test_str1); kaa_string_t *kaa_str2 = kaa_string_move_create(plain_test_str2, &kaa_data_destroy); ASSERT_NOT_NULL(kaa_str2); ASSERT_NOT_NULL(kaa_str2->data); ASSERT_EQUAL(kaa_str2->destroy, &kaa_data_destroy); ASSERT_EQUAL(strcmp(kaa_str2->data, plain_test_str1), 0); kaa_string_destroy(kaa_str2); }
static float *create_float(void) { float *float_value = (float *)KAA_MALLOC(sizeof(float)); KAA_RETURN_IF_NIL(float_value, NULL); *float_value = rand() % rand(); return float_value; }
void test_empty_log_collector_extension_count(void) { kaa_service_t service = KAA_SERVICE_LOGGING; info = (kaa_serialize_info_t *) KAA_MALLOC(sizeof(kaa_serialize_info_t)); info->services = &service; info->services_count = 1; info->allocator = &allocator; info->allocator_context = mock; void *log_storage_context = NULL; void *log_upload_strategy_context = NULL; kaa_error_t error_code = ext_unlimited_log_storage_create(&log_storage_context, kaa_context->logger); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = ext_log_upload_strategy_create(kaa_context , &log_upload_strategy_context , KAA_LOG_UPLOAD_VOLUME_STRATEGY); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_logging_init(kaa_context->log_collector, log_storage_context, log_upload_strategy_context); ASSERT_EQUAL(error_code, KAA_ERR_NONE); error_code = kaa_platform_protocol_serialize_client_sync(kaa_context->platform_protocol, info, &buffer, &buffer_size); KAA_FREE(info); ASSERT_EQUAL(error_code, KAA_ERR_NONE); char count_of_extensions = *(buffer + 7); KAA_LOG_DEBUG(kaa_context->logger, KAA_ERR_NONE, "count of extensions is %d, expected 1", count_of_extensions); ASSERT_EQUAL(count_of_extensions, 1); }
kaa_error_t kaa_platform_protocol_alloc_serialize_client_sync(kaa_platform_protocol_t *self, const kaa_extension_id *services, size_t services_count, uint8_t **buffer, size_t *buffer_size) { if (!self || !buffer || !buffer_size) { return KAA_ERR_BADPARAM; } if (!services || services_count == 0) { return KAA_ERR_BADDATA; } *buffer_size = 0; kaa_error_t error = kaa_platform_protocol_serialize_client_sync(self, services, services_count, NULL, buffer_size); // TODO(982): assert error != KAA_ERR_NONE if (error != KAA_ERR_BUFFER_IS_NOT_ENOUGH) { return error; } *buffer = KAA_MALLOC(*buffer_size); if (!*buffer) { KAA_LOG_ERROR(self->logger, KAA_ERR_NOMEM, "No memory for buffer"); return KAA_ERR_NOMEM; } return kaa_platform_protocol_serialize_client_sync(self, services, services_count, *buffer, buffer_size); }
static char* copy_data(const char* data, size_t data_size) { KAA_RETURN_IF_NIL2(data, data_size, NULL); char *new_data = (char *)KAA_MALLOC(data_size); KAA_RETURN_IF_NIL(new_data, NULL); memcpy(new_data, data, data_size); return new_data; }
static kaa_list_node_t *create_node(void *data) { KAA_RETURN_IF_NIL(data, NULL); kaa_list_node_t *node = (kaa_list_node_t *)KAA_MALLOC(sizeof(kaa_list_node_t)); KAA_RETURN_IF_NIL(node, NULL); node->data = data; node->next = node->prev = NULL; return node; }
kaa_error_t kaa_profile_manager_update_profile(kaa_profile_manager_t *self, kaa_profile_t *profile_body) { #if KAA_PROFILE_SCHEMA_VERSION > 0 KAA_RETURN_IF_NIL2(self, profile_body, KAA_ERR_BADPARAM); size_t serialized_profile_size = profile_body->get_size(profile_body); if (!serialized_profile_size) { KAA_LOG_ERROR(self->logger, KAA_ERR_BADDATA, "Failed to update profile: serialize profile size is null. Maybe profile schema is empty"); return KAA_ERR_BADDATA; } char *serialized_profile = (char *) KAA_MALLOC(serialized_profile_size * sizeof(char)); KAA_RETURN_IF_NIL(serialized_profile, KAA_ERR_NOMEM); avro_writer_t writer = avro_writer_memory(serialized_profile, serialized_profile_size); if (!writer) { KAA_FREE(serialized_profile); return KAA_ERR_NOMEM; } profile_body->serialize(writer, profile_body); avro_writer_free(writer); kaa_digest new_hash; ext_calculate_sha_hash(serialized_profile, serialized_profile_size, new_hash); if (!memcmp(new_hash, self->status->profile_hash, SHA_1_DIGEST_LENGTH)) { self->need_resync = false; KAA_FREE(serialized_profile); return KAA_ERR_NONE; } KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Endpoint profile is updated"); if (ext_copy_sha_hash(self->status->profile_hash, new_hash)) { KAA_FREE(serialized_profile); return KAA_ERR_BAD_STATE; } if (self->profile_body.size > 0) { KAA_FREE(self->profile_body.buffer); self->profile_body.buffer = NULL; } self->profile_body.buffer = (uint8_t*)serialized_profile; self->profile_body.size = serialized_profile_size; self->need_resync = true; kaa_transport_channel_interface_t *channel = kaa_channel_manager_get_transport_channel(self->channel_manager, profile_sync_services[0]); if (channel) channel->sync_handler(channel->context, profile_sync_services, 1); #endif return KAA_ERR_NONE; }
kaa_error_t kaa_failover_strategy_create(kaa_failover_strategy_t** strategy, kaa_logger_t *logger) { KAA_RETURN_IF_NIL2(strategy, logger, KAA_ERR_BADPARAM); *strategy = (kaa_failover_strategy_t *) KAA_MALLOC(sizeof(kaa_failover_strategy_t)); KAA_RETURN_IF_NIL(*strategy, KAA_ERR_NOMEM); (*strategy)->decision.action = KAA_USE_NEXT_BOOTSTRAP; (*strategy)->decision.retry_period = KAA_FAILOVER_RETRY_PERIOD; (*strategy)->logger = logger; return KAA_ERR_NONE; }
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_get_endpoint_public_key(char **buffer, size_t *buffer_size, bool *needs_deallocation) { *buffer = (char *) KAA_MALLOC(TEST_PUB_KEY_SIZE * sizeof(char)); if (*buffer) { memcpy(*buffer, test_ep_key, TEST_PUB_KEY_SIZE); *buffer_size = TEST_PUB_KEY_SIZE; *needs_deallocation = true; } else { *buffer_size = 0; *needs_deallocation = false; } }
kaa_error_t kaa_platform_protocol_create(kaa_platform_protocol_t **platform_protocol_p , kaa_context_t *context , kaa_status_t *status) { KAA_RETURN_IF_NIL4(platform_protocol_p, context, context->logger, status, KAA_ERR_BADPARAM); *platform_protocol_p = KAA_MALLOC(sizeof(kaa_platform_protocol_t)); KAA_RETURN_IF_NIL(*platform_protocol_p, KAA_ERR_NOMEM); (*platform_protocol_p)->request_id = 0; (*platform_protocol_p)->kaa_context = context; (*platform_protocol_p)->status = status; (*platform_protocol_p)->logger = context->logger; return KAA_ERR_NONE; }
kaa_profile_basic_endpoint_profile_test_t *kaa_profile_basic_endpoint_profile_test_deserialize(avro_reader_t reader) { kaa_profile_basic_endpoint_profile_test_t *record = (kaa_profile_basic_endpoint_profile_test_t *)KAA_MALLOC(sizeof(kaa_profile_basic_endpoint_profile_test_t)); if (record) { record->serialize = kaa_profile_basic_endpoint_profile_test_serialize; record->get_size = kaa_profile_basic_endpoint_profile_test_get_size; record->destroy = kaa_profile_basic_endpoint_profile_test_destroy; record->profile_body = kaa_string_deserialize(reader); } return record; }
kaa_configuration_root_record_t *kaa_configuration_root_record_deserialize(avro_reader_t reader) { kaa_configuration_root_record_t *record = (kaa_configuration_root_record_t *)KAA_MALLOC(sizeof(kaa_configuration_root_record_t)); if (record) { record->serialize = kaa_configuration_root_record_serialize; record->get_size = kaa_configuration_root_record_get_size; record->destroy = kaa_configuration_root_record_destroy; record->data = kaa_string_deserialize(reader); record->__uuid = kaa_union_null_or_fixed_deserialize(reader); } return record; }
kaa_movement_class_movement_direction_t *kaa_movement_class_movement_direction_deserialize(avro_reader_t reader) { kaa_movement_class_movement_direction_t *record = (kaa_movement_class_movement_direction_t *)KAA_MALLOC(sizeof(kaa_movement_class_movement_direction_t)); if (record) { record->serialize = kaa_movement_class_movement_direction_serialize; record->get_size = kaa_movement_class_movement_direction_get_size; record->destroy = kaa_data_destroy; int64_t direction_value; avro_binary_encoding.read_long(reader, &direction_value); record->direction = direction_value; } return record; }
kaa_error_t kaa_buffer_create_buffer(kaa_buffer_t **buffer_p, size_t buffer_size) { KAA_RETURN_IF_NIL2(buffer_p, buffer_size, KAA_ERR_BADPARAM); kaa_buffer_t *buffer = (kaa_buffer_t *) KAA_MALLOC(sizeof(kaa_buffer_t)); KAA_RETURN_IF_NIL(buffer, KAA_ERR_NOMEM); buffer->begin = (char *) KAA_CALLOC(buffer_size , sizeof(char)); if (!buffer->begin) { KAA_FREE(buffer); return KAA_ERR_NOMEM; } buffer->end = buffer->begin + buffer_size; buffer->current = buffer->begin; *buffer_p = buffer; return KAA_ERR_NONE; }
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id) { KAA_RETURN_IF_NIL(self, KAA_ERR_BADPARAM); timeout_info_t *info = (timeout_info_t *)KAA_MALLOC(sizeof(timeout_info_t)); KAA_RETURN_IF_NIL(info, KAA_ERR_NOMEM); info->log_bucket_id = bucket_id; info->timeout = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context); kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info); if (!it) { KAA_FREE(info); return KAA_ERR_NOMEM; } return KAA_ERR_NONE; }
kaa_error_t kaa_configuration_manager_create(kaa_configuration_manager_t **configuration_manager_p, kaa_channel_manager_t *channel_manager, kaa_status_t *status, kaa_logger_t *logger) { KAA_RETURN_IF_NIL3(configuration_manager_p, status, logger, KAA_ERR_BADPARAM); kaa_configuration_manager_t *manager = (kaa_configuration_manager_t *) KAA_MALLOC(sizeof(kaa_configuration_manager_t)); KAA_RETURN_IF_NIL(manager, KAA_ERR_NOMEM); manager->channel_manager = channel_manager; manager->status = status; manager->logger = logger; manager->root_receiver = (kaa_configuration_root_receiver_t) { NULL, NULL }; char *buffer = NULL; size_t buffer_size = 0; bool need_deallocation = false; if (status->is_updated) ext_configuration_read(&buffer, &buffer_size, &need_deallocation); else ext_configuration_delete(); if (!buffer || !buffer_size) { need_deallocation = false; #if KAA_CONFIGURATION_DATA_LENGTH > 0 buffer = (char *)KAA_CONFIGURATION_DATA; buffer_size = KAA_CONFIGURATION_DATA_LENGTH; #endif } if (buffer && buffer_size > 0) { ext_calculate_sha_hash(buffer, buffer_size, manager->configuration_hash); manager->root_record = kaa_configuration_manager_deserialize(buffer, buffer_size); if (!manager->root_record) { KAA_FREE(manager); if (need_deallocation && buffer) KAA_FREE(buffer); return KAA_ERR_NOMEM; } if (need_deallocation) KAA_FREE(buffer); } *configuration_manager_p = manager; return KAA_ERR_NONE; }
static void test_list_push_back() { kaa_list_t *list = kaa_list_create(); ASSERT_NOT_NULL(list); int32_t *number; int node_number = 2; for (int i = 0; i < node_number; ++i) { number = KAA_MALLOC(sizeof(int32_t *)); ASSERT_NOT_NULL(number); *number = rand(); kaa_list_push_back(list, number); } ASSERT_EQUAL(kaa_list_get_size(list), node_number); ASSERT_EQUAL((*(int32_t *)kaa_list_get_data(kaa_list_back(list))), *number); kaa_list_destroy(list, NULL); }
kaa_error_t ext_log_storage_add_log_record(void *context, kaa_log_record_t *record) { mock_storage_context_t *self = context; test_log_record_t *test_rec = KAA_MALLOC(sizeof(*test_rec)); if (!test_rec) { return KAA_ERR_NOMEM; } test_rec->rec = *record; test_rec->processed = false; kaa_list_push_back(self->logs, test_rec); self->record_count++; self->total_size += test_rec->rec.size; return KAA_ERR_NONE; }
kaa_error_t kaa_platform_protocol_create(kaa_platform_protocol_t **platform_protocol_p , kaa_context_t *context , kaa_status_t *status) { KAA_RETURN_IF_NIL4(platform_protocol_p, context, context->logger, status, KAA_ERR_BADPARAM); kaa_platform_protocol_t *protocol = KAA_MALLOC(sizeof(*protocol)); if (!protocol) return KAA_ERR_NOMEM; protocol->request_id = 0; protocol->kaa_context = context; protocol->status = status; protocol->logger = context->logger; *platform_protocol_p = protocol; return KAA_ERR_NONE; }
void test_serializing(void) { KAA_TRACE_IN(context->logger); kaa_serialize_info_t *info = (kaa_serialize_info_t *) KAA_MALLOC(sizeof(kaa_serialize_info_t)); kaa_service_t service[] = { KAA_SERVICE_NOTIFICATION }; info->services = service; info->services_count = 1; info->allocator = &allocator; info->allocator_context = &allocator; //mock err = kaa_platform_protocol_serialize_client_sync(context->platform_protocol, info, &buffer, &buffer_size); ASSERT_EQUAL(err, KAA_ERR_NONE); KAA_FREE(buffer); KAA_FREE(info); KAA_TRACE_OUT(context->logger); }
static void test_list_hash() { kaa_list_t *list = kaa_list_create(); ASSERT_NOT_NULL(list); uint64_t node_number = 100; for (uint64_t i = 0; i < node_number; ++i) { test_list_node_t *node = KAA_MALLOC(sizeof(test_list_node_t)); ASSERT_NOT_NULL(node); node->id = (uint64_t) node_number - i; kaa_list_push_back(list, node); } ASSERT_EQUAL(kaa_list_get_size(list), node_number); kaa_list_sort(list,&test_kaa_list_predicate); ASSERT_EQUAL(kaa_list_hash(list,&test_kaa_list_hash),-974344717); kaa_list_destroy(list, NULL); }
static void kaa_generate_pub_key(void) { const int kBits = 2048; const int kExp = 65537; RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0); BIO *bio_pem = BIO_new(BIO_s_mem()); i2d_RSA_PUBKEY_bio(bio_pem, rsa); kaa_public_key_length = BIO_pending(bio_pem); kaa_public_key = (char *) KAA_MALLOC(kaa_public_key_length); if (!kaa_public_key) { kaa_public_key_length = 0; BIO_free_all(bio_pem); RSA_free(rsa); return; } BIO_read(bio_pem, kaa_public_key, kaa_public_key_length); BIO_free_all(bio_pem); RSA_free(rsa); }
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id, uint16_t count) { // TODO(KAA-982): Use asserts if (!self) { return KAA_ERR_BADPARAM; } timeout_info_t *info = KAA_MALLOC(sizeof(*info)); if (!info) { return KAA_ERR_NOMEM; } info->log_bucket_id = bucket_id; info->deadline = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context); info->log_count = count; kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info); if (!it) { KAA_FREE(info); return KAA_ERR_NOMEM; } return KAA_ERR_NONE; }