summaryrefslogtreecommitdiff
path: root/interface/test/src/test-interface.c
blob: aacdafce92c2cef33a967306a5536b7e61a0b09b (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    interface/test/src/test-interface.c
 * \brief   Test the interface module. 
 * \ingroup interface
 *
 */
#include "common/std.h"
#include "lib/test.h"

#include "hal/hle/ipmbox.h"
#include "cl/cl.h"
#include "mac/sar/sar.h"
#include "interface/interface.h"

#include "interface/sniffer/sniffer.h"

#include "interface/inc/interface.h"
#include "interface/inc/context.h"

#include <cyg/hal/hal_arch.h>
#include <cyg/kernel/kapi.h>


#include "mac/common/config.h"

#include <stdlib.h>


interface_t *interface;
bool test_buffer_add;
bool test_copy;
bool test_mme_recv;
bool test_beacon_add;

void
test_interface_mme_buffer_add (void *user_data, u8 *buffer);

void
test_interface_mme_recv (void *user_data, mfs_rx_t *mfs, u8 *buffer, uint length, void *mme_recv, bool encrypted);

void
test_interface_beacon_add(void *user_data, pb_beacon_t *beacon);

void ipmbox_tx (ipmbox_t *ctx, u32 *first_msg, uint length);


void
test_configure_sniffer (test_t test)
{
    u8 buffer[3];
    test_case_begin (test, "Configure the sniffer");

    buffer[0] = INTERFACE_MODULE_SNIFFER;
    buffer[1] = 1;

    buffer[2] = 0x1;
    interface_configure (interface, buffer);  
    test_begin (test, "Activate the mme tx")
    {
        test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) !=
                      true, "Error sniffer mme TX shall be active");
    }
    test_end;

    buffer[2] = 0x3;
    interface_configure (interface, buffer);  
    test_begin (test, "Anactivate the mme tx/rx")
    {
        test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) !=
                      true, "Error sniffer mme TX shall be active");
        test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer) !=
                      true, "Error sniffer mme RX shall be active");
    }
    test_end;

    buffer[2] = 0x7;
    interface_configure (interface, buffer);  
    test_begin (test, "Anactivate the mme tx/rx, beacon Tx")
    {
        test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer) !=
                      true, "Error sniffer mme TX shall be active");
        test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer) !=
                      true, "Error sniffer mme RX shall be active");
        test_fail_if (interface_sniffer_beacon_status_tx (interface->sniffer) !=
                      true, "Error sniffer beacon TX shall be active");
    }
    test_end;

    buffer[2] = 0xF;
    interface_configure (interface, buffer);  
    test_begin (test, "Sniffer conf")
    {
        test_fail_if (interface_sniffer_beacon_status_rx (interface->sniffer)
                      != true, "Error sniffer beacon RX shall be active");
        test_fail_if (interface_sniffer_beacon_status_tx (interface->sniffer)
                      != true, "Error sniffer beacon TX shall be active");
        test_fail_if (interface_sniffer_mme_status_rx (interface->sniffer)
                      != true, "Error sniffer MME RX shall be active");
        test_fail_if (interface_sniffer_mme_status_tx (interface->sniffer)
                      != true, "Error sniffer MME TX shall be active");

    }
    test_end;
}

void
test_adding_buffers (test_t test)
{
    u8 *real_buffer;
    u8 *buffer_got;

    test_case_begin (test, "Adding buffers to Control Plane");

    test_begin (test, "add buffer")
    {
        real_buffer = malloc (2048);
        test_buffer_add = false; 
        interface_buffer_add (interface, real_buffer);

        test_fail_if (test_buffer_add != true, "Error the buffer shall be kept by the interface");
    }
    test_end;

    test_case_begin (test, "Adding and getting buffers");

    test_begin (test, "add buffer")
    {
        real_buffer = malloc (2048);
        test_buffer_add = false; 
        interface_buffer_work_add (interface, real_buffer);

        buffer_got = interface_buffer_work_get (interface);

        test_fail_if (buffer_got != real_buffer, "Error, buffer shall be the same");

        if (buffer_got)
            free (buffer_got);
    }
    test_end;

}

