summaryrefslogtreecommitdiff
path: root/cesar/cp2/sta/mgr/test/src/sta-test.c
blob: 68abb71aa176aabfd06447fc64423259e631cab2 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/sta-test.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"
#include "lib/test.h"
#include "lib/bitstream.h"

#include "cp2/sta/mgr/sta_own_data.h"

#include "cp2/inc/context.h"
#include "cp2/sta/mgr/inc/sta_own_data.h"

/**
 * Test the init of the module.
 *
 * \param  test  the test object.
 */
void
test_case_sta_own_data_init (test_t test)
{
    cp_t cp;
    cp_sta_own_data_private_t *data;

    test_case_begin (test, "Station own data init");

    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_begin (test, "Initialise")
    {
        /* Public data. */
        test_fail_if (data->public.nek_change_rtc != 0, "Wrong nek rtc");
        test_fail_if (data->public.cco_prefered != 0, "Wrong cco preferred");
        test_fail_if (data->public.hfid_avln[0] != 0, "Wrong hfid AVLN");
        test_fail_if (data->public.hfid_user[0] != 0, "Wrong hfid user");
        test_fail_if (data->public.hfid_manufacturer [0] != 0,
                      "Wrong hfid manufacturer");
        test_fail_if (data->public.hybrid_mode != 0, "Wrong hybrid mode");
        test_fail_if (data->public.snid_track != 0, "Wrong snid track");
        test_fail_if (data->public.nid_track != 0, "Wrong nid track");
        test_fail_if (data->public.eks_current != 0, "Wrong EKS");
        test_fail_if (data->pco_glid != 0, "Wrong PCo status");
        test_fail_if (data->public.pco != 0, "Wrong Pco");
        test_fail_if (data->public.authenticated != false,
                      "Wrong authenticated status");

        /* Private data. */
        test_fail_if (data->tei != 0, "Wrong tei");
        test_fail_if (data->mac_addr != 0, "Wrong Mac Address");
        test_fail_if (data->npw[0] != 0, "Wrong NPW");
        test_fail_if (data->nmk.key[0] != 0, "Wrong NMK");
        test_fail_if (data->dpw[0] != 0, "Wrong DPW");
        test_fail_if (data->was_cco != 0, "Wrong Was CCo");
        test_fail_if (data->security_level != 0, "Wrong Was SL");
    }
    test_end;
}

/**
 * Test the set and get TEI procedure.
 *
 * \param  test  the test object.
 *
 * This function will test that the TEI is correctly stored in both ma config
 * and station own data.
 */
void
test_case_sta_tei (test_t test)
{
    mac_config_t mac_config;
    cp_t cp;
    cp_sta_own_data_private_t *data;

    test_case_begin (test, "TEI");

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    // Add the mac config to the CP.
    cp.mac_config = &mac_config;

    mac_config.tei = 0xb;
    data->tei = 0xb;

    cp_sta_own_data_set_tei (&cp, 0xA);

    test_begin (test, "verify data->stored")
    {
        test_fail_if (data->tei != 0xA, "Wrong tei");
        test_fail_if (mac_config.tei != 0xA, "Wrong tei");
    }
    test_end;

    test_begin (test, "get the TEI with accessor")
    {
        test_fail_if (cp_sta_own_data_get_tei (&cp) !=
                      0xA, "Wrong tei");
    }
    test_end;
}

/**
 * Test the set and get Mac Address procedure.
 *
 * \param  test  the test object.
 *
 * This function will test that the mac address is correctly stored in
 * both ma config and station own data.
 */
void
test_case_sta_mac_address (test_t test)
{
    mac_config_t mac_config;
    mac_t mac_addr;
    cp_t cp;
    cp_sta_own_data_private_t *data;

    test_case_begin (test, "Mac address");

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    // Add the mac config to the CP.
    cp.mac_config = &mac_config;

    mac_addr = 0x123456789abcull;
    mac_config.sta_mac_address = mac_addr;
    data->tei = mac_addr;

    mac_addr = 0x9876543210abull;
    cp_sta_own_data_set_mac_address (&cp, mac_addr);

    test_begin (test, "verify data stored")
    {
        test_fail_if (data->mac_addr != mac_addr, "Wrong tei");
        test_fail_if (mac_config.sta_mac_address != mac_addr, "Wrong tei");
    }
    test_end;

    test_begin (test, "get the Mac address with accessor")
    {
        test_fail_if (cp_sta_own_data_get_mac_address (&cp) !=
                      mac_addr, "Wrong Mac address");
    }
    test_end;
}

/**
 * Test the set and get NPW procedure.
 *
 * \param  test  the test object.
 *
 * This function will test that the NPW is correctly stored in
 * station own data.
 */
