1 /* $OpenBSD: tests.c,v 1.4 2017/02/03 23:01:42 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 23 void 24 tests(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 #define CHECK_FILTER(string,filter,expected) \ 107 do { \ 108 char *result = match_filter_list((string), (filter)); \ 109 ASSERT_STRING_EQ(result, expected); \ 110 free(result); \ 111 } while (0) 112 113 TEST_START("match_filter_list"); 114 CHECK_FILTER("a,b,c", "", "a,b,c"); 115 CHECK_FILTER("a,b,c", "a", "b,c"); 116 CHECK_FILTER("a,b,c", "b", "a,c"); 117 CHECK_FILTER("a,b,c", "c", "a,b"); 118 CHECK_FILTER("a,b,c", "a,b", "c"); 119 CHECK_FILTER("a,b,c", "a,c", "b"); 120 CHECK_FILTER("a,b,c", "b,c", "a"); 121 CHECK_FILTER("a,b,c", "a,b,c", ""); 122 CHECK_FILTER("a,b,c", "b,c", "a"); 123 CHECK_FILTER("", "a,b,c", ""); 124 TEST_DONE(); 125 /* 126 * XXX TODO 127 * int match_host_and_ip(const char *, const char *, const char *); 128 * int match_user(const char *, const char *, const char *, const char *); 129 * char *match_list(const char *, const char *, u_int *); 130 * int addr_match_cidr_list(const char *, const char *); 131 */ 132 } 133