void
test_copy_recv_mme (test_t test)
{
    u8 mme [1000];
    uint i;

    for (i = 0; i < 1000; i++)
    {
        mme[i] = i;
    }

    test_copy = false;
    test_mme_recv = false;

    /* Add a buffer to the interface. */
    interface_buffer_work_add (interface, malloc (2048));

    interface_mme_recv (interface, NULL ,mme, 1000, NULL, false); 

    test_case_begin (test, "Sniff a received MME");

    test_begin (test, "verifing copy")
    {
        test_fail_if (test_copy == false, "MME not copied");
        test_fail_if (test_mme_recv == false, "MME not send to the CP");
    }
    test_end;

    interface_mme_recv_done (interface, interface);
}

void
test_copy_send_mme(test_t test)
{
    u8 mme [1000];
    uint i;

    for (i = 0; i < 1000; i++)
    {
        mme[i] = i;
    }

    /* Add a buffer to the interface. */
    interface_buffer_work_add (interface, malloc (2048));

    test_copy = false;
    test_mme_recv = false;
    interface_mme_send (interface, mme, 1000, NULL); 

    test_case_begin (test, "Sniff a MME tx");

    test_begin (test, "verifing copy")
    {
        test_fail_if (test_copy == false, "MME not copied");
    }
    test_end;

}

void
test_copy_send_beacon (test_t test)
{
    pb_beacon_t *beacon;
    mfs_tx_t *mfs;

    beacon = (pb_beacon_t *) blk_alloc_desc ();
    mfs = blk_alloc ();

    /* Add a buffer to the interface. */
    interface_buffer_work_add (interface, malloc (2048));

    test_copy = false; 
    interface_beacon_prepare (interface, beacon, 0x123456789abcull, mfs, &beacon->first_data_word);

    test_case_begin (test, "Sniff a beacon tx");

    test_begin (test, "verifing copy")
    {
        test_fail_if (test_copy == false, "beacon not copied");
    }
    test_end;


    blk_release_desc ((blk_t *) beacon);
    blk_release (mfs);
}

void
test_copy_recv_beacon (test_t test)
{
    pb_beacon_t *beacon;

    beacon = (pb_beacon_t *) blk_alloc_desc ();

    /* Add a buffer to the interface. */
    interface_buffer_work_add (interface, malloc (2048));

    test_copy = false; 
    test_beacon_add = false;
    interface_beacon_add (interface, beacon, NULL); 

    test_case_begin (test, "Sniff a beacon tx");

    test_begin (test, "verifing copy")
    {
        test_fail_if (test_copy == false, "beacon not copied");
        test_fail_if (test_beacon_add == false, "beacon not transmitted");
    }
    test_end;

    blk_release_desc ((blk_t *) beacon);
}


int
main (void)
{
    test_t test;
    cl_t *cl;
    sar_t *sar;
    hle_t *hle;
    mac_config_t mac_config;

    test_init (test, 0, NULL);

    cl = cl_init(NULL, NULL, NULL);
    sar = sar_init (NULL, NULL, NULL);
    hle = blk_alloc ();

    mac_config.sta_mac_address = 0x123456789abcull;
    interface = interface_init (hle, cl, sar, &mac_config);
    interface_callback_init (interface, test_interface_mme_recv,
                         test_interface_mme_buffer_add, 
                         test_interface_beacon_add, interface);


    test_configure_sniffer (test); 
    test_adding_buffers (test);
    test_copy_recv_mme (test);
    test_copy_send_mme(test);
    test_copy_send_beacon(test);
    test_copy_recv_beacon (test); 
    interface_uninit (interface);
    blk_release (hle);

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

/** 
 * Initialise the interface to add an Interface buffer.
 *
 * \param  ctx  the hle context.
 * \param  cb  the function to call on interface buffer reception.
 * \param  user_data  the user_data to provide on function call.
 */
void
hle_init_interface_buffer_add_cb (hle_t *ctx, hle_interface_buffer_add_cb_t
                                  cb, void *user_data)
{
}


void
test_interface_mme_buffer_add (void *user_data, u8 *buffer)
{
    dbg_assert (buffer);
    free (buffer);

    test_buffer_add = true;
}


void
test_interface_mme_recv (void *user_data, mfs_rx_t *mfs, u8 *buffer, uint length, void *mme_recv, bool encryption)
{
    dbg_assert (buffer);

    test_mme_recv = true;
}


void
test_interface_beacon_add(void *user_data, pb_beacon_t *beacon)
{
    dbg_assert (beacon);
    test_beacon_add = true;
}

void
hle_ipmbox_send (hle_t *ctx, u32 *msg, uint length)
{
    test_copy = true;
}