Mercurial > dovecot > core-2.2
annotate src/lib/test-data-stack.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | 2e2563132d5f |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21318
diff
changeset
|
1 /* Copyright (c) 2014-2017 Dovecot authors, see the included COPYING file */ |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
2 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
3 #include "test-lib.h" |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
4 #include "data-stack.h" |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
5 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
6 static void test_ds_buffers(void) |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
7 { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
8 test_begin("data-stack buffer growth"); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
9 T_BEGIN { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
10 size_t i; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
11 unsigned char *p; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
12 size_t left = t_get_bytes_available(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
13 while (left < 10000) { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
14 t_malloc(left); /* force a new block */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
15 left = t_get_bytes_available(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
16 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
17 left -= 64; /* make room for the sentry if DEBUG */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
18 p = t_buffer_get(1); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
19 p[0] = 1; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
20 for (i = 2; i <= left; i++) { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
21 /* grow it */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
22 unsigned char *p2 = t_buffer_get(i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
23 test_assert_idx(p == p2, i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
24 p[i-1] = i; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
25 test_assert_idx(p[i-2] == (unsigned char)(i-1), i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
26 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
27 /* now fix it permanently */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
28 t_buffer_alloc_last_full(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
29 test_assert(t_get_bytes_available() < 64 + MEM_ALIGN(1)); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
30 } T_END; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
31 test_end(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
32 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
33 test_begin("data-stack buffer interruption"); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
34 T_BEGIN { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
35 void *b = t_buffer_get(1000); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
36 void *a = t_malloc(1); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
37 void *b2 = t_buffer_get(1001); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
38 test_assert(a == b); /* expected, not guaranteed */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
39 test_assert(b2 != b); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
40 } T_END; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
41 test_end(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
42 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
43 test_begin("data-stack buffer with reallocs"); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
44 T_BEGIN { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
45 size_t bigleft = t_get_bytes_available(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
46 size_t i; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
47 for (i = 1; i < bigleft-64; i += rand()%32) T_BEGIN { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
48 unsigned char *p, *p2; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
49 size_t left; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
50 t_malloc(i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
51 left = t_get_bytes_available(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
52 /* The most useful idx for the assert is 'left' */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
53 test_assert_idx(left <= bigleft-i, left); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
54 p = t_buffer_get(left/2); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
55 p[0] = 'Z'; p[left/2 - 1] = 'Z'; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
56 p2 = t_buffer_get(left + left/2); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
57 test_assert_idx(p != p2, left); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
58 test_assert_idx(p[0] == 'Z', left); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
59 test_assert_idx(p[left/2 -1] == 'Z', left); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
60 } T_END; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
61 } T_END; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
62 test_end(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
63 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
64 |
17651
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
65 static void test_ds_realloc() |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
66 { |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
67 test_begin("data-stack realloc"); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
68 T_BEGIN { |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
69 size_t i; |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
70 unsigned char *p; |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
71 size_t left = t_get_bytes_available(); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
72 while (left < 10000) { |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
73 t_malloc(left); /* force a new block */ |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
74 left = t_get_bytes_available(); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
75 } |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
76 left -= 64; /* make room for the sentry if DEBUG */ |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
77 p = t_malloc(1); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
78 p[0] = 1; |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
79 for (i = 2; i <= left; i++) { |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
80 /* grow it */ |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
81 test_assert_idx(t_try_realloc(p, i), i); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
82 p[i-1] = i; |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
83 test_assert_idx(p[i-2] == (unsigned char)(i-1), i); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
84 } |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
85 test_assert(t_get_bytes_available() < 64 + MEM_ALIGN(1)); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
86 } T_END; |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
87 test_end(); |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
88 } |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
89 |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
90 static void test_ds_recurse(int depth, int number, size_t size) |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
91 { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
92 int i; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
93 char **ps; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
94 char tag[2] = { depth+1, '\0' }; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
95 int try_fails = 0; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
96 unsigned int t_id = t_push_named("test_ds_recurse[%i]", depth); |
21318
5e70e91e4c78
lib: Remove t_buffer_*_type()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21299
diff
changeset
|
97 ps = t_buffer_get(sizeof(char *) * number); |
18266
ca7441fc784e
lib: Another test_assert() -> i_assert() change to avoid static analyzer warnings.
Timo Sirainen <tss@iki.fi>
parents:
18265
diff
changeset
|
98 i_assert(ps != NULL); |
21318
5e70e91e4c78
lib: Remove t_buffer_*_type()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21299
diff
changeset
|
99 t_buffer_alloc(sizeof(char *) * number); |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
100 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
101 for (i = 0; i < number; i++) { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
102 ps[i] = t_malloc(size/2); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
103 bool re = t_try_realloc(ps[i], size); |
18265
c8aeb0cb4a6e
lib: Replaced two test_assert()s checking for NULLs with i_assert()s
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
104 i_assert(ps[i] != NULL); |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
105 if (!re) { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
106 try_fails++; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
107 ps[i] = t_malloc(size); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
108 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
109 /* drop our own canaries */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
110 memset(ps[i], tag[0], size); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
111 ps[i][size-2] = 0; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
112 } |
17651
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
113 /* Do not expect a high failure rate from t_try_realloc */ |
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
114 test_assert_idx(try_fails <= number / 20, depth); |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
115 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
116 /* Now recurse... */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
117 if(depth>0) |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
118 test_ds_recurse(depth-1, number, size); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
119 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
120 /* Test our canaries are still intact */ |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
121 for (i = 0; i < number; i++) { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
122 test_assert_idx(strspn(ps[i], tag) == size - 2, i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
123 test_assert_idx(ps[i][size-1] == tag[0], i); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
124 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
125 test_assert_idx(t_id == t_pop(), depth); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
126 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
127 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
128 static void test_ds_recursive(int count, int depth) |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
129 { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
130 int i; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
131 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
132 test_begin("data-stack recursive"); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
133 for(i = 0; i < count; i++) T_BEGIN { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
134 int number=rand()%100+50; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
135 int size=rand()%100+50; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
136 test_ds_recurse(depth, number, size); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
137 } T_END; |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
138 test_end(); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
139 } |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
140 |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
141 void test_data_stack(void) |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
142 { |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
143 test_ds_buffers(); |
17651
5d290af2d1da
lib: test-data-stack - add realloc tests
Phil Carmody <phil@dovecot.fi>
parents:
17641
diff
changeset
|
144 test_ds_realloc(); |
17641
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
145 test_ds_recursive(20, 80); |
1778c2e77cfa
lib: test-data-stack - too important a library not to be thrashed hard
Phil Carmody <phil@dovecot.fi>
parents:
diff
changeset
|
146 } |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
147 |
21299
5be9ee567034
lib-test: Change test_fatal_func_t to take unsigned int stage as parameter.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
148 enum fatal_test_state fatal_data_stack(unsigned int stage) |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
149 { |
18872
e7c896eff939
lib: test-data-stack - simplify #if-ing out of DEBUG-only fatal test
Phil Carmody <phil@dovecot.fi>
parents:
18871
diff
changeset
|
150 #ifdef DEBUG |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
151 /* If we abort, then we'll be left with a dangling t_push() |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
152 keep a record of our temporary stack id, so we can clean up. */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
153 static unsigned int t_id = 999999999; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
154 static unsigned char *undo_ptr = NULL; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
155 static unsigned char undo_data; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
156 static bool things_are_messed_up = FALSE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
157 if (stage != 0) { |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
158 /* Presume that we need to clean up from the prior test: |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
159 undo the evil write, then we will be able to t_pop cleanly, |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
160 and finally we can end the test stanza. */ |
18871
69b231fdf5d7
lib: test-data-stack - ensure t_push() and t_pop() are balanced in fatal tests
Phil Carmody <phil@dovecot.fi>
parents:
18266
diff
changeset
|
161 if (things_are_messed_up || undo_ptr == NULL) |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
162 return FATAL_TEST_ABORT; /* abort, things are messed up with t_pop */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
163 *undo_ptr = undo_data; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
164 undo_ptr = NULL; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
165 /* t_pop musn't abort, that would cause recursion */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
166 things_are_messed_up = TRUE; |
18871
69b231fdf5d7
lib: test-data-stack - ensure t_push() and t_pop() are balanced in fatal tests
Phil Carmody <phil@dovecot.fi>
parents:
18266
diff
changeset
|
167 if (t_id != 999999999 && t_pop() != t_id) |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
168 return FATAL_TEST_ABORT; /* abort, things are messed up with us */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
169 things_are_messed_up = FALSE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
170 t_id = 999999999; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
171 test_end(); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
172 } |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
173 |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
174 switch(stage) { |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
175 case 0: { |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
176 unsigned char *p; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
177 test_begin("fatal data-stack underrun"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
178 t_id = t_push_named("fatal_data_stack underrun"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
179 size_t left = t_get_bytes_available(); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
180 p = t_malloc(left-80); /* will fit */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
181 p = t_malloc(100); /* won't fit, will get new block */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
182 int seek = 0; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
183 /* Seek back for the canary, don't assume endianness */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
184 while(seek > -60 && |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
185 ((p[seek+1] != 0xDB) || |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
186 ((p[seek] != 0xBA || p[seek+2] != 0xAD) && |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
187 (p[seek+2] != 0xBA || p[seek] != 0xAD)))) |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
188 seek--; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
189 if (seek <= -60) |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
190 return FATAL_TEST_ABORT; /* abort, couldn't find header */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
191 undo_ptr = p + seek; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
192 undo_data = *undo_ptr; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
193 *undo_ptr = '*'; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
194 /* t_malloc will panic block header corruption */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
195 (void)t_malloc(10); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
196 return FATAL_TEST_FAILURE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
197 } |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
198 |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
199 case 1: case 2: { |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
200 test_begin(stage == 1 ? "fatal t_malloc overrun near" : "fatal t_malloc overrun far"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
201 t_id = t_push_named(stage == 1 ? "fatal t_malloc overrun first" : "fatal t_malloc overrun far"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
202 unsigned char *p = t_malloc(10); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
203 undo_ptr = p + 10 + (stage == 1 ? 0 : 8*4-1); /* presumes sentry size */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
204 undo_data = *undo_ptr; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
205 *undo_ptr = '*'; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
206 /* t_pop will now fail */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
207 (void)t_pop(); |
18871
69b231fdf5d7
lib: test-data-stack - ensure t_push() and t_pop() are balanced in fatal tests
Phil Carmody <phil@dovecot.fi>
parents:
18266
diff
changeset
|
208 t_id = 999999999; /* We're FUBAR, mustn't pop next entry */ |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
209 return FATAL_TEST_FAILURE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
210 } |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
211 |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
212 case 3: case 4: { |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
213 test_begin(stage == 3 ? "fatal t_buffer_get overrun near" : "fatal t_buffer_get overrun far"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
214 t_id = t_push_named(stage == 3 ? "fatal t_buffer overrun near" : "fatal t_buffer_get overrun far"); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
215 unsigned char *p = t_buffer_get(10); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
216 undo_ptr = p + 10 + (stage == 3 ? 0 : 8*4-1); |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
217 undo_data = *undo_ptr; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
218 *undo_ptr = '*'; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
219 /* t_pop will now fail */ |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
220 (void)t_pop(); |
18871
69b231fdf5d7
lib: test-data-stack - ensure t_push() and t_pop() are balanced in fatal tests
Phil Carmody <phil@dovecot.fi>
parents:
18266
diff
changeset
|
221 t_id = 999999999; /* We're FUBAR, mustn't pop next entry */ |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
222 return FATAL_TEST_FAILURE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
223 } |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
224 |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
225 default: |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
226 things_are_messed_up = TRUE; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
227 return FATAL_TEST_FINISHED; |
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
228 } |
18872
e7c896eff939
lib: test-data-stack - simplify #if-ing out of DEBUG-only fatal test
Phil Carmody <phil@dovecot.fi>
parents:
18871
diff
changeset
|
229 #else |
e7c896eff939
lib: test-data-stack - simplify #if-ing out of DEBUG-only fatal test
Phil Carmody <phil@dovecot.fi>
parents:
18871
diff
changeset
|
230 return stage == 0 ? FATAL_TEST_FINISHED : FATAL_TEST_ABORT; |
e7c896eff939
lib: test-data-stack - simplify #if-ing out of DEBUG-only fatal test
Phil Carmody <phil@dovecot.fi>
parents:
18871
diff
changeset
|
231 #endif |
17672
8e990ad4db0e
lib: test-data-stack - add some fatal tests.
Phil Carmody <phil@dovecot.fi>
parents:
17651
diff
changeset
|
232 } |