1 // Copyright 2017 syzkaller project authors. All rights reserved.
2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4 #if GOOS_linux && GOARCH_amd64
5 #include "test_linux.h"
6 #endif
7
test_copyin()8 static int test_copyin()
9 {
10 static uint16 buf[3];
11 STORE_BY_BITMASK(uint16, &buf[1], 0x1234, 0, 0);
12 unsigned char x[sizeof(buf)];
13 memcpy(x, buf, sizeof(x));
14 if (x[0] != 0 || x[1] != 0 ||
15 x[2] != 0x34 || x[3] != 0x12 ||
16 x[4] != 0 || x[5] != 0) {
17 printf("bad result of STORE_BY_BITMASK(0, 0): %x %x %x %x %x %x\n",
18 x[0], x[1], x[2], x[3], x[4], x[5]);
19 return 1;
20 }
21 STORE_BY_BITMASK(uint16, &buf[1], 0x555a, 5, 4);
22 memcpy(x, buf, sizeof(x));
23 if (x[0] != 0 || x[1] != 0 ||
24 x[2] != 0x54 || x[3] != 0x13 ||
25 x[4] != 0 || x[5] != 0) {
26 printf("bad result of STORE_BY_BITMASK(7, 3): %x %x %x %x %x %x\n",
27 x[0], x[1], x[2], x[3], x[4], x[5]);
28 return 1;
29 }
30 return 0;
31 }
32
test_csum_inet()33 static int test_csum_inet()
34 {
35 struct csum_inet_test {
36 const char* data;
37 size_t length;
38 uint16 csum;
39 };
40 struct csum_inet_test tests[] = {
41 {// 0
42 "",
43 0,
44 0xffff},
45 {
46 // 1
47 "\x00",
48 1,
49 0xffff,
50 },
51 {
52 // 2
53 "\x00\x00",
54 2,
55 0xffff,
56 },
57 {
58 // 3
59 "\x00\x00\xff\xff",
60 4,
61 0x0000,
62 },
63 {
64 // 4
65 "\xfc",
66 1,
67 0xff03,
68 },
69 {
70 // 5
71 "\xfc\x12",
72 2,
73 0xed03,
74 },
75 {
76 // 6
77 "\xfc\x12\x3e",
78 3,
79 0xecc5,
80 },
81 {
82 // 7
83 "\xfc\x12\x3e\x00\xc5\xec",
84 6,
85 0x0000,
86 },
87 {
88 // 8
89 "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd",
90 17,
91 0x43e1,
92 },
93 {
94 // 9
95 "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd\x00",
96 18,
97 0x43e1,
98 },
99 {
100 // 10
101 "\x00\x00\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd",
102 19,
103 0x43e1,
104 },
105 {
106 // 11
107 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\xab\xcd",
108 15,
109 0x5032,
110 },
111 {
112 // 12
113 "\x00\x00\x12\x34\x56\x78",
114 6,
115 0x5397,
116 },
117 {
118 // 13
119 "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x06\x00\x04\xab\xcd",
120 14,
121 0x7beb,
122 },
123 {
124 // 14
125 "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x06\x00\x00\xab\xcd",
126 44,
127 0x2854,
128 },
129 {
130 // 15
131 "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x11\x00\x04\xab\xcd",
132 14,
133 0x70eb,
134 },
135 {
136 // 16
137 "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x11\x00\x00\xab\xcd",
138 44,
139 0x1d54,
140 },
141 {
142 // 17
143 "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x3a\x00\x00\xab\xcd",
144 44,
145 0xf453,
146 }};
147
148 for (unsigned i = 0; i < ARRAY_SIZE(tests); i++) {
149 struct csum_inet csum;
150 csum_inet_init(&csum);
151 csum_inet_update(&csum, (const uint8*)tests[i].data, tests[i].length);
152 if (csum_inet_digest(&csum) != tests[i].csum) {
153 fprintf(stderr, "bad checksum in test #%u, want: %hx, got: %hx\n", i, tests[i].csum, csum_inet_digest(&csum));
154 return 1;
155 }
156 }
157
158 return 0;
159 }
160
rand_int_range(int start,int end)161 static int rand_int_range(int start, int end)
162 {
163 return rand() % (end + 1 - start) + start;
164 }
165
test_csum_inet_acc()166 static int test_csum_inet_acc()
167 {
168 uint8 buffer[128];
169
170 int test;
171 for (test = 0; test < 256; test++) {
172 int size = rand_int_range(1, 128);
173 int step = rand_int_range(1, 8) * 2;
174
175 int i;
176 for (i = 0; i < size; i++)
177 buffer[i] = rand_int_range(0, 255);
178
179 struct csum_inet csum_acc;
180 csum_inet_init(&csum_acc);
181
182 for (i = 0; i < size / step; i++)
183 csum_inet_update(&csum_acc, &buffer[i * step], step);
184 if (size % step != 0)
185 csum_inet_update(&csum_acc, &buffer[size - size % step], size % step);
186
187 struct csum_inet csum;
188 csum_inet_init(&csum);
189 csum_inet_update(&csum, &buffer[0], size);
190
191 if (csum_inet_digest(&csum_acc) != csum_inet_digest(&csum))
192 return 1;
193 }
194 return 0;
195 }
196
197 static struct {
198 const char* name;
199 int (*f)();
200 } tests[] = {
201 {"test_copyin", test_copyin},
202 {"test_csum_inet", test_csum_inet},
203 {"test_csum_inet_acc", test_csum_inet_acc},
204 #if GOOS_linux && GOARCH_amd64
205 {"test_kvm", test_kvm},
206 #endif
207 };
208
run_tests()209 int run_tests()
210 {
211 int ret = 0;
212 for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
213 const char* name = tests[i].name;
214 printf("=== RUN %s\n", name);
215 int res = tests[i].f();
216 ret |= res > 0;
217 const char* strres = res < 0 ? "SKIP" : (res > 0 ? "FAIL" : "OK");
218 printf("--- %-4s %s\n", strres, name);
219 }
220 return ret;
221 }
222