summaryrefslogtreecommitdiff
path: root/cesar/cp/eoc/cco/action/test/utest/src/test_actions.c
blob: 3545b61b4736f798d21d4184d542d95dafe70a46 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_actions.c
 * \brief   Test eoc cco action module.
 * \ingroup test
 */
#include "common/std.h"
#include "lib/test.h"
#include "cp/cp.h"
#include "cp/mme.h"
#include "cp/inc/context.h"
#include "cl/inc/context.h"
#include "cp/eoc/msg/msg.h"
#include "cp/eoc/msg/inc/vs_eoc_get_topo.h"
#include "mac/sar/inc/context.h"
#include "common/defs/spidcom.h"
#include "cp/eoc/msg/inc/msg_vs.h"
#include "cp/eoc/cco/action/vs_eoc_master.h"

void
cp_eoc_cco_action__power_on_no_beacons (cp_t *ctx);

/** Override MFS store context. */
struct mac_store_t
{
};

static cp_t cp;
static mac_config_t mac_config;
static mac_store_t mac_store;
static cl_t cl;
static sar_t sar;

struct mme_header_t
{
    mac_t oda;
    mac_t osa;
    uint vlan;
    uint mtype;
    uint mmv;
    mmtype_t mmtype;
    uint fmi_inf;
    uint fmi_mi;
    uint fmi_ssn;
};
typedef struct mme_header_t mme_header_t;

static u8 buffer[2048];

mac_t own_mac_addr = 0x123456789ABCull;
mac_t my_mac_addr = 0x112233445566ull;
cp_nid_t nid = 0x1234567;
test_t test;
cp_mme_peer_t peer;
cp_net_t *net;

static void
test_write_header (bool vlan_present, mme_header_t *pheader,
                   bitstream_t *pstream)
{
    bitstream_init (pstream, buffer, ETH_PACKET_MAX_NOVLAN_SIZE,
                    BITSTREAM_WRITE);

    bitstream_write_large (pstream, pheader->oda, 48);
    bitstream_write_large (pstream, pheader->osa, 48);
    bitstream_write (pstream, HPAV_MTYPE_MME, 16);
    bitstream_write (pstream, HPAV_MMV, 8);
    bitstream_write (pstream, pheader->mmtype, 16);
    bitstream_write (pstream, pheader->fmi_inf, 4);
    bitstream_write (pstream, pheader->fmi_mi, 4);
    bitstream_write (pstream, pheader->fmi_ssn, 8);
    if (pheader->mmtype >= VS_MIN && pheader->mmtype <= VS_MAX)
        bitstream_write (pstream, SPC_OUI, 24);
}

static void
test_read_header (bool vlan_present, mme_header_t *pheader,
                  mme_header_t *pexpected, bitstream_t *pstream)
{
    bitstream_init (pstream, buffer, ETH_PACKET_MAX_NOVLAN_SIZE,
                    BITSTREAM_READ);

    pheader->oda = bitstream_read_large (pstream, 48);
    pheader->osa = bitstream_read_large (pstream, 48);
    if (vlan_present)
        pheader->vlan = bitstream_read (pstream, 32);
    pheader->mtype = bitstream_read (pstream, 16);
    pheader->mtype = pheader->mtype;
    pheader->mmv = bitstream_read (pstream, 8);
    pheader->mmtype = bitstream_read (pstream, 16);
    pheader->fmi_inf = bitstream_read (pstream, 4);
    pheader->fmi_mi = bitstream_read (pstream, 4);
    pheader->fmi_ssn = bitstream_read (pstream, 8);
    if (MMTYPE_IS_VS (pheader->mmtype) || MMTYPE_IS_IMAC (pheader->mmtype))
        bitstream_read (pstream, 24);

    test_begin (test, "Header")
    {
        test_fail_if (pheader->oda != pexpected->oda);
        test_fail_if (pheader->osa != pexpected->osa);
        test_fail_if (pheader->mtype != HPAV_MTYPE_MME);
        test_fail_if (pheader->mmv != HPAV_MMV);
        test_fail_if (pheader->mmtype != pexpected->mmtype);
        test_fail_if (pheader->fmi_inf != pexpected->fmi_inf);
        test_fail_if (pheader->fmi_mi != pexpected->fmi_mi);
        test_fail_if (pheader->fmi_ssn != pexpected->fmi_ssn);
    }
    test_end;
}

