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
|
#include <ccan/tal/tal.h>
#include <ccan/tal/tal.c>
#include <ccan/tap/tap.h>
static int alloc_count, when_to_fail, err_count;
static bool stealing;
static void *failing_alloc(size_t len)
{
if (alloc_count++ == when_to_fail)
return NULL;
/* once we've failed once, it shouldn't ask again (steal can though). */
assert(stealing || alloc_count <= when_to_fail);
return malloc(len);
}
static void *failing_realloc(void *p, size_t len)
{
if (alloc_count++ == when_to_fail)
return NULL;
return realloc(p, len);
}
static void nofail_on_error(const char *msg)
{
diag("ERROR: %s", msg);
err_count++;
}
static void destroy_p(void *p)
{
}
int main(void)
{
char *p, *c1, *c2;
bool success;
plan_tests(25);
tal_set_backend(failing_alloc, failing_realloc, NULL, nofail_on_error);
/* Fail at each possible point in an allocation. */
when_to_fail = err_count = 0;
do {
alloc_count = 0;
p = tal(NULL, char);
when_to_fail++;
} while (!p);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
/* Do it again. */
when_to_fail = err_count = 0;
do {
alloc_count = 0;
c1 = tal(p, char);
when_to_fail++;
} while (!c1);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
/* Now during resize. */
c2 = c1;
when_to_fail = err_count = 0;
for (;;) {
alloc_count = 0;
if (tal_resize(&c1, 100))
break;
/* Failing alloc will not change pointer. */
ok1(c1 == c2);
when_to_fail++;
};
ok1(alloc_count == 1);
ok1(when_to_fail == 1);
ok1(err_count == 1);
/* Make sure it's really resized. */
memset(c1, 1, 100);
/* Now for second child. */
when_to_fail = err_count = 0;
do {
alloc_count = 0;
c2 = tal(p, char);
when_to_fail++;
} while (!c2);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
/* Note: adding a child will fall through if group alloc fails. */
ok1 (err_count == when_to_fail - 1 || err_count == when_to_fail);
/* Now while adding a destructor. */
when_to_fail = err_count = 0;
do {
alloc_count = 0;
success = tal_add_destructor(p, destroy_p);
when_to_fail++;
} while (!success);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
/* Now while adding a name. */
when_to_fail = err_count = 0;
do {
const char name[] = "some name";
alloc_count = 0;
success = tal_set_name(p, name);
when_to_fail++;
} while (!success);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
/* Now while stealing. */
stealing = true;
when_to_fail = err_count = 0;
do {
alloc_count = 0;
success = tal_steal(c2, c1) != NULL;
when_to_fail++;
} while (!success);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
/* Now stealing with more children (more coverage). */
when_to_fail = 1000;
(void)tal(p, char);
c1 = tal(p, char);
c2 = tal(p, char);
(void)tal(p, char);
/* Now steal again. */
when_to_fail = err_count = 0;
do {
alloc_count = 0;
success = tal_steal(c2, c1) != NULL;
when_to_fail++;
} while (!success);
ok1(alloc_count >= 1);
ok1(when_to_fail > 1);
ok1(err_count == when_to_fail - 1);
tal_free(p);
tal_cleanup();
return exit_status();
}
|