summaryrefslogtreecommitdiff
path: root/cesar/cp2/cco/action/test/src/action-test.c
blob: 900953d5fb6620e59a18e88d2edac6f1846f1c5d (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/action-test.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"
#include "string.h"

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

#include "cl/cl_mactotei.h"

#include "common/defs/homeplugAV.h"
#include "common/defs/ethernet.h"

#include "mac/common/ntb.h"

#include "cp2/cp.h"
#include "cp2/fsm/fsm.h"
#include "cp2/msg/msg.h"
#include "cp2/sta/mgr/sta.h"

#include "cp2/cco/action/cco_action.h"
#include "cp2/cco/action/inc/cco_action.h"

#include "cp2/inc/context.h"

test_t test;

// The value provided to the module beacon.
u16 snid_compute;

// A Simple counter.
uint tei_map_cnt;

static cp_fsm_event_t fsm_event;
static bool fsm_event_posted = false;

/**
 * Verify TEI lease computing function.
 * \param  test  the test object.
 *
 * Test 1:
 * Initialise the TEI flags to the value 0x0124 in the first word.
 * The TEI return shall be 2.
 *
 * Test 2:
 * Initialise the TEI flags first word to 0xFFFFFFFE.
 * The second one with 0xFFFFEFFF
 * The TEI returned shall be 45.
 *
 * Test 3:
 * Initialise the TEI flags first word to 0xFFFFFFFE.
 * Initialise the TEI flags second word to 0xFFFFFFFF.
 * Initialise the TEI flags ... word to 0xFFFFFFFF.
 * Initialise the TEI flags eights word to 0x7FFFFFFF.
 * The TEI returned shall be 0.
 */
void
test_case_tei_lease_create (test_t test)
{
   cp_cco_action_t action;
   uint tei;

   test_case_begin (test, "TEI Lease create");

   action.tei_flags[0] = 0x0124;

   tei = cp_cco_action_tei_compute (&action);

   test_begin (test, "Test 1")
   {
       test_fail_if (tei != 2, "TEI shall be 2");
       test_fail_if ((action.tei_flags[0] & 0x2) != 2,
                     "TEI bit flag shall be setted");
   }
   test_end;

   action.tei_flags[0] = 0xFFFFFFFE;
   action.tei_flags[1] = 0xFFFFEFFF;

   tei = cp_cco_action_tei_compute (&action);

   test_begin (test, "Test 2")
   {
       test_fail_if (tei != 45, "TEI shall be 57");
       test_fail_if ((action.tei_flags[1] & 0xC) != 12,
                     "TEI bit flag shall be setted");
   }
   test_end;

   action.tei_flags[0] = 0xFFFFFFFE;
   action.tei_flags[1] = 0xFFFFFFFF;
   action.tei_flags[2] = 0xFFFFFFFF;
   action.tei_flags[3] = 0xFFFFFFFF;
   action.tei_flags[4] = 0xFFFFFFFF;
   action.tei_flags[5] = 0xFFFFFFFF;
   action.tei_flags[6] = 0xFFFFFFFF;
   action.tei_flags[7] = 0x7FFFFFFF;

   tei = cp_cco_action_tei_compute (&action);

   test_begin (test, "Test 3")
   {
       test_fail_if (tei != 0, "TEI shall be 0");
       test_fail_if (action.tei_flags[7] != 0x7FFFFFFF,
                     "TEI bit flag shall be setted");
   }
   test_end;

}

/**
 * Verify TEI release function.
 * \param  test  the test object.
 *
 * Test 1:
 * Initialise the TEI flags to the value 0x0126 in the first word.
 * The TEI return shall be 2
 *
 * Test 2:
 * Initialise the TEI flags first word to 0xFFFFFFFE.
 * The second one with 0xFFFFFFFF
 * The TEI returned shall be 45.
 */
void
test_case_tei_release (test_t test)
{
   cp_cco_action_t action;
   uint tei;

   test_case_begin (test, "TEI Lease release");

   action.tei_flags[0] = 0x0126;

   //Release TEI 2.
   cp_cco_action_tei_release (&action, 2);

   // Get TEI.
   tei = cp_cco_action_tei_compute (&action);

   test_begin (test, "Test 1")
   {
       test_fail_if (tei != 2, "TEI shall be 2");
       test_fail_if ((action.tei_flags[0] & 0x2) != 2,
                     "TEI bit flag shall be setted");
   }
   test_end;

   action.tei_flags[0] = 0xFFFFFFFE;
   action.tei_flags[1] = 0xFFFFFFFF;

   cp_cco_action_tei_release (&action, 45);
   tei = cp_cco_action_tei_compute (&action);

   test_begin (test, "Test 2")
   {
       test_fail_if (tei != 45, "TEI shall be 57");
       test_fail_if ((action.tei_flags[1] & 0xC) != 12,
                     "TEI bit flag shall be setted");
   }
   test_end;
}

/** Test the initialisation of the cco_action module.
 * \param  test  the test object.
 *
 * The initialise procedure shall only initialise the set for the expiration
 * process.
 * At the end of this test the set shall be empty.
 */
void
test_case_cco_action_init (test_t test)
{
    cp_t cp;
    uint i;

    test_case_begin (test, "Initialise the module.");

    cp_cco_action_init (&cp);

    test_begin (test, "Verify")
    {
        for (i = 0; i < 8; i++)
        {
            test_fail_if (cp.cco_action.tei_flags[i] != 0,
                      "TEI flags shall be null");
        }
    }
    test_end;
}

/** Change SNID.
 * \param  test  the test object.
 *
 * Test the procedure to modify the SNID at the beginning of the AVLN, and
 * simulating an AVLN SNID collision.
 *
 * Test 1: Choosing SNID at the beginning.
 * This shall choose randomly a value of SNID. The random generator is
 * initialised with the mac_ntb date.
 *
 * Environment:
 *  The phy_date override in the test shall always return the same value
 *  in order to verify the SNID chosen.
 *
 *  The all range of SNID values shall be available. The flags returned
 *  by the override of the get_snids function from the sta manager shall
 *  return 0x0.
 *
 * Test 2: Choosing SNID after collision.
 *
 * This shall choose randomly a value of SNID between the following ones,
 * 2, 3, 4, 7, 9, 11, 13, 14. So the flag returned by the
 * get_snids function from the sta manager shall be not(0x9563) so 0x6A9C.
 *
 * Once this is done the value of the new SNID shall be provide to the beacon
 * module using change_snid function.
 */
void
test_case_change_snid (test_t test)
{
    cp_t cp;

    test_case_begin (test, "change SNID test 1");

    cp_cco_action_init (&cp);
    cp_sta_mgr_init (&cp);

    cp_cco_action_snid_change (&cp);

    test_begin (test, "Verify SNID chosen")
    {
        test_fail_if (snid_compute != 13, "Wrong snid");
    }
    test_end;

    test_case_begin (test, "change SNID test 2");

    cp_cco_action_init (&cp);

    /* Adding the AVLN. */
    cp_sta_mgr_add_avln (&cp, 1, 1);
    cp_sta_mgr_add_avln (&cp, 5, 2);
    cp_sta_mgr_add_avln (&cp, 6, 3);
    cp_sta_mgr_add_avln (&cp, 8, 4);
    cp_sta_mgr_add_avln (&cp, 10, 5);
    cp_sta_mgr_add_avln (&cp, 12, 6);
    cp_sta_mgr_add_avln (&cp, 15, 7);

    cp_cco_action_snid_change (&cp);

    test_begin (test, "Verify SNID chosen")
    {
        test_fail_if (snid_compute != 13, "Wrong snid");
    }
    test_end;

    cp_sta_mgr_uninit (&cp);
}

/** Sta association procedure.
 * \param  test  the test object.
 *
 * Test 1: Receiving a CC_ASSOC.REQ join
 *
 * First station to associate
 * The CCo shall try to associate the station to its AVLN.
 *  1. Get an available TEI different from the one of the own Station's TEI.
 *  2. Add the station to the station list (use the station manager).
 *  3. Answer the request.
 *
 * At the end of this test, the station shall be present in the station
 * manager list. The station shall be in the associate state.
 *
 * Second station to associate
 * The CCo tries to associate another station to the AVLN.
 *
 *  1. Get an available TEI different from the previous registered station
 *     and the own station.
 *  2. Add the station to the station manager list.
 *  3. Answer the request.
 *
 * At the end of the test two station shall be present with different TEIs.
 *
 * No more station can be associate
 * The CCo tries to associate another station to the AVLN, but there are
 * no more TEIs available.
 *
 *  1. The CCo tries to get a TEI for the station, the function shall
 *     return 0.
 *  2. Add the station to the sta manager as unassociated sta.
 *  3. Answer the request with a failed code.
 *
 *  Test 2: Receiving a CC_ASSOC.REQ renew
 *
 *  This shall only verify if the TEI lease is correctly update in the
 *  station.
 *
 *  At the end of this test, the station requiring the renew shall have 45
 *  minutes more before the expiration of the TEI lease.
 *
 *  Test 3: Receiving a CC_ASSOC_REQ join from a associated station.
 *
 *  At the end the station shall still exists with a different TEI.
 *   1. The station is removed from the AVLN. (a SET_TEI_MAP is send)
 *   2. The station is added in the AVLN (a SET_TEI_MAP is send).
 */
void
test_case_sta_assoc_procedure (test_t test)
{
    cp_t cp;
    bitstream_t bitstream;
    u8 buffer[256] __attribute__((aligned(256)));
    uint req_type;
    u64 nid = 12;
    uint cco_cap = 2;
    uint pco_cap = true;
    cp_mme_rx_t mme_rx;
    cp_net_t *net;
    cp_sta_t *sta;
    cl_mactotei_blk_t *table;

    cp.cl = blk_alloc ();
    memset (cp.cl, 0, BLK_SIZE);
    cp_cco_action_init (&cp);
    cp_sta_mgr_init (&cp);

    cp_sta_mgr_update_our_avln_nid (&cp, nid);

    test_case_begin (test, "Test 1: Receiving a CC_ASSOC.REQ join");

    // Construct the MME to associate the first station.
    mme_rx.peer.mac = 2;
    mme_rx.peer.vlan_tag = 0;
    mme_rx.peer.tei = 0;

    // Write the request.
    bitstream_init (&bitstream, buffer + 19, 10, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &req_type, 8);
    bitstream_access (&bitstream, &nid, 56);
    bitstream_access (&bitstream, &cco_cap, 8);
    bitstream_access (&bitstream, &pco_cap, 8);
    bitstream_finalise (&bitstream);

    // Initialise the MME Rx object.
    mme_rx.p_mme = buffer;
    mme_rx.length = 60;
    bitstream_init (&mme_rx.bitstream, buffer + 19, 60, BITSTREAM_READ);

    cp_cco_action_manage_sta_assoc (&cp, &mme_rx);

    // Second station to add.
    // Construct the MME to associate the second station.
    mme_rx.peer.mac = 3;
    mme_rx.peer.vlan_tag = 0;
    mme_rx.peer.tei = 0;


    // Write the request.
    bitstream_init (&bitstream, buffer + 19, 10, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &req_type, 8);
    bitstream_access (&bitstream, &nid, 56);
    bitstream_access (&bitstream, &cco_cap, 8);
    bitstream_access (&bitstream, &pco_cap, 8);
    bitstream_finalise (&bitstream);

    // Initialise the MME Rx object.
    mme_rx.p_mme = buffer;
    mme_rx.length = 60;
    bitstream_init (&mme_rx.bitstream, buffer + 19, 60, BITSTREAM_READ);

    cp_cco_action_manage_sta_assoc (&cp, &mme_rx);

    // The next station can not be associated because the TEIs are already in
    // use.
    cp.cco_action.tei_flags[0] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[1] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[2] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[3] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[4] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[5] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[6] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[7] = 0xFFFFFFFF;

    // third station to add.
    // Construct the MME to associate the third station.
    mme_rx.peer.mac = 4;
    mme_rx.peer.vlan_tag = 0;
    mme_rx.peer.tei = 0;

    // Write the request.
    bitstream_init (&bitstream, buffer + 19, 10, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &req_type, 8);
    bitstream_access (&bitstream, &nid, 56);
    bitstream_access (&bitstream, &cco_cap, 8);
    bitstream_access (&bitstream, &pco_cap, 8);
    bitstream_finalise (&bitstream);

    // Initialise the MME Rx object.
    mme_rx.p_mme = buffer;
    mme_rx.length = 60;
    bitstream_init (&mme_rx.bitstream, buffer + 19, 60, BITSTREAM_READ);

    cp_cco_action_manage_sta_assoc (&cp, &mme_rx);

    test_case_begin (test, "Test 2: Receiving a CC_ASSOC.REQ renew");

    // Renew the lease.
    mme_rx.peer.mac = 2;
    mme_rx.peer.vlan_tag = 0;
    mme_rx.peer.tei = 2;

    // Write the request.
    req_type = CP_MSG_CC_ASSOC_REQ_TYPE_RENEW;
    bitstream_init (&bitstream, buffer + 19, 10, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &req_type, 8);
    bitstream_access (&bitstream, &nid, 56);
    bitstream_access (&bitstream, &cco_cap, 8);
    bitstream_access (&bitstream, &pco_cap, 8);
    bitstream_finalise (&bitstream);

    // Initialise the MME Rx object.
    mme_rx.p_mme = buffer;
    mme_rx.length = 60;
    bitstream_init (&mme_rx.bitstream, buffer + 19, 60, BITSTREAM_READ);

    net = cp_sta_mgr_get_our_avln (&cp);
    sta = cp_net_get_sta (&cp, net, 2);
    cp_sta_set_state (sta, CP_STA_STATE_AUTHENTICATED);
    slab_release (sta);

    cp_cco_action_manage_sta_assoc (&cp, &mme_rx);

    test_case_begin (test,
                     "Test 3: Receiving a CC_ASSOC_REQ join from a associated station");

    cp.cco_action.tei_flags[0] = 0xF0FFFFFF;
    cp.cco_action.tei_flags[1] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[2] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[3] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[4] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[5] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[6] = 0xFFFFFFFF;
    cp.cco_action.tei_flags[7] = 0xFFFFFFFF;


    table = cl_mactotei_new ();
    cl_mactotei_addr_add (table, 0x2, 0x2, 1);
    cl_mactotei_use_table (cp.cl, table);

    // Construct the MME to associate the first station.
    mme_rx.peer.mac = 2;
    mme_rx.peer.vlan_tag = 0;
    mme_rx.peer.tei = 0;

    req_type = CP_MSG_CC_ASSOC_REQ_TYPE_NEW;
    // Write the request.
    bitstream_init (&bitstream, buffer + 19, 10, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &req_type, 8);
    bitstream_access (&bitstream, &nid, 56);
    bitstream_access (&bitstream, &cco_cap, 8);
    bitstream_access (&bitstream, &pco_cap, 8);
    bitstream_finalise (&bitstream);

    // Initialise the MME Rx object.
    mme_rx.p_mme = buffer;
    mme_rx.length = 60;
    bitstream_init (&mme_rx.bitstream, buffer + 19, 60, BITSTREAM_READ);

    cp_cco_action_manage_sta_assoc (&cp, &mme_rx);

    cp_cco_action_uninit (&cp);
    cp_sta_mgr_uninit (&cp);
    blk_release_desc ((blk_t *) table);
    blk_release (cp.cl);
}

/** Sta authentication procedure
 * \param  test  the test object.
 *
 * this shall only answer with a CM_GET_KEY.CNF message if the station is known
 *
 * Test 1: Station is not known.
 * Environment
 *  A MME is received with a authentication request and the Station is not
 *  known.
 * Result
 *  A MME with the CM_GET_KEY.CNF containing the result "request refused"
 *  shall be sent.
 *
 * Test 2: Station is known.
 *
 * Environment
 *  Some station have been added and are associated. A CM_GET_KEY.REQ MME
 *  is received from a station.
 * Result
 *  The CCo shall send a answer CM_GET_KEY.CNF with the following data.
 *
 *  - Result = 0 (guaranteed)
 *  - key = 2 (NEK)
 *  - NID = 0
 */
void
test_case_sta_authentication (test_t test)
{
    cp_t cp;
    cp_net_t *net;
    cp_sta_t *sta;
    mfs_rx_t mfs;
    bitstream_t bitstream;
    cp_mme_rx_t mme;
    u64 data;
    u8 buffer[1024]__attribute__((aligned(1024)));

    cp_cco_action_init (&cp);
    cp_sta_mgr_init (&cp);

    // Update the net NID and SNID.
    cp_sta_mgr_update_our_avln_nid (&cp, 0x1);
    cp_sta_mgr_update_our_avln_snid (&cp, 0x2);

    net = cp_sta_mgr_get_our_avln (&cp);

    test_case_begin (test, "Sta authentication procedure");

    // Build the MME.
    bitstream_init (&bitstream, buffer, sizeof(buffer), BITSTREAM_WRITE);
    // ODA.
    data = 0x2;
    bitstream_access (&bitstream, &data, 48);
    // OSA.
    data = 0x1;
    bitstream_access (&bitstream, &data, 48);
    // Mtype
    data = swap16 (HPAV_MTYPE_MME);
    bitstream_access (&bitstream, &data, 16);
    // MMV.
    data = HPAV_MMV1;
    bitstream_access (&bitstream, &data, 8);
    // MMtype.
    data = CM_GET_KEY_REQ;
    bitstream_access (&bitstream, &data, 16);
    // FMI
    data = 0;
    bitstream_access (&bitstream, &data, 16);
    // Request type.
    data = 0;
    bitstream_access (&bitstream, &data, 8);
    // Request key type.
    data = CP_MSG_KEY_NEK;
    bitstream_access (&bitstream, &data, 8);
    // NID.
    data = cp_net_get_nid (&cp, net);
    bitstream_access (&bitstream, &data, 56);
    // Nonce.
    data = 0xc;
    bitstream_access (&bitstream, &data, 32);
    // PID.
    data = 0;
    bitstream_access (&bitstream, &data, 32);
    // Key.
    data = 0xA;
    bitstream_access (&bitstream, &data, 8);
    bitstream_finalise (&bitstream);

    mme.length = 37;
    mme.p_mme = buffer;
    mme.peer.tei = 0xFF;

    cp_cco_action_manage_sta_auth (&cp, &mme);

    mfs.common.tei = 0x1;
    sta = cp_net_sta_add (&cp, net, mfs.common.tei, 0x1);
    cp_sta_set_state (sta, CP_STA_STATE_AUTHENTICATED);
    slab_release (sta);
    cp_cco_action_manage_sta_auth (&cp, &mme);

    cp_sta_mgr_uninit (&cp);
    cp_cco_action_uninit (&cp);
}

/** Test the station leaving procedure.
 * \param  test  the test object.
 *
 * This shall only verify that a message is post in the FSM when the station
 * leave the AVLN.
 */
void
test_case_sta_leave (test_t test)
{
    cp_t cp;
    cp_net_t *net;
    cp_sta_t *sta;

    cp_sta_mgr_init (&cp);
    cp_cco_action_init (&cp);

    test_case_begin (test, "CCO action : Station leave");

    net = cp_sta_mgr_get_our_avln (&cp);
    sta = cp_net_sta_add (&cp, net, 0x1, 0x1);

    cp_cco_action_manage_sta_leave (&cp, NULL, sta);
    slab_release (sta);

    test_begin (test, "Verify the post event")
    {
        test_fail_if (fsm_event_posted != true, "FSM Event not posted");
    }
    test_end;

    cp_cco_action_uninit (&cp);
    cp_sta_mgr_uninit (&cp);
}

int
main (void)
{
    uint phy;
    mac_config_t mac_config;

    test_init (test, 0, NULL);
    mac_ntb_init ((phy_t *) &phy, &mac_config);

    test_case_tei_lease_create (test);
    test_case_tei_release (test);
    test_case_cco_action_init (test);
    test_case_change_snid (test);
    test_case_sta_assoc_procedure (test);
    test_case_sta_authentication (test);
    test_case_sta_leave (test);

    test_case_begin (test, "Memory leaks");

    test_begin (test, "Memory leaks verification")
    {
        test_fail_if (blk_check_memory() == false, "Memory not freed");
    }
    test_end;

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

void
cp_beacon_change_snid (cp_t *ctx, u8 snid)
{
    snid_compute = snid;
}

u32
phy_date (phy_t *phy)
{
    return 42;
}

/**
 * Begin a CC_SET_TEI_MAP.IND construction.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  mode  update, add or delete
 * \param  sta_nb  number of stations sent
 * \return  the MME being constructed
 */
cp_mme_tx_t *
cp_msg_cc_set_tei_map_ind_send_begin (
    cp_t *ctx, cp_mme_peer_t *peer,
    enum cp_msg_cc_set_tei_map_ind_mode_t mode, uint sta_nb)
{
    return NULL;
}

/**
 *
 * Write a station to a CC_SET_TEI_MAP.IND being constructed.
 * \param  ctx  control plane context
 * \param  mme  MME being constructed
 * \param  tei  STA TEI
 * \param  mac  STA mac address
 * \param  status  STA status
 */
void
cp_msg_cc_set_tei_map_ind_send_sta (
    cp_t *ctx, cp_mme_tx_t *mme, cp_tei_t tei, mac_t mac,
    enum cp_msg_cc_set_tei_map_ind_status_t status)
{
}

/**
 * Finalise and send a CC_SET_TEI_MAP.IND message.
 * \param  ctx  control plane context
 * \param  mme  MME being constructed
 */
void
cp_msg_cc_set_tei_map_ind_send_end (cp_t *ctx, cp_mme_tx_t *mme)
{
}

/**
 * Receive a CC_ASSOC.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  data  received MME data
 * \return  true on success
 */
bool
cp_msg_cc_assoc_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                             cp_msg_cc_assoc_req_t *data)
{
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (data);

    bitstream_access (&mme->bitstream, &data->request_type, 8);
    bitstream_access (&mme->bitstream, &data->nid, 56);
    bitstream_access (&mme->bitstream, &data->cco_cap, 8);
    bitstream_access (&mme->bitstream, &data->proxy_cap, 8);
    bitstream_finalise (&mme->bitstream);

    return true;
}

/**
 * Send a CC_ASSOC.CNF.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  data  MME data to send
 */
void
cp_msg_cc_assoc_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
                          const cp_msg_cc_assoc_cnf_t *data)
{
    cp_sta_t *sta;
    cp_net_t *net;

    dbg_assert (ctx);
    dbg_assert (data);

    net = cp_sta_mgr_get_our_avln (ctx);

    if (data->sta_tei)
        sta = cp_net_get_sta (ctx, net, data->sta_tei);
    else
        sta = cp_net_get_sta_unassoc (ctx, net, peer->mac);

    test_begin (test, "Verifying CNF Data")
    {
        test_fail_if (sta == NULL, "Station shall exists");

        switch (cp_sta_get_state (sta))
        {
        case CP_STA_STATE_ASSOCIATED:
            test_fail_if (data->result != CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS,
                          "Wrong result value");
            test_fail_if (data->lease_time_min != CP_CCO_ACTION_LEASE_ASSOC_MIN,
                          "Wrong TEI lease time");
            break;
        case CP_STA_STATE_AUTHENTICATED:
            test_fail_if (data->result != CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS,
                          "Wrong result value");
            test_fail_if (data->lease_time_min != CP_CCO_ACTION_LEASE_AUTH_MIN,
                          "Wrong TEI lease time");
            break;
        default:
            test_fail_if (data->result !=
            CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_TEMPORARY_RESSOURCE_EXHAUSTION,
                "Wrong result value");
        }

        test_fail_if (data->sta_tei != cp_sta_get_tei (sta),
                      "Wrong TEI");
        test_fail_if (data->nid != cp_net_get_nid (ctx, net),
                      "Wrong NID");
        test_fail_if (data->snid != cp_net_get_snid (ctx, net),
                      "Wrong SNID");
    }
    test_end;

    slab_release(sta);
}

