summaryrefslogtreecommitdiff
path: root/cesar/cp2/cl_interf/test/src/test-cl-interf.c
blob: 62d8addbbf27eaf4eb848bb87956ae57c63d5117 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/cl_interf/test/src/test-cl-interf.c
 * \brief   Test the CL interface.
 * \ingroup cp2_cl_interf
 *
 * See [source:trunk/cesar/cp2/cl_interf/test/doc/cl-intef.txt]
 * or
 * [wiki:UT-CPM1-CL-INTERF]
 */
#include "common/std.h"
#include "common/defs/homeplugAV.h"

#include "lib/test.h"
#include "lib/swap.h"

#include "mac/common/store.h"

#include "cp2/cp.h"
#include "cp2/cl_interf/cl_interf.h"

#include "cp2/inc/context.h"
#include "cp2/cl_interf/inc/context.h"
#include "cp2/cl_interf/inc/cl_interf.h"
#include "cp2/cl_interf/inc/cl_interf_msg.h"

#include "cp2/msg/msg.h"

static test_t test;
static bool test_send_mme_without_mfs = false;

// Use by the msg dispatch stub.
static cp_mme_rx_t *dispatch_mme_rx;

/** Thread context. */
u8 thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];
cyg_handle_t thread_handle;
cyg_thread thread;

void
test_case_cl_interf_init (void)
{
    cp_t cp;

    test_case_begin (test, "Initialise");

    cp_cl_interf_init (&cp);

    test_begin (test, "verify")
    {
        test_fail_if (cp.cl_interf.buffer_tx_list.num_slots !=
                      CP_CL_INTERF_LIST_SIZE,
                      "Wrong number of slots for the TX buffer list");
        test_fail_if (cp.cl_interf.buffer_tx_list.nb_elements!= 0,
                      "Wrong number of elements in TX buffer list");
        test_fail_if (cyg_mbox_peek (cp.cl_interf.mailbox_handle) != 0,
                      "Wrong number of messages in the mailbox.");
    }
    test_end;
}

void
test_case_cl_interf_receive_mme (void)
{
    cp_t cp;
    mfs_rx_t *mfs;
    cp_cl_interf_msg_t *msg;
    void *cl_data;
    u8 buffer[2048] __attribute__((aligned(2048)));

    mfs = blk_alloc ();
    cp_cl_interf_init (&cp);

    test_case_begin (test, "Receive MME");

    cp_cl_interf_rx_mme (&cp, mfs, buffer, 1200, cl_data, true);

    msg = cyg_mbox_get (cp.cl_interf.mailbox_handle);
    test_begin (test, "Verify.")
    {
        test_fail_if (msg == NULL, "Message shall not be null");
        test_fail_if (msg->buffer != buffer, "Wrong MME buffer address");
        test_fail_if (msg->length != 1200, "Wrong MME length");
        test_fail_if (msg->mfs != mfs, "Wrong MFS");
        test_fail_if (msg->hard_encrypt != true, "Wrong encryption status");
        test_fail_if (msg->cl_data != cl_data, "Wrong data_cl");
    }
    test_end;
    blk_release (msg);
    blk_release (mfs);
    blk_release (mfs);

    cp_cl_interf_uninit (&cp);
}

