<commit_before>/*
 * Copyright 2014 CyberVision, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include "platform/ext_sha.h"

#include "kaa_logging.h"

#ifndef KAA_DISABLE_FEATURE_LOGGING

#include "kaa_test.h"

#include "kaa_context.h"
#include "kaa_platform_protocol.h"
#include "kaa_channel_manager.h"
#include "kaa_platform_utils.h"
#include "kaa_status.h"
#include "utilities/kaa_mem.h"
#include "utilities/kaa_log.h"
#include "platform/sock.h"
#include "platform/ext_log_storage.h"
#include "platform/ext_log_upload_strategy.h"



extern kaa_error_t kaa_status_create(kaa_status_t **kaa_status_p);
extern void        kaa_status_destroy(kaa_status_t *self);

extern kaa_error_t kaa_channel_manager_create(kaa_channel_manager_t **channel_manager_p, kaa_context_t *context);
extern void        kaa_channel_manager_destroy(kaa_channel_manager_t *self);

extern kaa_error_t kaa_log_collector_create(kaa_log_collector_t ** log_collector_p
                                          , kaa_status_t *status
                                          , kaa_channel_manager_t *channel_manager
                                          , kaa_logger_t *logger);
extern void        kaa_log_collector_destroy(kaa_log_collector_t *self);

extern kaa_error_t kaa_logging_request_serialize(kaa_log_collector_t *self, kaa_platform_message_writer_t *writer);
extern kaa_error_t kaa_logging_handle_server_sync(kaa_log_collector_t *self
                                                , kaa_platform_message_reader_t *reader
                                                , uint32_t extension_options
                                                , size_t extension_length);
extern kaa_error_t kaa_logging_request_get_size(kaa_log_collector_t *self, size_t *expected_size);



static kaa_context_t kaa_context;
static kaa_logger_t *logger = NULL;
static kaa_status_t *status = NULL;
static kaa_channel_manager_t *channel_manager = NULL;

static kaa_service_t OPERATIONS_SERVICES[] = { KAA_SERVICE_PROFILE
                                             , KAA_SERVICE_USER
                                             , KAA_SERVICE_CONFIGURATION
                                             , KAA_SERVICE_EVENT
                                             , KAA_SERVICE_LOGGING
                                             , KAA_SERVICE_NOTIFICATION
                                             };
static const int OPERATIONS_SERVICES_COUNT = sizeof(OPERATIONS_SERVICES) / sizeof(kaa_service_t);

#define TEST_LOG_BUFFER  "log_record"



typedef struct {
    size_t timeout;
    size_t batch_size;
    size_t max_parallel_uploads;
    bool on_timeout_count;
    bool on_failure_count;
    ext_log_upload_decision_t decision;
} mock_strategy_context_t;

typedef struct {
    kaa_list_t *logs;
    size_t record_count;
    size_t total_size;
    bool on_remove_by_id_count;
    bool on_unmark_by_id_count;
} mock_storage_context_t;

typedef struct {
    size_t on_sync_count;
} mock_transport_channel_context_t;



/*
 * TRANSPORT_CHANNEL INTERFACE
 */