void
test_case_sta_npw (test_t test)
{
    cp_t cp;
    u8 npw [CP_NPW_MAX_SIZE] = "HPAV_1.1\0";
    cp_sta_own_data_private_t *data;

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_case_begin (test, "NPW");

    cp_sta_own_data_set_npw (&cp, (char*) npw);

    test_begin (test, "store of the NPW")
    {
        test_fail_if (memcmp(data->npw, npw, sizeof(npw)) != 0,
                                       "wrong NPW");
    }
    test_end;

    test_begin (test, "get the NPW")
    {
        test_fail_if (bitstream_memcmp (
                    (u8 *) cp_sta_own_data_get_npw(&cp),
                    npw, sizeof (npw)) != true, "Wrong NPW");
    }
    test_end;
}

/**
 * Test the set and get NMK procedure.
 *
 * \param  test  the test object.
 *
 * This function will test that the NMK is correctly stored in
 * station own data.
 */
void
test_case_sta_nmk (test_t test)
{
    cp_t cp;
    cp_key_t nmk;
    cp_sta_own_data_private_t *data;

    // initialise.
    cp_sta_own_data_init (&cp);

    test_case_begin (test, "NMK");

    nmk.key[0] = 0x5426;
    cp_sta_own_data_set_nmk (&cp, nmk);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_begin (test, "store of the NMK")
    {
        test_fail_if (data->nmk.key[0] != nmk.key[0],
                                       "wrong NMK");
    }
    test_end;

    test_begin (test, "get the NMK")
    {
        test_fail_if (cp_sta_own_data_get_nmk(&cp).key[0] !=
                            nmk.key[0], "Wrong NMK");
    }
    test_end;
}

/**
 * Test the set and get DPW procedure.
 *
 * \param  test  the test object.
 *
 * This function will test that the DPW is correctly stored in
 * station own data.
 */
void
test_case_sta_dpw (test_t test)
{
    cp_t cp;
    u8 dpw [CP_DPW_MAX_SIZE] = "HPAV_1.1\0";
    cp_sta_own_data_private_t *data;

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_case_begin (test, "DPW");

    cp_sta_own_data_set_dpw (&cp, (char*)dpw);

    test_begin (test, "store of the DPW")
    {
        test_fail_if (bitstream_memcmp(data->dpw, dpw, sizeof(dpw)) != true,
                                       "wrong DPW");
    }
    test_end;

    test_begin (test, "get the DPW")
    {
        test_fail_if (bitstream_memcmp (
                    (u8 *) cp_sta_own_data_get_dpw(&cp),
                    dpw, sizeof (dpw)) != true, "Wrong NMK");
    }
    test_end;
}

/**
 * Test the security level APIs set and get.
 *
 * \param  test  the test object.
 */
void
test_case_security_level (test_t test)
{
    cp_t cp;
    uint sl;
    cp_sta_own_data_private_t *data;

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_case_begin (test, "SL");

    data->security_level = 0xF;
    sl = 1;

    cp_sta_own_data_set_security_level (&cp, sl);

    test_begin (test, "Security level storage")
    {
        test_fail_if (sl != data->security_level, "Wrong SL");
    }
    test_end;

    test_begin (test, "get Security level")
    {
        test_fail_if (sl != cp_sta_own_data_get_security_level(&cp),
                      "Wrong SL");
    }
    test_end;
}

/**
 * Test the was CCo APIs set and get.
 *
 * \param  test  the test object.
 */
void
test_case_was_cco (test_t test)
{
    cp_t cp;
    bool was_cco;
    cp_sta_own_data_private_t *data;

    // initialise.
    cp_sta_own_data_init (&cp);
    data = (cp_sta_own_data_private_t *) cp_sta_mgr_get_sta_own_data (&cp);

    test_case_begin (test, "Was CCo");

    data->was_cco = false;
    was_cco = true;

    cp_sta_own_data_set_was_cco (&cp, was_cco);

    test_begin (test, "Was CCo storage")
    {
        test_fail_if (was_cco != data->was_cco, "Wrong Was CCo status");
    }
    test_end;

    test_begin (test, "get Security level")
    {
        test_fail_if (was_cco != cp_sta_own_data_get_was_cco (&cp),
                      "Wrong Was cco status");
    }
    test_end;
}

int
main (void)
{
    test_t test;

    test_init (test, 0, NULL);

    test_case_sta_own_data_init (test);
    test_case_sta_tei (test);
    test_case_sta_mac_address (test);
    test_case_sta_npw (test);
    test_case_sta_nmk (test);
    test_case_sta_dpw (test);
    test_case_security_level (test);
    test_case_was_cco (test);

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