xref: /freebsd/crypto/openssh/regress/unittests/match/tests.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /* 	$OpenBSD: tests.c,v 1.8 2021/12/14 21:25:27 deraadt 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 <stdio.h>
12 #ifdef HAVE_STDINT_H
13 #include <stdint.h>
14 #endif
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "../test_helper/test_helper.h"
19 
20 #include "match.h"
21 
22 void
23 tests(void)
24 {
25 	TEST_START("match_pattern");
26 	ASSERT_INT_EQ(match_pattern("", ""), 1);
27 	ASSERT_INT_EQ(match_pattern("", "aaa"), 0);
28 	ASSERT_INT_EQ(match_pattern("aaa", ""), 0);
29 	ASSERT_INT_EQ(match_pattern("aaa", "aaaa"), 0);
30 	ASSERT_INT_EQ(match_pattern("aaaa", "aaa"), 0);
31 	TEST_DONE();
32 
33 	TEST_START("match_pattern wildcard");
34 	ASSERT_INT_EQ(match_pattern("", "*"), 1);
35 	ASSERT_INT_EQ(match_pattern("a", "?"), 1);
36 	ASSERT_INT_EQ(match_pattern("aa", "a?"), 1);
37 	ASSERT_INT_EQ(match_pattern("a", "*"), 1);
38 	ASSERT_INT_EQ(match_pattern("aa", "a*"), 1);
39 	ASSERT_INT_EQ(match_pattern("aa", "?*"), 1);
40 	ASSERT_INT_EQ(match_pattern("aa", "**"), 1);
41 	ASSERT_INT_EQ(match_pattern("aa", "?a"), 1);
42 	ASSERT_INT_EQ(match_pattern("aa", "*a"), 1);
43 	ASSERT_INT_EQ(match_pattern("ba", "a?"), 0);
44 	ASSERT_INT_EQ(match_pattern("ba", "a*"), 0);
45 	ASSERT_INT_EQ(match_pattern("ab", "?a"), 0);
46 	ASSERT_INT_EQ(match_pattern("ab", "*a"), 0);
47 	TEST_DONE();
48 
49 	TEST_START("match_pattern_list");
50 	ASSERT_INT_EQ(match_pattern_list("", "", 0), 0); /* no patterns */
51 	ASSERT_INT_EQ(match_pattern_list("", "*", 0), 1);
52 	ASSERT_INT_EQ(match_pattern_list("", "!*", 0), -1);
53 	ASSERT_INT_EQ(match_pattern_list("", "!a,*", 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), 0);
58 	ASSERT_INT_EQ(match_pattern_list("a", "*", 0), 1);
59 	ASSERT_INT_EQ(match_pattern_list("a", "!*", 0), -1);
60 	ASSERT_INT_EQ(match_pattern_list("a", "!a", 0), -1);
61 	/* XXX negated ASSERT_INT_EQ(match_pattern_list("a", "!b", 0), 1); */
62 	ASSERT_INT_EQ(match_pattern_list("a", "!a,*", 0), -1);
63 	ASSERT_INT_EQ(match_pattern_list("b", "!a,*", 0), 1);
64 	ASSERT_INT_EQ(match_pattern_list("a", "*,!a", 0), -1);
65 	ASSERT_INT_EQ(match_pattern_list("b", "*,!a", 0), 1);
66 	ASSERT_INT_EQ(match_pattern_list("a", "a,!*", 0), -1);
67 	ASSERT_INT_EQ(match_pattern_list("b", "a,!*", 0), -1);
68 	ASSERT_INT_EQ(match_pattern_list("a", "a,!a", 0), -1);
69 	/* XXX negated ASSERT_INT_EQ(match_pattern_list("b", "a,!a", 0), 1); */
70 	ASSERT_INT_EQ(match_pattern_list("a", "!*,a", 0), -1);
71 	ASSERT_INT_EQ(match_pattern_list("b", "!*,a", 0), -1);
72 	TEST_DONE();
73 
74 	TEST_START("match_pattern_list lowercase");
75 	ASSERT_INT_EQ(match_pattern_list("abc", "ABC", 0), 0);
76 	ASSERT_INT_EQ(match_pattern_list("ABC", "abc", 0), 0);
77 	ASSERT_INT_EQ(match_pattern_list("abc", "ABC", 1), 1);
78 	ASSERT_INT_EQ(match_pattern_list("ABC", "abc", 1), 0);
79 	TEST_DONE();
80 
81 	TEST_START("addr_match_list");
82 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.1/44"), -2);
83 	ASSERT_INT_EQ(addr_match_list(NULL, "127.0.0.1/44"), -2);
84 	ASSERT_INT_EQ(addr_match_list("a", "*"), 0);
85 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "*"), 1);
86 	ASSERT_INT_EQ(addr_match_list(NULL, "*"), 0);
87 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.1"), 1);
88 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.2"), 0);
89 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.1"), -1);
90 	/* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.2"), 1); */
91 	ASSERT_INT_EQ(addr_match_list("127.0.0.255", "127.0.0.0/24"), 1);
92 	ASSERT_INT_EQ(addr_match_list("127.0.1.1", "127.0.0.0/24"), 0);
93 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.0/24"), 1);
94 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.1.0/24"), 0);
95 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.0/24"), -1);
96 	/* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.1.0/24"), 1); */
97 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,!127.0.0.1"), -1);
98 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.1,10.0.0.1"), -1);
99 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,127.0.0.2"), 0);
100 	ASSERT_INT_EQ(addr_match_list("127.0.0.1", "127.0.0.2,10.0.0.1"), 0);
101 	/* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "10.0.0.1,!127.0.0.2"), 1); */
102 	/* XXX negated ASSERT_INT_EQ(addr_match_list("127.0.0.1", "!127.0.0.2,10.0.0.1"), 1); */
103 	TEST_DONE();
104 
105 #define CHECK_FILTER(string,filter,expected) \
106 	do { \
107 		char *result = match_filter_denylist((string), (filter)); \
108 		ASSERT_STRING_EQ(result, expected); \
109 		free(result); \
110 	} while (0)
111 
112 	TEST_START("match_filter_list");
113 	CHECK_FILTER("a,b,c", "", "a,b,c");
114 	CHECK_FILTER("a,b,c", "a", "b,c");
115 	CHECK_FILTER("a,b,c", "b", "a,c");
116 	CHECK_FILTER("a,b,c", "c", "a,b");
117 	CHECK_FILTER("a,b,c", "a,b", "c");
118 	CHECK_FILTER("a,b,c", "a,c", "b");
119 	CHECK_FILTER("a,b,c", "b,c", "a");
120 	CHECK_FILTER("a,b,c", "a,b,c", "");
121 	CHECK_FILTER("a,b,c", "b,c", "a");
122 	CHECK_FILTER("", "a,b,c", "");
123 	TEST_DONE();
124 /*
125  * XXX TODO
126  * int      match_host_and_ip(const char *, const char *, const char *);
127  * int      match_user(const char *, const char *, const char *, const char *);
128  * char    *match_list(const char *, const char *, u_int *);
129  * int      addr_match_cidr_list(const char *, const char *);
130  */
131 }
132