summaryrefslogtreecommitdiff
path: root/test/configuration_tests/configuration-test.cpp
blob: 37ebfeb25296fc1be071079bd85ce814bc15770b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (C) 2015-2016 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <cstdlib>
#include <iostream>

#include <vsomeip/constants.hpp>

#include "../implementation/configuration/include/configuration.hpp"
#include "../implementation/logging/include/logger.hpp"

#define CONFIGURATION_FILE              "configuration-test.json"
#define DEPRECATED_CONFIGURATION_FILE   "configuration-test-deprecated.json"

#define EXPECTED_UNICAST_ADDRESS        "10.0.2.15"

#define EXPECTED_HAS_CONSOLE            true
#define EXPECTED_HAS_FILE                true
#define EXPECTED_HAS_DLT                false
#define EXPECTED_LOGLEVEL                "debug"
#define EXPECTED_LOGFILE                "/home/someip/another-file.log"

#define EXPECTED_ROUTING_MANAGER_HOST    "my_application"

// Services
#define EXPECTED_UNICAST_ADDRESS_1234_0022                                  EXPECTED_UNICAST_ADDRESS
#define EXPECTED_RELIABLE_PORT_1234_0022                                    30506
#define EXPECTED_UNRELIABLE_PORT_1234_0022                                  31000

#define EXPECTED_UNICAST_ADDRESS_1234_0023                                  EXPECTED_UNICAST_ADDRESS
#define EXPECTED_RELIABLE_PORT_1234_0023                                    30503
#define EXPECTED_UNRELIABLE_PORT_1234_0023                                  vsomeip::ILLEGAL_PORT

#define EXPECTED_UNICAST_ADDRESS_2277_0022                                  EXPECTED_UNICAST_ADDRESS
#define EXPECTED_RELIABLE_PORT_2277_0022                                    30505
#define EXPECTED_UNRELIABLE_PORT_2277_0022                                  31001

#define EXPECTED_UNICAST_ADDRESS_2266_0022                                  EXPECTED_UNICAST_ADDRESS
#define EXPECTED_RELIABLE_PORT_2266_0022                                    30505
#define EXPECTED_UNRELIABLE_PORT_2266_0022                                  30507

#define EXPECTED_UNICAST_ADDRESS_4466_0321                                  "10.0.2.23"
#define EXPECTED_RELIABLE_PORT_4466_0321                                    30506
#define EXPECTED_UNRELIABLE_PORT_4466_0321                                  30444

// Service Discovery
#define EXPECTED_SD_ENABLED                                                 true
#define EXPECTED_SD_PROTOCOL                                                "udp"
#define EXPECTED_SD_MULTICAST                                               "224.212.244.223"
#define EXPECTED_SD_PORT                                                    30666

#define EXPECTED_INITIAL_DELAY_MIN                                          1234
#define EXPECTED_INITIAL_DELAY_MAX                                          2345
#define EXPECTED_REPETITIONS_BASE_DELAY                                     4242
#define EXPECTED_REPETITIONS_MAX                                            4
#define EXPECTED_TTL                                                        13
#define EXPECTED_CYCLIC_OFFER_DELAY                                         2132
#define EXPECTED_REQUEST_RESPONSE_DELAY                                     1111

#define EXPECTED_DEPRECATED_INITIAL_DELAY_MIN                               10
#define EXPECTED_DEPRECATED_INITIAL_DELAY_MAX                               100
#define EXPECTED_DEPRECATED_REPETITIONS_BASE_DELAY                          200
#define EXPECTED_DEPRECATED_REPETITIONS_MAX                                 7
#define EXPECTED_DEPRECATED_TTL                                             5
#define EXPECTED_DEPRECATED_REQUEST_RESPONSE_DELAY                          2001

template<class T>
void check(const T &_is, const T &_expected, const std::string &_test) {
    if (_is == _expected) {
        VSOMEIP_INFO << "Test \"" << _test << "\" succeeded.";
    } else {
        VSOMEIP_ERROR << "Test \"" << _test << "\" failed! ("
                      << _is << " != " << _expected << ")";
    }
}

