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