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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*++
/* NAME
/* dict_sqlite_test 1t
/* SUMMARY
/* dict_sqlite unit test
/* SYNOPSIS
/* ./dict_sqlite_test
/* DESCRIPTION
/* dict_sqlite_test runs and logs each configured test, reports if
/* a test is a PASS or FAIL, and returns an exit status of zero if
/* all tests are a PASS.
/*
/* Each test creates a temporary test database and a corresponding
/* Postfix sqlite client configuration file, both having unique
/* names. Otherwise, each test is hermetic.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema porcupine.org
/*--*/
/*
* System library.
*/
#include <sys_defs.h>
#include <stdlib.h>
#include <string.h>
/*
* Utility library.
*/
#include <msg.h>
#include <msg_vstream.h>
#include <stringops.h>
/*
* Global library.
*/
#include <dict_sqlite.h>
/*
* TODO(wietse) make this a proper VSTREAM interface or test helper API.
*/
/* vstream_swap - capture output for testing */
static void vstream_swap(VSTREAM *one, VSTREAM *two)
{
VSTREAM save;
save = *one;
*one = *two;
*two = save;
}
/*
* Override the printable.c module because it may break some tests.
*
* TODO(wietse) move this to a fake_printable.c module that can override all
* printable.c global symbols.
*/
int util_utf8_enable;
char *printable_except(char *string, int replacement, const char *except)
{
return (string);
}
/*
* Scaffolding for dict_sqlite(3) tests.
*/
/* create_and_populate_db - create an empty database and optionally populate */
static void create_and_populate_db(char *dbpath, const char *commands)
{
int fd;
/*
* Create an empty database file with a unique name. Assume that an
* adversary cannot rename or remove the file.
*/
if ((fd = mkstemp(dbpath)) < 0)
msg_fatal("mkstemp(\"%s\"): %m", dbpath);
if (close(fd) < 0)
msg_fatal("close %s: %m", dbpath);
/*
* TODO(wietse) Open the database file, prepare and execute commands to
* populate the database, and close the database.
*/
if (commands) {
msg_fatal("commands are not yet supported");
}
}
/* create_and_populate_cf - create sqlite_table(5) configuration file */
static void create_and_populate_cf(char *cfpath, const char *dbpath,
const char *cftext)
{
int fd;
VSTREAM *fp;
/*
* Create an empty sqlite_table(5) configuration file with a unique name.
* Assume that an adversary cannot rename or remove the file.
*/
if ((fd = mkstemp(cfpath)) < 0)
msg_fatal("mkstemp(\"%s\"): %m", cfpath);
if ((fp = vstream_fdopen(fd, O_WRONLY)) == 0)
msg_fatal("vstream_fdopen: %m");
(void) vstream_fprintf(fp, "%s\ndbpath = %s\n", cftext, dbpath);
if (vstream_fclose(fp) != 0)
msg_fatal("vstream_fdclose: %m");
}
/*
* Test structure. Some tests may come their own.
*/
typedef struct TEST_CASE {
const char *label;
int (*action) (const struct TEST_CASE *);
const char *commands; /* commands or null */
const char *settings; /* sqlite_table(5) */
const char *exp_warning; /* substring match or null */
} TEST_CASE;
#define PASS (0)
#define FAIL (1)
#define PATH_TEMPLATE "/tmp/test-XXXXXXX"
/* test_flag_non_recommended_query - flag non-recommended query payloads */
static int test_flag_non_recommended_query(const TEST_CASE *tp)
{
static VSTRING *msg_buf;
VSTREAM *memory_stream;
const char template[] = PATH_TEMPLATE;
char dbpath[sizeof(template)];
char cfpath[sizeof(template)];
DICT *dict;
if (msg_buf == 0)
msg_buf = vstring_alloc(100);
/* Prepare scaffolding database and configuration files. */
memcpy(dbpath, template, sizeof(dbpath));
create_and_populate_db(dbpath, tp->commands);
memcpy(cfpath, template, sizeof(cfpath));
create_and_populate_cf(cfpath, dbpath, tp->settings);
/* Run the test with custom STDERR stream. */
VSTRING_RESET(msg_buf);
VSTRING_TERMINATE(msg_buf);
if ((memory_stream = vstream_memopen(msg_buf, O_WRONLY)) == 0)
msg_fatal("open memory stream: %m");
vstream_swap(VSTREAM_ERR, memory_stream);
if ((dict = dict_sqlite_open(cfpath, O_RDONLY, DICT_FLAG_UTF8_REQUEST)) != 0)
dict_close(dict);
vstream_swap(memory_stream, VSTREAM_ERR);
if (vstream_fclose(memory_stream))
msg_fatal("close memory stream: %m");
/* Cleanup scaffolding database and configuration files. */
if (unlink(dbpath) < 0)
msg_fatal("unlink %s: %m", dbpath);
if (unlink(cfpath) < 0)
msg_fatal("unlink %s: %m", cfpath);
/* Verify the results. */
if (tp->exp_warning == 0 && VSTRING_LEN(msg_buf) > 0) {
msg_warn("got warning ``%s'', want ``null''", vstring_str(msg_buf));
return (FAIL);
}
if (tp->exp_warning != 0
&& strstr(vstring_str(msg_buf), tp->exp_warning) == 0) {
msg_warn("got warning ``%s'', want ``%s''",
vstring_str(msg_buf), tp->exp_warning);
return (FAIL);
}
return (PASS);
}
/*
* The list of test cases.
*/
static const TEST_CASE test_cases[] = {
/*
* Tests to flag non-recommended query forms. These create an empty test
* database, and open it with the dict_sqlite client without querying it.
*/
{.label = "no_dynamic_payload",
.action = test_flag_non_recommended_query,
.settings = "query = select a from b where c = 5",
},
{.label = "dynamic_payload_inside_recommended_quotes",
.action = test_flag_non_recommended_query,
.settings = "query = select a from b where c = 'xx%syy'",
},
{.label = "dynamic_payload_without_quotes",
.action = test_flag_non_recommended_query,
.settings = "query = select s from b where c = xx%syy",
.exp_warning = "contains >%s< without the recommended '' quotes",
},
{.label = "payload_inside_double_quotes",
.action = test_flag_non_recommended_query,
.settings = "query = select s from b where c = \"xx%syy\"",
.exp_warning = "contains >%s< without the recommended '' quotes",
},
/*
* TODO: Tests that actually populate a test database, and that query it
* with the dict_sqlite client.
*/
{0},
};
int main(int argc, char **argv)
{
const TEST_CASE *tp;
int pass = 0;
int fail = 0;
msg_vstream_init(sane_basename((VSTRING *) 0, argv[0]), VSTREAM_ERR);
for (tp = test_cases; tp->label != 0; tp++) {
int test_failed;
msg_info("RUN %s", tp->label);
test_failed = tp->action(tp);
if (test_failed) {
msg_info("FAIL %s", tp->label);
fail++;
} else {
msg_info("PASS %s", tp->label);
pass++;
}
}
msg_info("PASS=%d FAIL=%d", pass, fail);
exit(fail != 0);
}
|