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