xref: /openbsd-src/regress/usr.sbin/httpd/patterns/patterns-tester.c (revision 2963fcf0839035a36944679523fe0bc7c3ec7c5e)
1*2963fcf0Ssemarie /* $OpenBSD: patterns-tester.c,v 1.1 2015/06/23 18:03:09 semarie Exp $ */
2*2963fcf0Ssemarie /*
3*2963fcf0Ssemarie  * Copyright (c) 2015 Sebastien Marie <semarie@openbsd.org>
4*2963fcf0Ssemarie  *
5*2963fcf0Ssemarie  * Permission to use, copy, modify, and distribute this software for any
6*2963fcf0Ssemarie  * purpose with or without fee is hereby granted, provided that the above
7*2963fcf0Ssemarie  * copyright notice and this permission notice appear in all copies.
8*2963fcf0Ssemarie  *
9*2963fcf0Ssemarie  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*2963fcf0Ssemarie  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*2963fcf0Ssemarie  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*2963fcf0Ssemarie  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*2963fcf0Ssemarie  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*2963fcf0Ssemarie  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*2963fcf0Ssemarie  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*2963fcf0Ssemarie  */
17*2963fcf0Ssemarie 
18*2963fcf0Ssemarie #include <stdio.h>
19*2963fcf0Ssemarie #include <stdlib.h>
20*2963fcf0Ssemarie #include <err.h>
21*2963fcf0Ssemarie #include <string.h>
22*2963fcf0Ssemarie 
23*2963fcf0Ssemarie #include "patterns.h"
24*2963fcf0Ssemarie 
25*2963fcf0Ssemarie extern char *	malloc_options;
26*2963fcf0Ssemarie 
27*2963fcf0Ssemarie static void read_string(char *, size_t);
28*2963fcf0Ssemarie static void read_string_stop(void);
29*2963fcf0Ssemarie 
30*2963fcf0Ssemarie static void
read_string(char * buf,size_t len)31*2963fcf0Ssemarie read_string(char *buf, size_t len)
32*2963fcf0Ssemarie {
33*2963fcf0Ssemarie 	size_t i;
34*2963fcf0Ssemarie 
35*2963fcf0Ssemarie 	/* init */
36*2963fcf0Ssemarie 	bzero(buf, len);
37*2963fcf0Ssemarie 
38*2963fcf0Ssemarie 	/* read */
39*2963fcf0Ssemarie 	if (fgets(buf, len, stdin) == NULL)
40*2963fcf0Ssemarie 		err(1, "fgets");
41*2963fcf0Ssemarie 
42*2963fcf0Ssemarie 	/* strip '\n' */
43*2963fcf0Ssemarie 	i = strnlen(buf, len);
44*2963fcf0Ssemarie 	if (i != 0)
45*2963fcf0Ssemarie 		buf[i-1] = '\0';
46*2963fcf0Ssemarie }
47*2963fcf0Ssemarie 
48*2963fcf0Ssemarie static void
read_string_stop()49*2963fcf0Ssemarie read_string_stop()
50*2963fcf0Ssemarie {
51*2963fcf0Ssemarie 	if (getchar() != EOF)
52*2963fcf0Ssemarie 		errx(1, "read_string_stop: too many input");
53*2963fcf0Ssemarie }
54*2963fcf0Ssemarie 
55*2963fcf0Ssemarie int
main(int argc,char * argv[])56*2963fcf0Ssemarie main(int argc, char *argv[])
57*2963fcf0Ssemarie {
58*2963fcf0Ssemarie 	char string[1024];
59*2963fcf0Ssemarie 	char pattern[1024];
60*2963fcf0Ssemarie 	struct str_match m;
61*2963fcf0Ssemarie 	const char *errstr = NULL;
62*2963fcf0Ssemarie 	int ret;
63*2963fcf0Ssemarie 	size_t i;
64*2963fcf0Ssemarie 
65*2963fcf0Ssemarie 	/* configure malloc */
66*2963fcf0Ssemarie 	malloc_options = "S";
67*2963fcf0Ssemarie 
68*2963fcf0Ssemarie 	/* read testcase */
69*2963fcf0Ssemarie 	if (argc != 3) {
70*2963fcf0Ssemarie 		/* from stdin (useful for afl) */
71*2963fcf0Ssemarie 		read_string(string, sizeof(string));
72*2963fcf0Ssemarie 		read_string(pattern, sizeof(pattern));
73*2963fcf0Ssemarie 		read_string_stop();
74*2963fcf0Ssemarie 	} else {
75*2963fcf0Ssemarie 		/* from arguments */
76*2963fcf0Ssemarie 		strlcpy(string, argv[1], sizeof(string));
77*2963fcf0Ssemarie 		strlcpy(pattern, argv[2], sizeof(pattern));
78*2963fcf0Ssemarie 	}
79*2963fcf0Ssemarie 
80*2963fcf0Ssemarie 	/* print testcase */
81*2963fcf0Ssemarie 	printf("string='%s'\n", string);
82*2963fcf0Ssemarie 	printf("pattern='%s'\n", pattern);
83*2963fcf0Ssemarie 
84*2963fcf0Ssemarie 	/* test it ! */
85*2963fcf0Ssemarie 	ret = str_match(string, pattern, &m, &errstr);
86*2963fcf0Ssemarie 	if (errstr != NULL)
87*2963fcf0Ssemarie 		errx(1, "str_match: %s", errstr);
88*2963fcf0Ssemarie 
89*2963fcf0Ssemarie 	/* print result */
90*2963fcf0Ssemarie 	printf("ret=%d num=%d\n", ret, m.sm_nmatch);
91*2963fcf0Ssemarie 	for (i=0; i<m.sm_nmatch; i++) {
92*2963fcf0Ssemarie 		printf("%ld: %s\n", i, m.sm_match[i]);
93*2963fcf0Ssemarie 	}
94*2963fcf0Ssemarie 
95*2963fcf0Ssemarie 	str_match_free(&m);
96*2963fcf0Ssemarie 
97*2963fcf0Ssemarie 	return (EXIT_SUCCESS);
98*2963fcf0Ssemarie }
99