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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
* Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
* Copyright (C) 2008 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <expat.h>
#include "razor.h"
#define XML_BUFFER_SIZE 4096
static void
parse_xml_file(const char *filename,
XML_StartElementHandler start,
XML_EndElementHandler end,
void *data)
{
XML_Parser parser;
char *buffer;
int fd, len, err;
parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start, end);
XML_SetUserData(parser, data);
fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "failed to open %s: %m\n", filename);
exit(-1);
}
while (1) {
buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
len = read(fd, buffer, XML_BUFFER_SIZE);
if (len == 0)
break;
err = XML_ParseBuffer(parser, len, len == 0);
if (err == XML_STATUS_ERROR) {
fprintf(stderr, "parse error at line %lu:\n%s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
exit(-1);
}
}
if (fd < 0) {
fprintf(stderr, "read: %m\n");
exit(-1);
}
close(fd);
}
struct test_context {
struct razor_set *system_set, *repo_set, *result_set;
struct razor_importer *importer;
struct razor_set **importer_set;
struct razor_transaction *trans;
char *install_pkgs[3], *remove_pkgs[3];
int n_install_pkgs, n_remove_pkgs;
int unsat;
int in_result;
int debug, errors;
};
static void
get_atts(const char **atts, ...)
{
va_list ap;
const char *name, **ptr;
int i;
va_start(ap, atts);
while (name = va_arg(ap, const char *), name != NULL) {
ptr = va_arg(ap, const char **);
*ptr = NULL;
for (i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], name) == 0)
*ptr = atts[i + 1];
}
}
va_end(ap);
}
static enum razor_property_flags
parse_relation (const char *rel_str)
{
if (!rel_str)
return -1;
if (rel_str[0] == 'L')
return rel_str[1] == 'E' ? RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_LESS;
else if (rel_str[0] == 'G')
return rel_str[1] == 'E' ? RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_GREATER;
else if (rel_str[0] == 'E' || rel_str[1] == 'Q')
return RAZOR_PROPERTY_EQUAL;
else
return -1;
}
static void
start_test(struct test_context *ctx, const char **atts)
{
const char *name = NULL;
get_atts(atts, "name", &name, NULL);
if (!name) {
fprintf(stderr, "Test with no name\n");
exit(1);
}
printf("%s\n", name);
}
static void
end_test(struct test_context *ctx)
{
if (ctx->system_set) {
razor_set_destroy(ctx->system_set);
ctx->system_set = NULL;
}
if (ctx->repo_set) {
razor_set_destroy(ctx->repo_set);
ctx->repo_set = NULL;
}
if (ctx->result_set) {
razor_set_destroy(ctx->result_set);
ctx->result_set = NULL;
}
if (ctx->trans) {
razor_transaction_destroy(ctx->trans);
ctx->trans = NULL;
}
}
static void
start_set(struct test_context *ctx, const char **atts)
{
const char *name = NULL;
ctx->importer = razor_importer_create();
get_atts(atts, "name", &name, NULL);
if (!name)
ctx->importer_set = &ctx->result_set;
else if (!strcmp(name, "system"))
ctx->importer_set = &ctx->system_set;
else if (!strcmp(name, "repo"))
ctx->importer_set = &ctx->repo_set;
else {
fprintf(stderr, " bad set name '%s'\n", name);
exit(1);
}
}
static void
end_set(struct test_context *ctx)
{
*ctx->importer_set = razor_importer_finish(ctx->importer);
ctx->importer = NULL;
}
static void
start_package(struct test_context *ctx, const char **atts)
{
const char *name = NULL, *version = NULL, *arch = NULL;
get_atts(atts, "name", &name,
"version", &version,
"arch", &arch,
NULL);
if (!name) {
fprintf(stderr, " package with no name\n");
exit(1);
}
razor_importer_begin_package(ctx->importer, name, version, arch);
razor_importer_add_property(ctx->importer, name,
RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_PROVIDES,
version);
}
static void
end_package(struct test_context *ctx)
{
razor_importer_finish_package(ctx->importer);
}
static void
add_property(struct test_context *ctx, enum razor_property_flags type, const char *name, enum razor_property_flags rel, const char *version)
{
razor_importer_add_property(ctx->importer, name,
rel | type, version);
}
static const char*
razor_property_flags_relation_to_string(enum razor_property_flags rel)
{
if (rel == RAZOR_PROPERTY_LESS)
return "<";
if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_LESS))
return "<=";
if (rel == RAZOR_PROPERTY_EQUAL)
return "=";
if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_GREATER))
return ">=";
if (rel == RAZOR_PROPERTY_GREATER)
return ">";
return "";
}
static void
check_unsatisfiable_property(struct test_context *ctx,
enum razor_property_flags type,
const char *name,
enum razor_property_flags rel,
const char *version)
{
if (!version)
version = "";
if (razor_transaction_unsatisfied_property(ctx->trans,
name, rel | type, version))
return;
fprintf(stderr, " didn't get unsatisfiable '%s %s %s'\n",
name, razor_property_flags_relation_to_string(rel), version);
ctx->errors++;
}
static void
start_property(struct test_context *ctx, enum razor_property_flags type, const char **atts)
{
const char *name = NULL, *rel_str = NULL, *version = NULL;
enum razor_property_flags rel;
get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
if (name == NULL) {
fprintf(stderr, " no name specified for property\n");
exit(1);
}
if (version) {
rel = parse_relation(rel_str);
if (rel == -1) {
fprintf(stderr, " bad or missing version relation for property %s\n", name);
exit(1);
}
} else
rel = RAZOR_PROPERTY_EQUAL;
if (ctx->unsat)
check_unsatisfiable_property(ctx, type, name, rel, version);
else
add_property(ctx, type, name, rel, version);
}
static void
start_transaction(struct test_context *ctx, const char **atts)
{
ctx->n_install_pkgs = 0;
ctx->n_remove_pkgs = 0;
}
static struct razor_package *
get_package(struct razor_set *set, const char *package)
{
struct razor_package_iterator *pi;
struct razor_package *p;
const char *name, *version, *arch;
pi = razor_package_iterator_create(set);
while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_NAME, &name,
RAZOR_DETAIL_VERSION, &version,
RAZOR_DETAIL_ARCH, &arch,
RAZOR_DETAIL_LAST)) {
if (strcmp(package, name) == 0)
break;
}
razor_package_iterator_destroy(pi);
return p;
}
static void
end_transaction(struct test_context *ctx)
{
struct razor_package *pkg;
int errors, i;
ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set);
for (i = 0; i < ctx->n_install_pkgs; i++) {
pkg = get_package(ctx->repo_set, ctx->install_pkgs[i]);
razor_transaction_install_package(ctx->trans, pkg);
}
for (i = 0; i < ctx->n_remove_pkgs; i++) {
pkg = get_package(ctx->system_set, ctx->remove_pkgs[i]);
if (!pkg)
pkg = get_package(ctx->repo_set, ctx->remove_pkgs[i]);
razor_transaction_remove_package(ctx->trans, pkg);
}
razor_transaction_resolve(ctx->trans);
errors = razor_transaction_describe(ctx->trans);
printf("\n");
while (ctx->n_install_pkgs--)
free(ctx->install_pkgs[ctx->n_install_pkgs]);
while (ctx->n_remove_pkgs--)
free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
if (!errors) {
struct razor_set *new;
new = razor_transaction_finish(ctx->trans);
ctx->trans = NULL;
ctx->system_set = new;
}
}
static void
start_install_or_update(struct test_context *ctx, const char **atts)
{
const char *name = NULL;
get_atts(atts, "name", &name, NULL);
if (!name) {
fprintf(stderr, " install/update with no name\n");
exit(1);
}
ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
}
static void
start_remove(struct test_context *ctx, const char **atts)
{
const char *name = NULL;
get_atts(atts, "name", &name, NULL);
if (!name) {
fprintf(stderr, " remove with no name\n");
exit(1);
}
ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
}
static void
start_result(struct test_context *ctx, const char **atts)
{
ctx->in_result = 1;
}
static void
diff_callback(enum razor_diff_action action,
struct razor_package *package,
const char *name,
const char *version,
const char *arch,
void *data)
{
struct test_context *ctx = data;
ctx->errors++;
if (action == RAZOR_DIFF_ACTION_REMOVE) {
fprintf(stderr, " result set should not contain %s %s\n",
name, version);
} else {
fprintf(stderr, " result set should contain %s %s\n",
name, version);
}
}
static void
end_result(struct test_context *ctx)
{
ctx->in_result = 0;
if (ctx->result_set) {
if (!ctx->system_set)
ctx->system_set = razor_set_create();
razor_set_diff(ctx->system_set, ctx->result_set,
diff_callback, ctx);
}
}
static void
start_unsatisfiable(struct test_context *ctx, const char **atts)
{
if (ctx->result_set) {
fprintf(stderr, "Expected to fail, but didn't\n");
exit(1);
}
ctx->unsat = 1;
}
static void
end_unsatisfiable(struct test_context *ctx)
{
ctx->unsat = 0;
}
static void
start_test_element(void *data, const char *element, const char **atts)
{
struct test_context *ctx = data;
if (strcmp(element, "tests") == 0) {
;
} else if (strcmp(element, "test") == 0) {
start_test(ctx, atts);
} else if (strcmp(element, "set") == 0) {
start_set(ctx, atts);
} else if (strcmp(element, "transaction") == 0) {
start_transaction(ctx, atts);
} else if (strcmp(element, "install") == 0) {
start_install_or_update(ctx, atts);
} else if (strcmp(element, "install") == 0) {
start_install_or_update(ctx, atts);
} else if (strcmp(element, "remove") == 0) {
start_remove(ctx, atts);
} else if (strcmp(element, "result") == 0) {
start_result(ctx, atts);
} else if (strcmp(element, "unsatisfiable") == 0) {
start_unsatisfiable(ctx, atts);
} else if (strcmp(element, "package") == 0) {
start_package(ctx, atts);
} else if (strcmp(element, "requires") == 0) {
start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
} else if (strcmp(element, "provides") == 0) {
start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
} else if (strcmp(element, "conflicts") == 0) {
start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
} else if (strcmp(element, "obsoletes") == 0) {
start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
} else {
fprintf(stderr, "Unrecognized element '%s'\n", element);
exit(1);
}
}
static void
end_test_element (void *data, const char *element)
{
struct test_context *ctx = data;
if (strcmp(element, "test") == 0) {
end_test(ctx);
} else if (strcmp(element, "set") == 0) {
end_set(ctx);
} else if (strcmp(element, "package") == 0) {
end_package(ctx);
} else if (strcmp(element, "transaction") == 0) {
end_transaction(ctx);
} else if (strcmp(element, "result") == 0) {
end_result(ctx);
} else if (strcmp(element, "unsatisfiable") == 0) {
end_unsatisfiable(ctx);
}
}
int main(int argc, char *argv[])
{
struct test_context ctx;
const char *test_file, *srcdir;
char path[PATH_MAX];
memset(&ctx, 0, sizeof ctx);
if (argc > 3) {
fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
exit(-1);
}
if (argc >= 2 && !strcmp (argv[1], "-d")) {
ctx.debug = 1;
argc--;
argv++;
}
srcdir = getenv("srcdir");
if (srcdir != NULL)
snprintf(path, sizeof path, "%s/test.xml", srcdir);
if (argc == 2)
test_file = argv[1];
else
test_file = path;
fprintf(stderr, "test-driver: using %s\n", path);
parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
if (ctx.errors)
fprintf(stderr, "\n%d errors\n", ctx.errors);
return 0;
}
|