void
test_case_vs_eoc_get_topo (test_t test)
{
    test_case_begin (test, "VS_EOC_GET_TOPO");

    test_begin (test, "VS_EOC_GET_TOPO.REQ Receive")
    {
        bitstream_t stream;
        mme_header_t header;
        mme_header_t expected;
        uint fmi;
        u64 data;
        cp_t *ctx = &cp;

        cp_mme_rx_t *rx_mme;
        bool res;
        /* init msg */
        cp_msg_init (ctx);


        /* add my sta to avln */
        cp_sta_t *my_sta;
        cp_tei_t my_tei = 5;
        my_sta = cp_sta_mgr_sta_add (ctx, net, my_tei, my_mac_addr);
        sta_t *sta = mac_store_sta_get (ctx->mac_store, my_tei);
        s8 sta_attenuation_db;

        my_sta->multi_sta.output_level = 90;
        /* AGC gain value depends on the output level of the emitter */
        sta->agc_gain = 42;
        /*********************************/
        /*  Good values.                 */
        /*********************************/
        u8 result = CP_MSG_VS_EOC_GET_TOPO_CNF_RESULT_SUCCESS;
        u8 sta_nb = 1;

        /* Build MME Header */
        header.oda = own_mac_addr;
        header.osa = peer.mac;
        header.mmtype = VS_EOC_GET_TOPO_REQ;
        header.fmi_inf = 0;
        header.fmi_mi = 0;
        header.fmi_ssn = 0;

        test_write_header (false, &header, &stream);

        /*********************************/
        /*  Receive MME (empty data)     */
        /*********************************/
        bitstream_finalise (&stream);

        /* Receive MME */
        rx_mme = cp_msg_mme_read_header (&cp, buffer,
                                         ETH_PACKET_MAX_NOVLAN_SIZE,
                                         peer.tei, &fmi);
        test_fail_unless (rx_mme != NULL);
        test_fail_unless (rx_mme->mmtype == VS_EOC_GET_TOPO_REQ);
        data = cp_msg_mme_read_error (ctx, rx_mme);
        test_fail_unless (data == true);

        cp_eoc_cco_action_vs_eoc__cco__vs_eoc_get_topo_req (ctx, rx_mme);

        /* Expected header */
        expected.oda = peer.mac;
        expected.osa = own_mac_addr;
        expected.mmtype = VS_EOC_GET_TOPO_CNF;
        expected.fmi_inf = 0;
        expected.fmi_mi = 0;
        expected.fmi_ssn = 0;

        /* Read MME for Data comparison. */
        test_read_header (false, &header, &expected, &stream);

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != result, "Wrong Result");

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != sta_nb, "Wrong station number");

        bitstream_access (&stream, &data, 48);
        test_fail_unless (data == my_mac_addr);
        bitstream_access (&stream, &data, 8);
        test_fail_unless (data == 1);
        bitstream_access (&stream, &sta_attenuation_db, 8);
        test_fail_unless (sta_attenuation_db == 10);

        /* End TEST VS_EOC_GET_TOPO.REQ Receive */
        slab_release (rx_mme);
        blk_release (sta);
        res = mac_store_sta_remove (cp.mac_store, my_tei);
        test_fail_unless (res == true);
        slab_release (my_sta);

        /* remove my sta from avln */
        cp_sta_mgr_sta_remove_from_mac (ctx, my_mac_addr);

        /* uninit msg */
        cp_msg_uninit (ctx);
    }
    test_end;
}

