summaryrefslogtreecommitdiff
path: root/host/test/src/test_sci.c
blob: c116856e3a753ba92e3b7c1f7fbc6adce0640452 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    test_sci.c
 * \brief   Test all sci functions.
 * \ingroup test
 */
 
#include <fcntl.h> 
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include "common/std.h"
#include "host/sci.h"
#include "lib/test.h"

extern char dump_buffer[];

//void sci_new_test_case(test_t t)
//{
//    sci_ctx_t *sci;
//    station_ctx_t *station;
//    
//    test_case_begin(t, "new");
//    station = station_new();
//    
//    test_begin(t, "NULL station")
//    {
//        test_fail_unless(
//            (sci_new(NULL) == NULL)
//            && (errno == EINVAL));
//    } test_end;
//    
//    test_begin(t, "check new")
//    {
//        sci = sci_new(station);
//        test_fail_unless(
//            (sci != NULL)
//            && (sci->station == station));
//    }
//    
//    sci_free(sci);
//    station_free(station);
//}

void sci_init_test_case(test_t t)
{
    sci_ctx_t sci;
    station_ctx_t station;
    
    test_case_begin(t, "init");
    
    test_begin(t, "NULL sci")
    {
        test_fail_unless(
            (sci_init(NULL, &station) < 0)
            && (errno == EINVAL)
        );
    } test_end;

    test_begin(t, "NULL station")
    {
        test_fail_unless(
            (sci_init(&sci, NULL) < 0)
            && (errno == EINVAL)
        );
    } test_end;
    
    test_begin(t, "check init call")
    {
        test_fail_unless(
            (sci_init(&sci, &station) >= 0)
            && (sci.station == &station)
        );
    } test_end;
    return;    
}

//void sci_free_test_case(test_t t)
//{
//    station_ctx_t *station
//    sci_ctx_t *sci;
//    
//    test_case_begin(t, "free");
//    
//    test_begin(t, "NULL sci")
//    {
//        sci_free(NULL); /* must not crash */
//        test_fail_if(0);
//    } test_end;
//  
//    station = station_new();
//    sci = sci_new(station);
//
//    test_begin(t, "check station integrity")
//    {
//        sci_free(sci);
//        test_fail_unless(
//            (station->id != 0)); /* station has not been freed */
//    } test_end;
//
//    station_free(station);
//}

static int _sci_callback(sci_msg_t *msg, void *data)
{
    return 0;
}

