src/test-driver.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 23 13:54:56 2008 -0400 (2008-06-23)
changeset 262 63644cc28e0b
parent 241 c3eb520e2219
child 284 6e19775d8930
permissions -rw-r--r--
Set up gtkdoc for razor.

Despite its name, gtkdoc doesn't require gtk+ and is a nice way to
generate DocBook format API documentation.
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdarg.h>
     4 #include <unistd.h>
     5 #include <fcntl.h>
     6 #include <errno.h>
     7 #include <expat.h>
     8 
     9 #include "razor.h"
    10 
    11 #define XML_BUFFER_SIZE 4096
    12 
    13 static void
    14 parse_xml_file(const char *filename,
    15 	       XML_StartElementHandler start,
    16 	       XML_EndElementHandler end,
    17 	       void *data)
    18 {
    19 	XML_Parser parser;
    20 	char *buffer;
    21 	int fd, len, err;
    22 
    23 	parser = XML_ParserCreate(NULL);
    24 	XML_SetElementHandler(parser, start, end);
    25 	XML_SetUserData(parser, data);
    26 
    27 	fd = open(filename, O_RDONLY);
    28 	if (fd < 0) {
    29 		fprintf(stderr, "failed to open %s: %m\n", filename);
    30 		exit(-1);
    31 	}
    32 
    33 	while (1) {
    34 		buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
    35 		len = read(fd, buffer, XML_BUFFER_SIZE);
    36 		if (len == 0)
    37 			break;
    38 		err = XML_ParseBuffer(parser, len, len == 0);
    39 		if (err == XML_STATUS_ERROR) {
    40 			fprintf(stderr, "parse error at line %lu:\n%s\n",
    41 				XML_GetCurrentLineNumber(parser),
    42 				XML_ErrorString(XML_GetErrorCode(parser)));
    43 			exit(-1);
    44 		}
    45 	}
    46 
    47 	if (fd < 0) {
    48 		fprintf(stderr, "read: %m\n");
    49 		exit(-1);
    50 	}
    51 
    52 	close(fd);
    53 }
    54 
    55 struct test_context {
    56 	struct razor_set *system_set, *repo_set, *result_set;
    57 
    58 	struct razor_importer *importer;
    59 	struct razor_set **importer_set;
    60 
    61 	struct razor_transaction *trans;
    62 
    63 	char *install_pkgs[3], *remove_pkgs[3];
    64 	int n_install_pkgs, n_remove_pkgs;
    65 
    66 	int unsat;
    67 	int in_result;
    68 
    69 	int debug, errors;
    70 };
    71 
    72 static void
    73 get_atts(const char **atts, ...)
    74 {
    75 	va_list ap;
    76 	const char *name, **ptr;
    77 	int i;
    78 
    79 	va_start(ap, atts);
    80 	while (name = va_arg(ap, const char *), name != NULL) {
    81 		ptr = va_arg(ap, const char **);
    82 		*ptr = NULL;
    83 		for (i = 0; atts[i]; i += 2) {
    84 			if (strcmp(atts[i], name) == 0)
    85 				*ptr = atts[i + 1];
    86 		}
    87 	}
    88 	va_end(ap);
    89 }
    90 
    91 static enum razor_version_relation
    92 parse_relation (const char *rel_str)
    93 {
    94 	if (!rel_str)
    95 		return -1;
    96 	if (rel_str[0] == 'L')
    97 		return rel_str[1] == 'E' ? RAZOR_VERSION_LESS_OR_EQUAL : RAZOR_VERSION_LESS;
    98 	else if (rel_str[0] == 'G')
    99 		return rel_str[1] == 'E' ? RAZOR_VERSION_GREATER_OR_EQUAL : RAZOR_VERSION_GREATER;
   100 	else if (rel_str[0] == 'E' || rel_str[1] == 'Q')
   101 		return RAZOR_VERSION_EQUAL;
   102 	else
   103 		return -1;
   104 }
   105 
   106 static void
   107 start_test(struct test_context *ctx, const char **atts)
   108 {
   109 	const char *name = NULL;
   110 
   111 	get_atts(atts, "name", &name, NULL);
   112 	if (!name) {
   113 		fprintf(stderr, "Test with no name\n");
   114 		exit(1);
   115 	}
   116 	printf("%s\n", name);
   117 }
   118 
   119 static void
   120 end_test(struct test_context *ctx)
   121 {
   122 	if (ctx->system_set) {
   123 		razor_set_destroy(ctx->system_set);
   124 		ctx->system_set = NULL;
   125 	}
   126 	if (ctx->repo_set) {
   127 		razor_set_destroy(ctx->repo_set);
   128 		ctx->repo_set = NULL;
   129 	}
   130 	if (ctx->result_set) {
   131 		razor_set_destroy(ctx->result_set);
   132 		ctx->result_set = NULL;
   133 	}
   134 	if (ctx->trans) {
   135 		razor_transaction_destroy(ctx->trans);
   136 		ctx->trans = NULL;
   137 	}
   138 }
   139 
   140 static void
   141 start_set(struct test_context *ctx, const char **atts)
   142 {
   143 	const char *name = NULL;
   144 
   145 	ctx->importer = razor_importer_new();
   146 	get_atts(atts, "name", &name, NULL);
   147 	if (!name)
   148 		ctx->importer_set = &ctx->result_set;
   149 	else if (!strcmp(name, "system"))
   150 		ctx->importer_set = &ctx->system_set;
   151 	else if (!strcmp(name, "repo"))
   152 		ctx->importer_set = &ctx->repo_set;
   153 	else {
   154 		fprintf(stderr, "  bad set name '%s'\n", name);
   155 		exit(1);
   156 	}
   157 }
   158 
   159 static void
   160 end_set(struct test_context *ctx)
   161 {
   162 	*ctx->importer_set = razor_importer_finish(ctx->importer);
   163 	ctx->importer = NULL;
   164 }
   165 
   166 static void
   167 start_package(struct test_context *ctx, const char **atts)
   168 {
   169 	const char *name = NULL, *version = NULL, *arch = NULL;
   170 
   171 	get_atts(atts, "name", &name,
   172 		 "version", &version,
   173 		 "arch", &arch,
   174 		 NULL);
   175 
   176 	if (!name) {
   177 		fprintf(stderr, "  package with no name\n");
   178 		exit(1);
   179 	}
   180 
   181 	razor_importer_begin_package(ctx->importer, name, version, arch);
   182 	razor_importer_add_property(ctx->importer, name,
   183 				    RAZOR_VERSION_EQUAL, version,
   184 				    RAZOR_PROPERTY_PROVIDES);
   185 }
   186 
   187 static void
   188 end_package(struct test_context *ctx)
   189 {
   190 	razor_importer_finish_package(ctx->importer);
   191 }
   192 
   193 static void
   194 add_property(struct test_context *ctx, enum razor_property_type type, const char *name, enum razor_version_relation rel, const char *version)
   195 {
   196 	razor_importer_add_property(ctx->importer, name,
   197 				    rel, version, type);
   198 }
   199 
   200 static void
   201 check_unsatisfiable_property(struct test_context *ctx,
   202 			     enum razor_property_type type,
   203 			     const char *name,
   204 			     enum razor_version_relation rel,
   205 			     const char *version)
   206 {
   207 	static const char *relation_string[] = { "<", "<=", "=", ">=", ">" };
   208 
   209 	if (!version)
   210 		version = "";
   211 
   212 	if (razor_transaction_unsatisfied_property(ctx->trans,
   213 						   name, rel, version, type))
   214 		return;
   215 
   216 	fprintf(stderr, "  didn't get unsatisfiable '%s %s %s'\n",
   217 		name, relation_string[rel], version);
   218 	ctx->errors++;
   219 }
   220 
   221 static void
   222 start_property(struct test_context *ctx, enum razor_property_type type, const char **atts)
   223 {
   224 	const char *name = NULL, *rel_str = NULL, *version = NULL;
   225 	enum razor_version_relation rel;
   226 
   227 	get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
   228 	if (name == NULL) {
   229 		fprintf(stderr, "  no name specified for property\n");
   230 		exit(1);
   231 	}
   232 	if (version) {
   233 		rel = parse_relation(rel_str);
   234 		if (rel == -1) {
   235 			fprintf(stderr, "  bad or missing version relation for property %s\n", name);
   236 			exit(1);
   237 		}
   238 	} else
   239 		rel = RAZOR_VERSION_EQUAL;
   240 
   241 	if (ctx->unsat)
   242 		check_unsatisfiable_property(ctx, type, name, rel, version);
   243 	else
   244 		add_property(ctx, type, name, rel, version);
   245 }
   246 
   247 static void
   248 start_transaction(struct test_context *ctx, const char **atts)
   249 {
   250 	ctx->n_install_pkgs = 0;
   251 	ctx->n_remove_pkgs = 0;
   252 }
   253 
   254 static void
   255 end_transaction(struct test_context *ctx)
   256 {
   257 	struct razor_package *pkg;
   258 	int errors, i;
   259 
   260 	ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set);
   261 	for (i = 0; i < ctx->n_install_pkgs; i++) {
   262 		pkg = razor_set_get_package(ctx->repo_set,
   263 					    ctx->install_pkgs[i]);
   264 		razor_transaction_install_package(ctx->trans, pkg);
   265 	}
   266 	for (i = 0; i < ctx->n_remove_pkgs; i++) {
   267 		pkg = razor_set_get_package(ctx->system_set,
   268 					    ctx->remove_pkgs[i]);
   269 		if (!pkg)
   270 			pkg = razor_set_get_package(ctx->repo_set,
   271 						    ctx->remove_pkgs[i]);
   272 
   273 		razor_transaction_remove_package(ctx->trans, pkg);
   274 	}
   275 
   276 	errors = razor_transaction_resolve(ctx->trans);
   277 	printf("\n");
   278 
   279 	while (ctx->n_install_pkgs--)
   280 		free(ctx->install_pkgs[ctx->n_install_pkgs]);
   281 	while (ctx->n_remove_pkgs--)
   282 		free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
   283 
   284 	if (!errors) {
   285 		struct razor_set *new;
   286 		new = razor_transaction_finish(ctx->trans);
   287 		ctx->trans = NULL;
   288 		ctx->system_set = new;
   289 	}
   290 }
   291 
   292 static void
   293 start_install_or_update(struct test_context *ctx, const char **atts)
   294 {
   295 	const char *name = NULL;
   296 
   297 	get_atts(atts, "name", &name, NULL);
   298 	if (!name) {
   299 		fprintf(stderr, "  install/update with no name\n");
   300 		exit(1);
   301 	}
   302 
   303 	ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
   304 }
   305 
   306 static void
   307 start_remove(struct test_context *ctx, const char **atts)
   308 {
   309 	const char *name = NULL;
   310 
   311 	get_atts(atts, "name", &name, NULL);
   312 	if (!name) {
   313 		fprintf(stderr, "  remove with no name\n");
   314 		exit(1);
   315 	}
   316 
   317 	ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
   318 }
   319 
   320 static void
   321 start_result(struct test_context *ctx, const char **atts)
   322 {
   323 	ctx->in_result = 1;
   324 }
   325 
   326 static void
   327 diff_callback(const char *name,
   328 	      const char *old_version,
   329 	      const char *new_version,
   330 	      const char *arch,
   331 	      void *data)
   332 {
   333 	struct test_context *ctx = data;
   334 
   335 	ctx->errors++;
   336 	if (old_version) {
   337 		fprintf(stderr, "  result set should not contain %s %s\n",
   338 			name, old_version);
   339 	} else {
   340 		fprintf(stderr, "  result set should contain %s %s\n",
   341 			name, new_version);
   342 	}
   343 }
   344 
   345 static void
   346 end_result(struct test_context *ctx)
   347 {
   348 	ctx->in_result = 0;
   349 
   350 	if (ctx->result_set) {
   351 		if (!ctx->system_set)
   352 			ctx->system_set = razor_set_create();
   353 		razor_set_diff(ctx->system_set, ctx->result_set,
   354 			       diff_callback, ctx);
   355 	}
   356 }
   357 
   358 static void
   359 start_unsatisfiable(struct test_context *ctx, const char **atts)
   360 {
   361 	if (ctx->result_set) {
   362 		fprintf(stderr, "Expected to fail, but didn't\n");
   363 		exit(1);
   364 	}
   365 
   366 	ctx->unsat = 1;
   367 }
   368 
   369 static void
   370 end_unsatisfiable(struct test_context *ctx)
   371 {
   372 	ctx->unsat = 0;
   373 }
   374 
   375 static void
   376 start_test_element(void *data, const char *element, const char **atts)
   377 {
   378 	struct test_context *ctx = data;
   379 
   380 	if (strcmp(element, "tests") == 0) {
   381 		;
   382 	} else if (strcmp(element, "test") == 0) {
   383 		start_test(ctx, atts);
   384 	} else if (strcmp(element, "set") == 0) {
   385 		start_set(ctx, atts);
   386 	} else if (strcmp(element, "transaction") == 0) {
   387 		start_transaction(ctx, atts);
   388 	} else if (strcmp(element, "install") == 0) {
   389 		start_install_or_update(ctx, atts);
   390 	} else if (strcmp(element, "install") == 0) {
   391 		start_install_or_update(ctx, atts);
   392 	} else if (strcmp(element, "remove") == 0) {
   393 		start_remove(ctx, atts);
   394 	} else if (strcmp(element, "result") == 0) {
   395 		start_result(ctx, atts);
   396 	} else if (strcmp(element, "unsatisfiable") == 0) {
   397 		start_unsatisfiable(ctx, atts);
   398 	} else if (strcmp(element, "package") == 0) {
   399 		start_package(ctx, atts);
   400 	} else if (strcmp(element, "requires") == 0) {
   401 		start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
   402 	} else if (strcmp(element, "provides") == 0) {
   403 		start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
   404 	} else if (strcmp(element, "conflicts") == 0) {
   405 		start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
   406 	} else if (strcmp(element, "obsoletes") == 0) {
   407 		start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
   408 	} else {
   409 		fprintf(stderr, "Unrecognized element '%s'\n", element);
   410 		exit(1);
   411 	}
   412 }
   413 
   414 static void
   415 end_test_element (void *data, const char *element)
   416 {
   417 	struct test_context *ctx = data;
   418 
   419 	if (strcmp(element, "test") == 0) {
   420 		end_test(ctx);
   421 	} else if (strcmp(element, "set") == 0) {
   422 		end_set(ctx);
   423 	} else if (strcmp(element, "package") == 0) {
   424 		end_package(ctx);
   425 	} else if (strcmp(element, "transaction") == 0) {
   426 		end_transaction(ctx);
   427 	} else if (strcmp(element, "result") == 0) {
   428 		end_result(ctx);
   429 	} else if (strcmp(element, "unsatisfiable") == 0) {
   430 		end_unsatisfiable(ctx);
   431 	}
   432 }
   433 
   434 int main(int argc, char *argv[])
   435 {
   436 	struct test_context ctx;
   437 	const char *test_file;
   438 
   439 	memset(&ctx, 0, sizeof ctx);
   440 
   441 	if (argc > 3) {
   442 		fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
   443 		exit(-1);
   444 	}
   445 
   446 	if (argc >= 2 && !strcmp (argv[1], "-d")) {
   447 		ctx.debug = 1;
   448 		argc--;
   449 		argv++;
   450 	}
   451 	if (argc == 2)
   452 		test_file = argv[1];
   453 	else
   454 		test_file = "test.xml";
   455 
   456 	parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
   457 
   458 	if (ctx.errors) {
   459 		fprintf(stderr, "\n%d errors\n", ctx.errors);
   460 		return 1;
   461 	} else
   462 		return 0;
   463 }