Debian Codegraph @

tests/unittests/torture_threads_buffer.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Anderson Toshiyuki Sasaki <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #define DEBUG_BUFFER
28 #include "buffer.c"
29
30 #include <pthread.h>
31
32 #define NUM_THREADS 20
33
34 #define BUFFER_LIMIT (8 * 1024 * 1024)
35
36 static int run_on_threads(void *(*func)(void *))
37 {
38 pthread_t threads[NUM_THREADS];
39 int rc;
40 int i;
41
42 for (i = 0; i < NUM_THREADS; ++i) {
43 rc = pthread_create(&threads[i], NULL, func, NULL);
44 assert_int_equal(rc, 0);
45 }
46
47 for (i = 0; i < NUM_THREADS; ++i) {
48 void *p = NULL;
49 uint64_t *result = NULL;
50
51 rc = pthread_join(threads[i], &p);
52 assert_int_equal(rc, 0);
53
54 result = (uint64_t *)p;
55 assert_null(result);
56 }
57
58 return rc;
59 }
60
61 /*
62 * Test if the continuously growing buffer size never exceeds 2 time its
63 * real capacity
64 */
65 static void *thread_growing_buffer(void *threadid)
66 {
67 ssh_buffer buffer = NULL;
68 int i;
69
70 /* Unused */
71 (void) threadid;
72
73 /* Setup */
74 buffer = ssh_buffer_new();
75 if (buffer == NULL) {
76 pthread_exit((void *)-1);
77 }
78 ssh_buffer_set_secure(buffer);
79
80 for (i = 0; i < BUFFER_LIMIT; ++i) {
81 ssh_buffer_add_data(buffer,"A",1);
82 if (buffer->used >= 128) {
83 if (ssh_buffer_get_len(buffer) * 2 < buffer->allocated) {
84 assert_true(ssh_buffer_get_len(buffer) * 2 >= buffer->allocated);
85 }
86 }
87 }
88
89 /* Teardown */
90 SSH_BUFFER_FREE(buffer);
91 pthread_exit(NULL);
92 }
93
94 static void torture_growing_buffer(void **state)
95 {
96 int rc;
97
98 /* Unused */
99 (void) state;
100
101 rc = run_on_threads(thread_growing_buffer);
102 assert_int_equal(rc, 0);
103 }
104
105 /*
106 * Test if the continuously growing buffer size never exceeds 2 time its
107 * real capacity, when we remove 1 byte after each call (sliding window)
108 */
109 static void *thread_growing_buffer_shifting(void *threadid)
110 {
111 ssh_buffer buffer;
112 int i;
113 unsigned char c;
114
115 /* Unused */
116 (void) threadid;
117
118 /* Setup */
119 buffer = ssh_buffer_new();
120 if (buffer == NULL) {
121 pthread_exit((void *)-1);
122 /* dummy analyzers ... */
123 return NULL;
124 }
125 ssh_buffer_set_secure(buffer);
126
127
128 for (i = 0; i < 1024; ++i) {
129 ssh_buffer_add_data(buffer,"S",1);
130 }
131
132 for (i = 0; i < BUFFER_LIMIT; ++i) {
133 ssh_buffer_get_u8(buffer,&c);
134 ssh_buffer_add_data(buffer,"A",1);
135 if (buffer->used >= 128) {
136 if (ssh_buffer_get_len(buffer) * 4 < buffer->allocated) {
137 assert_true(ssh_buffer_get_len(buffer) * 4 >= buffer->allocated);
138 /* Teardown */
139 SSH_BUFFER_FREE(buffer);
140 pthread_exit(NULL);
141 }
142 }
143 }
144
145 /* Teardown */
146 SSH_BUFFER_FREE(buffer);
147 pthread_exit(NULL);
148 }
149
150 static void torture_growing_buffer_shifting(void **state)
151 {
152 int rc;
153
154 /* Unused */
155 (void) state;
156
157 rc = run_on_threads(thread_growing_buffer_shifting);
158 assert_int_equal(rc, 0);
159 }
160
161 /*
162 * Test the behavior of ssh_buffer_prepend_data
163 */
164 static void *thread_buffer_prepend(void *threadid)
165 {
166 ssh_buffer buffer = NULL;
167 uint32_t v;
168
169 /* Unused */
170 (void) threadid;
171
172 /* Setup */
173 buffer = ssh_buffer_new();
174 if (buffer == NULL) {
175 pthread_exit((void *)-1);
176 }
177 ssh_buffer_set_secure(buffer);
178
179 ssh_buffer_add_data(buffer, "abcdef", 6);
180 ssh_buffer_prepend_data(buffer, "xyz", 3);
181 assert_int_equal(ssh_buffer_get_len(buffer), 9);
182 assert_memory_equal(ssh_buffer_get(buffer), "xyzabcdef", 9);
183
184 /* Now remove 4 bytes and see if we can replace them */
185 ssh_buffer_get_u32(buffer, &v);
186 assert_int_equal(ssh_buffer_get_len(buffer), 5);
187 assert_memory_equal(ssh_buffer_get(buffer), "bcdef", 5);
188
189 ssh_buffer_prepend_data(buffer, "aris", 4);
190 assert_int_equal(ssh_buffer_get_len(buffer), 9);
191 assert_memory_equal(ssh_buffer_get(buffer), "arisbcdef", 9);
192
193 /* same thing but we add 5 bytes now */
194 ssh_buffer_get_u32(buffer, &v);
195 assert_int_equal(ssh_buffer_get_len(buffer), 5);
196 assert_memory_equal(ssh_buffer_get(buffer), "bcdef", 5);
197
198 ssh_buffer_prepend_data(buffer, "12345", 5);
199 assert_int_equal(ssh_buffer_get_len(buffer), 10);
200 assert_memory_equal(ssh_buffer_get(buffer), "12345bcdef", 10);
201
202 /* Teardown */
203 SSH_BUFFER_FREE(buffer);
204 pthread_exit(NULL);
205 }
206
207 static void torture_buffer_prepend(void **state)
208 {
209 int rc;
210
211 /* Unused */
212 (void) state;
213
214 rc = run_on_threads(thread_buffer_prepend);
215 assert_int_equal(rc, 0);
216 }
217
218 /*
219 * Test the behavior of ssh_buffer_get_ssh_string with invalid data
220 */
221 static void *thread_ssh_buffer_get_ssh_string(void *threadid)
222 {
223 ssh_buffer buffer = NULL;
224 size_t i, j, k, l;
225 int rc;
226 /* some values that can go wrong */
227 uint32_t values[] = {
228 0xffffffff, 0xfffffffe, 0xfffffffc, 0xffffff00,
229 0x80000000, 0x80000004, 0x7fffffff};
230 char data[128] = {0};
231
232 /* Unused */
233 (void)threadid;
234
235 memset(data, 'X', sizeof(data));
236
237 for (i = 0; i < ARRAY_SIZE(values); ++i) {
238 for (j = 0; j < (int)sizeof(data); ++j) {
239 for (k = 1; k < 5; ++k) {
240 buffer = ssh_buffer_new();
241 assert_non_null(buffer);
242
243 for (l = 0; l < k; ++l) {
244 rc = ssh_buffer_add_u32(buffer, htonl(values[i]));
245 assert_int_equal(rc, 0);
246 }
247 rc = ssh_buffer_add_data(buffer,data,j);
248 assert_int_equal(rc, 0);
249 for (l = 0; l < k; ++l) {
250 ssh_string str = ssh_buffer_get_ssh_string(buffer);
251 assert_null(str);
252 SSH_STRING_FREE(str);
253 }
254 SSH_BUFFER_FREE(buffer);
255 }
256 }
257 }
258
259 pthread_exit(NULL);
260 }
261
262 static void torture_ssh_buffer_get_ssh_string(void **state){
263 int rc;
264
265 /* Unused */
266 (void) state;
267
268 rc = run_on_threads(thread_ssh_buffer_get_ssh_string);
269 assert_int_equal(rc, 0);
270 }
271
272 static void *thread_ssh_buffer_add_format(void *threadid)
273 {
274 ssh_buffer buffer = NULL;
275 uint8_t b;
276 uint16_t w;
277 uint32_t d;
278 uint64_t q;
279 ssh_string s = NULL;
280 int rc;
281 size_t len;
282 uint8_t verif[] = "\x42\x13\x37\x0b\xad\xc0\xde\x13\x24\x35\x46"
283 "\xac\xbd\xce\xdf"
284 "\x00\x00\x00\x06" "libssh"
285 "\x00\x00\x00\x05" "rocks"
286 "So much"
287 "Fun!";
288
289 /* Unused */
290 (void) threadid;
291
292 /* Setup */
293 buffer = ssh_buffer_new();
294 if (buffer == NULL) {
295 pthread_exit((void *)-1);
296 }
297 ssh_buffer_set_secure(buffer);
298
299 b = 0x42;
300 w = 0x1337;
301 d = 0xbadc0de;
302 q = 0x13243546acbdcedf;
303 s = ssh_string_from_char("libssh");
304 rc = ssh_buffer_pack(buffer,
305 "bwdqSsPt",
306 b,
307 w,
308 d,
309 q,
310 s,
311 "rocks",
312 (size_t)7,
313 "So much",
314 "Fun!");
315 assert_int_equal(rc, SSH_OK);
316
317 len = ssh_buffer_get_len(buffer);
318 assert_int_equal(len, sizeof(verif) - 1);
319 assert_memory_equal(ssh_buffer_get(buffer), verif, sizeof(verif) -1);
320
321 SSH_STRING_FREE(s);
322
323 /* Teardown */
324 SSH_BUFFER_FREE(buffer);
325 pthread_exit(NULL);
326 }
327
328 static void torture_ssh_buffer_add_format(void **state){
329 int rc;
330
331 /* Unused */
332 (void) state;
333
334 rc = run_on_threads(thread_ssh_buffer_add_format);
335 assert_int_equal(rc, 0);
336 }
337
338 static void *thread_ssh_buffer_get_format(void *threadid) {
339 ssh_buffer buffer;
340 uint8_t b = 0;
341 uint16_t w = 0;
342 uint32_t d = 0;
343 uint64_t q = 0;
344 ssh_string s = NULL;
345 char *s1 = NULL, *s2 = NULL;
346 int rc;
347 size_t len;
348 uint8_t verif[] = "\x42\x13\x37\x0b\xad\xc0\xde\x13\x24\x35\x46"
349 "\xac\xbd\xce\xdf"
350 "\x00\x00\x00\x06" "libssh"
351 "\x00\x00\x00\x05" "rocks"
352 "So much";
353
354 /* Unused */
355 (void) threadid;
356
357 /* Setup */
358 buffer = ssh_buffer_new();
359 if (buffer == NULL) {
360 pthread_exit((void *)-1);
361 }
362 ssh_buffer_set_secure(buffer);
363
364 rc = ssh_buffer_add_data(buffer, verif, sizeof(verif) - 1);
365 assert_int_equal(rc, SSH_OK);
366
367 rc = ssh_buffer_unpack(buffer,
368 "bwdqSsP",
369 &b,
370 &w,
371 &d,
372 &q,
373 &s,
374 &s1,
375 (size_t)7,
376 &s2);
377 assert_int_equal(rc, SSH_OK);
378
379 assert_int_equal(b, 0x42);
380 assert_int_equal(w, 0x1337);
381
382 assert_true(d == 0xbadc0de);
383 assert_true(q == 0x13243546acbdcedf);
384
385 assert_non_null(s);
386 assert_int_equal(ssh_string_len(s), 6);
387 assert_memory_equal(ssh_string_data(s), "libssh", 6);
388
389 assert_non_null(s1);
390 assert_string_equal(s1, "rocks");
391
392 assert_non_null(s2);
393 assert_memory_equal(s2, "So much", 7);
394
395 len = ssh_buffer_get_len(buffer);
396 assert_int_equal(len, 0);
397 SAFE_FREE(s);
398 SAFE_FREE(s1);
399 SAFE_FREE(s2);
400
401 /* Teardown */
402 SSH_BUFFER_FREE(buffer);
403 pthread_exit(NULL);
404 }
405
406 static void torture_ssh_buffer_get_format(void **state)
407 {
408 int rc;
409
410 /* Unused */
411 (void) state;
412
413 rc = run_on_threads(thread_ssh_buffer_get_format);
414 assert_int_equal(rc, 0);
415 }
416
417 static void *thread_ssh_buffer_get_format_error(void *threadid)
418 {
419 ssh_buffer buffer = NULL;
420 uint8_t b = 0;
421 uint16_t w = 0;
422 uint32_t d = 0;
423 uint64_t q = 0;
424 ssh_string s = NULL;
425 char *s1 = NULL, *s2 = NULL;
426 int rc;
427 uint8_t verif[] = "\x42\x13\x37\x0b\xad\xc0\xde\x13\x24\x35\x46"
428 "\xac\xbd\xce\xdf"
429 "\x00\x00\x00\x06" "libssh"
430 "\x00\x00\x00\x05" "rocks"
431 "So much";
432
433 /* Unused */
434 (void) threadid;
435
436 /* Setup */
437 buffer = ssh_buffer_new();
438 if (buffer == NULL) {
439 pthread_exit((void *)-1);
440 }
441 ssh_buffer_set_secure(buffer);
442
443 rc = ssh_buffer_add_data(buffer, verif, sizeof(verif) - 1);
444 assert_int_equal(rc, SSH_OK);
445 rc = ssh_buffer_unpack(buffer,
446 "bwdqSsPb",
447 &b,
448 &w,
449 &d,
450 &q,
451 &s,
452 &s1,
453 (size_t)7,
454 &s2,
455 &b);
456 assert_int_equal(rc, SSH_ERROR);
457
458 assert_null(s);
459 assert_null(s1);
460 assert_null(s2);
461
462 /* Teardown */
463 SSH_BUFFER_FREE(buffer);
464 pthread_exit(NULL);
465 }
466
467 static void torture_ssh_buffer_get_format_error(void **state)
468 {
469 int rc;
470
471 /* Unused */
472 (void) state;
473
474 rc = run_on_threads(thread_ssh_buffer_get_format_error);
475 assert_int_equal(rc, 0);
476 }
477
478 static void *thread_buffer_pack_badformat(void *threadid)
479 {
480 ssh_buffer buffer = NULL;
481 uint8_t b = 42;
482 int rc;
483
484 /* Unused */
485 (void) threadid;
486
487 /* Setup */
488 buffer = ssh_buffer_new();
489 if (buffer == NULL) {
490 pthread_exit((void *)-1);
491 }
492 ssh_buffer_set_secure(buffer);
493
494 /* first with missing format */
495 rc = ssh_buffer_pack(buffer, "b", b, b);
496 assert_int_equal(rc, SSH_ERROR);
497 ssh_buffer_reinit(buffer);
498
499 /* with additional format */
500 rc = ssh_buffer_pack(buffer, "bb", b);
501 /* check that we detect the missing parameter */
502 assert_int_equal(rc, SSH_ERROR);
503
504 /* unpack with missing format */
505 ssh_buffer_reinit(buffer);
506
507 rc = ssh_buffer_pack(buffer, "bb", 42, 43);
508 assert_int_equal(rc, SSH_OK);
509
510 rc = ssh_buffer_unpack(buffer, "b", &b, &b);
511 assert_int_equal(rc, SSH_ERROR);
512
513 /* not doing the test with additional format as
514 * it could crash the process */
515
516 /* Teardown */
517 SSH_BUFFER_FREE(buffer);
518 pthread_exit(NULL);
519 }
520
521 static void torture_buffer_pack_badformat(void **state)
522 {
523 int rc;
524
525 /* Unused */
526 (void) state;
527
528 rc = run_on_threads(thread_buffer_pack_badformat);
529 assert_int_equal(rc, 0);
530 }
531
532 #define NUM_TESTS 8
533
534 static void torture_mixed(void **state)
535 {
536 pthread_t threads[NUM_TESTS][NUM_THREADS];
537 int i;
538 int f;
539 int rc;
540
541 /* Array of functions to run on threads */
542 static void *(*funcs[NUM_TESTS])(void *) = {
543 thread_growing_buffer,
544 thread_growing_buffer_shifting,
545 thread_buffer_prepend,
546 thread_ssh_buffer_get_ssh_string,
547 thread_ssh_buffer_add_format,
548 thread_ssh_buffer_get_format,
549 thread_ssh_buffer_get_format_error,
550 thread_buffer_pack_badformat
551 };
552
553 (void) state;
554
555 /* Call tests in a round-robin fashion */
556 for (i = 0; i < NUM_THREADS; ++i) {
557 for (f = 0; f < NUM_TESTS; f++) {
558 rc = pthread_create(&threads[f][i], NULL, funcs[f], NULL);
559 assert_int_equal(rc, 0);
560 }
561 }
562
563 for (f = 0; f < NUM_TESTS; f++) {
564 for (i = 0; i < NUM_THREADS; ++i) {
565 void *p = NULL;
566 uint64_t *result = NULL;
567
568 rc = pthread_join(threads[f][i], &p);
569 assert_int_equal(rc, 0);
570
571 result = (uint64_t *)p;
572 assert_null(result);
573 }
574 }
575 }
576
577 int torture_run_tests(void)
578 {
579 int rc;
580 struct CMUnitTest tests[] = {
581 cmocka_unit_test(torture_growing_buffer),
582 cmocka_unit_test(torture_growing_buffer_shifting),
583 cmocka_unit_test(torture_buffer_prepend),
584 cmocka_unit_test(torture_ssh_buffer_get_ssh_string),
585 cmocka_unit_test(torture_ssh_buffer_add_format),
586 cmocka_unit_test(torture_ssh_buffer_get_format),
587 cmocka_unit_test(torture_ssh_buffer_get_format_error),
588 cmocka_unit_test(torture_buffer_pack_badformat),
589 cmocka_unit_test(torture_mixed),
590 };
591
592 /*
593 * If the library is statically linked, ssh_init() is not called
594 * automatically
595 */
596 ssh_init();
597 torture_filter_tests(tests);
598 rc = cmocka_run_group_tests(tests, NULL, NULL);
599 ssh_finalize();
600
601 return rc;
602 }