summaryrefslogtreecommitdiff
path: root/cesar/mac/sar/test/utest/host/src/expiration.c
blob: 7fd452e5e958a059bdb9015ecdadd015d7bcc378 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    reassembly_mfs_update_expiration_ntb.c
 * \brief   test to verify the mfs update expiration date
 * \ingroup mac/sar/test/unit_test/reassembly_mfs_update_expiration_ntb/src
 *
 */
#include "common/std.h"
#include "lib/test.h"
#include "lib/trace.h"
#include "lib/stats.h"
#include "mac/common/ntb.h"
#include "mac/common/timings.h"
#include "mac/sar/test/utest/host/tests.h"
#include <string.h>

/* Override context. */
#include "mac/pbproc/inc/context.h"

void
test_case_expiration__add (sar_test_t *t, u8 tei, u8 lid, bool tx,
                           u32 expiration_ntb)
{
    bool added;
    mfs_t *mfs =
        mac_store_mfs_add (t->mac_store, tx, false, false, lid, tei, &added);
    dbg_assert (added);
    sar_mfs_add (t->sar, mfs);
    mfs->common.expiration_ntb = expiration_ntb;
    pb_t *pb = (pb_t*) blk_alloc_desc ();
    pb->next = NULL;
    pb->expiration_ntb = expiration_ntb / 2;
    if (tx)
    {
        mfs->tx.head = mfs->tx.tail = pb;
        mfs->tx.seg_nb = 1;
    }
    else
        mfs->rx.head = pb;
    blk_release (mfs);
}

void
test_case_expiration (test_t test)
{
    uint phy_date = 0;
    sar_test_t t;
    uint pbproc = 0;
    test_case_begin (test, "Expiration");
    sar_test_init (&t, &phy_date, &pbproc);
    u32 expiration_ntb[] = { 10, 20, 30, 40 };
    test_begin (test, "MFS expiration")
    {
        uint i,j;
        mfs_t *mfs;
        for (i = 1; i <= COUNT (expiration_ntb); i++)
        {
            test_case_expiration__add (&t, i, 1, true,
                                       expiration_ntb[i-1]);
            test_case_expiration__add (&t, i, 1, false,
                                       expiration_ntb[i-1]);
        }
        for (phy_date = 0;
             phy_date < expiration_ntb[COUNT(expiration_ntb) - 1];
             phy_date += 5)
        {
            sar_expiration_mfs (t.sar);
            for (j = 0; j < COUNT (expiration_ntb); j++)
            {
                mfs = mac_store_mfs_get (t.mac_store, true, false, false, 1,
                                         j + 1);
                test_fail_unless (mfs != NULL);
                if (phy_date < expiration_ntb [j])
                {
                    test_fail_unless (mfs->tx.fsm_state == MFS_FSM_CMD_INIT);
                    if (phy_date < expiration_ntb [j] / 2)
                        test_fail_unless (mfs->tx.head != NULL);
                    else
                        test_fail_unless (mfs->tx.head == NULL);
                }
                else
                {
                    test_fail_unless (
                        mfs->tx.fsm_state == MFS_FSM_CMD_RELEASE);
                    test_fail_unless (mfs->tx.head == NULL);
                }
                blk_release (mfs);
                mfs = mac_store_mfs_get (t.mac_store, false, false, false, 1,
                                         j + 1);
                if (phy_date < expiration_ntb [j])
                {
                    test_fail_unless (mfs->rx.release == false);
                    if (phy_date < expiration_ntb [j] / 2)
                        test_fail_unless (mfs->rx.head != NULL);
                    else
                        test_fail_unless (mfs->rx.head == NULL);
                }
                else
                {
                    test_fail_unless (mfs->rx.release == true);
                    test_fail_unless (mfs->rx.head == NULL);
                }
                blk_release (mfs);
            }
        }
    }
    test_end;
    sar_cleanup (t.sar);
    sar_test_uninit (&t);
}

mfs_t*
test_case_expiration_bcast__common (sar_test_t *ctx, test_t t, bool tx)
{
    test_within (t);
    bool added;
    mfs_t *mfs = mac_store_mfs_add (ctx->mac_store, tx, true, false, 1,
                                    tx ? MAC_TEI_BCAST : 1, &added);
    test_fail_unless (added);
    pb_t *pb = (pb_t*) blk_alloc_desc ();
    pb->next = NULL;
    pb->expiration_ntb =
        mac_ntb () - MAC_MS_TO_TCK (MFS_PB_TX_DELAY_MS) - 10;
    if (tx)
    {
        mfs->tx.head = mfs->tx.tail = pb;
        mfs->tx.seg_nb = 1;
    }
    else
        mfs->rx.head = pb;
    /* Set expiration date. */
    mfs->common.expiration_ntb = mac_ntb ()
        - MAC_MS_TO_TCK (MFS_RELEASE_DELAY_MS) - 10;
    sar_expiration_mfs (ctx->sar);
    return mfs;
}

void
test_case_expiration_bcast (test_t test)
{
    test_case_begin (test, "Broadcast MFS expiration");
    sar_test_t ctx;
    pbproc_t pbproc;
    memset (&pbproc, 0, sizeof (pbproc_t));
    sar_test_init (&ctx, INVALID_PTR, &pbproc);
    test_begin (test, "TX")
    {
        mfs_t *mfs = test_case_expiration_bcast__common (&ctx, test, true);
        /* TX bcast MFS should not be released. */
        test_fail_unless (mfs->tx.fsm_state != MFS_FSM_CMD_RELEASE);
        test_fail_unless (!mfs->tx.head);
        blk_release (mfs);
    }
    test_end;
    test_begin (test, "RX")
    {
        mfs_t *mfs = test_case_expiration_bcast__common (&ctx, test, false);
        /* RX bcast MFS should be released. */
        test_fail_unless (mfs->rx.release);
        blk_release (mfs);
    }
    test_end;
    sar_cleanup (ctx.sar);
    sar_test_uninit (&ctx);
}

void
test_suite_expiration (test_t test)
{
    test_suite_begin (test, "MFS expiration");
    test_case_expiration (test);
    test_case_expiration_bcast (test);
    test_begin (test, "Memory")
    {
        test_fail_if (blk_check_memory() == false, "Memory not freed");
    }
    test_end;
}