void sci_register_callback_test_case(test_t t)
{
    station_ctx_t station;
    sci_ctx_t sci;
    
    test_case_begin(t, "register_callback");
    sci_init(&sci, &station);
    
    test_begin(t, "sci = NULL")
    {
        test_fail_unless(
            (sci_register_callback(NULL, 0, NULL, NULL) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "bad type -1")
    {
        test_fail_unless(
            (sci_register_callback(&sci, -1, NULL, NULL) < 0)
            && (errno == EINVAL));
    } test_end;
 
    test_begin(t, "bad type > max")
    {
        test_fail_unless(
            (sci_register_callback(&sci, SCI_MSG_TYPE_NB, NULL, NULL) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "registering NULL function")
    {
        test_fail_unless(
            (sci_register_callback(&sci, SCI_MSG_TYPE_SYSTEM, NULL, &station) < 0)
            &(errno == EINVAL)
        );
    } test_end;
    
    test_begin(t, "registering test function")
    {
        test_fail_unless(
            (sci_register_callback(&sci, SCI_MSG_TYPE_PHY, _sci_callback, &station) >= 0)
            && (sci.msg_callback[SCI_MSG_TYPE_PHY].function == _sci_callback)
            && (sci.msg_callback[SCI_MSG_TYPE_PHY].data = &station));
    } test_end;
    
}

void sci_fill_hdr_test_case(test_t t)
{
    station_ctx_t station;
    sci_ctx_t sci;
    sci_msg_t msg;
    unsigned char buffer[128];
    unsigned short previous_msg_id;
    
    
    test_case_begin(t, "fill_hdr");

    sci_msg_init(&msg, buffer, 128);
    station_init(&station);
    sci_init(&sci, &station);
    
    test_begin(t, "sci = NULL")
    {
        test_fail_unless(
            (sci_fill_hdr(NULL, &msg, SCI_MSG_TYPE_PHY, 0) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "msg = NULL")
    {
        test_fail_unless(
            (sci_fill_hdr(&sci, NULL, SCI_MSG_TYPE_PHY, 0) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "type < 0")
    {
        test_fail_unless(
            (sci_fill_hdr(&sci, &msg, -1, 0) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "type >= max")
    {
        test_fail_unless(
            (sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_NB, 0) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "check content")
    {
        sci_msg_push(&msg, 64);
        previous_msg_id = sci.current_msg_id;
        test_fail_unless(
            (sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_SYSTEM, 0xaa55) >= 0)
            && ((unsigned char *)msg.sci_hdr == msg.data_begin)
            && !memcmp((unsigned char *)&msg.sci_hdr->magic_id, SCI_MSG_MAGIC, 4)
            && (msg.sci_hdr->version == SCI_MSG_VERSION)
            && (msg.sci_hdr->type == SCI_MSG_TYPE_SYSTEM)
            && (ntohs(msg.sci_hdr->length) == 64)
            && (ntohs(msg.sci_hdr->flags) == 0xaa55)
            && (ntohs(msg.sci_hdr->station_id) == sci.station->id)
            && ((ntohs(msg.sci_hdr->msg_id) & ~SCI_MSG_ID_STATION) == sci.current_msg_id - 1)
            && (ntohs(msg.sci_hdr->msg_id) & SCI_MSG_ID_STATION));
    } test_end;

    test_begin(t, "check msg_id rollover")
    {
        sci_msg_init(&msg, buffer, 128);
        sci.current_msg_id = SCI_MSG_ID_MASK;
        test_fail_unless(
            (sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_SYSTEM, 0) >= 0)
            && (ntohs(msg.sci_hdr->msg_id) == (SCI_MSG_ID_MASK | SCI_MSG_ID_STATION))
            && (sci.current_msg_id == 0)
        );
    } test_end;
    
    station_down(&station);
}

#define TEST_DATA_STR "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
void sci_send_test_case(test_t t)
{
    station_ctx_t station;
    sci_ctx_t sci;
    unsigned char msg_buffer[256], buffer[256];
    sci_msg_t msg;
    int fd_out;
    
    test_case_begin(t, "send");
    station_init(&station);
    sci_msg_init(&msg, msg_buffer, 256);
    sci_init(&sci, &station);
    
    test_begin(t, "sci = NULL")
    {
        test_fail_unless(
            (sci_send(NULL, &msg) < 0)
            && (errno == EINVAL));
    } test_end;
    
    test_begin(t, "msg = NULL")
    {
        test_fail_unless(
            (sci_send(&sci, NULL) < 0)
            && (errno == EINVAL));
    } test_end;

    test_begin(t, "check send")
    {
        fd_out = open(station.pipe_out_name, O_RDONLY);
        sci_msg_push(&msg, strlen(TEST_DATA_STR));
        memcpy(msg.data_begin, TEST_DATA_STR, msg.length);
        sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_FUNCTION_CALL, 0);
        test_fail_unless(
            (sci_send(&sci, &msg) == msg.length)
            && (read(fd_out, buffer, msg.length) == msg.length)
            && !memcmp(msg.data_begin, buffer, msg.length)
        );
    } test_end;
    close(fd_out);
    station_down(&station);
}

#define TEST_RECV_STR "test_recv"
static int _sci_send_callback(sci_msg_t *msg, void *data)
{
    unsigned char *buffer;
    buffer = (unsigned char *)data;

    if((msg == NULL)
        || (msg->length != sizeof(station_msg_hdr_t) + strlen(TEST_DATA_STR))
        || (msg->length != msg->data_end - msg->data_begin)
        || ((unsigned char *)msg->hdr.station != msg->data_begin)
        || (buffer == NULL))
    return -1;
    if(msg->hdr.station->type != SYSTEM_TYPE_IDLE)
        return -1;
    sci_msg_pop(msg, sizeof(station_msg_hdr_t));
    if(memcmp(msg->data_begin, TEST_DATA_STR, strlen(TEST_DATA_STR)))
        return -1;
   
    memcpy(buffer, TEST_RECV_STR, strlen(TEST_RECV_STR));
    return 0;
}

void sci_recv_test_case(test_t t)
{
    station_ctx_t station;
    sci_ctx_t sci;
    unsigned char msg_buffer[256], data_buffer[16];
    sci_msg_t msg;
    int fd_in;
    char *magic_id = SCI_MSG_MAGIC;
    
    test_case_begin(t, "recv");
    memset(msg_buffer, '\0', 256);
    memset(data_buffer, '\0', 16);
    station_init(&station);
    //station.pipe_log_fd = 1;
    station.current_tick_tck = 0x1234567890abcdefULL;
    sci_msg_init(&msg, msg_buffer, 256);
    sci_init(&sci, &station);
   
    test_begin(t, "sci = NULL")
    {
        test_fail_unless(
            (sci_recv(NULL) < 0)
            && (errno == EINVAL));
    } test_end;
    
    sci_register_callback(&sci, SCI_MSG_TYPE_SYSTEM, _sci_send_callback, data_buffer);
    sci_msg_push(&msg, strlen(TEST_DATA_STR));
    memcpy(msg.data_begin, TEST_DATA_STR, strlen(TEST_DATA_STR));
    sci_msg_push(&msg, sizeof(station_msg_hdr_t));
    msg.hdr.station = (station_msg_hdr_t *)msg.data_begin;
    msg.hdr.station->type = SYSTEM_TYPE_IDLE;
    sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_SYSTEM, 0);
    fd_in  = open(station.pipe_in_name, O_WRONLY);
    test_begin(t, "bad magic")
    {
        ((char *)&msg.sci_hdr->magic_id)[2] = magic_id[2] + 1;        
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) < 0)
            && (errno == EPROTOTYPE)
        );
        ((char *)&msg.sci_hdr->magic_id)[2] = magic_id[2];        
    } test_end;
    
    test_begin(t, "length > SCI_MSG_MAX_SIZE")
    {
        msg.sci_hdr->length = htons(SCI_MSG_MAX_SIZE + 1 - sizeof(sci_msg_hdr_t));    
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) < 0)
            && (errno == ENOSPC)
        );
        msg.sci_hdr->length = htons(msg.length - sizeof(sci_msg_hdr_t));
    } test_end;
    

   test_begin(t, "type > SCI_MSG_TYPE_NB")
    {
        msg.sci_hdr->type = SCI_MSG_TYPE_NB;        
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) < 0)
            && (errno == EPROTOTYPE)
        );
        msg.sci_hdr->type = SCI_MSG_TYPE_SYSTEM;
    } test_end;
            
   test_begin(t, "bad station id")
    {
        msg.sci_hdr->station_id = htons(station.id + 1);        
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) < 0)
            && (errno == ENODEV)
        );
        msg.sci_hdr->station_id = htons(station.id);        
    } test_end;
    
   test_begin(t, "msg_tick is older than current_tick")
    {
        msg.sci_hdr->netclock_low = 0;
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) >= 0)
            && (station.current_tick_tck == 0x1234567890abcdefULL)
        );
        msg.sci_hdr->netclock_low = 0x90abcdef;        
    } test_end;
    
    memset(data_buffer, '\0', 16);   
    test_begin(t, "callback process")
    {
        msg.sci_hdr->netclock_low = 0xffffffff;
        test_fail_unless(
            (write(fd_in, msg.data_begin, msg.length) == msg.length)
            && (sci_recv(&sci) >= 0)
            && !memcmp(data_buffer, TEST_RECV_STR, strlen(TEST_RECV_STR))
            && (station.current_tick_tck == 0x12345678ffffffffULL)
        );
        close(fd_in);
    } test_end;

    close(fd_in);
    station_down(&station);
    return;
} 

void sci_test_suite(test_t t)
{
    test_suite_begin(t, "sci");
    //sci_new_test_case(t);
    sci_init_test_case(t);
    //sci_free_test_case(t);
    sci_register_callback_test_case(t);
    sci_fill_hdr_test_case(t);
    sci_send_test_case(t);
    sci_recv_test_case(t);
}