summaryrefslogtreecommitdiff
path: root/tests/unit/map65/test_map65_ptt_unix.c
blob: 65af255dc3da0104e34e7a17f8ddfe8ca485c507 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <poll.h>

static int serial_open(const char *, int, ...);
static int serial_ioctl(int, unsigned long, ...);
static int serial_close(int);
static int serial_poll(struct pollfd *, nfds_t, int);

#define open serial_open
#define ioctl serial_ioctl
#define close serial_close
#define poll serial_poll
#ifndef PTT_UNIX_SOURCE
#define PTT_UNIX_SOURCE "../../../map65/libm65/ptt_unix.c"
#endif
#include PTT_UNIX_SOURCE
#undef open
#undef ioctl
#undef close
#undef poll

enum operation { OPEN_PORT, GET_LINES, SET_LINES, CLOSE_PORT };
struct event {
    enum operation operation;
    int descriptor;
    int lines;
    char path[256];
};

static struct event events[128];
static int event_count;
static int lines[64];
static int opened[64];
static int next_descriptor = 10;
static int fail_open;
static unsigned long fail_ioctl_request;
static int fail_ioctl_countdown;
static int failed_descriptor;
static short poll_events;
static int fail_poll;
static int mock_failed;
static int iptt;
static const int ptt_lines = TIOCM_RTS | TIOCM_DTR;

#define CHECK(condition) do { \
    if (!(condition)) { \
        fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #condition); \
        return 0; \
    } \
} while (0)

static struct event *record(enum operation operation, int descriptor)
{
    struct event *event;
    if (event_count == (int)(sizeof(events) / sizeof(events[0]))) {
        fprintf(stderr, "Serial event buffer exhausted\n");
        exit(2);
    }
    event = &events[event_count++];
    memset(event, 0, sizeof(*event));
    event->operation = operation;
    event->descriptor = descriptor;
    return event;
}

static int serial_open(const char *path, int flags, ...)
{
    int descriptor = next_descriptor++;
    struct event *event = record(OPEN_PORT, descriptor);
    snprintf(event->path, sizeof(event->path), "%s", path);
    if (!(flags & O_NONBLOCK) || !(flags & O_NOCTTY)
        || (flags & O_ACCMODE) != O_RDWR
        || descriptor >= (int)(sizeof(opened) / sizeof(opened[0]))) {
        mock_failed = 1;
        return -1;
    }
    if (fail_open) {
        errno = EACCES;
        return -1;
    }
    opened[descriptor] = 1;
    lines[descriptor] = TIOCM_CTS | TIOCM_DTR | TIOCM_RTS;
    return descriptor;
}

static int serial_ioctl(int descriptor, unsigned long request, ...)
{
    va_list arguments;
    int *value;
    struct event *event = record(request == TIOCMGET ? GET_LINES : SET_LINES,
                                descriptor);
    va_start(arguments, request);
    value = va_arg(arguments, int *);
    va_end(arguments);
    if (descriptor < 0 || descriptor >= 64 || !opened[descriptor]
        || (request != TIOCMGET && request != TIOCMSET)) {
        mock_failed = 1;
        return -1;
    }
    event->lines = request == TIOCMSET ? *value : lines[descriptor];
    if (descriptor == failed_descriptor
        || (request == fail_ioctl_request && --fail_ioctl_countdown == 0)) {
        errno = EIO;
        return -1;
    }
    if (request == TIOCMGET)
        *value = lines[descriptor];
    else
        lines[descriptor] = *value;
    return 0;
}

static int serial_close(int descriptor)
{
    record(CLOSE_PORT, descriptor);
    if (descriptor < 0 || descriptor >= 64 || !opened[descriptor]) {
        mock_failed = 1;
        return -1;
    }
    opened[descriptor] = 0;
    return 0;
}

static int serial_poll(struct pollfd *ports, nfds_t count, int timeout)
{
    if (count != 1 || timeout != 0 || ports[0].events != 0
        || ports[0].fd < 0 || ports[0].fd >= 64 || !opened[ports[0].fd]) {
        mock_failed = 1;
        return -1;
    }
    if (fail_poll) {
        errno = EINTR;
        return -1;
    }
    ports[0].revents = poll_events;
    return poll_events ? 1 : 0;
}

static int command(int on)
{
    int unused = 0;
    return ptt_(&unused, &on, &iptt);
}

static void fail_next_ioctl(unsigned long request, int occurrence)
{
    fail_ioctl_request = request;
    fail_ioctl_countdown = occurrence;
}

static void reset(void)
{
    fail_open = 0;
    fail_ioctl_request = 0;
    failed_descriptor = -1;
    poll_events = 0;
    fail_poll = 0;
    ptt_close();
    ptt_set_override(NULL);
    event_count = 0;
    next_descriptor = 10;
    mock_failed = 0;
    iptt = 0;
    memset(opened, 0, sizeof(opened));
    memset(lines, 0, sizeof(lines));
}

