summaryrefslogtreecommitdiff
path: root/cesar/cp/eoc/multi_sta_fsm/test/utest/src/test_fsm.c
blob: a98a1eb268c8f45a6defc358a79ec7cdccd7b4a0 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_fsm.c
 * \brief   Test Multi STA FSM module.
 * \ingroup test
 */
#include "common/std.h"

#include "lib/blk.h"
#include "lib/test.h"
#include "lib/scenario/scenario.h"
#include "cp/sta/mgr/inc/sta.h"
#include "cp/sta/mgr/sta_mgr.h"

#include "config/trace.h"
#include "cp/eoc/multi_sta_fsm/fsm.h"
#include "cp/inc/context.h"


extern int cp_fsm__STATE1__enter_nb;

void
test_fsm_basic_test_case (test_t t)
{
    test_case_begin (t, "basic");
    cp_t ctx;
    cp_trace_init (&ctx);
    cp_net_t *net;
    cp_tei_t tei = 5;
    uint cl;

    sar_t sar;
    ctx.sar = &sar;
    cp_mme_rx_t my_mme;
    cp_mme_rx_t *mme = &my_mme;
    cp_sta_t *sta;
    mac_config_t mac_config;

    ctx.mac_config = &mac_config;
    ctx.cl = (cl_t *) &cl;
    ctx.mac_store = mac_store_init ();
    lib_rnd_init (&ctx.rnd, 1234);

    cp_eoc_sta_mgr_init (&ctx);

    net = cp_sta_mgr_add_avln (&ctx, 1, 1);    // snid = 1, nid = 1
    cp_sta_mgr_set_our_avln (&ctx, net);
    mme->peer.mac = 2;
    mme->peer.tei = 0;
    sta = cp_sta_mgr_sta_add (&ctx, net, tei, mme->peer.mac);

    test_begin (t, "regular")
    {
        cp_fsm__STATE1__enter_nb = 0;
        cp_eoc_multi_sta_fsm_init (&ctx, sta);
        scenario_entry_t entries[] = {
            /* STATE1 */
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event1),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE1__event1,
                            .sta = sta,
                            .mme = mme),
            /* STATE2 */
            SCENARIO_ACTION (process,
                 .sta = sta,
                 .mme = mme,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event2),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE2__event2,
                            .mme = mme,
                            .sta = sta,
                            .branch =
                CP_EOC_MULTI_STA_FSM_BRANCH(STATE2, event2, branch1)),
            /* STATE3*/
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event3),
            /*no SCENARIO_EVENT */
            /* STATE4 */
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event4),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE4__event4,
                            .mme = mme,
                            .sta = sta,
                            .branch =
                CP_EOC_MULTI_STA_FSM_BRANCH(STATE4, event4, branch4)),
            /* STATE4 */
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event4),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE4__event4,
                            .mme = mme,
                            .sta = sta,
                            .branch =
                CP_EOC_MULTI_STA_FSM_BRANCH(STATE4, event4, branch1)),
            /* STATE5*/
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event5),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE5__event5,
                            .mme = mme,
                            .sta=sta),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE6__enter,
                            .mme = mme,
                            .sta=sta),
            /* STATE6 */
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event6),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE6__event6,
                            .mme = mme,
                            .sta=sta),
            /* STATE6 */
            SCENARIO_ACTION (process,
                 .mme = mme,
                 .sta = sta,
                 .type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_event1),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE6__event1,
                            .mme = mme,
                            .sta=sta),
            SCENARIO_EVENT (cp_eoc_multi_sta_fsm__STATE6__leave,
                            .mme = mme,
                            .sta=sta),
            /* STATE1 */
            SCENARIO_END
        };
        scenario_globals_t globals = {
            .cp = &ctx,
        };
        scenario_run (t, entries, &globals);
        test_fail_unless (sta->fsm.state ==
            CP_EOC_MULTI_STA_FSM_STATE_STATE1);
        test_fail_unless (cp_fsm__STATE1__enter_nb == 2);
        cp_eoc_multi_sta_fsm_uninit (&ctx, sta);

    } test_end;
    cp_sta_mgr_uninit (&ctx);
    slab_release (sta);
#if CONFIG_TRACE
    trace_buffer_dbg_dump (&cp.trace);
#endif /* CONFIG_TRACE */
    cp_trace_uninit (&cp);
}

