test-driver.c
changeset 93 f173765f7623
parent 80 72671f8e5741
child 94 0aa93cfbcb3f
     1.1 --- a/test-driver.c	Tue Nov 13 01:07:03 2007 -0500
     1.2 +++ b/test-driver.c	Tue Jan 08 12:55:34 2008 -0500
     1.3 @@ -28,7 +28,7 @@
     1.4  
     1.5  	fd = open(filename, O_RDONLY);
     1.6  	if (fd < 0) {
     1.7 -		fprintf(stderr, "open: %m\n");
     1.8 +		fprintf(stderr, "failed to open %s: %m\n", filename);
     1.9  		exit(-1);
    1.10  	}
    1.11  
    1.12 @@ -59,6 +59,8 @@
    1.13  struct test_context {
    1.14  	struct razor_importer *importer;
    1.15  	struct test_set *sets;
    1.16 +	struct razor_package_iterator *package_iterator;
    1.17 +	struct razor_property_iterator *property_iterator;
    1.18  };
    1.19  
    1.20  static void
    1.21 @@ -71,6 +73,7 @@
    1.22  	va_start(ap, atts);
    1.23  	while (name = va_arg(ap, const char *), name != NULL) {
    1.24  		ptr = va_arg(ap, const char **);
    1.25 +		*ptr = NULL;
    1.26  		for (i = 0; atts[i]; i += 2) {
    1.27  			if (strcmp(atts[i], name) == 0)
    1.28  				*ptr = atts[i + 1];
    1.29 @@ -84,14 +87,8 @@
    1.30  	       enum razor_property_type type)
    1.31  {
    1.32  	const char *name = NULL, *version = NULL;
    1.33 -	int i;
    1.34  
    1.35 -	for (i = 0; atts[i]; i += 2) {
    1.36 -		if (strcmp(atts[i], "name") == 0)
    1.37 -			name = atts[i + 1];
    1.38 -		else if (strcmp(atts[i], "eq") == 0)
    1.39 -			version = atts[i + 1];
    1.40 -	}
    1.41 +	get_atts(atts, "name", &name, "eq", &version, NULL);
    1.42  
    1.43  	if (name == NULL) {
    1.44  		fprintf(stderr, "no name specified for property\n");
    1.45 @@ -102,7 +99,7 @@
    1.46  }
    1.47  
    1.48  static void
    1.49 -start_test_sets_element(void *data, const char *element, const char **atts)
    1.50 +start_set_element(void *data, const char *element, const char **atts)
    1.51  {
    1.52  	struct test_context *ctx = data;
    1.53  	struct test_set *set;
    1.54 @@ -136,7 +133,7 @@
    1.55  }
    1.56  
    1.57  static void
    1.58 -end_test_sets_element (void *data, const char *name)
    1.59 +end_set_element (void *data, const char *name)
    1.60  {
    1.61  	struct test_context *ctx = data;
    1.62  
    1.63 @@ -147,24 +144,193 @@
    1.64  	}
    1.65  }
    1.66  
    1.67 +static struct razor_set *
    1.68 +lookup_set(struct test_context *ctx, const char *name)
    1.69 +{
    1.70 +	struct test_set *set;
    1.71 +
    1.72 +	for (set = ctx->sets; set != NULL; set = set->next) {
    1.73 +		if (strcmp(set->name, name) == 0)
    1.74 +			return set->set;
    1.75 +	}
    1.76 +
    1.77 +	return NULL;
    1.78 +}
    1.79 +
    1.80 +static void
    1.81 +verify_begin(struct test_context *ctx, const char **atts)
    1.82 +{
    1.83 +	struct razor_set *set;
    1.84 +	const char *type, *name;
    1.85 +
    1.86 +	get_atts(atts, "type", &type, "set", &name, NULL);
    1.87 +	set = lookup_set(ctx, name);
    1.88 +	if (set == NULL) {
    1.89 +		fprintf(stderr, "set %s not found\n", name);
    1.90 +		exit(-1);
    1.91 +	}
    1.92 +
    1.93 +	if (strcmp(type, "packages") == 0) {
    1.94 +		ctx->package_iterator =
    1.95 +			razor_package_iterator_create(set);
    1.96 +	} else if (strcmp(type, "properties") == 0) {
    1.97 +		ctx->property_iterator =
    1.98 +			razor_property_iterator_create(set, NULL);
    1.99 +	} else {
   1.100 +		fprintf(stderr,
   1.101 +			"unknown compare type \"%s\"\n", type);
   1.102 +		exit(-1);
   1.103 +	}
   1.104 +}
   1.105 +
   1.106 +static void
   1.107 +verify_end(struct test_context *ctx)
   1.108 +{
   1.109 +	struct razor_package *package;
   1.110 +	struct razor_property *property;
   1.111 +	const char *name, *version, *ref_name, *ref_version;
   1.112 +	enum razor_property_type type;
   1.113 +
   1.114 +	if (ctx->package_iterator != NULL) {
   1.115 +		if (razor_package_iterator_next(ctx->package_iterator,
   1.116 +						&package,
   1.117 +						&name, &version)) {
   1.118 +			fprintf(stderr, "too few packages in set\n");
   1.119 +			exit(-1);
   1.120 +		}
   1.121 +				
   1.122 +		razor_package_iterator_destroy(ctx->package_iterator);
   1.123 +		ctx->package_iterator = NULL;
   1.124 +	}
   1.125 +
   1.126 +	if (ctx->property_iterator != NULL) {
   1.127 +		if (razor_property_iterator_next(ctx->property_iterator,
   1.128 +						 &property,
   1.129 +						 &name, &version, &type)) {
   1.130 +			fprintf(stderr, "too few properties in set\n");
   1.131 +			exit(-1);
   1.132 +		}
   1.133 +
   1.134 +		razor_property_iterator_destroy(ctx->property_iterator);
   1.135 +		ctx->property_iterator = NULL;
   1.136 +	}
   1.137 +}
   1.138 +
   1.139 +static void
   1.140 +verify_package(struct test_context *ctx, const char **atts)
   1.141 +{
   1.142 +	struct razor_package *package;
   1.143 +	const char *name, *version, *ref_name, *ref_version;
   1.144 +
   1.145 +	if (ctx->package_iterator == NULL) {
   1.146 +		fprintf(stderr,
   1.147 +			"\"package\" element seen, "
   1.148 +			"but not in package verify mode\n");
   1.149 +		exit(-1);
   1.150 +	}
   1.151 +
   1.152 +	get_atts(atts, "name", &ref_name, "version", &ref_version, NULL);
   1.153 +	if (!razor_package_iterator_next(ctx->package_iterator,
   1.154 +					 &package, &name, &version)) {
   1.155 +		fprintf(stderr, "too many packages in set\n");
   1.156 +		exit(-1);
   1.157 +	}
   1.158 +			
   1.159 +	if (strcmp(name, ref_name) != 0 || strcmp(version, ref_version) != 0) {
   1.160 +		fprintf(stderr,
   1.161 +			"package mismatch; expected %s-%s, got %s-%s\n",
   1.162 +			ref_name, ref_version, name, version);
   1.163 +		exit(-1);
   1.164 +	}
   1.165 +}
   1.166 +
   1.167 +static void
   1.168 +verify_property(struct test_context *ctx,
   1.169 +		enum razor_property_type ref_type, const char **atts)
   1.170 +{
   1.171 +	struct razor_property *property;
   1.172 +	const char *name, *version, *ref_name, *ref_version;
   1.173 +	enum razor_property_type type;
   1.174 +	int same_version;
   1.175 +
   1.176 +	if (ctx->property_iterator == NULL) {
   1.177 +		fprintf(stderr,
   1.178 +			"\"requires/provides\" element seen, "
   1.179 +			"but not in property verify mode\n");
   1.180 +		exit(-1);
   1.181 +	}
   1.182 +
   1.183 +	get_atts(atts, "name", &ref_name, "eq", &ref_version, NULL);
   1.184 +	if (!razor_property_iterator_next(ctx->property_iterator, &property,
   1.185 +					  &name, &version, &type)) {
   1.186 +		fprintf(stderr, "too many properties in set\n");
   1.187 +		exit(-1);
   1.188 +	}
   1.189 +			
   1.190 +	if (version != NULL && ref_version != NULL)
   1.191 +		same_version = strcmp(version, ref_version) == 0;
   1.192 +	else if (version == NULL && ref_version == NULL)
   1.193 +		same_version = 1;
   1.194 +	else
   1.195 +		same_version = 0;
   1.196 +
   1.197 +	if (strcmp(name, ref_name) != 0 || !same_version || type != ref_type) {
   1.198 +		fprintf(stderr,
   1.199 +			"property mismatch; expected %s-%s/%d, got %s-%s/%d\n",
   1.200 +			ref_name, ref_version, ref_type,
   1.201 +			name, version, type);
   1.202 +		exit(-1);
   1.203 +	}
   1.204 +}
   1.205 +
   1.206 +static void
   1.207 +start_test_element(void *data, const char *element, const char **atts)
   1.208 +{
   1.209 +	struct test_context *ctx = data;
   1.210 +	struct razor_set *set;
   1.211 +	const char *name;
   1.212 +
   1.213 +	if (strcmp(element, "import") == 0) {
   1.214 +		get_atts(atts, "file", &name, NULL);
   1.215 +		parse_xml_file(name, start_set_element, end_set_element, ctx);
   1.216 +	} else if (strcmp(element, "update") == 0) {
   1.217 +		/* run update to create new set */
   1.218 +	} else if (strcmp(element, "verify") == 0) {
   1.219 +		verify_begin(ctx, atts);
   1.220 +	} else if (strcmp(element, "package") == 0) {
   1.221 +		verify_package(ctx, atts);
   1.222 +	} else if (strcmp(element, "requires") == 0) {
   1.223 +		verify_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
   1.224 +	} else if (strcmp(element, "provides") == 0) {
   1.225 +		verify_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
   1.226 +	} else if (strcmp(element, "conflicts") == 0) {
   1.227 +		verify_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
   1.228 +	} else if (strcmp(element, "obsoletes") == 0) {
   1.229 +		verify_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
   1.230 +	}
   1.231 +}
   1.232 +
   1.233 +static void
   1.234 +end_test_element (void *data, const char *element)
   1.235 +{
   1.236 +	struct test_context *ctx = data;
   1.237 +
   1.238 +	if (strcmp(element, "verify") == 0)
   1.239 +		verify_end(ctx);
   1.240 +}
   1.241 +
   1.242  int main(int argc, char *argv[])
   1.243  {
   1.244  	struct test_context ctx;
   1.245  	struct test_set *set;
   1.246  
   1.247 -	if (argc != 3) {
   1.248 -		fprintf(stderr, "usage: %s SETS-FILE TESTS-FILE\n", argv[0]);
   1.249 +	if (argc != 2) {
   1.250 +		fprintf(stderr, "usage: %s TESTS-FILE\n", argv[0]);
   1.251  		exit(-1);			
   1.252  	}
   1.253  
   1.254  	memset(&ctx, 0, sizeof ctx);
   1.255 -	parse_xml_file(argv[1],
   1.256 -		       start_test_sets_element,
   1.257 -		       end_test_sets_element,
   1.258 -		       &ctx);
   1.259 -
   1.260 -	for (set = ctx.sets; set != NULL; set = set->next)
   1.261 -		printf("set %s\n", set->name);
   1.262 +	parse_xml_file(argv[1], start_test_element, end_test_element, &ctx);
   1.263  
   1.264  	return 0;
   1.265  }