static int count(enum operation operation)
{
    int result = 0;
    int i;
    for (i = 0; i < event_count; ++i)
        result += events[i].operation == operation;
    return result;
}

static int first(enum operation operation)
{
    int i;
    for (i = 0; i < event_count; ++i)
        if (events[i].operation == operation)
            return i;
    return -1;
}

static int disabled_and_unopened_off(void)
{
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(event_count == 0);
    ptt_set_override("/dev/serial-A");
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(event_count == 0);
    return 1;
}

static int reuse_and_preserve_lines(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(events[0].operation == OPEN_PORT);
    CHECK(events[1].operation == GET_LINES);
    CHECK(events[2].operation == SET_LINES && events[2].lines == TIOCM_CTS);
    CHECK(lines[10] == (TIOCM_CTS | ptt_lines));
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(lines[10] == TIOCM_CTS);
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 0);
    return 1;
}

static int idle_switch(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0);
    CHECK(command(0) == 0);
    event_count = 0;
    ptt_set_override("/dev/serial-B");
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(count(CLOSE_PORT) == 1 && count(OPEN_PORT) == 1);
    CHECK(first(CLOSE_PORT) < first(OPEN_PORT));
    CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
    CHECK(!opened[10] && opened[11]);
    CHECK(lines[10] == TIOCM_CTS && lines[11] == (TIOCM_CTS | ptt_lines));
    return 1;
}

static int active_switch(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0);
    event_count = 0;
    ptt_set_override("/dev/serial-B");
    CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 1);
    CHECK(lines[10] == TIOCM_CTS);
    CHECK(first(SET_LINES) < first(CLOSE_PORT));
    CHECK(command(1) == 0);
    CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
    return 1;
}

static int active_disable(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0);
    event_count = 0;
    ptt_set_override(NULL);
    CHECK(count(CLOSE_PORT) == 0);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(lines[10] == TIOCM_CTS && !opened[10]);
    CHECK(count(CLOSE_PORT) == 1 && count(OPEN_PORT) == 0);
    event_count = 0;
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(event_count == 0);
    return 1;
}

static int repeated_switches(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0);
    ptt_set_override("/dev/serial-B");
    ptt_set_override("/dev/serial-A");
    CHECK(command(0) == 0);
    CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 0);
    CHECK(command(1) == 0);
    ptt_set_override("/dev/serial-B");
    ptt_set_override(NULL);
    ptt_set_override("/dev/serial-C");
    CHECK(command(0) == 0);
    CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 1);
    event_count = 0;
    CHECK(command(1) == 0);
    CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-C") == 0);
    return 1;
}

static int open_failure(void)
{
    ptt_set_override("/dev/serial-A");
    fail_open = 1;
    CHECK(command(1) != 0 && iptt == 0);
    CHECK(count(GET_LINES) == 0 && count(SET_LINES) == 0);
    fail_open = 0;
    CHECK(command(1) == 0 && iptt == 1);
    return 1;
}

static int initialization_failure(unsigned long request)
{
    int i;
    ptt_set_override("/dev/serial-A");
    fail_next_ioctl(request, 1);
    CHECK(command(1) != 0 && iptt == 0);
    for (i = 0; i < event_count; ++i)
        CHECK(events[i].operation != SET_LINES || !(events[i].lines & ptt_lines));
    event_count = 0;
    CHECK(command(1) != 0 && iptt == 0);
    CHECK(count(OPEN_PORT) == 0 && count(SET_LINES) == 0);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(command(1) == 0 && iptt == 1);
    return 1;
}

static int initial_get_failure(void) { return initialization_failure(TIOCMGET); }
static int initial_set_failure(void) { return initialization_failure(TIOCMSET); }

static int keying_failure(unsigned long request)
{
    ptt_set_override("/dev/serial-A");
    fail_next_ioctl(request, 2);
    CHECK(command(1) != 0 && iptt == 0);
    event_count = 0;
    ptt_set_override("/dev/serial-B");
    CHECK(command(1) != 0 && iptt == 0);
    CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(!opened[10]);
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
    return 1;
}

static int keying_get_failure(void) { return keying_failure(TIOCMGET); }
static int keying_set_failure(void) { return keying_failure(TIOCMSET); }

static int release_failure(unsigned long request, const char *next_path)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0 && iptt == 1);
    event_count = 0;
    ptt_set_override(next_path);
    fail_next_ioctl(request, 1);
    CHECK(command(0) != 0 && iptt == 1);
    CHECK(opened[10] && count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
    CHECK(command(1) != 0 && iptt == 1);
    CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
    CHECK(command(0) == 0 && iptt == 0);
    CHECK(lines[10] == TIOCM_CTS && !opened[10]);
    CHECK(count(CLOSE_PORT) == 1);
    CHECK(command(1) == 0 && iptt == 1);
    CHECK(count(OPEN_PORT) == (next_path ? 1 : 0));
    return 1;
}