/**
 * Receive a CM_GET_KEY.REQ.
 * \param  ctx  control plane context
 * \param  mme  MME handle
 * \param  data  received MME data
 * \return  true on success
 *
 * Encryption and protocol run information is available in the MME handle.
 */
bool
cp_msg_cm_get_key_req_receive (cp_t *ctx, cp_mme_rx_t *mme,
                               cp_msg_cm_get_key_req_t *data)
{
    uint offset;
    bool vlantag_present;
    u64 unused;
    dbg_assert (ctx);
    dbg_assert (mme);
    dbg_assert (mme->p_mme);
    dbg_assert (mme->length < ETH_PACKET_MAX_SIZE);
    dbg_assert (data);

    if (read_u16_from_word (mme->p_mme + 12) == swap16(HPAV_MTYPE_MME))
        vlantag_present = false;
    else
        vlantag_present = true;

    // Read the MME.
    bitstream_init (&mme->bitstream, mme->p_mme, mme->length, BITSTREAM_READ);
    bitstream_access (&mme->bitstream, &mme->peer.mac, 48);
    bitstream_access (&mme->bitstream, &unused, 48);
    if (vlantag_present)
        bitstream_access (&mme->bitstream, &mme->peer.vlan_tag, 32);
    bitstream_access (&mme->bitstream, &unused, 16);
    bitstream_access (&mme->bitstream, &unused, 8);
    bitstream_access (&mme->bitstream, &unused, 16);
    bitstream_access (&mme->bitstream, &unused, 16);

    // Read the data in the MME.
    bitstream_access (&mme->bitstream, &data->relayed, 8);
    bitstream_access (&mme->bitstream, &data->key_type, 8);
    bitstream_access (&mme->bitstream, &data->nid, 56);

    if (vlantag_present)
        offset = 41;
    else
        offset = 37;

    memcpy (mme->p_mme + offset, &data->hash_key, mme->length - offset);
    return true;
}

