summaryrefslogtreecommitdiff
path: root/regress/unittests/match/tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/match/tests.c')
-rw-r--r--regress/unittests/match/tests.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/regress/unittests/match/tests.c b/regress/unittests/match/tests.c
new file mode 100644
index 000000000..7ff319c16
--- /dev/null
+++ b/regress/unittests/match/tests.c
@@ -0,0 +1,113 @@
1/* $OpenBSD: tests.c,v 1.3 2016/09/21 17:03:54 djm Exp $ */
2/*
3 * Regress test for matching functions
4 *
5 * Placed in the public domain
6 */
7
8#include "includes.h"
9
10#include <sys/types.h>
11#include <sys/param.h>
12#include <stdio.h>
13#ifdef HAVE_STDINT_H
14#include <stdint.h>
15#endif
16#include <stdlib.h>
17#include <string.h>
18
19#include "../test_helper/test_helper.h"
20
21#include "match.h"
22
23void
24tests(void)
25{
26 TEST_START("match_pattern");
27 ASSERT_INT_EQ(match_pattern("", ""), 1);
28 ASSERT_INT_EQ(match_pattern("", "aaa"), 0);
29 ASSERT_INT_EQ(match_pattern("aaa", ""), 0);
30 ASSERT_INT_EQ(match_pattern("aaa", "aaaa"), 0);
31 ASSERT_INT_EQ(match_pattern("aaaa", "aaa"), 0);
32 TEST_DONE();
33
34 TEST_START("match_pattern wildcard");
35 ASSERT_INT_EQ(match_pattern("", "*"), 1);
36 ASSERT_INT_EQ(match_pattern("a", "?"), 1);
37 ASSERT_INT_EQ(match_pattern("aa", "a?"), 1);
38 ASSERT_INT_EQ(match_pattern("a", "*"), 1);
39 ASSERT_INT_EQ(match_pattern("aa", "a*"), 1);
40 ASSERT_INT_EQ(match_pattern("aa", "?*"), 1);
41 ASSERT_INT_EQ(match_pattern("aa", "**"), 1);
42 ASSERT_INT_EQ(match_pattern("aa", "?a"), 1);
43 ASSERT_INT_EQ(match_pattern("aa", "*a"), 1);
44 ASSERT_INT_EQ(match_pattern("ba", "a?"), 0);
45 ASSERT_INT_EQ(match_pattern("ba", "a*"), 0);
46 ASSERT_INT_EQ(match_pattern("ab", "?a"), 0);
47 ASSERT_INT_EQ(match_pattern("ab", "*a"), 0);
48 TEST_DONE();
49
50 TEST_START("match_pattern_list");
51 ASSERT_INT_EQ(match_pattern_list("", "", 0), 0); /* no patterns */
52 ASSERT_INT_EQ(match_pattern_list("", "*", 0), 1);
53 ASSERT_INT_EQ(match_pattern_list("", "!*", 0), -1);
54 ASSERT_INT_EQ(match_pattern_list("", "!a,*", 0), 1);
55 ASSERT_INT_EQ(match_pattern_list("", "*,!a", 0), 1);
56 ASSERT_INT_EQ(match_pattern_list("", "a,!*", 0), -1);
57 ASSERT_INT_EQ(match_pattern_list("", "!*,a", 0), -1);
58 ASSERT_INT_EQ(match_pattern_list("a", "", 0), 0);
59 ASSERT_INT_EQ(match_pattern_list("a", "*", 0), 1);
60 ASSERT_INT_EQ(match_pattern_list("a", "!*", 0), -1);
61 ASSERT_INT_EQ(match_pattern_list("a", "!a", 0), -1);
62 /* XXX negated ASSERT_INT_EQ(match_pattern_list("a", "!b", 0), 1); */
63 ASSERT_INT_EQ(match_pattern_list("a", "!a,*", 0), -1);
64 ASSERT_INT_EQ(match_pattern_list("b", "!a,*", 0), 1);
65 ASSERT_INT_EQ(match_pattern_list("a", "*,!a", 0), -1);
66 ASSERT_INT_EQ(match_pattern_list("b", "*,!a", 0), 1);
67 ASSERT_INT_EQ(match_pattern_list("a", "a,!*", 0), -1);
68 ASSERT_INT_EQ(match_pattern_list("b", "a,!*", 0), -1);
69 ASSERT_INT_EQ(match_pattern_list("a", "a,!a", 0), -1);
70 /* XXX negated ASSERT_INT_EQ(match_pattern_list("b", "a,!a", 0), 1); */
71 ASSERT_INT_EQ(match_pattern_list("a", "!*,a", 0), -1);
72 ASSERT_INT_EQ(match_pattern_list("b", "!*,a", 0), -1);
73 TEST_DONE();
74
75 TEST_START("match_pattern_list lowercase");
76 ASSERT_INT_EQ(match_pattern_list("abc", "ABC", 0), 0);
77 ASSERT_INT_EQ(match_pattern_list("ABC", "abc", 0), 0);
78 ASSERT_INT_EQ(match_pattern_list("abc", "ABC", 1), 1);
79 ASSERT_INT_EQ(match_pattern_list("ABC", "abc", 1), 0);
80 TEST_DONE();
81
82 TEST_START("addr_match_list");
83 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.1/44"), -2);
84 ASSERT_INT_EQ(addr_match_list(NULL, "127.0.0.1/44"), -2);
85 ASSERT_INT_EQ(addr_match_list("a", "*"), 0);
86 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "*"), 1);
87 ASSERT_INT_EQ(addr_match_list(NULL, "*"), 0);
88 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.1"), 1);
89 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.2"), 0);
90 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.1"), -1);
91 /* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.2"), 1); */
92 ASSERT_INT_EQ(addr_match_list("127.0.0.255", "127.0.0.0/24"), 1);
93 ASSERT_INT_EQ(addr_match_list("127.0.1.1", "127.0.0.0/24"), 0);
94 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.0/24"), 1);
95 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.1.0/24"), 0);
96 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.0/24"), -1);
97 /* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.1.0/24"), 1); */
98 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,!127.0.0.1"), -1);
99 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.1,10.0.0.1"), -1);
100 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,127.0.0.2"), 0);
101 ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.2,10.0.0.1"), 0);
102 /* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,!127.0.0.2"), 1); */
103 /* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.2,10.0.0.1"), 1); */
104 TEST_DONE();
105
106/*
107 * XXX TODO
108 * int match_host_and_ip(const char *, const char *, const char *);
109 * int match_user(const char *, const char *, const char *, const char *);
110 * char *match_list(const char *, const char *, u_int *);
111 * int addr_match_cidr_list(const char *, const char *);
112 */
113}