void
test_case_vs_eoc_cco_get_wl (test_t test)
{
    test_case_begin (test, "VS_EOC_CCO_GET_WL");

    test_begin (test, "VS_EOC_CCO_GET_WL.REQ Receive")
    {
        uint i;
        uint j;
        bitstream_t stream;
        mme_header_t header;
        mme_header_t expected;
        uint fmi;
        u64 data;
        cp_t *ctx = &cp;
        cp_mme_rx_t *rx_mme;
        bool res;
        uint total_wl_index_nb = 4;
        uint first_wl_index_nb = 0;
        uint wl_index_nb = 4;

        /* init msg */
        cp_msg_init (ctx);

        /* add my sta to avln */
        cp_sta_t *my_stas[5];
        cp_tei_t my_teis[5];
        mac_t my_macs[5];

        for (i=0; i < 5; i++)
        {
            my_teis[i] = i;
            my_macs[i] = 0x112233445500ull + i;
            my_stas[i] = cp_sta_mgr_sta_add (ctx, net, my_teis[i], my_macs[i]);
            my_stas[i]->multi_sta.allowed = 1;
            my_stas[i]->multi_sta.output_level = 2;
            my_stas[i]->multi_sta.start_time = 55;
            my_stas[i]->multi_sta.end_time = 77;
            my_stas[i]->multi_sta.dak.key[0] = 0x11223344;
            my_stas[i]->multi_sta.dak.key[1] = 0x55667788;
            my_stas[i]->multi_sta.dak.key[2] = 0x99001122;
            my_stas[i]->multi_sta.dak.key[3] = 0x33445566;
            strcpy (((cp_sta_private_t *)my_stas[i])->dpw.dpw,
                    "SPiDCOM - SPC300");
            my_stas[i]->multi_sta.action = 1;
        }
        /*********************************/
        /*  Good values.                 */
        /*********************************/
        u8 result = CP_MSG_VS_EOC_CCO_GET_WL_REQ_RESULT_SUCCESS;
        u8 sta_nb = 4;

        /* Build MME Header */
        header.oda = own_mac_addr;
        header.osa = peer.mac;
        header.mmtype = VS_EOC_CCO_GET_WL_REQ;
        header.fmi_inf = 0;
        header.fmi_mi = 0;
        header.fmi_ssn = 0;

        test_write_header (false, &header, &stream);

        /*********************************/
        /*  Receive MME (empty data)     */
        /*********************************/
        bitstream_finalise (&stream);

        /* Receive MME */
        rx_mme = cp_msg_mme_read_header (&cp, buffer,
                                         ETH_PACKET_MAX_NOVLAN_SIZE,
                                         peer.tei, &fmi);
        test_fail_unless (rx_mme != NULL);
        test_fail_unless (rx_mme->mmtype == VS_EOC_CCO_GET_WL_REQ);
        data = cp_msg_mme_read_error (ctx, rx_mme);
        test_fail_unless (data == true);

        cp_eoc_cco_action_vs__stopped__vs_cco_get_wl_req (ctx, rx_mme);

        /* Expected header */
        expected.oda = peer.mac;
        expected.osa = own_mac_addr;
        expected.mmtype = VS_EOC_CCO_GET_WL_CNF;
        expected.fmi_inf = 0;
        expected.fmi_mi = 0;
        expected.fmi_ssn = 0;

        /* Read MME for Data comparison. */
        test_read_header (false, &header, &expected, &stream);

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != result, "Wrong Result");

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != total_wl_index_nb, "Wrong total_wl_index_nb");

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != first_wl_index_nb, "Wrong first_wl_index_nb");

        bitstream_access (&stream, &data, 8);
        test_fail_if (data != wl_index_nb, "Wrong wl_index_nb");

        for(i = 0; i < sta_nb; i++)
        {
            bitstream_access (&stream, &data, 48);
            test_fail_if ((data != my_macs[i+1]), "Wrong mac address");
            bitstream_access (&stream, &data, 8);
            test_fail_if (data != my_teis[i+1], "Wrong tei");
            test_fail_if (data == 0, "Tei = 0");
            bitstream_access (&stream, &data, 8);
            test_fail_if (data != my_stas[i+1]->multi_sta.allowed,
                         "Wrong authorization");
            bitstream_access (&stream, &data, 8);
            test_fail_if (data != my_stas[i]->multi_sta.output_level,
                         "Wrong output level");
            bitstream_access (&stream, &data, 32);
            test_fail_if (data != my_stas[i]->multi_sta.start_time,
                         "Wrong start time");
            bitstream_access (&stream, &data, 32);
            test_fail_if (data != my_stas[i]->multi_sta.end_time,
                         "Wrong end time");
            for (j=0; j<4; j++)
            {
                bitstream_access (&stream, &data, 32);
                test_fail_if (data != my_stas[i]->multi_sta.dak.key[j],
                             "Wrong sta dak");
            }
            for (j=0; j<CP_DPW_MAX_SIZE; j++)
            {
                bitstream_access (&stream, &data, 8);
                test_fail_if (data != (u64)((cp_sta_private_t *)my_stas[i])->dpw.dpw[j],
                             "Wrong sta dak");
            }
            bitstream_access (&stream, &data, 8);
            test_fail_if (data != my_stas[i]->multi_sta.action,
                         "Wrong action");
        }

        /* End TEST VS_EOC_CCO_GET_WL.REQ Receive */

        slab_release (rx_mme);

        for (i = 0; i < 4; i++)
        {
            res = mac_store_sta_remove (cp.mac_store, my_teis[i+1]);
            test_fail_unless (res == true);
        }

        /* remove my sta from avln */
        for (i = 0; i < 5; i++)
        {
            slab_release (my_stas[i]);
            cp_sta_mgr_sta_remove_from_mac (ctx, my_macs[i]);
        }
        /* uninit msg */
        cp_msg_uninit (ctx);
    }
    test_end;
}

