aboutsummaryrefslogtreecommitdiff
path: root/fw/protocol_test.c
blob: 5f02ef182515c91e1704c547a2bcff48715ac24d (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
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "protocol.h"
#include "8b10b.h"

int urandom_fd = -1;

struct test_cmd_if {
    struct command_if_def cmd_if;
    int payload_len[256];
};

struct {
    int ncalls;
    int last_cmd;
    uint8_t last_args[sizeof(((struct proto_rx_st *)0)->argbuf)];
} handler_state;


void handle_command(int command, uint8_t *args) {
    handler_state.ncalls++;
    handler_state.last_cmd = command;
    if (args)
        memcpy(handler_state.last_args, args, 8);
}

void send_test_command_single(struct test_cmd_if *cmd_if, struct proto_rx_st *st, int cmd, int address, unsigned char pattern[256]) {
    receive_symbol(st, -K28_1);
    receive_symbol(st, cmd);
    receive_symbol(st, address);
    for (int i=0; i<cmd_if->payload_len[i]; i++)
        receive_symbol(st, pattern[i]);
}

void send_test_command_bulk(struct test_cmd_if *cmd_if, struct proto_rx_st *st, int cmd, int index, int len, unsigned char pattern[256]) {
    receive_symbol(st, -K28_1);
    receive_symbol(st, cmd | PKT_TYPE_BULK_FLAG);
    for (int j=0; j<len; j++) {
        for (int i=0; i<cmd_if->payload_len[cmd]; i++) {
            if (j == index)
                receive_symbol(st, pattern[i]);
            else
                receive_symbol(st, 0xaa);
        }
    }
}

void test_commands_with_pattern(struct test_cmd_if *cmd_if, unsigned char pattern[256]) {
    struct proto_rx_st st;

    for (int cmd=0; cmd<cmd_if->cmd_if.packet_type_max; cmd++) {
        /* Addresssed tests */
        /*
        reset_receiver(&st, &cmd_if->cmd_if);
        st.address = 23;
        handler_state.ncalls = 0;
        send_test_command_single(cmd_if, &st, cmd, 23, pattern);
        assert(handler_state.ncalls == 1);
        assert(handler_state.last_cmd == cmd);
        assert(!memcmp(handler_state.last_args, pattern, cmd_if->payload_len[cmd]));

        reset_receiver(&st, &cmd_if->cmd_if);
        st.address = 23;
        handler_state.ncalls = 0;
        send_test_command_single(cmd_if, &st, cmd, 5, pattern);
        assert(handler_state.ncalls == 0);

        reset_receiver(&st, &cmd_if->cmd_if);
        st.address = 5;
        handler_state.ncalls = 0;
        send_test_command_single(cmd_if, &st, cmd, 5, pattern);
        assert(handler_state.ncalls == 1);
        assert(handler_state.last_cmd == cmd);
        assert(!memcmp(handler_state.last_args, pattern, cmd_if->payload_len[cmd]));
        */

        /* Bulk test */
        reset_receiver(&st, &cmd_if->cmd_if);
        st.address = 5;
        handler_state.ncalls = 0;
        send_test_command_bulk(cmd_if, &st, cmd, 5, 8, pattern);
        assert(handler_state.ncalls == 1);
        assert(handler_state.last_cmd == cmd);
        assert(!memcmp(handler_state.last_args, pattern, cmd_if->payload_len[cmd]));
    }
}

void test_commands(struct test_cmd_if *cmd_if) {
    unsigned char data[256];

    memset(data, 0, sizeof(data));
    test_commands_with_pattern(cmd_if, data);

    memset(data, 1, sizeof(data));
    test_commands_with_pattern(cmd_if, data);

    memset(data, 255, sizeof(data));
    test_commands_with_pattern(cmd_if, data);

    for (int i=0; i<5; i++) {
        assert(read(urandom_fd, (char *)data, sizeof(data)) == sizeof(data));
        test_commands_with_pattern(cmd_if, data);
    }
}

int main(void) {
    struct test_cmd_if cmd_if;

    urandom_fd = open("/dev/urandom", O_RDONLY);
    assert(urandom_fd > 0);

    for (int ncmds=1; ncmds<128; ncmds++) {
        cmd_if.cmd_if.packet_type_max = ncmds;

        /* Case 1 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = 0;
        test_commands(&cmd_if);

        /* Case 2 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = 1;
        test_commands(&cmd_if);

        /* Case 3 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = i&1 ? 1 : 0;
        test_commands(&cmd_if);

        /* Case 4 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = i&1 ? 0 : 1;
        test_commands(&cmd_if);

        /* Case 5 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = i&1 ? 1 : 2;
        test_commands(&cmd_if);

        /* Case 6 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = i%8;
        test_commands(&cmd_if);

        /* Case 7 */
        for (int i=0; i<ncmds; i++)
            cmd_if.payload_len[i] = 4;
        test_commands(&cmd_if);
    }

    assert(!close(urandom_fd));
}