xref: /freebsd/contrib/libarchive/test_utils/test_utils.c (revision acc60b03c17eb1e46c3993509dd8383185207183)
1*acc60b03SMartin Matuska /*
2*acc60b03SMartin Matuska  * Copyright (c) 2003-2012 Tim Kientzle
3*acc60b03SMartin Matuska  * Copyright (c) 2012 Andres Mejia
4*acc60b03SMartin Matuska  * All rights reserved.
5*acc60b03SMartin Matuska  *
6*acc60b03SMartin Matuska  * Redistribution and use in source and binary forms, with or without
7*acc60b03SMartin Matuska  * modification, are permitted provided that the following conditions
8*acc60b03SMartin Matuska  * are met:
9*acc60b03SMartin Matuska  * 1. Redistributions of source code must retain the above copyright
10*acc60b03SMartin Matuska  *    notice, this list of conditions and the following disclaimer.
11*acc60b03SMartin Matuska  * 2. Redistributions in binary form must reproduce the above copyright
12*acc60b03SMartin Matuska  *    notice, this list of conditions and the following disclaimer in the
13*acc60b03SMartin Matuska  *    documentation and/or other materials provided with the distribution.
14*acc60b03SMartin Matuska  *
15*acc60b03SMartin Matuska  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16*acc60b03SMartin Matuska  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*acc60b03SMartin Matuska  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*acc60b03SMartin Matuska  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19*acc60b03SMartin Matuska  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*acc60b03SMartin Matuska  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*acc60b03SMartin Matuska  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*acc60b03SMartin Matuska  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*acc60b03SMartin Matuska  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*acc60b03SMartin Matuska  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*acc60b03SMartin Matuska  */
26*acc60b03SMartin Matuska 
27*acc60b03SMartin Matuska #include "test_utils.h"
28*acc60b03SMartin Matuska 
29*acc60b03SMartin Matuska #include <stdlib.h>
30*acc60b03SMartin Matuska #include <string.h>
31*acc60b03SMartin Matuska 
32*acc60b03SMartin Matuska /* Filter tests against a glob pattern. Returns non-zero if test matches
33*acc60b03SMartin Matuska  * pattern, zero otherwise. A '^' at the beginning of the pattern negates
34*acc60b03SMartin Matuska  * the return values (i.e. returns zero for a match, non-zero otherwise.
35*acc60b03SMartin Matuska  */
36*acc60b03SMartin Matuska static int
37*acc60b03SMartin Matuska test_filter(const char *pattern, const char *test)
38*acc60b03SMartin Matuska {
39*acc60b03SMartin Matuska 	int retval = 0;
40*acc60b03SMartin Matuska 	int negate = 0;
41*acc60b03SMartin Matuska 	const char *p = pattern;
42*acc60b03SMartin Matuska 	const char *t = test;
43*acc60b03SMartin Matuska 
44*acc60b03SMartin Matuska 	if (p[0] == '^')
45*acc60b03SMartin Matuska 	{
46*acc60b03SMartin Matuska 		negate = 1;
47*acc60b03SMartin Matuska 		p++;
48*acc60b03SMartin Matuska 	}
49*acc60b03SMartin Matuska 
50*acc60b03SMartin Matuska 	while (1)
51*acc60b03SMartin Matuska 	{
52*acc60b03SMartin Matuska 		if (p[0] == '\\')
53*acc60b03SMartin Matuska 			p++;
54*acc60b03SMartin Matuska 		else if (p[0] == '*')
55*acc60b03SMartin Matuska 		{
56*acc60b03SMartin Matuska 			while (p[0] == '*')
57*acc60b03SMartin Matuska 				p++;
58*acc60b03SMartin Matuska 			if (p[0] == '\\')
59*acc60b03SMartin Matuska 				p++;
60*acc60b03SMartin Matuska 			if ((t = strchr(t, p[0])) == 0)
61*acc60b03SMartin Matuska 				break;
62*acc60b03SMartin Matuska 		}
63*acc60b03SMartin Matuska 		if (p[0] != t[0])
64*acc60b03SMartin Matuska 			break;
65*acc60b03SMartin Matuska 		if (p[0] == '\0') {
66*acc60b03SMartin Matuska 			retval = 1;
67*acc60b03SMartin Matuska 			break;
68*acc60b03SMartin Matuska 		}
69*acc60b03SMartin Matuska 		p++;
70*acc60b03SMartin Matuska 		t++;
71*acc60b03SMartin Matuska 	}
72*acc60b03SMartin Matuska 
73*acc60b03SMartin Matuska 	return (negate) ? !retval : retval;
74*acc60b03SMartin Matuska }
75*acc60b03SMartin Matuska 
76*acc60b03SMartin Matuska int get_test_set(int *test_set, int limit, const char *test,
77*acc60b03SMartin Matuska 	struct test_list_t *tests)
78*acc60b03SMartin Matuska {
79*acc60b03SMartin Matuska 	int start, end;
80*acc60b03SMartin Matuska 	int idx = 0;
81*acc60b03SMartin Matuska 
82*acc60b03SMartin Matuska 	if (test == NULL) {
83*acc60b03SMartin Matuska 		/* Default: Run all tests. */
84*acc60b03SMartin Matuska 		for (;idx < limit; idx++)
85*acc60b03SMartin Matuska 			test_set[idx] = idx;
86*acc60b03SMartin Matuska 		return (limit);
87*acc60b03SMartin Matuska 	}
88*acc60b03SMartin Matuska 	if (*test >= '0' && *test <= '9') {
89*acc60b03SMartin Matuska 		const char *vp = test;
90*acc60b03SMartin Matuska 		start = 0;
91*acc60b03SMartin Matuska 		while (*vp >= '0' && *vp <= '9') {
92*acc60b03SMartin Matuska 			start *= 10;
93*acc60b03SMartin Matuska 			start += *vp - '0';
94*acc60b03SMartin Matuska 			++vp;
95*acc60b03SMartin Matuska 		}
96*acc60b03SMartin Matuska 		if (*vp == '\0') {
97*acc60b03SMartin Matuska 			end = start;
98*acc60b03SMartin Matuska 		} else if (*vp == '-') {
99*acc60b03SMartin Matuska 			++vp;
100*acc60b03SMartin Matuska 			if (*vp == '\0') {
101*acc60b03SMartin Matuska 				end = limit - 1;
102*acc60b03SMartin Matuska 			} else {
103*acc60b03SMartin Matuska 				end = 0;
104*acc60b03SMartin Matuska 				while (*vp >= '0' && *vp <= '9') {
105*acc60b03SMartin Matuska 					end *= 10;
106*acc60b03SMartin Matuska 					end += *vp - '0';
107*acc60b03SMartin Matuska 					++vp;
108*acc60b03SMartin Matuska 				}
109*acc60b03SMartin Matuska 			}
110*acc60b03SMartin Matuska 		} else
111*acc60b03SMartin Matuska 			return (-1);
112*acc60b03SMartin Matuska 		if (start < 0 || end >= limit || start > end)
113*acc60b03SMartin Matuska 			return (-1);
114*acc60b03SMartin Matuska 		while (start <= end)
115*acc60b03SMartin Matuska 			test_set[idx++] = start++;
116*acc60b03SMartin Matuska 	} else {
117*acc60b03SMartin Matuska 		for (start = 0; start < limit; ++start) {
118*acc60b03SMartin Matuska 			const char *name = tests[start].name;
119*acc60b03SMartin Matuska 			if (test_filter(test, name))
120*acc60b03SMartin Matuska 				test_set[idx++] = start;
121*acc60b03SMartin Matuska 		}
122*acc60b03SMartin Matuska 	}
123*acc60b03SMartin Matuska 	return ((idx == 0)?-1:idx);
124*acc60b03SMartin Matuska }
125