void check_file(const std::string &_config_file,
                const std::string &_expected_unicast_address,
                bool _expected_has_console,
                bool _expected_has_file,
                bool _expected_has_dlt,
                const std::string &_expected_logfile,
                const std::string &_expected_loglevel,
                const std::string &_expected_unicast_address_1234_0022,
                uint16_t _expected_reliable_port_1234_0022,
                uint16_t _expected_unreliable_port_1234_0022,
                const std::string &_expected_unicast_address_1234_0023,
                uint16_t _expected_reliable_port_1234_0023,
                uint16_t _expected_unreliable_port_1234_0023,
                const std::string &_expected_unicast_address_2277_0022,
                uint16_t _expected_reliable_port_2277_0022,
                uint16_t _expected_unreliable_port_2277_0022,
                const std::string &_expected_unicast_address_2266_0022,
                uint16_t _expected_reliable_port_2266_0022,
                uint16_t _expected_unreliable_port_2266_0022,
                const std::string &_expected_unicast_address_4466_0321,
                uint16_t _expected_reliable_port_4466_0321,
                uint16_t _expected_unreliable_port_4466_0321,
                bool _expected_enabled,
                const std::string &_expected_protocol,
                const std::string &_expected_multicast,
                uint16_t _expected_port,
                int32_t _expected_initial_delay_min,
                int32_t _expected_initial_delay_max,
                int32_t _expected_repetitions_base_delay,
                uint8_t _expected_repetitions_max,
                vsomeip::ttl_t _expected_ttl,
                vsomeip::ttl_t _expected_cyclic_offer_delay,
                vsomeip::ttl_t _expected_request_response_delay) {

    // 0. Create configuration object
    std::shared_ptr<vsomeip::configuration> its_configuration
            = vsomeip::configuration::get();

    // 1. Did we get a configuration object?
    if (0 == its_configuration) {
        VSOMEIP_ERROR << "No configuration object. "
                "Either memory overflow or loading error detected!";
        return;
    }

    // 2. Set environment variable to config file and load it
#ifndef WIN32
    setenv("VSOMEIP_CONFIGURATION", _config_file.c_str(), 1);
#else
    _putenv_s("VSOMEIP_CONFIGURATION", _config_file.c_str()
#endif
    its_configuration->load(EXPECTED_ROUTING_MANAGER_HOST);

    // 3. Check host address
    boost::asio::ip::address its_host_unicast_address
        = its_configuration->get_unicast_address();
    check<std::string>(its_host_unicast_address.to_string(),
                       _expected_unicast_address, "UNICAST ADDRESS");

    // 4. Check logging
    bool has_console = its_configuration->has_console_log();
    bool has_file = its_configuration->has_file_log();
    bool has_dlt = its_configuration->has_dlt_log();
    std::string logfile = its_configuration->get_logfile();
    boost::log::trivial::severity_level loglevel
        = its_configuration->get_loglevel();

    check<bool>(has_console, _expected_has_console, "HAS CONSOLE");
    check<bool>(has_file, _expected_has_file, "HAS FILE");
    check<bool>(has_dlt, _expected_has_dlt, "HAS DLT");
    check<std::string>(logfile, _expected_logfile, "LOGFILE");
    check<std::string>(boost::log::trivial::to_string(loglevel),
                       _expected_loglevel, "LOGLEVEL");

    // 5. Services
    std::string its_unicast_address
        = its_configuration->get_unicast_address(0x1234, 0x0022);
    uint16_t its_reliable_port
        = its_configuration->get_reliable_port(0x1234, 0x0022);
    uint16_t its_unreliable_port
        = its_configuration->get_unreliable_port(0x1234, 0x0022);

    check<std::string>(its_unicast_address,
            _expected_unicast_address_1234_0022,
            "UNICAST_ADDRESS_1234_0022");
    check<uint16_t>(its_reliable_port,
            _expected_reliable_port_1234_0022,
            "RELIABLE_PORT_1234_0022");
    check<uint16_t>(its_unreliable_port,
            _expected_unreliable_port_1234_0022,
            "UNRELIABLE_PORT_1234_0022");

    its_unicast_address
        = its_configuration->get_unicast_address(0x1234, 0x0023);
    its_reliable_port
        = its_configuration->get_reliable_port(0x1234, 0x0023);
    its_unreliable_port
        = its_configuration->get_unreliable_port(0x1234, 0x0023);

    check<std::string>(its_unicast_address,
            _expected_unicast_address_1234_0023,
            "UNICAST_ADDRESS_1234_0023");
    check<uint16_t>(its_reliable_port,
            _expected_reliable_port_1234_0023,
            "RELIABLE_PORT_1234_0023");
    check<uint16_t>(its_unreliable_port,
            _expected_unreliable_port_1234_0023,
            "UNRELIABLE_PORT_1234_0023");

    its_unicast_address
        = its_configuration->get_unicast_address(0x2277, 0x0022);
    its_reliable_port
        = its_configuration->get_reliable_port(0x2277, 0x0022);
    its_unreliable_port
        = its_configuration->get_unreliable_port(0x2277, 0x0022);

    check<std::string>(its_unicast_address,
            _expected_unicast_address_2277_0022,
            "UNICAST_ADDRESS_2277_0022");
    check<uint16_t>(its_reliable_port,
            _expected_reliable_port_2277_0022,
            "RELIABLE_PORT_2277_0022");
    check<uint16_t>(its_unreliable_port,
            _expected_unreliable_port_2277_0022,
            "UNRELIABLE_PORT_2277_0022");

    its_unicast_address
        = its_configuration->get_unicast_address(0x2266, 0x0022);
    its_reliable_port
        = its_configuration->get_reliable_port(0x2266, 0x0022);
    its_unreliable_port
        = its_configuration->get_unreliable_port(0x2266, 0x0022);

    check<std::string>(its_unicast_address,
            _expected_unicast_address_2266_0022,
            "UNICAST_ADDRESS_2266_0022");
    check<uint16_t>(its_reliable_port,
            _expected_reliable_port_2266_0022,
            "RELIABLE_PORT_2266_0022");
    check<uint16_t>(its_unreliable_port,
            _expected_unreliable_port_2266_0022,
            "UNRELIABLE_PORT_2266_0022");

    its_unicast_address
        = its_configuration->get_unicast_address(0x4466, 0x0321);
    its_reliable_port
        = its_configuration->get_reliable_port(0x4466, 0x0321);
    its_unreliable_port
        = its_configuration->get_unreliable_port(0x4466, 0x0321);

    check<std::string>(its_unicast_address,
            _expected_unicast_address_4466_0321,
            "UNICAST_ADDRESS_4466_0321");
    check<uint16_t>(its_reliable_port,
            _expected_reliable_port_4466_0321,
            "RELIABLE_PORT_4466_0321");
    check<uint16_t>(its_unreliable_port,
            _expected_unreliable_port_4466_0321,
            "UNRELIABLE_PORT_4466_0321");

    // 6. Service discovery
    bool enabled = its_configuration->is_sd_enabled();
    std::string protocol = its_configuration->get_sd_protocol();
    uint16_t port = its_configuration->get_sd_port();
    std::string multicast = its_configuration->get_sd_multicast();

    int32_t initial_delay_min = its_configuration->get_sd_initial_delay_min();
    int32_t initial_delay_max = its_configuration->get_sd_initial_delay_max();
    int32_t repetitions_base_delay = its_configuration->get_sd_repetitions_base_delay();
    uint8_t repetitions_max = its_configuration->get_sd_repetitions_max();
    vsomeip::ttl_t ttl = its_configuration->get_sd_ttl();
    int32_t cyclic_offer_delay = its_configuration->get_sd_cyclic_offer_delay();
    int32_t request_response_delay = its_configuration->get_sd_request_response_delay();

    check<bool>(enabled, _expected_enabled, "SD ENABLED");
    check<std::string>(protocol, _expected_protocol, "SD PROTOCOL");
    check<std::string>(multicast, _expected_multicast, "SD MULTICAST");
    check<uint16_t>(port, _expected_port, "SD PORT");

    check<int32_t>(initial_delay_min, _expected_initial_delay_min, "SD INITIAL DELAY MIN");
    check<int32_t>(initial_delay_max, _expected_initial_delay_max, "SD INITIAL DELAY MAX");
    check<int32_t>(repetitions_base_delay, _expected_repetitions_base_delay, "SD REPETITION BASE DELAY");
    check<uint8_t>(repetitions_max,_expected_repetitions_max, "SD REPETITION MAX");
    check<vsomeip::ttl_t>(ttl, _expected_ttl, "SD TTL");
    check<int32_t>(cyclic_offer_delay, _expected_cyclic_offer_delay, "SD CYCLIC OFFER DELAY");
    check<int32_t>(request_response_delay, _expected_request_response_delay, "SD RESPONSE REQUEST DELAY");
}



int main() {
    // Check current configuration file format
    std::cout << "/////////////////////////////////" << std::endl
              << "// CHECKING CONFIGURATION FILE //" << std::endl
              << "/////////////////////////////////" << std::endl;
    check_file(CONFIGURATION_FILE,
               EXPECTED_UNICAST_ADDRESS,
               EXPECTED_HAS_CONSOLE,
               EXPECTED_HAS_FILE,
               EXPECTED_HAS_DLT,
               EXPECTED_LOGFILE,
               EXPECTED_LOGLEVEL,
               EXPECTED_UNICAST_ADDRESS_1234_0022,
               EXPECTED_RELIABLE_PORT_1234_0022,
               EXPECTED_UNRELIABLE_PORT_1234_0022,
               EXPECTED_UNICAST_ADDRESS_1234_0023,
               EXPECTED_RELIABLE_PORT_1234_0023,
               EXPECTED_UNRELIABLE_PORT_1234_0023,
               EXPECTED_UNICAST_ADDRESS_2277_0022,
               EXPECTED_RELIABLE_PORT_2277_0022,
               EXPECTED_UNRELIABLE_PORT_2277_0022,
               EXPECTED_UNICAST_ADDRESS_2266_0022,
               EXPECTED_RELIABLE_PORT_2266_0022,
               EXPECTED_UNRELIABLE_PORT_2266_0022,
               EXPECTED_UNICAST_ADDRESS_4466_0321,
               EXPECTED_RELIABLE_PORT_4466_0321,
               EXPECTED_UNRELIABLE_PORT_4466_0321,
               EXPECTED_SD_ENABLED,
               EXPECTED_SD_PROTOCOL,
               EXPECTED_SD_MULTICAST,
               EXPECTED_SD_PORT,
               EXPECTED_INITIAL_DELAY_MIN,
               EXPECTED_INITIAL_DELAY_MAX,
               EXPECTED_REPETITIONS_BASE_DELAY,
               EXPECTED_REPETITIONS_MAX,
               EXPECTED_TTL,
               EXPECTED_CYCLIC_OFFER_DELAY,
               EXPECTED_REQUEST_RESPONSE_DELAY);

    // Check deprecated configuration file format
    std::cout << "////////////////////////////////////////////" << std::endl
              << "// CHECKING DEPRECATED CONFIGURATION FILE //" << std::endl
              << "////////////////////////////////////////////" << std::endl;
    check_file(DEPRECATED_CONFIGURATION_FILE,
               EXPECTED_UNICAST_ADDRESS,
               EXPECTED_HAS_CONSOLE,
               EXPECTED_HAS_FILE,
               EXPECTED_HAS_DLT,
               EXPECTED_LOGFILE,
               EXPECTED_LOGLEVEL,
               EXPECTED_UNICAST_ADDRESS_1234_0022,
               EXPECTED_RELIABLE_PORT_1234_0022,
               EXPECTED_UNRELIABLE_PORT_1234_0022,
               EXPECTED_UNICAST_ADDRESS_1234_0023,
               EXPECTED_RELIABLE_PORT_1234_0023,
               EXPECTED_UNRELIABLE_PORT_1234_0023,
               EXPECTED_UNICAST_ADDRESS_2277_0022,
               EXPECTED_RELIABLE_PORT_2277_0022,
               EXPECTED_UNRELIABLE_PORT_2277_0022,
               EXPECTED_UNICAST_ADDRESS_2266_0022,
               EXPECTED_RELIABLE_PORT_2266_0022,
               EXPECTED_UNRELIABLE_PORT_2266_0022,
               EXPECTED_UNICAST_ADDRESS_4466_0321,
               EXPECTED_RELIABLE_PORT_4466_0321,
               EXPECTED_UNRELIABLE_PORT_4466_0321,
               EXPECTED_SD_ENABLED,
               EXPECTED_SD_PROTOCOL,
               EXPECTED_SD_MULTICAST,
               EXPECTED_SD_PORT,
               EXPECTED_DEPRECATED_INITIAL_DELAY_MIN,
               EXPECTED_DEPRECATED_INITIAL_DELAY_MAX,
               EXPECTED_DEPRECATED_REPETITIONS_BASE_DELAY,
               EXPECTED_DEPRECATED_REPETITIONS_MAX,
               EXPECTED_DEPRECATED_TTL,
               EXPECTED_CYCLIC_OFFER_DELAY,
               EXPECTED_DEPRECATED_REQUEST_RESPONSE_DELAY);

    return 0;
}