summaryrefslogtreecommitdiff
path: root/cesar/cl/test/utest/src/misc.c
blob: 401d83444512a0f664a1cbec821dbdc232756e74 (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
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cl/test/src/misc.c
 * \brief   CL Miscellaneous tests.
 * \ingroup cl
 */
#include "common/std.h"
#include "lib/test.h"
#include "cl/test/utest/test.h"
#include "cl/cl_mactotei.h"
#include <stdio.h>
#include "cl/inc/cl.h"

uint
cl_classifer_get_lid (cl_t *ctx, uint tei, uint tag,
                      bool *bcast, bool *acs,
                      bool *drop);

void
cl_test_case__classifier (test_t test)
{
    test_case_begin (test, "Testing the Classifier");
    test_begin (test, "Priority not broadcast")
    {
        cl_test_t t;
        uint lid, i;
        bool bcast = false, acs, drop;
        uint tag [] = {0, 1, 2, 3, 4, 5, 6, 7};
        uint lid_expected [] = {1, 0, 0, 1, 2, 2, 3, 3};
        cl_test_init (&t, 0x4354);
        for (i = 0; i < COUNT (tag); i++)
        {
            lid = cl_classifer_get_lid (t.cl, 10, tag[i],
                                        &bcast, &acs, &drop);
            test_fail_unless (bcast == false);
            test_fail_unless (acs == false);
            test_fail_unless (drop == false);
            test_fail_unless (lid == lid_expected [i]);
        }
        lid = cl_classifer_get_lid (t.cl, MAC_TEI_BCAST, 0, &bcast, &acs,
                                    &drop);
        test_fail_unless (bcast == true);
        test_fail_unless (acs == false);
        test_fail_unless (drop == false);
        test_fail_unless (lid == lid_expected[0]);
        cl_test_uninit (&t);
    }
    test_end;
}

/**
 * Test the public API of the MAC to TEI table.
 * \param  test  Test structure.
 * \param  cl  CL structure.
 *
 * This test is a basic usage of the MAC to TEI table.
 */
void
cl_test_case__mactotei_test_api_copy_tag_and_tei (test_t test)
{
    test_case_begin (test, "MAC TO TEI Public API");
    /* Number of elements to add to the MAC to TEI table. */
    uint const max_elements = 250;
    /* Title of the test. */
    char test_title[100];
    snprintf (test_title, 100,
             "Check MAC to TEI with copy tag & TEI for %d elements",
             max_elements);
    test_begin (test, test_title)
    {
        cl_test_t t;
        cl_test_init (&t, 0x9540);
        /* Allocate a new MAC to TEI table. */
        cl_mactotei_blk_t *table = cl_mactotei_new ();
        /* Copy all the old entries from the previous MAC to TEI table. There
         * should be no entry to copy for the moment because previous table is
         * empty. */
        cl_mactotei_copy_tei_and_tag (t.cl, table, 0, 0);
        /* Add MAC addresses with TEI and tag. */
        uint count;
        for (count = 1; count < max_elements; count++)
            cl_mactotei_addr_add (table, count * count, count, count % 3);
        /* Cancel the creation of this table. */
        cl_mactotei_cancel (table);
        /* Create a new MAC to TEI table. */
        table = cl_mactotei_new ();
        /* Add entries. */
        for (count = 1; count < max_elements; count++)
            cl_mactotei_addr_add (table, count * count, count, count % 3);
        /* Convert the whole table. */
        cl_mactotei_use_table (t.cl, table);
        /* Allocate a new MAC to TEI table. */
        table = cl_mactotei_new ();
        /* Copy one entry which exists, one that does not. */
        cl_mactotei_copy_tei_and_tag (t.cl, table, max_elements,
                                      max_elements % 3);
        cl_mactotei_copy_tei_and_tag (t.cl, table, 1, 1);
        /* Index table to use it. */
        cl_mactotei_use_table (t.cl, table);
        /* Try to find the two entries. */
        /* Entry should exist. */
        uint tei = cl_mactotei_table_find_tei_from_mac (t.cl, 1);
        test_fail_if (tei == 0, "MAC %d not found", 1);
        test_fail_if (tei != 1, "TEI %d does not correspond to MAC %d",
                      1, 1);
        /* try with cl_mactotei_table_find_tei_and_tag_from_mac */
        uint tag;
        tei = cl_mactotei_table_find_tei_and_tag_from_mac (t.cl, 1, &tag);
        test_fail_if (tei == 0, "MAC %d not found", 1);
        test_fail_if (tei != 1, "TEI %d does not correspond to MAC %d",
                      1, 1);
        test_fail_if (tag != 1, "tag %d does not correspond to MAC %d",
                      1, 1);
        /* Entry should not exist. */
        test_fail_if (cl_mactotei_table_find_tei_from_mac (t.cl, max_elements),
                      "Entry should not exist.");
        /* Clean. */
        cl_mactotei_release_table (t.cl);
        /* Refill the table. */
        table = cl_mactotei_new ();
        /* Add MAC addresses with TEI and tag. */
        for (count = 1; count < max_elements; count++)
            cl_mactotei_addr_add (table, count * count, count, count % 3);
        /* Convert the whole table. */
        cl_mactotei_use_table (t.cl, table);
        /* Allocate a new MAC to TEI table. */
        table = cl_mactotei_new ();
        /* Copy all the entries expect the one with a tag set to 0. */
        cl_mactotei_copy_except_tag (t.cl, table, 0);
        /* Convert the whole table. */
        cl_mactotei_use_table (t.cl, table);
        /* Check copied entries are the right one. */
        for (count = 1; count < max_elements; count++)
        {
            /* Get entry. */
            uint tei = cl_mactotei_table_find_tei_from_mac (t.cl,
                                                            count * count);
            /* Should have been copied. */
            if ((count % 3) != 0)
                test_fail_unless (tei == count);
            /* Should not have been copied. */
            else
                test_fail_unless (tei == 0x0);
        }
        /* Clean. */
        cl_mactotei_release_table (t.cl);
        cl_test_uninit (&t);
    }
    test_end;
}

/**
 * Test the function cl_update_igmp_groups.
 * \param  test  Test structure.
 */
void
cl_test_case__cl_update_igmp_groups (test_t test)
{
    uint i, j;
    cl_mactotei_blk_t *table = cl_mactotei_new ();
    cl_test_t t;
    cl_test_init (&t, 0x9540);

    /** Create the mactotei table with entries for members and groups. */
    for (i = 0; i < 4 + MCAST_MEMBER_MAX_NB; i++)
    {
        cl_mactotei_addr_add (
        table, MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, i),
        i + 1, i +1);
    }

    /** Create the igmp groups. */
    t.cl->groups.nb = 3;

    t.cl->groups.group_mac[0] = MAC_ADDRESS (0x00, 0x13, 0xd7, 0x00, 0, 0x01);
    t.cl->groups.nb_total_members[0] = 2;
    t.cl->groups.member_mac[0][0] = MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 0);
    t.cl->groups.member_mac[0][1] = MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 1);

    t.cl->groups.group_mac[1] = MAC_ADDRESS (0x00, 0x13, 0xd7, 0x00, 1, 0x01);
    t.cl->groups.nb_total_members[1] = 2;
    t.cl->groups.member_mac[1][0] = MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 2);
    t.cl->groups.member_mac[1][1] = MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 3);

    t.cl->groups.group_mac[2] = MAC_ADDRESS (0x00, 0x13, 0xd7, 0x00, 2, 0x01);
    t.cl->groups.nb_total_members[2] = MCAST_MEMBER_MAX_NB;
    for (i = 4; i < 4 + MCAST_MEMBER_MAX_NB; i++)
    {
        t.cl->groups.member_mac[2][i - 4] =
            MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, i);
    }

    for (j = 0; j < t.cl->groups.nb; j++)
    {
        cl_mactotei_addr_add (
            table, MAC_ADDRESS (0x00, 0x13, 0xd7, 0x00, j, 0x01),
            MAC_TEI_BCAST, j);
    }
    cl_mactotei_use_table (t.cl, table);

    test_case_begin (test, "cl_update_igmp_groups");
    /** Create a valid tei list for each group based on the mactotei and
     * the list of mac addresses of members of each group. */
    test_begin (test, "create valid tei list")
    {
        cl_update_igmp_groups (t.cl);
        uint tei = 1;

        for (j = 0; j < t.cl->groups.nb; j++)
        {
            test_fail_if (t.cl->groups.nb_actual_members[j] !=
                    t.cl->groups.nb_total_members[j]);
            for (i = 0; i < t.cl->groups.nb_actual_members[j]; i++)
            {
                test_fail_if (t.cl->groups.member_tei[j][i] != tei++);
            }
        }
    }
    test_end;
    cl_mactotei_release_table (t.cl);

    /** Update the tei list when the mactotei list is modified:
     * we remove all sta excepted one for group 0, one for group 1, group 2 has
     * no member left available. */
    table = cl_mactotei_new ();
    cl_mactotei_addr_add (table, MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 1),
            2, 2);
    cl_mactotei_addr_add (table, MAC_ADDRESS(0x12, 0x34, 0x56, 0x78, 0x9A, 2),
            3, 3);
    cl_mactotei_use_table (t.cl, table);

    test_begin (test, "update tei list")
    {
        cl_update_igmp_groups (t.cl);

        test_fail_if (t.cl->groups.nb_actual_members[0] != 1);
        test_fail_if (t.cl->groups.member_tei[0][0] != 2);

        test_fail_if (t.cl->groups.nb_actual_members[1] != 1);
        test_fail_if (t.cl->groups.member_tei[1][0] != 3);

        test_fail_if (t.cl->groups.nb_actual_members[2] != 0);
        test_fail_if (t.cl->groups.member_tei[2][0] != MAC_TEI_BCAST);
    }
    test_end;
    cl_mactotei_release_table (t.cl);

    /** Test with full groups */
    table = cl_mactotei_new ();
    uint tei = 1;
    t.cl->groups.nb = MCAST_GROUP_MAX_NB;
    for (j = 0; j < MCAST_GROUP_MAX_NB; j++)
    {
        t.cl->groups.group_mac[j] =
                MAC_ADDRESS (0x00, 0x13, 0xd7, 0x00, j, 0x01);
        t.cl->groups.nb_total_members[j] = MCAST_MEMBER_MAX_NB;

        for (i = 0; i < t.cl->groups.nb_total_members[j]; i++)
        {
            t.cl->groups.member_mac[j][i] =
                MAC_ADDRESS (0x12, 0x34, 0x56, 0x78, 0x9A, tei - 1);
            cl_mactotei_addr_add (table, t.cl->groups.member_mac[j][i],
                                  tei, tei);
            tei++;
        }
        cl_mactotei_addr_add (table, t.cl->groups.group_mac[j], MAC_TEI_BCAST, j);
    }

    cl_mactotei_use_table (t.cl, table);

    test_begin (test, "full groups")
    {
        cl_update_igmp_groups (t.cl);

        uint tei = 1;
        for (j = 0; j < t.cl->groups.nb; j++)
        {
            test_fail_if (t.cl->groups.nb_actual_members[j] !=
                          t.cl->groups.nb_total_members[j]);
            for (i = 0; i < t.cl->groups.nb_actual_members[j]; i++)
            {
                test_fail_if (t.cl->groups.member_tei[j][i] != tei++);
            }
        }
    }
    test_end;

    cl_mactotei_release_table (t.cl);
    cl_test_uninit (&t);
}

void
cl_test_suite_misc (test_t test)
{
    test_suite_begin (test, "CL miscellaneous");
    cl_test_case__classifier (test);
    cl_test_case__mactotei_test_api_copy_tag_and_tei (test);
    cl_test_case__cl_update_igmp_groups (test);
}