static int release_get_failure(void) { return release_failure(TIOCMGET, "/dev/serial-B"); }
static int release_set_failure(void) { return release_failure(TIOCMSET, "/dev/serial-B"); }
static int disable_release_failure(void) { return release_failure(TIOCMSET, NULL); }

static int disconnected_port(int during_tx, const char *next_path)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == PTT_OK);
    if (!during_tx)
        CHECK(command(0) == PTT_OK);
    failed_descriptor = 10;
    poll_events = POLLHUP | POLLERR;
    event_count = 0;
    CHECK(command(during_tx ? 0 : 1) == PTT_DEVICE_LOST && iptt == 0);
    CHECK(!opened[10] && count(CLOSE_PORT) == 1);
    CHECK(count(OPEN_PORT) == 0);
    ptt_set_override(next_path);
    poll_events = 0;
    CHECK(command(1) == PTT_OK && iptt == 1);
    CHECK(count(OPEN_PORT) == (next_path ? 1 : 0));
    if (next_path)
        CHECK(strcmp(events[first(OPEN_PORT)].path, next_path) == 0);
    CHECK(command(0) == PTT_OK && iptt == 0);
    return 1;
}

static int disconnect_idle(void) { return disconnected_port(0, "/dev/serial-B"); }
static int disconnect_active(void) { return disconnected_port(1, "/dev/serial-B"); }
static int disconnect_same_path(void) { return disconnected_port(1, "/dev/serial-A"); }
static int disconnect_disable(void) { return disconnected_port(1, NULL); }

static int persistent_control_failure(void)
{
    int i;
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == PTT_OK);
    ptt_set_override("/dev/serial-B");
    failed_descriptor = 10;
    event_count = 0;
    for (i = 0; i < 3; ++i) {
        poll_events = i == 1 ? POLLERR : 0;
        fail_poll = i == 2;
        CHECK(command(0) == PTT_ERROR && iptt == 1);
        CHECK(command(1) == PTT_ERROR && iptt == 1);
        CHECK(opened[10] && count(CLOSE_PORT) == 0 && count(OPEN_PORT) == 0);
    }
    fail_poll = 0;
    poll_events = POLLHUP;
    CHECK(command(0) == PTT_DEVICE_LOST && iptt == 0);
    CHECK(!opened[10] && count(CLOSE_PORT) == 1);
    poll_events = 0;
    CHECK(command(1) == PTT_OK && iptt == 1);
    CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
    return 1;
}

static int disconnect_initialization(void)
{
    ptt_set_override("/dev/serial-A");
    failed_descriptor = 10;
    poll_events = POLLHUP;
    CHECK(command(1) == PTT_DEVICE_LOST && iptt == 0);
    CHECK(!opened[10] && count(CLOSE_PORT) == 1);
    CHECK(count(SET_LINES) == 0);
    poll_events = 0;
    CHECK(command(1) == PTT_OK && iptt == 1);
    return 1;
}

static int shutdown_cleanup(void)
{
    ptt_set_override("/dev/serial-A");
    CHECK(command(1) == 0);
    event_count = 0;
    ptt_close();
    CHECK(lines[10] == TIOCM_CTS && !opened[10]);
    CHECK(count(SET_LINES) == 1 && count(CLOSE_PORT) == 1);
    CHECK(first(SET_LINES) < first(CLOSE_PORT));
    event_count = 0;
    ptt_close();
    CHECK(event_count == 0);
    CHECK(command(1) == 0);
    fail_next_ioctl(TIOCMSET, 1);
    event_count = 0;
    ptt_close();
    CHECK(count(SET_LINES) == 1 && count(CLOSE_PORT) == 1 && !opened[11]);
    return 1;
}

int main(void)
{
    const struct {
        const char *name;
        int (*run)(void);
    } tests[] = {
        {"disabled and unopened off", disabled_and_unopened_off},
        {"reuse and preserve lines", reuse_and_preserve_lines},
        {"idle switch", idle_switch},
        {"active switch", active_switch},
        {"active disable", active_disable},
        {"repeated switches", repeated_switches},
        {"open failure", open_failure},
        {"initial get failure", initial_get_failure},
        {"initial set failure", initial_set_failure},
        {"keying get failure", keying_get_failure},
        {"keying set failure", keying_set_failure},
        {"release get failure", release_get_failure},
        {"release set failure", release_set_failure},
        {"disable release failure", disable_release_failure},
        {"disconnect while idle", disconnect_idle},
        {"disconnect while active", disconnect_active},
        {"disconnect and reuse path", disconnect_same_path},
        {"disconnect and disable", disconnect_disable},
        {"persistent control failure", persistent_control_failure},
        {"disconnect during initialization", disconnect_initialization},
        {"shutdown cleanup", shutdown_cleanup}
    };
    int failed = 0;
    size_t i;
    for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
        int passed;
        reset();
        passed = tests[i].run() && !mock_failed;
        printf("%s: %s\n", passed ? "PASS" : "FAIL", tests[i].name);
        failed += !passed;
    }
    reset();
    return failed ? 1 : 0;
}