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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/profile/t_profile.c - profile library regression tests */
/*
* Copyright (C) 2021 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "profile.h"
static void
check(long code)
{
assert(code == 0);
}
static void
check_fail(long code, long expected)
{
assert(code == expected);
}
static void
write_file(const char *name, int nlines, ...)
{
FILE *f;
va_list ap;
int i;
(void)unlink(name);
f = fopen(name, "w");
assert(f != NULL);
va_start(ap, nlines);
for (i = 0; i < nlines; i++)
fprintf(f, "%s\n", va_arg(ap, char *));
va_end(ap);
fclose(f);
}
/* Regression test for #2685 (profile iterator breaks when modifications
* made) */
static void
test_iterate(void)
{
profile_t p;
void *iter;
const char *names[] = { "test section 1", "child_section", "child", NULL };
const char *values[] = { "slick", "harry", "john", NULL };
char *name, *value;
int i;
check(profile_init_path("test2.ini", &p));
/* Iterate and check for the expected values. */
check(profile_iterator_create(p, names, 0, &iter));
for (i = 0;; i++) {
check(profile_iterator(&iter, &name, &value));
if (name == NULL && value == NULL)
break;
assert(strcmp(name, names[2]) == 0);
assert(values[i] != NULL);
assert(strcmp(value, values[i]) == 0);
profile_release_string(name);
profile_release_string(value);
}
assert(values[i] == NULL);
profile_iterator_free(&iter);
/* Iterate again, deleting each value as we go. Flush the result to a
* separate file. */
check(profile_iterator_create(p, names, 0, &iter));
for (;;) {
check(profile_iterator(&iter, NULL, &value));
if (value == NULL)
break;
check(profile_update_relation(p, names, value, NULL));
profile_release_string(value);
}
profile_iterator_free(&iter);
(void)unlink("test3.ini");
profile_flush_to_file(p, "test3.ini");
profile_abandon(p);
/* Check that no values for the section are found in the resulting file. */
check(profile_init_path("test3.ini", &p));
check(profile_iterator_create(p, names, 0, &iter));
check(profile_iterator(&iter, &name, &value));
assert(name == NULL && value == NULL);
profile_iterator_free(&iter);
profile_abandon(p);
}
/*
* Regression test for a 1.4-era bug where updating the underlying file data of
* a profile object lost track of the flag indicating that it was part of the
* global shared profiles list.
*/
static void
test_shared(void)
{
profile_t a, b;
struct utimbuf times;
system("cp test2.ini test3.ini");
/* Create an entry in the shared table. */
check(profile_init_path("test3.ini", &a));
/*
* Force an update of the underlying data. With the bug present, the
* shared flag is erroneously cleared. The easiest way to force an update
* is to reopen the file (since we don't enforce the one-stat-per-second
* limit during open) after changing the timestamp.
*/
times.actime = time(NULL) + 2;
times.modtime = times.actime;
utime("test3.ini", ×);
check(profile_init_path("test3.ini", &b));
profile_release(b);
/* Release the profile. With the bug present, a dangling reference is left
* behind in the shared table. */
profile_release(a);
/* Open the profile again to dereference the dangling pointer if one was
* created. */
check(profile_init_path("test3.ini", &a));
profile_release(a);
}
/* Regression test for #2950 (profile_clear_relation not reflected within
* handle where deletion is performed) */
static void
test_clear(void)
{
profile_t p;
const char *names[] = { "test section 1", "quux", NULL };
char **values, **dummy;
check(profile_init_path("test2.ini", &p));
check(profile_get_values(p, names, &values));
check(profile_clear_relation(p, names));
check_fail(profile_get_values(p, names, &dummy), PROF_NO_RELATION);
check(profile_add_relation(p, names, values[0]));
profile_free_list(values);
check(profile_get_values(p, names, &values));
assert(values[0] != NULL && values[1] == NULL);
profile_free_list(values);
profile_abandon(p);
}
static void
test_include(void)
{
profile_t p;
const char *names[] = { "test section 1", "bar", NULL };
char **values;
/* Test expected error code when including nonexistent file. */
write_file("testinc.ini", 1, "include does-not-exist");
check_fail(profile_init_path("testinc.ini", &p), PROF_FAIL_INCLUDE_FILE);
/* Test expected error code when including nonexistent directory. */
write_file("testinc.ini", 1, "includedir does-not-exist");
check_fail(profile_init_path("testinc.ini", &p), PROF_FAIL_INCLUDE_DIR);
/* Test including a file. */
write_file("testinc.ini", 1, "include test2.ini");
check(profile_init_path("testinc.ini", &p));
check(profile_get_values(p, names, &values));
assert(strcmp(values[0], "foo") == 0 && values[1] == NULL);
profile_free_list(values);
profile_release(p);
/*
* Test including a directory. Put four copies of test2.ini inside the
* directory, two with invalid names. Check that we get two values for one
* of the variables.
*/
system("rm -rf test_include_dir");
system("mkdir test_include_dir");
system("cp test2.ini test_include_dir/a");
system("cp test2.ini test_include_dir/a~");
system("cp test2.ini test_include_dir/b.conf");
system("cp test2.ini test_include_dir/b.conf.rpmsave");
write_file("testinc.ini", 1, "includedir test_include_dir");
check(profile_init_path("testinc.ini", &p));
check(profile_get_values(p, names, &values));
assert(strcmp(values[0], "foo") == 0);
assert(strcmp(values[1], "foo") == 0);
assert(values[2] == NULL);
profile_free_list(values);
profile_release(p);
/* Directly list the directory in the profile path and try again. */
check(profile_init_path("test_include_dir", &p));
check(profile_get_values(p, names, &values));
assert(strcmp(values[0], "foo") == 0);
assert(strcmp(values[1], "foo") == 0);
assert(values[2] == NULL);
profile_free_list(values);
profile_release(p);
}
/* Test syntactic independence of included profile files. */
static void
test_independence(void)
{
profile_t p;
const char *names1[] = { "sec1", "var", "a", NULL };
const char *names2[] = { "sec2", "b", NULL };
const char *names3[] = { "sec1", "var", "c", NULL };
char **values;
write_file("testinc.ini", 6, "[sec1]", "var = {", "a = 1",
"include testinc2.ini", "c = 3", "}");
write_file("testinc2.ini", 2, "[sec2]", "b = 2");
check(profile_init_path("testinc.ini", &p));
check(profile_get_values(p, names1, &values));
assert(strcmp(values[0], "1") == 0 && values[1] == NULL);
profile_free_list(values);
check(profile_get_values(p, names2, &values));
assert(strcmp(values[0], "2") == 0 && values[1] == NULL);
profile_free_list(values);
check(profile_get_values(p, names3, &values));
assert(strcmp(values[0], "3") == 0 && values[1] == NULL);
profile_free_list(values);
profile_release(p);
}
/* Regression test for #7971 (deleted sections should not be iterable) */
static void
test_delete_section(void)
{
profile_t p;
const char *sect[] = { "test section 1", NULL };
const char *newrel[] = { "test section 1", "testkey", NULL };
const char *oldrel[] = { "test section 1", "child", NULL };
char **values;
check(profile_init_path("test2.ini", &p));
/* Remove and replace a section. */
check(profile_rename_section(p, sect, NULL));
check(profile_add_relation(p, sect, NULL));
check(profile_add_relation(p, newrel, "6"));
/* Check that we can read the new relation but not the old one. */
check(profile_get_values(p, newrel, &values));
assert(strcmp(values[0], "6") == 0 && values[1] == NULL);
profile_free_list(values);
check_fail(profile_get_values(p, oldrel, &values), PROF_NO_RELATION);
profile_abandon(p);
}
/* Regression test for #7971 (profile_clear_relation() error with deleted node
* at end of value set) */
static void
test_delete_clear_relation(void)
{
profile_t p;
const char *names[] = { "test section 1", "testkey", NULL };
check(profile_init_path("test2.ini", &p));
check(profile_add_relation(p, names, "1"));
check(profile_add_relation(p, names, "2"));
check(profile_update_relation(p, names, "2", NULL));
check(profile_clear_relation(p, names));
profile_abandon(p);
}
/* Test that order of relations is preserved if some relations are deleted. */
static void
test_delete_ordering(void)
{
profile_t p;
const char *names[] = { "test section 1", "testkey", NULL };
char **values;
check(profile_init_path("test2.ini", &p));
check(profile_add_relation(p, names, "1"));
check(profile_add_relation(p, names, "2"));
check(profile_add_relation(p, names, "3"));
check(profile_update_relation(p, names, "2", NULL));
check(profile_add_relation(p, names, "4"));
check(profile_get_values(p, names, &values));
assert(strcmp(values[0], "1") == 0);
assert(strcmp(values[1], "3") == 0);
assert(strcmp(values[2], "4") == 0);
assert(values[3] == NULL);
profile_free_list(values);
profile_abandon(p);
}
/* Regression test for #8431 (profile_flush_to_file erroneously changes flag
* state on source object) */
static void
test_flush_to_file(void)
{
profile_t p;
/* Flush a profile object to a file without making any changes, so that the
* source object is still within g_shared_trees. */
check(profile_init_path("test2.ini", &p));
unlink("test3.ini");
check(profile_flush_to_file(p, "test3.ini"));
profile_release(p);
/* Check for a dangling reference in g_shared_trees by creating another
* profile object. */
profile_init_path("test2.ini", &p);
profile_release(p);
}
/* Regression test for #7863 (multiply-specified subsections should
* be merged) */
static void
test_merge_subsections(void)
{
profile_t p;
const char *n1[] = { "test section 2", "child_section2", "child", NULL };
const char *n2[] = { "test section 2", "child_section2", "chores", NULL };
char **values;
check(profile_init_path("test2.ini", &p));
check(profile_get_values(p, n1, &values));
assert(strcmp(values[0], "slick") == 0);
assert(strcmp(values[1], "harry") == 0);
assert(strcmp(values[2], "john\tb ") == 0);
assert(strcmp(values[3], "ron") == 0);
assert(values[4] == NULL);
profile_free_list(values);
check(profile_get_values(p, n2, &values));
assert(strcmp(values[0], "cleaning") == 0 && values[1] == NULL);
profile_free_list(values);
profile_release(p);
}
int
main(void)
{
test_iterate();
test_shared();
test_clear();
test_include();
test_independence();
test_delete_section();
test_delete_clear_relation();
test_delete_ordering();
test_flush_to_file();
test_merge_subsections();
}
|