void
test_fsm_error_test_case (test_t t)
{
    test_case_begin (t, "error");
    cp_t cp;
    cp_trace_init (&cp);
    cp_net_t *net;
    cp_tei_t tei = 5;
    uint cl;
    sar_t sar;
    cp.sar = &sar;

    cp_sta_t *sta;
    cp_mme_rx_t my_mme;
    cp_mme_rx_t *mme = &my_mme;
    cp.cl = (cl_t *) &cl;
    cp.mac_store = mac_store_init ();
    mac_config_t mac_config;
    mac_config_init (&mac_config);
    cp.mac_config = &mac_config;
    lib_rnd_init (&cp.rnd, 1234);

    cp_eoc_sta_mgr_init (&cp);

    net = cp_sta_mgr_add_avln (&cp, 1, 1);
    cp_sta_mgr_set_our_avln (&cp, net);
    mme->peer.mac = 2;
    mme->peer.tei = 0;
    sta = cp_sta_mgr_sta_add (&cp, net, tei, mme->peer.mac);

    test_begin (t, "no branch")
    {
        cp_eoc_multi_sta_fsm_init (&cp, sta);

        cp_eoc_multi_sta_fsm_event_mme_t e;
        cp_eoc_multi_sta_fsm_event_type_t type;
        type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_error_event_no_branch;
        e.event.type = type;
        e.event.handler = cp_eoc_multi_sta_fsm_event_handler;
        e.mme = mme;


        const char *asserted = NULL;
        dbg_fatal_try_begin
        {
            cp_eoc_multi_sta_fsm_handled_event (&cp, sta, &(e.event));
        }
        dbg_fatal_try_catch (const char *msg)
        {
            asserted = msg;
        }
        dbg_fatal_try_end;
        test_fail_unless (asserted);
        cp_eoc_multi_sta_fsm_uninit (&cp, sta);
    } test_end;
    test_begin (t, "bad branch")
    {
        cp_eoc_multi_sta_fsm_init (&cp, sta);

        cp_eoc_multi_sta_fsm_event_mme_t e;
        cp_eoc_multi_sta_fsm_event_type_t type;
        type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_error_event_bad_branch;
        e.event.type = type;
        e.event.handler = cp_eoc_multi_sta_fsm_event_handler;
        e.mme = mme;

        const char *asserted = NULL;
        dbg_fatal_try_begin
        {
            cp_eoc_multi_sta_fsm_handled_event (&cp, sta, &(e.event));
        }
        dbg_fatal_try_catch (const char *msg)
        {
            asserted = msg;
        }
        dbg_fatal_try_end;
        test_fail_unless (asserted);
        cp_eoc_multi_sta_fsm_uninit (&cp, sta);
    } test_end;
    test_begin (t, "dup branch")
    {
        cp_eoc_multi_sta_fsm_init (&cp, sta);

        cp_eoc_multi_sta_fsm_event_mme_t e;
        cp_eoc_multi_sta_fsm_event_type_t type;
        type = CP_EOC_MULTI_STA_FSM_EVENT_TYPE_error_event_dup_branch;
        e.event.type = type;
        e.event.handler = cp_eoc_multi_sta_fsm_event_handler;
        e.mme = mme;

        const char *asserted = NULL;
        dbg_fatal_try_begin
        {
            cp_eoc_multi_sta_fsm_handled_event (&cp, sta, &(e.event));
        }
        dbg_fatal_try_catch (const char *msg)
        {
            asserted = msg;
        }
        dbg_fatal_try_end;
        test_fail_unless (asserted);
        cp_eoc_multi_sta_fsm_uninit (&cp, sta);
    } test_end;

    cp_sta_mgr_uninit (&cp);
    slab_release (sta);

#if CONFIG_TRACE
    trace_buffer_dbg_dump (&cp.trace);
#endif // CONFIG_TRACE
    cp_trace_uninit (&cp);
}




void
test_fsm_test_suite (test_t t)
{
    test_suite_begin (t, "fsm");
    test_fsm_basic_test_case (t);
    test_fsm_error_test_case (t);
}

int
main (int argc, char **argv)
{
    test_t t;
    lib_stats_init();
    trace_init ();
    test_init (t, argc, argv);
    test_fsm_test_suite (t);
    trace_uninit ();
    lib_stats_uninit();
    test_result (t);
    return test_nb_failed (t) == 0 ? 0 : 1;
}