static void test_fixed_deserialize(void **state) { (void)state; const uint8_t plain_fixed1[] = { 0x0, 0x1, 0x2, 0x3, 0x4 }; size_t plain_fixed1_size = sizeof(plain_fixed1) / sizeof(char); kaa_bytes_t *kaa_fixed1 = kaa_fixed_copy_create(plain_fixed1, plain_fixed1_size); ASSERT_NOT_NULL(kaa_fixed1); size_t expected_size = kaa_fixed_get_size(kaa_fixed1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_fixed_serialize(avro_writer, kaa_fixed1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); kaa_bytes_t *kaa_fixed2 = kaa_fixed_deserialize(avro_reader, &expected_size); ASSERT_NOT_NULL(kaa_fixed2); ASSERT_EQUAL(memcmp(kaa_fixed2->buffer, plain_fixed1, plain_fixed1_size), 0); ASSERT_EQUAL(memcmp(kaa_fixed2->buffer, kaa_fixed1->buffer, plain_fixed1_size), 0); kaa_fixed_destroy(kaa_fixed2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); kaa_fixed_destroy(kaa_fixed1); }
static int write_header(avro_file_writer_t w) { int rval; uint8_t version = 1; /* TODO: remove this static buffer */ avro_writer_t schema_writer; char schema_buf[64 * 1024]; const avro_encoding_t *enc = &avro_binary_encoding; /* Generate random sync */ generate_sync(w); check(rval, avro_write(w->writer, "Obj", 3)); check(rval, avro_write(w->writer, &version, 1)); check(rval, enc->write_long(w->writer, 3)); check(rval, enc->write_string(w->writer, "avro.sync")); check(rval, enc->write_bytes(w->writer, w->sync, sizeof(w->sync))); check(rval, enc->write_string(w->writer, "avro.codec")); check(rval, enc->write_bytes(w->writer, "null", 4)); check(rval, enc->write_string(w->writer, "avro.schema")); schema_writer = avro_writer_memory(schema_buf, sizeof(schema_buf)); rval = avro_schema_to_json(w->writers_schema, schema_writer); if (rval) { avro_writer_free(schema_writer); return rval; } check(rval, enc->write_bytes(w->writer, schema_buf, avro_writer_tell(schema_writer))); check(rval, enc->write_long(w->writer, 0)); return write_sync(w); }
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); }
int main(void) { int pass; for (pass = 0 ; json_schemas[pass] ; pass++) { int rval = 0; size_t len; static char buf[4096]; avro_writer_t writer; avro_file_writer_t file_writer; avro_file_reader_t file_reader; avro_schema_t schema = NULL; avro_schema_error_t error = NULL; char outpath[64]; const char *json_schema = json_schemas[pass]; printf("pass %d with schema %s\n", pass, json_schema); check(rval, avro_schema_from_json(json_schema, strlen(json_schema), &schema, &error)); avro_value_iface_t *iface = avro_generic_class_from_schema(schema); avro_value_t val; avro_generic_value_new(iface, &val); avro_value_t out; avro_generic_value_new(iface, &out); /* create the val */ avro_value_reset(&val); avro_value_set_string(&val, "test-1691"); /* Write value to file */ snprintf(outpath, sizeof(outpath), "test-1691-%d.avro", pass); /* create the writers */ writer = avro_writer_memory(buf, sizeof(buf)); check(rval, avro_file_writer_create(outpath, schema, &file_writer)); check(rval, avro_value_write(writer, &val)); len = avro_writer_tell(writer); check(rval, avro_file_writer_append_encoded(file_writer, buf, len)); check(rval, avro_file_writer_close(file_writer)); /* Read the value back */ check(rval, avro_file_reader(outpath, &file_reader)); check(rval, avro_file_reader_read_value(file_reader, &out)); if (!avro_value_equal(&val, &out)) { fprintf(stderr, "fail!\n"); exit(EXIT_FAILURE); } fprintf(stderr, "pass %d: ok: schema %s\n", pass, json_schema); check(rval, avro_file_reader_close(file_reader)); remove(outpath); } exit(EXIT_SUCCESS); }
static void test_fixed_serialize(void **state) { (void)state; uint8_t plain_fixed1[] = { 0x0, 0x1, 0x2, 0x3, 0x4 }; size_t plain_fixed1_size = sizeof(plain_fixed1) / sizeof(char); kaa_bytes_t *kaa_fixed1 = kaa_fixed_copy_create(plain_fixed1, plain_fixed1_size); size_t expected_size = kaa_fixed_get_size(kaa_fixed1); 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_fixed_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_bytes_t fake_kaa_fixed = { NULL, 0, NULL }; kaa_fixed_serialize(auto_avro_writer, &fake_kaa_fixed); 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_fixed_serialize(auto_avro_writer, kaa_fixed1); avro_write(manual_avro_writer, plain_fixed1, plain_fixed1_size); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); kaa_fixed_destroy(kaa_fixed1); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
/* typedef struct { int jobid; int vpid; } process_name_t; typedef struct { char *en_vars; char *args; char *host_name; process_name_t proc_name; } launch_context_t; typedef struct { bool is_successful; process_name_t proc_name; } launch_response_t; */ static void build_launch_response(launch_response_t *launch_response_array, int array_size, avro_slice_t **slice) { char filename[FILE_NAME_LEN]; char buf[BUFFER_SIZE]; long len = 0; avro_schema_t schema; avro_value_iface_t *iface; avro_value_t record; avro_value_t results_value, LaunchResult_value, is_successful_value, name_value, jobid_value, vpid_value; size_t index; int i; avro_writer_t writer; sprintf(filename, "%s/%s", SCHEMA_PATH, "LaunchResponseRecordAvro.avsc"); init_schema(filename, &schema); iface = avro_generic_class_from_schema(schema); avro_generic_value_new(iface, &record); avro_value_get_by_name(&record, "results", &results_value, &index); for (i = 0; i < array_size; i++) { avro_value_append(&results_value, &LaunchResult_value, &index); avro_value_get_by_name(&LaunchResult_value, "is_successful", &is_successful_value, &index); avro_value_set_boolean(&is_successful_value, launch_response_array[i].is_successful); avro_value_get_by_name(&LaunchResult_value, "name", &name_value, &index); avro_value_get_by_name(&name_value, "jobid", &jobid_value, &index); avro_value_set_int(&jobid_value, launch_response_array[i].proc_name.jobid); avro_value_get_by_name(&name_value, "vpid", &vpid_value, &index); avro_value_set_int(&vpid_value, launch_response_array[i].proc_name.vpid); } /* create a writer with memory buffer */ writer = avro_writer_memory(buf, sizeof(buf)); /* write record to writer (buffer) */ if (avro_value_write(writer, &record)) { fprintf(stderr, "Unable to write record to memory buffer\n"); fprintf(stderr, "Error: %s\n", avro_strerror()); exit(1); } avro_writer_flush(writer); len = avro_writer_tell(writer); //avro_generic_value_free(&record); avro_value_iface_decref(iface); avro_schema_decref(schema); *slice = xmalloc(sizeof(avro_slice_t)); (*slice)->buffer = xmalloc(len); (*slice)->len = len; memcpy((*slice)->buffer, buf, len); }
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); }
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; }
static void test_double_serialize(void **state) { (void)state; double double_value = rand() / rand(); size_t expected_size = kaa_double_get_size(&double_value); 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); kaa_double_serialize(auto_avro_writer, &double_value); avro_binary_encoding.write_double(manual_avro_writer, double_value); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
static void test_boolean_serialize(void **state) { (void)state; int8_t boolean_value = true; size_t expected_size = kaa_boolean_get_size(&boolean_value); 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); kaa_boolean_serialize(auto_avro_writer, &boolean_value); avro_binary_encoding.write_boolean(manual_avro_writer, boolean_value); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
static void test_enum_serialize(void **state) { (void)state; test_enum_t enum_value = (test_enum_t)rand() % TEST_VAL_5; size_t expected_size = kaa_enum_get_size(&enum_value); 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); kaa_enum_serialize(auto_avro_writer, &enum_value); avro_binary_encoding.write_long(manual_avro_writer, (int)enum_value); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
static void test_long_serialize(void **state) { (void)state; int64_t long_value = rand(); size_t expected_size = kaa_long_get_size(&long_value); 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); kaa_long_serialize(auto_avro_writer, &long_value); avro_binary_encoding.write_long(manual_avro_writer, long_value); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
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 write_read_check(avro_schema_t writers_schema, avro_datum_t datum, avro_schema_t readers_schema, avro_datum_t expected, char *type) { avro_datum_t datum_out; int validate; for (validate = 0; validate <= 1; validate++) { reader = avro_reader_memory(buf, sizeof(buf)); writer = avro_writer_memory(buf, sizeof(buf)); if (!expected) { expected = datum; } /* Validating read/write */ if (avro_write_data (writer, validate ? writers_schema : NULL, datum)) { fprintf(stderr, "Unable to write %s validate=%d\n %s\n", type, validate, avro_strerror()); exit(EXIT_FAILURE); } int64_t size = avro_size_data(writer, validate ? writers_schema : NULL, datum); if (size != avro_writer_tell(writer)) { fprintf(stderr, "Unable to calculate size %s validate=%d " "(%"PRId64" != %"PRId64")\n %s\n", type, validate, size, avro_writer_tell(writer), avro_strerror()); exit(EXIT_FAILURE); } if (avro_read_data (reader, writers_schema, readers_schema, &datum_out)) { fprintf(stderr, "Unable to read %s validate=%d\n %s\n", type, validate, avro_strerror()); fprintf(stderr, " %s\n", avro_strerror()); exit(EXIT_FAILURE); } if (!avro_datum_equal(expected, datum_out)) { fprintf(stderr, "Unable to encode/decode %s validate=%d\n %s\n", type, validate, avro_strerror()); exit(EXIT_FAILURE); } avro_reader_dump(reader, stderr); avro_datum_decref(datum_out); avro_reader_free(reader); avro_writer_free(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); }
static void test_null_serialize(void **state) { (void)state; size_t some_data = rand(); size_t expected_size = rand() % 10; 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); int seed = rand(); memset(auto_buffer, seed, expected_size); memset(manual_buffer, seed, expected_size); kaa_null_serialize(auto_avro_writer, &some_data); avro_binary_encoding.write_null(manual_avro_writer); ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0); avro_writer_free(manual_avro_writer); avro_writer_free(auto_avro_writer); }
static void test_null_array_serialize(void **state) { (void)state; size_t empty_array_buffer_size = 1; char empty_array_buffer[empty_array_buffer_size]; memset(empty_array_buffer, 1 + rand(), empty_array_buffer_size); avro_writer_t avro_writer = avro_writer_memory(empty_array_buffer, empty_array_buffer_size); kaa_array_serialize(avro_writer, NULL, NULL); ASSERT_EQUAL((int)empty_array_buffer[0], 0); avro_writer_free(avro_writer); }
static int file_writer_create(const char *path, avro_schema_t schema, avro_file_writer_t w) { int rval = file_writer_init_fp(path, "wx", w); if (rval) { check(rval, file_writer_init_fp(path, "w", w)); } w->datum_writer = avro_writer_memory(w->datum_buffer, sizeof(w->datum_buffer)); if (!w->datum_writer) { avro_writer_free(w->writer); return ENOMEM; } w->writers_schema = schema; return write_header(w); }
static void test_array_deserialize_w_ctx(void **state) { (void)state; const uint8_t plain_fixed[] = { 0x0, 0x1, 0x2, 0x3, 0x4 }; size_t plain_fixed_size = sizeof(plain_fixed) / sizeof(char); size_t array_size = 1 + rand() % 10; kaa_list_t *avro_array1 = kaa_list_create(); size_t i = 0; for (i = 0; i < array_size; ++i) { kaa_list_push_back(avro_array1, kaa_fixed_copy_create(plain_fixed, plain_fixed_size)); } size_t buffer_size = kaa_array_get_size(avro_array1, kaa_fixed_get_size); char buffer[buffer_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, buffer_size); kaa_array_serialize(avro_writer, avro_array1, kaa_fixed_serialize); avro_reader_t avro_reader = avro_reader_memory(buffer, buffer_size); kaa_list_t *avro_array2 = kaa_array_deserialize_w_ctx(avro_reader, (deserialize_w_ctx_fn)kaa_fixed_deserialize, &plain_fixed_size); ASSERT_NOT_NULL(avro_array2); ASSERT_EQUAL(kaa_list_get_size(avro_array2), array_size); kaa_list_node_t *it = kaa_list_begin(avro_array2); while (it) { kaa_bytes_t *fixed = kaa_list_get_data(it); ASSERT_NOT_NULL(fixed); ASSERT_EQUAL((size_t)fixed->size, plain_fixed_size); ASSERT_EQUAL(memcmp(fixed->buffer, plain_fixed, plain_fixed_size), 0); it = kaa_list_next(it); } kaa_list_destroy(avro_array2, kaa_fixed_destroy); avro_reader_free(avro_reader); avro_writer_free(avro_writer); kaa_list_destroy(avro_array1, kaa_fixed_destroy); }
static void test_enum_deserialize(void **state) { (void)state; test_enum_t enum_value1 = (test_enum_t)rand() % TEST_VAL_5; size_t expected_size = kaa_enum_get_size(&enum_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_enum_serialize(avro_writer, &enum_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); test_enum_t *enum_value2 = (test_enum_t *)kaa_enum_deserialize(avro_reader); ASSERT_EQUAL(*enum_value2, enum_value1); kaa_data_destroy(enum_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
static void test_float_deserialize(void **state) { (void)state; float float_value1 = rand() / rand(); size_t expected_size = kaa_float_get_size(&float_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_float_serialize(avro_writer, &float_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); float *float_value2 = kaa_float_deserialize(avro_reader); ASSERT_EQUAL(*float_value2, float_value1); kaa_data_destroy(float_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
static void test_double_deserialize(void **state) { (void)state; double double_value1 = rand() / rand(); size_t expected_size = kaa_double_get_size(&double_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_double_serialize(avro_writer, &double_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); double *double_value2 = kaa_double_deserialize(avro_reader); ASSERT_EQUAL(*double_value2, double_value1); kaa_data_destroy(double_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
static void test_boolean_deserialize(void **state) { (void)state; int8_t boolean_value1 = true; size_t expected_size = kaa_boolean_get_size(&boolean_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_boolean_serialize(avro_writer, &boolean_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); int8_t *boolean_value2 = kaa_boolean_deserialize(avro_reader); ASSERT_EQUAL(*boolean_value2, boolean_value1); kaa_data_destroy(boolean_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
static void test_long_deserialize(void **state) { (void)state; int64_t long_value1 = rand(); size_t expected_size = kaa_long_get_size(&long_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_long_serialize(avro_writer, &long_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); int64_t *long_value2 = kaa_long_deserialize(avro_reader); ASSERT_EQUAL(*long_value2, long_value1); kaa_data_destroy(long_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
static void test_int_deserialize(void **state) { (void)state; int32_t int_value1 = rand(); size_t expected_size = kaa_int_get_size(&int_value1); char buffer[expected_size]; avro_writer_t avro_writer = avro_writer_memory(buffer, expected_size); kaa_int_serialize(avro_writer, &int_value1); avro_reader_t avro_reader = avro_reader_memory(buffer, expected_size); int32_t *int_value2 = kaa_int_deserialize(avro_reader); ASSERT_EQUAL(*int_value2, int_value1); kaa_data_destroy(int_value2); avro_reader_free(avro_reader); avro_writer_free(avro_writer); }
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); }
avro_writer_t prepare_writer_status() { avro_writer_t writer; long lSize = 0; CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, LMLite %s : ENTER \n", __FUNCTION__ )); CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, Avro prepares to serialize data\n")); if ( schema_file_parsed == FALSE ) { FILE *fp; /* open schema file */ fp = fopen ( NETWORK_DEVICE_STATUS_AVRO_FILENAME , "rb" ); if ( !fp ) perror( NETWORK_DEVICE_STATUS_AVRO_FILENAME " doesn't exist."), exit(1); /* seek through file and get file size*/ fseek( fp , 0L , SEEK_END); lSize = ftell( fp ); /*back to the start of the file*/ rewind( fp ); /* allocate memory for entire content */ ndsschemabuffer = calloc( 1, lSize + 1 ); if ( !ndsschemabuffer ) fclose(fp), fputs("memory alloc fails", stderr), exit(1); /* copy the file into the buffer */ if ( 1 != fread( ndsschemabuffer , lSize, 1 , fp) ) fclose(fp), free(ndsschemabuffer), ndsschemabuffer=NULL, fputs("entire read fails", stderr), exit(1); fclose(fp); avro_schema_error_t error = NULL; //Master report/datum avro_schema_t network_device_report_schema = NULL; avro_schema_from_json(ndsschemabuffer, strlen(ndsschemabuffer), &network_device_report_schema, &error); //generate an avro class from our schema and get a pointer to the value interface iface = avro_generic_class_from_schema(network_device_report_schema); avro_schema_decref(network_device_report_schema); schema_file_parsed = TRUE; // parse schema file once only CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, Read Avro schema file ONCE, lSize = %ld, pbuffer = 0x%lx.\n", lSize + 1, (ulong)ndsschemabuffer )); } else { CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, Stored lSize = %ld, pbuffer = 0x%lx.\n", lSize + 1, (ulong)ndsschemabuffer )); } memset(&AvroSerializedBuf[0], 0, sizeof(AvroSerializedBuf)); AvroSerializedBuf[0] = MAGIC_NUMBER; /* fill MAGIC number = Empty, i.e. no Schema ID */ memcpy( &AvroSerializedBuf[ MAGIC_NUMBER_SIZE ], UUID, sizeof(UUID)); memcpy( &AvroSerializedBuf[ MAGIC_NUMBER_SIZE + sizeof(UUID) ], HASH, sizeof(HASH)); writer = avro_writer_memory( (char*)&AvroSerializedBuf[MAGIC_NUMBER_SIZE + SCHEMA_ID_LENGTH], sizeof(AvroSerializedBuf) - MAGIC_NUMBER_SIZE - SCHEMA_ID_LENGTH ); CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, LMLite %s : EXIT \n", __FUNCTION__ )); return writer; }
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 kafka_cache_purge(struct chained_cache *queue[], int index, int safe_action) { struct pkt_primitives *data = NULL; struct pkt_bgp_primitives *pbgp = NULL; struct pkt_nat_primitives *pnat = NULL; struct pkt_mpls_primitives *pmpls = NULL; struct pkt_tunnel_primitives *ptun = NULL; char *pcust = NULL; struct pkt_vlen_hdr_primitives *pvlen = NULL; struct pkt_bgp_primitives empty_pbgp; struct pkt_nat_primitives empty_pnat; struct pkt_mpls_primitives empty_pmpls; struct pkt_tunnel_primitives empty_ptun; char *empty_pcust = NULL; char src_mac[18], dst_mac[18], src_host[INET6_ADDRSTRLEN], dst_host[INET6_ADDRSTRLEN], ip_address[INET6_ADDRSTRLEN]; char rd_str[SRVBUFLEN], misc_str[SRVBUFLEN], dyn_kafka_topic[SRVBUFLEN], *orig_kafka_topic = NULL; int i, j, stop, batch_idx, is_topic_dyn = FALSE, qn = 0, ret, saved_index = index; int mv_num = 0, mv_num_save = 0; time_t start, duration; pid_t writer_pid = getpid(); char *json_buf = NULL; int json_buf_off = 0; #ifdef WITH_AVRO avro_writer_t avro_writer; char *avro_buf = NULL; int avro_buffer_full = FALSE; #endif p_kafka_init_host(&kafkap_kafka_host, config.kafka_config_file); /* setting some defaults */ if (!config.sql_host) config.sql_host = default_kafka_broker_host; if (!config.kafka_broker_port) config.kafka_broker_port = default_kafka_broker_port; if (!config.sql_table) config.sql_table = default_kafka_topic; else { if (strchr(config.sql_table, '$')) { is_topic_dyn = TRUE; orig_kafka_topic = config.sql_table; } } if (config.amqp_routing_key_rr) orig_kafka_topic = config.sql_table; p_kafka_init_topic_rr(&kafkap_kafka_host); p_kafka_set_topic_rr(&kafkap_kafka_host, config.amqp_routing_key_rr); empty_pcust = malloc(config.cpptrs.len); if (!empty_pcust) { Log(LOG_ERR, "ERROR ( %s/%s ): Unable to malloc() empty_pcust. Exiting.\n", config.name, config.type); exit_plugin(1); } memset(&empty_pbgp, 0, sizeof(struct pkt_bgp_primitives)); memset(&empty_pnat, 0, sizeof(struct pkt_nat_primitives)); memset(&empty_pmpls, 0, sizeof(struct pkt_mpls_primitives)); memset(&empty_ptun, 0, sizeof(struct pkt_tunnel_primitives)); memset(empty_pcust, 0, config.cpptrs.len); p_kafka_connect_to_produce(&kafkap_kafka_host); p_kafka_set_broker(&kafkap_kafka_host, config.sql_host, config.kafka_broker_port); if (!is_topic_dyn && !config.amqp_routing_key_rr) p_kafka_set_topic(&kafkap_kafka_host, config.sql_table); p_kafka_set_partition(&kafkap_kafka_host, config.kafka_partition); p_kafka_set_key(&kafkap_kafka_host, config.kafka_partition_key, config.kafka_partition_keylen); if (config.message_broker_output & PRINT_OUTPUT_JSON) p_kafka_set_content_type(&kafkap_kafka_host, PM_KAFKA_CNT_TYPE_STR); else if (config.message_broker_output & PRINT_OUTPUT_AVRO) p_kafka_set_content_type(&kafkap_kafka_host, PM_KAFKA_CNT_TYPE_BIN); else { Log(LOG_ERR, "ERROR ( %s/%s ): Unsupported kafka_output value specified. Exiting.\n", config.name, config.type); exit_plugin(1); } for (j = 0, stop = 0; (!stop) && P_preprocess_funcs[j]; j++) stop = P_preprocess_funcs[j](queue, &index, j); Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - START (PID: %u) ***\n", config.name, config.type, writer_pid); start = time(NULL); if (config.print_markers) { if (config.message_broker_output & PRINT_OUTPUT_JSON || config.message_broker_output & PRINT_OUTPUT_AVRO) { void *json_obj; char *json_str; json_obj = compose_purge_init_json(config.name, writer_pid); if (json_obj) json_str = compose_json_str(json_obj); if (json_str) { Log(LOG_DEBUG, "DEBUG ( %s/%s ): %s\n\n", config.name, config.type, json_str); ret = p_kafka_produce_data(&kafkap_kafka_host, json_str, strlen(json_str)); free(json_str); json_str = NULL; } } } if (config.message_broker_output & PRINT_OUTPUT_JSON) { if (config.sql_multi_values) { json_buf = malloc(config.sql_multi_values); if (!json_buf) { Log(LOG_ERR, "ERROR ( %s/%s ): malloc() failed (json_buf). Exiting ..\n", config.name, config.type); exit_plugin(1); } else memset(json_buf, 0, config.sql_multi_values); } } else if (config.message_broker_output & PRINT_OUTPUT_AVRO) { #ifdef WITH_AVRO if (!config.avro_buffer_size) config.avro_buffer_size = LARGEBUFLEN; avro_buf = malloc(config.avro_buffer_size); if (!avro_buf) { Log(LOG_ERR, "ERROR ( %s/%s ): malloc() failed (avro_buf). Exiting ..\n", config.name, config.type); exit_plugin(1); } else memset(avro_buf, 0, config.avro_buffer_size); avro_writer = avro_writer_memory(avro_buf, config.avro_buffer_size); #endif } for (j = 0; j < index; j++) { void *json_obj; char *json_str; if (queue[j]->valid != PRINT_CACHE_COMMITTED) continue; data = &queue[j]->primitives; if (queue[j]->pbgp) pbgp = queue[j]->pbgp; else pbgp = &empty_pbgp; if (queue[j]->pnat) pnat = queue[j]->pnat; else pnat = &empty_pnat; if (queue[j]->pmpls) pmpls = queue[j]->pmpls; else pmpls = &empty_pmpls; if (queue[j]->ptun) ptun = queue[j]->ptun; else ptun = &empty_ptun; if (queue[j]->pcust) pcust = queue[j]->pcust; else pcust = empty_pcust; if (queue[j]->pvlen) pvlen = queue[j]->pvlen; else pvlen = NULL; if (queue[j]->valid == PRINT_CACHE_FREE) continue; if (config.message_broker_output & PRINT_OUTPUT_JSON) { #ifdef WITH_JANSSON json_t *json_obj = json_object(); int idx; for (idx = 0; idx < N_PRIMITIVES && cjhandler[idx]; idx++) cjhandler[idx](json_obj, queue[j]); add_writer_name_and_pid_json(json_obj, config.name, writer_pid); json_str = compose_json_str(json_obj); #endif } else if (config.message_broker_output & PRINT_OUTPUT_AVRO) { #ifdef WITH_AVRO avro_value_iface_t *avro_iface = avro_generic_class_from_schema(avro_acct_schema); avro_value_t avro_value = compose_avro(config.what_to_count, config.what_to_count_2, queue[j]->flow_type, &queue[j]->primitives, pbgp, pnat, pmpls, ptun, pcust, pvlen, queue[j]->bytes_counter, queue[j]->packet_counter, queue[j]->flow_counter, queue[j]->tcp_flags, &queue[j]->basetime, queue[j]->stitch, avro_iface); size_t avro_value_size; add_writer_name_and_pid_avro(avro_value, config.name, writer_pid); avro_value_sizeof(&avro_value, &avro_value_size); if (avro_value_size > config.avro_buffer_size) { Log(LOG_ERR, "ERROR ( %s/%s ): AVRO: insufficient buffer size (avro_buffer_size=%u)\n", config.name, config.type, config.avro_buffer_size); Log(LOG_ERR, "ERROR ( %s/%s ): AVRO: increase value or look for avro_buffer_size in CONFIG-KEYS document.\n\n", config.name, config.type); exit_plugin(1); } else if (avro_value_size >= (config.avro_buffer_size - avro_writer_tell(avro_writer))) { avro_buffer_full = TRUE; j--; } else if (avro_value_write(avro_writer, &avro_value)) { Log(LOG_ERR, "ERROR ( %s/%s ): AVRO: unable to write value: %s\n", config.name, config.type, avro_strerror()); exit_plugin(1); } else { mv_num++; } avro_value_decref(&avro_value); avro_value_iface_decref(avro_iface); #else if (config.debug) Log(LOG_DEBUG, "DEBUG ( %s/%s ): compose_avro(): AVRO object not created due to missing --enable-avro\n", config.name, config.type); #endif } if (config.message_broker_output & PRINT_OUTPUT_JSON) { char *tmp_str = NULL; if (json_str && config.sql_multi_values) { int json_strlen = (strlen(json_str) ? (strlen(json_str) + 1) : 0); if (json_strlen >= (config.sql_multi_values - json_buf_off)) { if (json_strlen >= config.sql_multi_values) { Log(LOG_ERR, "ERROR ( %s/%s ): kafka_multi_values not large enough to store JSON elements. Exiting ..\n", config.name, config.type); exit(1); } tmp_str = json_str; json_str = json_buf; } else { strcat(json_buf, json_str); mv_num++; string_add_newline(json_buf); json_buf_off = strlen(json_buf); free(json_str); json_str = NULL; } } if (json_str) { if (is_topic_dyn) { P_handle_table_dyn_strings(dyn_kafka_topic, SRVBUFLEN, orig_kafka_topic, queue[j]); p_kafka_set_topic(&kafkap_kafka_host, dyn_kafka_topic); } if (config.amqp_routing_key_rr) { P_handle_table_dyn_rr(dyn_kafka_topic, SRVBUFLEN, orig_kafka_topic, &kafkap_kafka_host.topic_rr); p_kafka_set_topic(&kafkap_kafka_host, dyn_kafka_topic); } Log(LOG_DEBUG, "DEBUG ( %s/%s ): %s\n\n", config.name, config.type, json_str); ret = p_kafka_produce_data(&kafkap_kafka_host, json_str, strlen(json_str)); if (config.sql_multi_values) { json_str = tmp_str; strcpy(json_buf, json_str); mv_num_save = mv_num; mv_num = 1; string_add_newline(json_buf); json_buf_off = strlen(json_buf); } free(json_str); json_str = NULL; if (!ret) { if (!config.sql_multi_values) qn++; else qn += mv_num_save; } else break; } } else if (config.message_broker_output & PRINT_OUTPUT_AVRO) { #ifdef WITH_AVRO if (!config.sql_multi_values || (mv_num >= config.sql_multi_values) || avro_buffer_full) { if (is_topic_dyn) { P_handle_table_dyn_strings(dyn_kafka_topic, SRVBUFLEN, orig_kafka_topic, queue[j]); p_kafka_set_topic(&kafkap_kafka_host, dyn_kafka_topic); } if (config.amqp_routing_key_rr) { P_handle_table_dyn_rr(dyn_kafka_topic, SRVBUFLEN, orig_kafka_topic, &kafkap_kafka_host.topic_rr); p_kafka_set_topic(&kafkap_kafka_host, dyn_kafka_topic); } ret = p_kafka_produce_data(&kafkap_kafka_host, avro_buf, avro_writer_tell(avro_writer)); avro_writer_reset(avro_writer); avro_buffer_full = FALSE; mv_num_save = mv_num; mv_num = 0; if (!ret) qn += mv_num_save; else break; } #endif } } if (config.sql_multi_values) { if (config.message_broker_output & PRINT_OUTPUT_JSON) { if (json_buf && json_buf_off) { /* no handling of dyn routing keys here: not compatible */ Log(LOG_DEBUG, "DEBUG ( %s/%s ): %s\n\n", config.name, config.type, json_buf); ret = p_kafka_produce_data(&kafkap_kafka_host, json_buf, strlen(json_buf)); if (!ret) qn += mv_num; } } else if (config.message_broker_output & PRINT_OUTPUT_AVRO) { #ifdef WITH_AVRO if (avro_writer_tell(avro_writer)) { ret = p_kafka_produce_data(&kafkap_kafka_host, avro_buf, avro_writer_tell(avro_writer)); avro_writer_free(avro_writer); if (!ret) qn += mv_num; } #endif } } duration = time(NULL)-start; if (config.print_markers) { if (config.message_broker_output & PRINT_OUTPUT_JSON || config.message_broker_output & PRINT_OUTPUT_AVRO) { void *json_obj; char *json_str; json_obj = compose_purge_close_json(config.name, writer_pid, qn, saved_index, duration); if (json_obj) json_str = compose_json_str(json_obj); if (json_str) { sleep(1); /* Let's give a small delay to facilitate purge_close being the last message in batch in case of partitioned topics */ Log(LOG_DEBUG, "DEBUG ( %s/%s ): %s\n\n", config.name, config.type, json_str); ret = p_kafka_produce_data(&kafkap_kafka_host, json_str, strlen(json_str)); free(json_str); json_str = NULL; } } } p_kafka_close(&kafkap_kafka_host, FALSE); Log(LOG_INFO, "INFO ( %s/%s ): *** Purging cache - END (PID: %u, QN: %u/%u, ET: %u) ***\n", config.name, config.type, writer_pid, qn, saved_index, duration); if (config.sql_trigger_exec && !safe_action) P_trigger_exec(config.sql_trigger_exec); if (empty_pcust) free(empty_pcust); if (json_buf) free(json_buf); #ifdef WITH_AVRO if (avro_buf) free(avro_buf); #endif }
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); }