File: packetparsertest.c

package info (click to toggle)
tinyssh 20190101-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,208 kB
  • sloc: ansic: 11,914; sh: 1,005; python: 385; makefile: 18
file content (221 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (4)
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
/*
20140303
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include "run.h"
#include "fail.h"
#include "byte.h"
#include "packetparser.h"

/* 
checks packetparser_uint8(), packetparser_uint32() function
and packetparser_end called more than once
 */
static void test1(void) {

    unsigned char buf[4];
    crypto_uint8 ch;
    crypto_uint32 u;
    long long i, pos = 0;

    byte_copy(buf, 4, "abcd");

    /* uint8 */
    pos = 0;
    for (i = 0; i < sizeof buf; ++i) {
        pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
        if (ch != buf[i]) fail("failure");
    }
    if (pos != sizeof buf) fail("failure");

    /* uint32 */
    pos = 0;
    pos = packetparser_uint8(buf, sizeof buf, 0, &ch);
    pos = packetparser_uint32(buf, sizeof buf, 0, &u);
    if (pos != sizeof buf) fail("failure");

    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
}


/* checks if packetparser_skip() skips 0 bytes */
static void test2(void) {

    unsigned char buf[5];
    crypto_uint8 ch;
    crypto_uint32 len;
    long long i, pos = 0;

    for (i = 0; i < sizeof buf; ++i) buf[i] = 0;

    pos = packetparser_uint32(buf, sizeof buf, pos, &len);
    if (len != 0) fail("failure");
    pos = packetparser_skip(buf, sizeof buf, pos, len);
    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
    if (ch) fail("failure");
    if (pos != sizeof buf) fail("failure");

    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
}

/* checks if packetparser_copy() accepts 0 bytes */
static void test4(void) {

    unsigned char buf[5];
    unsigned char buf2[6];
    crypto_uint8 ch;
    crypto_uint32 len;
    long long i, pos = 0;

    for (i = 0; i < sizeof buf; ++i) buf[i] = 0;

    pos = packetparser_uint32(buf, sizeof buf, pos, &len);
    if (len != 0) fail("failure");
    pos = packetparser_copy(buf, sizeof buf, pos, buf2, len);
    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
    if (ch) fail("failure");
    if (pos != sizeof buf) fail("failure");

    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
    pos = packetparser_end(buf, sizeof buf, pos);
}


/* packetparser_skip() overflow */
static void _test3a(void) {

    unsigned char buf[5];
    long long pos = 0;

    pos = packetparser_skip(buf, sizeof buf, pos, 6);
}


static void _test3aa(void) {

    unsigned char buf[5];
    long long len, pos;
    len = pos = 9223372036854775807LL;

    pos = packetparser_skip(buf, len, pos, 1);
}

/* packetparser_uint32() overflow */
static void _test3b(void) {

    unsigned char buf[3];
    crypto_uint32 u;
    long long pos = 0;

    pos = packetparser_uint32(buf, sizeof buf, pos, &u);
}

static void _test3bb(void) {

    unsigned char buf[4];
    crypto_uint32 u;
    long long len, pos;
    len = pos = 9223372036854775807LL;
    
    pos = packetparser_uint32(buf, len, pos, &u);
}



/* packetparser_uint8() overflow */
static void _test3c(void) {

    unsigned char buf[3];
    crypto_uint8 ch;
    long long pos = 0;

    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
    pos = packetparser_uint8(buf, sizeof buf, pos, &ch);
}

static void _test3cc(void) {

    unsigned char buf[3];
    crypto_uint8 ch;
    long long len, pos;
    len = pos = 9223372036854775807LL;

    pos = packetparser_uint8(buf, len, pos, &ch);
}

/* packetparser_copy() overflow */
static void _test3d(void) {

    unsigned char buf1[10];
    unsigned char buf2[11];
    long long pos = 0;

    pos = packetparser_copy(buf1, sizeof buf1, pos, buf2, sizeof buf2);
}

static void _test3dd(void) {

    unsigned char buf1[10];
    unsigned char buf2[10];
    long long len, pos;
    len = pos = 9223372036854775807LL;

    pos = packetparser_copy(buf1, len, pos, buf2, sizeof buf2);
}


/* packetparser_end() failure */
static void _test3e(void) {

    unsigned char buf[1];

    packetparser_end(buf, 0, 1);
}

static void _test3ee(void) {

    unsigned char buf[1];
    long long pos = 9223372036854775807LL;
    
    packetparser_end(buf, pos, pos);
}


static void test3(void) {
    run_mustfail(_test3a);
    run_mustfail(_test3aa);
    run_mustfail(_test3b);
    run_mustfail(_test3bb);
    run_mustfail(_test3c);
    run_mustfail(_test3cc);
    run_mustfail(_test3d);
    run_mustfail(_test3dd);
    run_mustfail(_test3e);
    run_mustfail(_test3ee);
}

/* dummy test */
static void testdummy(void) {
    _exit(0);
}


int main(void) {

    test1();
    test2();
    test3();
    test4();
    run_mustpass(testdummy);
    _exit(0);
}