void
test_case_cl_intef_process_mme (void)
{
    cp_t cp;
    mfs_rx_t *mfs;
    void *cl_data;
    u8 buffer[2048] __attribute__((aligned(2048)));
    u8 buffer2[2048] __attribute__((aligned(2048)));
    bitstream_t bitstream;
    u64 data;
    cp_net_t *net;
    cp_sta_t *sta;
    blk_t *current;

    cp_cl_interf_init (&cp);

    test_case_begin (test, "Process the MME : Test 1");

    mfs = blk_alloc ();
    cl_data = buffer;

    bitstream_init (&bitstream, buffer, 23, BITSTREAM_WRITE);
    data = 0x123456789ABCull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x3456789ABCDEull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x12;
    bitstream_access (&bitstream, &data, 32);
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&bitstream, &data, 16);
    data = 1;
    bitstream_access (&bitstream, &data, 8);
    data = 0x3245;
    bitstream_access (&bitstream, &data, 16);
    data = 0;
    bitstream_access (&bitstream, &data, 16);
    bitstream_finalise (&bitstream);

    cp_cl_interf_rx_mme (&cp, mfs, buffer, 1200, cl_data, true);
    cp_cl_interf_process_mme (&cp);

    test_begin (test, "Verify")
    {
        test_fail_if (dispatch_mme_rx->p_mme != buffer, "Wrong buffer address");
        test_fail_if (dispatch_mme_rx->p_frag != NULL, "frag shall be null");
        test_fail_if (dispatch_mme_rx->length != 1200, "Wrong length");
        test_fail_if (dispatch_mme_rx->cl_data != cl_data, "Wrong cl_data");
    }
    test_end;
    blk_release (mfs);
    blk_release (dispatch_mme_rx);

    test_case_begin (test, "Process the MME : Test 2");

    cp_sta_mgr_init (&cp);
    net = cp_sta_mgr_get_our_avln (&cp);
    sta = cp_net_sta_add (net, 1, 1);
    blk_release (sta);

    mfs = blk_alloc ();
    mfs->common.tei = 1;

    bitstream_init (&bitstream, buffer, 23, BITSTREAM_WRITE);
    data = 0x123456789ABCull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x3456789ABCDEull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x12;
    bitstream_access (&bitstream, &data, 32);
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&bitstream, &data, 16);
    data = 1;
    bitstream_access (&bitstream, &data, 8);
    data = 0x3245;
    bitstream_access (&bitstream, &data, 16);
    data = 1; // 2 frags.
    bitstream_access (&bitstream, &data, 4);
    data = 0; // 1st frag.
    bitstream_access (&bitstream, &data, 4);
    data = 1; // SSN.
    bitstream_access (&bitstream, &data, 4);
    bitstream_finalise (&bitstream);

    cp_cl_interf_rx_mme (&cp, mfs, buffer, 1200, cl_data, true);

    bitstream_init (&bitstream, buffer2, 23, BITSTREAM_WRITE);
    data = 0x123456789ABCull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x3456789ABCDEull;
    bitstream_access (&bitstream, &data, 48);
    data = 0x12;
    bitstream_access (&bitstream, &data, 32);
    data = swap16(HPAV_MTYPE_MME);
    bitstream_access (&bitstream, &data, 16);
    data = 1;
    bitstream_access (&bitstream, &data, 8);
    data = 0x3245;
    bitstream_access (&bitstream, &data, 16);
    data = 1; // 2 frags.
    bitstream_access (&bitstream, &data, 4);
    data = 1; // 1st frag.
    bitstream_access (&bitstream, &data, 4);
    data = 1; // SSN.
    bitstream_access (&bitstream, &data, 4);
    bitstream_finalise (&bitstream);

    cp_cl_interf_rx_mme (&cp, mfs, buffer2, 1200, cl_data, true);

    cp_cl_interf_process_mme (&cp);
    cp_cl_interf_process_mme (&cp);

    test_begin (test, "Verify")
    {
        test_fail_if (dispatch_mme_rx->p_mme != NULL , "Wrong buffer address");
        test_fail_if (dispatch_mme_rx->p_frag == NULL,
                      "frag shall not be null");
        test_fail_if (dispatch_mme_rx->length != 2400, "Wrong length");
        test_fail_if (dispatch_mme_rx->cl_data != cl_data, "Wrong cl_data");
    }
    test_end;


    if (dispatch_mme_rx->p_frag)
    {
        current = dispatch_mme_rx->p_frag;
        dispatch_mme_rx->p_frag = dispatch_mme_rx->p_frag->next;
        while (current)
        {
            blk_release_desc (current);
            current = dispatch_mme_rx->p_frag;
            if (dispatch_mme_rx->p_frag)
                dispatch_mme_rx->p_frag = dispatch_mme_rx->p_frag->next;
        }
    }

    cp_sta_mgr_uninit (&cp);
    cp_cl_interf_uninit (&cp);
    blk_release (dispatch_mme_rx);
    blk_release (mfs);
}

void
test_case_cl_interf_add_and_get_tx_buffer (void)
{
    cp_t cp;
    u8 buffer[2048] __attribute__((aligned(2048)));
    u8 *mme;

    test_case_begin (test, "Get a TX buffer");

    cp_cl_interf_init (&cp);

    cp_cl_interf_add_buffer_tx (&cp, buffer);

    mme = cp_cl_interf_get_buffer_tx (&cp);

    test_begin (test, "Verify")
    {
        test_fail_if (mme != buffer, "Buffer shall not be null");
    }
    test_end;

    cp_cl_interf_uninit (&cp);
}