static kaa_error_t test_kaa_init_channel(void *channel_context
                                          , kaa_transport_context_t *transport_context)
{
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_set_access_point(void *channel_context
                                              , kaa_access_point_t *access_point)
{
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_get_protocol_id(void *context
                                             , kaa_transport_protocol_id_t *protocol_info)
{
    protocol_info->id = 1;
    protocol_info->version = 1;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_get_supported_services(void *context
                                                    , kaa_service_t **supported_services
                                                    , size_t *service_count)
{
    *supported_services = OPERATIONS_SERVICES;
    *service_count = OPERATIONS_SERVICES_COUNT;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_sync_handler(void *context
                                          , const kaa_service_t services[]
                                          , size_t service_count)
{
    ((mock_transport_channel_context_t *)context)->on_sync_count++;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_channel_destroy(void *context)
{
    KAA_FREE(context);
    return KAA_ERR_NONE;
}

static void test_kaa_channel_create(kaa_transport_channel_interface_t *self)
{
    self->context = KAA_CALLOC(1, sizeof(mock_transport_channel_context_t));<commit_msg>KAA-780: Make a test data constant.<commit_after>/*
 * Copyright 2014 CyberVision, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include "platform/ext_sha.h"

#include "kaa_logging.h"

#ifndef KAA_DISABLE_FEATURE_LOGGING

#include "kaa_test.h"

#include "kaa_context.h"
#include "kaa_platform_protocol.h"
#include "kaa_channel_manager.h"
#include "kaa_platform_utils.h"
#include "kaa_status.h"
#include "utilities/kaa_mem.h"
#include "utilities/kaa_log.h"
#include "platform/sock.h"
#include "platform/ext_log_storage.h"
#include "platform/ext_log_upload_strategy.h"



extern kaa_error_t kaa_status_create(kaa_status_t **kaa_status_p);
extern void        kaa_status_destroy(kaa_status_t *self);

extern kaa_error_t kaa_channel_manager_create(kaa_channel_manager_t **channel_manager_p, kaa_context_t *context);
extern void        kaa_channel_manager_destroy(kaa_channel_manager_t *self);

extern kaa_error_t kaa_log_collector_create(kaa_log_collector_t ** log_collector_p
                                          , kaa_status_t *status
                                          , kaa_channel_manager_t *channel_manager
                                          , kaa_logger_t *logger);
extern void        kaa_log_collector_destroy(kaa_log_collector_t *self);

extern kaa_error_t kaa_logging_request_serialize(kaa_log_collector_t *self, kaa_platform_message_writer_t *writer);
extern kaa_error_t kaa_logging_handle_server_sync(kaa_log_collector_t *self
                                                , kaa_platform_message_reader_t *reader
                                                , uint32_t extension_options
                                                , size_t extension_length);
extern kaa_error_t kaa_logging_request_get_size(kaa_log_collector_t *self, size_t *expected_size);



static kaa_context_t kaa_context;
static kaa_logger_t *logger = NULL;
static kaa_status_t *status = NULL;
static kaa_channel_manager_t *channel_manager = NULL;

static const kaa_service_t OPERATIONS_SERVICES[] = { KAA_SERVICE_PROFILE
                                                   , KAA_SERVICE_USER
                                                   , KAA_SERVICE_CONFIGURATION
                                                   , KAA_SERVICE_EVENT
                                                   , KAA_SERVICE_LOGGING
                                                   , KAA_SERVICE_NOTIFICATION
                                                    };
static const int OPERATIONS_SERVICES_COUNT = sizeof(OPERATIONS_SERVICES) / sizeof(kaa_service_t);

#define TEST_LOG_BUFFER  "log_record"



typedef struct {
    size_t timeout;
    size_t batch_size;
    size_t max_parallel_uploads;
    bool on_timeout_count;
    bool on_failure_count;
    ext_log_upload_decision_t decision;
} mock_strategy_context_t;

typedef struct {
    kaa_list_t *logs;
    size_t record_count;
    size_t total_size;
    bool on_remove_by_id_count;
    bool on_unmark_by_id_count;
} mock_storage_context_t;

typedef struct {
    size_t on_sync_count;
} mock_transport_channel_context_t;



/*
 * TRANSPORT_CHANNEL INTERFACE
 */
static kaa_error_t test_kaa_init_channel(void *channel_context
                                          , kaa_transport_context_t *transport_context)
{
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_set_access_point(void *channel_context
                                              , kaa_access_point_t *access_point)
{
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_get_protocol_id(void *context
                                             , kaa_transport_protocol_id_t *protocol_info)
{
    protocol_info->id = 1;
    protocol_info->version = 1;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_get_supported_services(void *context
                                                    , kaa_service_t **supported_services
                                                    , size_t *service_count)
{
    *supported_services = (kaa_service_t *)OPERATIONS_SERVICES;
    *service_count = OPERATIONS_SERVICES_COUNT;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_sync_handler(void *context
                                          , const kaa_service_t services[]
                                          , size_t service_count)
{
    ((mock_transport_channel_context_t *)context)->on_sync_count++;
    return KAA_ERR_NONE;
}

static kaa_error_t test_kaa_channel_destroy(void *context)
{
    KAA_FREE(context);
    return KAA_ERR_NONE;
}

static void test_kaa_channel_create(kaa_transport_channel_interface_t *self)
{
    self->context = KAA_CALLOC(1, sizeof(mock_transport_channel_context_t));
{'fraction_non_alphanumeric': 0.06426392067124333, 'fraction_numerical': 0.0027650648360030513, 'mean_word_length': 2.528086108308106, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 76, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1453399', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3131, 'n_words': 634}