/**
 * Send a CM_GET_KEY.CNF.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  peks  payload encryption key select
 * \param  prun  protocol run information
 * \param  data  MME data to send
 */
void
cp_msg_cm_get_key_cnf_send (cp_t *ctx, cp_mme_peer_t *peer,
                            cp_mme_peks_t peks,
                            const cp_secu_protocol_run_t *prun,
                            const cp_msg_cm_get_key_cnf_t *data)
{
    dbg_assert (ctx);
    dbg_assert (data);

    test_begin (test, "Verify data")
    {
        test_fail_if (data->result != (peer->tei == 0xFF ?
                            CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED :
                            CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED),
                      "Wrong result type");
    }
    test_end;
}

/**
 * Send a CC_LEAVE.CNF.
 * \param  ctx  control plane context
 * \param  peer  peer information
 */
void
cp_msg_cc_leave_cnf_send (cp_t *ctx, cp_mme_peer_t *peer)
{
}

/**
 * Send a CC_LEAVE.IND.
 * \param  ctx  control plane context
 * \param  peer  peer information
 * \param  reason  reason for the disassociation
 * \param  nid  NID
 */
void
cp_msg_cc_leave_ind_send (cp_t *ctx, cp_mme_peer_t *peer,
                          enum cp_msg_cc_leave_ind_reason_t reason,
                          cp_nid_t nid)
{
}

cp_fsm_event_t *
cp_fsm_event_bare_new (cp_t *ctx, cp_fsm_event_type_t type)
{
    return &fsm_event;
}

void
cp_fsm_post (cp_t *ctx, cp_fsm_event_t *event)
{
    fsm_event_posted = true;
}