Пример #1
0
void test_meta_extension_get_size(void)
{
    KAA_TRACE_IN(logger);

    const size_t expected_meta_extension_size = KAA_EXTENSION_HEADER_SIZE
                                              + sizeof(uint32_t) /* request id */
                                              + sizeof(uint32_t) /* timeout */
                                              + kaa_aligned_size_get(SHA_1_DIGEST_LENGTH)
                                              + kaa_aligned_size_get(SHA_1_DIGEST_LENGTH)
                                              + kaa_aligned_size_get(KAA_SDK_TOKEN_LENGTH);

    size_t meta_extension_size;
    kaa_error_t error_code = kaa_meta_data_request_get_size(&meta_extension_size);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    ASSERT_EQUAL(expected_meta_extension_size, meta_extension_size);
}
Пример #2
0
void test_response(void **state)
{
    (void)state;
    const size_t response_size = kaa_aligned_size_get(KAA_CONFIGURATION_DATA_LENGTH) + sizeof(uint32_t);
    uint8_t response[response_size];
    uint8_t *response_cursor = response;

    *((uint32_t *) response_cursor) = KAA_HTONL(KAA_CONFIGURATION_DATA_LENGTH);
    response_cursor += sizeof(uint32_t);

    memcpy(response_cursor, KAA_CONFIGURATION_DATA, KAA_CONFIGURATION_DATA_LENGTH);

    kaa_platform_message_reader_t *reader = NULL;
    ASSERT_EQUAL(kaa_platform_message_reader_create(&reader, response, response_size), KAA_ERR_NONE);

    bool is_callback_invoked = false;
    kaa_configuration_root_receiver_t receiver = { &is_callback_invoked, &on_configuration_updated };
    ASSERT_EQUAL(kaa_configuration_manager_set_root_receiver(config_manager, &receiver), KAA_ERR_NONE);

    ASSERT_EQUAL(kaa_configuration_manager_handle_server_sync(config_manager, reader, CONFIG_RESPONSE_FLAGS, response_size), KAA_ERR_NONE);

    ASSERT_EQUAL(is_callback_invoked, true);

    const kaa_root_configuration_t *root_config = kaa_configuration_manager_get_configuration(config_manager);
    ASSERT_EQUAL(strcmp(root_config->data->data, CONFIG_DATA_FIELD), 0);

    kaa_bytes_t *uuid = (kaa_bytes_t *) root_config->__uuid->data;
    ASSERT_EQUAL(uuid->size, CONFIG_UUID_SIZE);
    ASSERT_EQUAL(memcmp(uuid->buffer, CONFIG_UUID, uuid->size), 0);

    kaa_platform_message_reader_destroy(reader);
}
Пример #3
0
kaa_error_t kaa_profile_request_get_size(kaa_profile_manager_t *self, size_t *expected_size)
{
    // TODO(KAA-982): Use asserts
    if (!self || !expected_size) {
        return KAA_ERR_BADPARAM;
    }

    if (!resync_is_required(self)) {
        *expected_size = 0;
        return KAA_ERR_NONE;
    }

    *expected_size = KAA_EXTENSION_HEADER_SIZE;
    *expected_size += sizeof(uint32_t); // profile body size
#if KAA_PROFILE_SCHEMA_VERSION > 0
    if (resync_is_required(self))
        *expected_size += kaa_aligned_size_get(self->profile_body.size); // profile data
#endif

    if (!self->status->is_registered) {
        if (!self->extension_data->public_key.buffer) {
            ext_get_endpoint_public_key((const uint8_t **)&self->extension_data->public_key.buffer,
                                        (size_t *)&self->extension_data->public_key.size);
        }

        if (self->extension_data->public_key.buffer && self->extension_data->public_key.size > 0) {
            *expected_size += sizeof(uint32_t); // public key size
            *expected_size += kaa_aligned_size_get(self->extension_data->public_key.size); // public key

        } else {
            return KAA_ERR_BADDATA;
        }
    }

    self->extension_data->access_token.buffer = (uint8_t *) self->status->endpoint_access_token;
    if (self->extension_data->access_token.buffer) {
        self->extension_data->access_token.size = strlen((const char*)self->extension_data->access_token.buffer);
        *expected_size += sizeof(uint32_t); // access token length
        *expected_size += kaa_aligned_size_get(self->extension_data->access_token.size); // access token
    }
    self->extension_data->payload_size = *expected_size - KAA_EXTENSION_HEADER_SIZE;

    return KAA_ERR_NONE;
}
Пример #4
0
kaa_error_t kaa_meta_data_request_get_size(size_t *expected_size)
{
    KAA_RETURN_IF_NIL(expected_size, KAA_ERR_BADPARAM);

    static size_t size = 0;

    if (size == 0) {
        size = KAA_EXTENSION_HEADER_SIZE;
        size += sizeof(uint32_t); // request id
        size += sizeof(uint32_t); // timeout value
        size += kaa_aligned_size_get(SHA_1_DIGEST_LENGTH); // public key hash length
        size += kaa_aligned_size_get(SHA_1_DIGEST_LENGTH); // profile hash length
        size += kaa_aligned_size_get(KAA_SDK_TOKEN_LENGTH); // sdk token length
    }

    *expected_size = size;

    return KAA_ERR_NONE;
}
Пример #5
0
kaa_error_t kaa_configuration_manager_handle_server_sync(kaa_configuration_manager_t *self
                                                       , kaa_platform_message_reader_t *reader
                                                       , uint32_t extension_options
                                                       , size_t extension_length)
{
    KAA_RETURN_IF_NIL2(self, reader, KAA_ERR_BADPARAM);

    KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Received configuration server sync: options %u, payload size %u", extension_options, extension_length);

    if (extension_length >= sizeof(uint32_t)) {
        self->status->config_seq_n = KAA_NTOHL(*((uint32_t *) reader->current));
        KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Configuration state sequence number is '%d'", self->status->config_seq_n);
        reader->current += sizeof(uint32_t);
        if (extension_options & KAA_CONFIGURATION_BODY_PRESENT) {
            uint32_t body_size = KAA_NTOHL(*((uint32_t *) reader->current));
            reader->current += sizeof(uint32_t);
            KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Received configuration body, size '%u' ", body_size);
            const char* body = reader->current;
            kaa_error_t error = kaa_platform_message_skip(reader, kaa_aligned_size_get(body_size));
            if (error) {
                 KAA_LOG_ERROR(self->logger, error, "Failed to read configuration body, size %u", body_size);
                 return error;
            }

#if KAA_CONFIGURATION_DELTA_SUPPORT

#else
            if (self->root_record)
                self->root_record->destroy(self->root_record);

            self->root_record = kaa_configuration_manager_deserialize(body, body_size);
            if (!self->root_record) {
                KAA_LOG_ERROR(self->logger, KAA_ERR_READ_FAILED, "Failed to deserialize configuration body, size %u", body_size);
                return KAA_ERR_READ_FAILED;
            }

            kaa_error_t err = ext_calculate_sha_hash(body, body_size, self->configuration_hash);
            if (err) {
                KAA_LOG_WARN(self->logger, err, "Failed to calculate configuration body hash");
                return err;
            }
            ext_configuration_store(body, body_size);
#endif
            if (self->root_receiver.on_configuration_updated)
                self->root_receiver.on_configuration_updated(self->root_receiver.context, self->root_record);

            kaa_transport_channel_interface_t *channel =
                    kaa_channel_manager_get_transport_channel(self->channel_manager, configuration_sync_services[0]);
            if (channel)
                channel->sync_handler(channel->context, configuration_sync_services, 1);
        }
    }
    return KAA_ERR_NONE;
}
Пример #6
0
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);
}
Пример #7
0
void test_aligned_write(void)
{
    kaa_error_t error_code = KAA_ERR_NONE;
    char buffer[3 * KAA_ALIGNMENT];
    size_t buffer_size = sizeof(buffer) / sizeof(char);
    kaa_platform_message_writer_t *writer = NULL;

    error_code = kaa_platform_message_writer_create(&writer, buffer, buffer_size);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    char *match_alignment_data[KAA_ALIGNMENT];
    size_t match_alignment_data_len = KAA_ALIGNMENT;

    char *unmatch_alignment_data[KAA_ALIGNMENT + 1];
    size_t unmatch_alignment_data_len = KAA_ALIGNMENT + 1;

    error_code = kaa_platform_message_write_aligned(writer, unmatch_alignment_data, unmatch_alignment_data_len);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    ASSERT_EQUAL((writer->current - writer->begin), kaa_aligned_size_get(unmatch_alignment_data_len));
    error_code = (memcmp(writer->begin, unmatch_alignment_data, unmatch_alignment_data_len) == 0 ? KAA_ERR_NONE : KAA_ERR_WRITE_FAILED);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    size_t i = 0;
    size_t padding = kaa_aligned_size_get(unmatch_alignment_data_len) - unmatch_alignment_data_len;
    for (; i < padding; ++i) {
        if (*(writer->begin + unmatch_alignment_data_len + i) != 0) {
            error_code = KAA_ERR_WRITE_FAILED;
        }
    }
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    char* begin = writer->current;
    error_code = kaa_platform_message_write_aligned(writer, match_alignment_data, match_alignment_data_len);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    ASSERT_EQUAL((writer->current - begin), kaa_aligned_size_get(match_alignment_data_len));
    error_code = (memcmp(begin, match_alignment_data, kaa_aligned_size_get(match_alignment_data_len)) == 0 ? KAA_ERR_NONE : KAA_ERR_WRITE_FAILED);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    kaa_platform_message_writer_destroy(writer);
}
Пример #8
0
void test_specified_user_verifier(void **state)
{
    (void)state;

    ASSERT_EQUAL(kaa_user_manager_attach_to_user(user_manager, USER_EXTERNAL_ID, ACCESS_TOKEN, USER_VERIFIER), KAA_ERR_NONE);

    size_t expected_size = 0;
    ASSERT_EQUAL(kaa_user_request_get_size(user_manager, &expected_size), KAA_ERR_NONE);

    uint8_t buffer[expected_size];
    kaa_platform_message_writer_t *writer = NULL;
    ASSERT_EQUAL(kaa_platform_message_writer_create(&writer, buffer, expected_size), KAA_ERR_NONE);
    ASSERT_NOT_NULL(writer);

    ASSERT_EQUAL(kaa_user_request_serialize(user_manager, writer), KAA_ERR_NONE);

    uint8_t *buf_cursor = buffer;
    ASSERT_EQUAL(KAA_EXTENSION_USER, KAA_HTONS(*(uint16_t*)buf_cursor));
    buf_cursor += sizeof(uint16_t);

    char options[] = { 0x00, 0x01 };
    ASSERT_EQUAL(memcmp(buf_cursor, options, 2), 0);
    buf_cursor += 2;

    ASSERT_EQUAL(*(uint32_t * ) buf_cursor, KAA_HTONL(2 * sizeof(uint32_t)
                                                    + kaa_aligned_size_get(strlen(USER_EXTERNAL_ID))
                                                    + kaa_aligned_size_get(strlen(ACCESS_TOKEN)
                                                    + kaa_aligned_size_get(strlen(USER_VERIFIER)))));
    buf_cursor += sizeof(uint32_t);

    ASSERT_EQUAL(0, *buf_cursor);
    ++buf_cursor;

    ASSERT_EQUAL(strlen(USER_EXTERNAL_ID), *buf_cursor);
    ++buf_cursor;

    ASSERT_EQUAL(KAA_HTONS(strlen(ACCESS_TOKEN)), *(uint16_t *) buf_cursor);
    buf_cursor += sizeof(uint16_t);

    ASSERT_EQUAL(KAA_HTONS(strlen(USER_VERIFIER)), *(uint16_t *) buf_cursor);
    buf_cursor += sizeof(uint32_t); // + reserved 16B

    ASSERT_EQUAL(memcmp(buf_cursor, USER_EXTERNAL_ID, strlen(USER_EXTERNAL_ID)), 0);
    buf_cursor += kaa_aligned_size_get(strlen(USER_EXTERNAL_ID));

    ASSERT_EQUAL(memcmp(buf_cursor, ACCESS_TOKEN, strlen(ACCESS_TOKEN)), 0);
    buf_cursor += kaa_aligned_size_get(strlen(ACCESS_TOKEN));

    ASSERT_EQUAL(memcmp(buf_cursor, USER_VERIFIER, strlen(USER_VERIFIER)), 0);
    buf_cursor += kaa_aligned_size_get(strlen(USER_VERIFIER));

    kaa_platform_message_writer_destroy(writer);
}
Пример #9
0
void test_get_aligned_size(void)
{
    ASSERT_EQUAL(KAA_ALIGNMENT, kaa_aligned_size_get(KAA_ALIGNMENT));
    ASSERT_EQUAL(KAA_ALIGNMENT, kaa_aligned_size_get(KAA_ALIGNMENT - 1));
    ASSERT_EQUAL(2 * KAA_ALIGNMENT, kaa_aligned_size_get(KAA_ALIGNMENT + 1));
}
Пример #10
0
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);
}