fix crash from Conn use after free and double-free
[beanstalkd.git] / util-test.c
blobeb52934bb55bd14c5c9aaa4fdd99a4a58063ca3f
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <unistd.h>
7 #include "ct/ct.h"
8 #include "dat.h"
10 void
11 cttestallocf()
13 char *got;
15 got = fmtalloc("hello, %s %d", "world", 5);
16 assertf(strcmp("hello, world 5", got) == 0, "got \"%s\"", got);
20 void
21 cttestoptnone()
23 char *args[] = {
24 NULL,
27 optparse(&srv, args);
28 assert(strcmp(srv.port, Portdef) == 0);
29 assert(srv.addr == NULL);
30 assert(job_data_size_limit == JOB_DATA_SIZE_LIMIT_DEFAULT);
31 assert(srv.wal.filesize == Filesizedef);
32 assert(srv.wal.nocomp == 0);
33 assert(srv.wal.wantsync == 0);
34 assert(srv.user == NULL);
35 assert(srv.wal.dir == NULL);
36 assert(srv.wal.use == 0);
37 assert(verbose == 0);
41 static void
42 success(void)
44 _exit(0);
48 void
49 cttestoptminus()
51 char *args[] = {
52 "-",
53 NULL,
56 atexit(success);
57 optparse(&srv, args);
58 assertf(0, "optparse failed to call exit");
62 void
63 cttestoptp()
65 char *args[] = {
66 "-p1234",
67 NULL,
70 optparse(&srv, args);
71 assert(strcmp(srv.port, "1234") == 0);
75 void
76 cttestoptl()
78 char *args[] = {
79 "-llocalhost",
80 NULL,
83 optparse(&srv, args);
84 assert(strcmp(srv.addr, "localhost") == 0);
88 void
89 cttestoptlseparate()
91 char *args[] = {
92 "-l",
93 "localhost",
94 NULL,
97 optparse(&srv, args);
98 assert(strcmp(srv.addr, "localhost") == 0);
102 void
103 cttestoptz()
105 char *args[] = {
106 "-z1234",
107 NULL,
110 optparse(&srv, args);
111 assert(job_data_size_limit == 1234);
115 void
116 cttestopts()
118 char *args[] = {
119 "-s1234",
120 NULL,
123 optparse(&srv, args);
124 assert(srv.wal.filesize == 1234);
128 void
129 cttestoptc()
131 char *args[] = {
132 "-n",
133 "-c",
134 NULL,
137 optparse(&srv, args);
138 assert(srv.wal.nocomp == 0);
142 void
143 cttestoptn()
145 char *args[] = {
146 "-n",
147 NULL,
150 optparse(&srv, args);
151 assert(srv.wal.nocomp == 1);
155 void
156 cttestoptf()
158 char *args[] = {
159 "-f1234",
160 NULL,
163 optparse(&srv, args);
164 assert(srv.wal.syncrate == 1234000000);
165 assert(srv.wal.wantsync == 1);
169 void
170 cttestoptF()
172 char *args[] = {
173 "-f1234",
174 "-F",
175 NULL,
178 optparse(&srv, args);
179 assert(srv.wal.wantsync == 0);
183 void
184 cttestoptu()
186 char *args[] = {
187 "-ukr",
188 NULL,
191 optparse(&srv, args);
192 assert(strcmp(srv.user, "kr") == 0);
196 void
197 cttestoptb()
199 char *args[] = {
200 "-bfoo",
201 NULL,
204 optparse(&srv, args);
205 assert(strcmp(srv.wal.dir, "foo") == 0);
206 assert(srv.wal.use == 1);
210 void
211 cttestoptV()
213 char *args[] = {
214 "-V",
215 NULL,
218 optparse(&srv, args);
219 assert(verbose == 1);
223 void
224 cttestoptV_V()
226 char *args[] = {
227 "-V",
228 "-V",
229 NULL,
232 optparse(&srv, args);
233 assert(verbose == 2);
237 void
238 cttestoptVVV()
240 char *args[] = {
241 "-VVV",
242 NULL,
245 optparse(&srv, args);
246 assert(verbose == 3);
250 void
251 cttestoptVnVu()
253 char *args[] = {
254 "-VnVukr",
255 NULL,
258 optparse(&srv, args);
259 assert(verbose == 2);
260 assert(srv.wal.nocomp == 1);
261 assert(strcmp(srv.user, "kr") == 0);