summaryrefslogtreecommitdiff
path: root/src/irmd/reg/tests/flow_test.c
blob: f9d23fd1677dc6700e6b7be4212c07e73acfe6a6 (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
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * The IPC Resource Manager - Registry - Flows - Unit Tests
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#include "../flow.c"

#include <ouroboros/test.h>

#include <string.h>

#define TEST_DATA "testpiggybackdata"

static int test_reg_flow_create(void)
{
        struct reg_flow * f;

        struct flow_info info = {
                .id    = 1,
                .n_pid = 1,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        TEST_START();

        f = reg_flow_create(&info);
        if (f == NULL) {
                printf("Failed to create flow.\n");
                goto fail;
        }

        reg_flow_destroy(f);

        TEST_SUCCESS();

        return 0;
 fail:
        TEST_FAIL();
        return -1;
}

static int test_reg_flow_create_no_id(void) {
        struct flow_info info = {
                .id    = 0,
                .n_pid = 1,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        reg_flow_create(&info); /* assert fail */

        return 0;
}

static int test_reg_flow_create_no_pid(void) {
        struct flow_info info = {
                .id    = 1,
                .n_pid = 0,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        reg_flow_create(&info); /* assert fail */

        return 0;
}

static int test_reg_flow_create_has_n_1_pid(void) {
        struct flow_info info = {
                .id      = 1,
                .n_pid   = 0,
                .n_1_pid = 1,
                .qs      = qos_raw,
                .state   = FLOW_INIT
        };

        reg_flow_create(&info); /* assert fail */

        return 0;
}

static int test_reg_flow_create_wrong_state(void) {
        struct flow_info info = {
                .id      = 1,
                .n_pid   = 0,
                .n_1_pid = 1,
                .qs      = qos_raw,
                .state   = FLOW_ALLOC_PENDING
        };

        reg_flow_create(&info); /* assert fail */

        return 0;
}

static int test_reg_flow_create_has_mpl(void) {
        struct flow_info info = {
                .id      = 1,
                .n_pid   = 1,
                .n_1_pid = 0,
                .mpl     = 10,
                .qs      = qos_raw,
                .state   = FLOW_ALLOC_PENDING
        };

        reg_flow_create(&info); /* assert fail */

        return 0;
}

static int test_reg_flow_update(void)
{
        struct reg_flow * f;

        struct flow_info info = {
                .id    = 1,
                .n_pid = 1,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        struct flow_info upd = {
                .id    = 1,
                .n_pid = 1,
                .qs    = qos_data,
                .state = FLOW_DEALLOCATED
        };

        TEST_START();

        f = reg_flow_create(&info);
        if (f == NULL) {
                printf("Failed to create flow.\n");
                goto fail;
        }

        reg_flow_update(f, &upd);

        if (memcmp(&f->info, &upd, sizeof(upd)) != 0) {
                printf("Flow info not updated.\n");
                goto fail;
        }

        reg_flow_destroy(f);

        TEST_SUCCESS();

        return 0;
 fail:
        TEST_FAIL();
        return -1;
}

static int test_reg_flow_update_wrong_id(void)
{
        struct reg_flow * f;

        struct flow_info info = {
                .id    = 1,
                .n_pid = 1,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        struct flow_info upd = {
                .id    = 2,
                .n_pid = 1,
                .qs    = qos_data,
                .state = FLOW_DEALLOCATED
        };

        TEST_START();

        f = reg_flow_create(&info);
        if (f == NULL) {
                printf("Failed to create flow.\n");
                goto fail;
        }

        reg_flow_update(f, &upd); /* assert fail */

        TEST_SUCCESS();

        return 0;
 fail:
        TEST_FAIL();
        return -1;
}

static int test_reg_flow_assert_fails(void)
{
        int ret = 0;

        ret |= test_assert_fail(test_reg_flow_create_no_id);

        ret |= test_assert_fail(test_reg_flow_create_no_pid);

        ret |= test_assert_fail(test_reg_flow_create_has_n_1_pid);

        ret |= test_assert_fail(test_reg_flow_create_wrong_state);

        ret |= test_assert_fail(test_reg_flow_create_has_mpl);

        ret |= test_assert_fail(test_reg_flow_update_wrong_id);

        return ret;
}

static int test_flow_data(void)
{
        struct reg_flow * f;

        struct flow_info info = {
                .id    = 1,
                .n_pid = 1,
                .qs    = qos_raw,
                .state = FLOW_INIT
        };

        char * data;
        buffer_t buf;
        buffer_t rcv = {NULL, 0};

        TEST_START();

        data = strdup(TEST_DATA);
        if (data == NULL) {
                printf("Failed to strdup data.\n");
                goto fail;
        }

        buf.data = (uint8_t *) data;
        buf.len  = strlen(data);

        f = reg_flow_create(&info);
        if (f == NULL) {
                printf("Failed to create flow.\n");
                goto fail;
        }

        reg_flow_set_data(f, &buf);

        reg_flow_get_data(f, &rcv);

        freebuf(buf);
        clrbuf(rcv);

        reg_flow_destroy(f);

        TEST_SUCCESS();

        return 0;
 fail:
        free(data);
        TEST_FAIL();
        return -1;
}

int flow_test(int     argc,
              char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_reg_flow_create();

        ret |= test_reg_flow_update();

        ret |= test_reg_flow_assert_fails();

        ret |= test_flow_data();

        return ret;
}