void
test_case_cl_interf_send_mme (void)
{
    cp_t cp;
    cp_mme_tx_t mme;
    mfs_tx_t *mfs;
    bool added;

    cp_cl_interf_init (&cp);
    cp.mac_store = mac_store_init ();

    mfs = mac_store_mfs_add_tx (cp.mac_store, false, true, MAC_LID_NONE, 1,
                                &added);
    dbg_assert (added);

    mme.peer.tei = 1;
    mme.length = 1200;

    cp_cl_interf_mme_send (&cp, &mme);

    mac_store_mfs_remove (cp.mac_store, (mfs_t *) mfs);
    blk_release (mfs);

    mac_store_sta_remove (cp.mac_store, 0x1);
    mac_store_uninit (cp.mac_store);
    cp_cl_interf_uninit (&cp);

    /** Without MFS. */
    test_send_mme_without_mfs = true;
    cp_cl_interf_init (&cp);

    mme.peer.tei = 1;
    mme.length = 1200;

    cp_cl_interf_mme_send (&cp, &mme);

    cp_cl_interf_uninit (&cp);
}

void
test_cl_interf_thread_entry_point (cyg_addrword_t data)
{
    test_init (test, 0, NULL);

    test_case_cl_interf_init ();
    test_case_cl_interf_receive_mme ();
    test_case_cl_intef_process_mme ();
    test_case_cl_interf_add_and_get_tx_buffer ();
    test_case_cl_interf_send_mme ();

    test_case_begin (test, "Memory allocation");
    test_begin (test, "memory leaks")
    {
        test_fail_if (blk_check_memory () != true, "Memory leaks");
    }
    test_end;

    test_result (test);
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
}

int
main (void)
{
    cyg_thread_create (9, &test_cl_interf_thread_entry_point,
                       (cyg_addrword_t) 0,
                       "Test thread",
                       thread_stack,
                       CYGNUM_HAL_STACK_SIZE_TYPICAL,
                       &thread_handle, &thread);

    cyg_thread_resume (thread_handle);

    return 0;
}

/**
 * Initialise the callbacks functions.
 * \param  ctx the interface context.
 * \param  mme_recv_cb the function to call on reception of a MME.
 * \param  buffer_add_cb the function to call on buffer reception.
 * \param  beacon_add_cb the function to call on beacon reception
 * \param  user_data the data to provide on each callback function.
 */
void
interface_callback_init (interface_t *ctx, interface_mme_recv_cb_t mme_recv_cb,
           interface_mme_buffer_add_cb_t buffer_add_cb, void *user_data)
{
}

/** Provides a MME to send to the CL. This MME can be send as a MME or a data.
 * \param  ctx the interface context.
 * \param  buffer the buffer containing the MME.
 * \param  length the length of the MME.
 * \param  mfs the MFS to send the MME if the mme is to be sent over the PWL,
 * otherwise this pointer is NULL.
 */
void
interface_mme_send (interface_t *ctx, u8* buffer, uint length, mfs_tx_t *mfs)
{
    test_case_begin (test, "Send MME");

    if (!test_send_mme_without_mfs)
    {
        test_begin (test, "verify")
        {
            test_fail_if (length != 1200, "Wrong length");
            test_fail_if (mfs == NULL, "MFS shall be provided");
            test_fail_if (mfs->common.tei != 1, "Wrong MFS TEI");
        }
        test_end;
    }
    else
    {
        test_begin (test, "verify")
        {
            test_fail_if (length != 1200, "Wrong length");
        }
        test_end;
    }
}

/**
 * Inform the Data plane when the MME as been processed by the CP.
 * \param  ctx the interface context
 * \param  mme_recv the cl data (as a void pointer).
 */
void
interface_mme_recv_done (interface_t *ctx, void *mme_recv)
{
}

/**
 * Examine message type and post an event to the FSM.
 * \param  ctx  control plane context
 * \param  mme  received MME
 *
 * This function looks up the message type and translates it to a FSM event,
 * while checking the encryption is compliant with the message type.
 *
 * It also extracts and checks payload from encrypted messages.
 */
void
cp_msg_dispatch (cp_t *ctx, cp_mme_rx_t *mme)
{
    dispatch_mme_rx = mme;
}