int
main (void)
{
    test_t test;
    test_init (test, 0, NULL);

    /* configure cp */
    cp.mac_config = &mac_config;
    cp.mac_store = &mac_store;
    cp.cl = &cl;
    cp.sar=&sar;

    lib_stats_init();

    /* init sta manager */
    cp_eoc_sta_mgr_init (&cp);

    /* add avln */
    cp_snid_t snid = 4;
    net = cp_sta_mgr_add_avln (&cp, snid, nid);

    /* set avln */
    cp_sta_mgr_set_our_avln (&cp, net);

    /* set sta own data */
    cp_tei_t own_tei = 4;
    cp_sta_own_data_set_nid (&cp, nid);
    cp_sta_own_data_set_mac_address (&cp, own_mac_addr);
    cp_sta_own_data_set_tei (&cp, own_tei);
    cp_sta_own_data_set_cco_status (&cp, true);

    /* configure peer */
    peer.mac = 0x23456789ABCDull;
    peer.eth_type = HPAV_MTYPE_MME;
    peer.vlan_tci = 0x0;
    peer.tei = MAC_TEI_FOREIGN;

    /* 1. test VS_EOC_GET_TOPO */
    test_case_vs_eoc_get_topo (test);

    /* 2. test VS_EOC_CCO_GET_WL */
    test_case_vs_eoc_cco_get_wl (test);

    /* 3. test power on */
    test_begin (test, "Compile");
    {
        cp_eoc_cco_action__power_on_no_beacons (&cp);
    }
    test_end;

    /* remove avln */
    cp_sta_mgr_remove_avln (&cp, snid, nid);

    /* uninit sta own data */
    cp_sta_own_data_uninit (&cp);

    /* uninit sta manager */
    cp_sta_mgr_uninit (&cp);

    /* uninit net */
    cp_net_uninit (&cp, net);

    lib_stats_uninit();

    /* 4. check memory usage */
    test_begin (test, "Memory verification")
    {
        test_fail_if (blk_check_memory() == false, "Memory not freed");
    }
    test_end;

    /* 5. check test result */
    test_result (test);
    return test_nb_failed (test) == 0 ? 0 : 1;
}

u8 *
cp_cl_interf_get_buffer_tx (cp_t *ctx)
{
    dbg_assert (ctx);
    return buffer;
}

/**
 * Send a MME over the PWL or the HLE.
 * \param  ctx  the module context.
 * \param  mme  The MME to send.
 *
 */
void
cp_cl_interf_mme_send (cp_t *ctx, cp_mme_tx_t * mme)
{
    mme->p_mme = NULL;
}