]>
Commit | Line | Data |
---|---|---|
d5e69431 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2014 Vincent Bernat <bernat@luffy.cx> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or 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 <check.h> | |
19 | ||
20 | #include "../src/daemon/lldpd.h" | |
21 | ||
22 | START_TEST(test_empty) { | |
23 | ck_assert_int_eq(pattern_match("eth0", "", 0), 0); | |
24 | ck_assert_int_eq(pattern_match("eth0", "", 1), 1); | |
25 | } | |
26 | END_TEST | |
27 | ||
28 | START_TEST(test_simple_match) { | |
627e31c4 VB |
29 | ck_assert_int_eq(pattern_match("eth0", "eth0", 0), 2); |
30 | ck_assert_int_eq(pattern_match("eth0", "eth0", 1), 2); | |
d5e69431 VB |
31 | ck_assert_int_eq(pattern_match("eth0", "eth1", 0), 0); |
32 | ck_assert_int_eq(pattern_match("eth0", "eth1", 1), 1); | |
33 | } | |
34 | END_TEST | |
35 | ||
36 | START_TEST(test_wildcard) { | |
37 | ck_assert_int_eq(pattern_match("eth0", "eth*", 0), 1); | |
38 | ck_assert_int_eq(pattern_match("eth0", "eth*", 1), 1); | |
39 | ck_assert_int_eq(pattern_match("vlan0", "eth*", 0), 0); | |
40 | ck_assert_int_eq(pattern_match("vlan0", "eth*", 1), 1); | |
41 | } | |
42 | END_TEST | |
43 | ||
44 | START_TEST(test_match_list) { | |
627e31c4 VB |
45 | ck_assert_int_eq(pattern_match("eth0", "eth0,eth1,eth2", 0), 2); |
46 | ck_assert_int_eq(pattern_match("eth1", "eth0,eth1,eth2", 0), 2); | |
d5e69431 VB |
47 | ck_assert_int_eq(pattern_match("eth3", "eth0,eth1,eth2", 0), 0); |
48 | ck_assert_int_eq(pattern_match("eth3", "eth0,eth1,eth2", 1), 1); | |
49 | } | |
50 | END_TEST | |
51 | ||
52 | START_TEST(test_match_list_with_wildcards) { | |
627e31c4 | 53 | ck_assert_int_eq(pattern_match("eth0", "eth0,eth*,eth2", 0), 2); |
d5e69431 | 54 | ck_assert_int_eq(pattern_match("eth1", "eth0,eth*,eth2", 0), 1); |
627e31c4 | 55 | ck_assert_int_eq(pattern_match("eth2", "eth0,eth*,eth2", 0), 2); |
d5e69431 VB |
56 | ck_assert_int_eq(pattern_match("eth3", "eth0,eth*,eth2", 0), 1); |
57 | ck_assert_int_eq(pattern_match("vlan3", "eth0,eth*,eth2", 0), 0); | |
58 | ck_assert_int_eq(pattern_match("vlan3", "eth0,eth*,eth2", 1), 1); | |
59 | } | |
60 | END_TEST | |
61 | ||
62 | START_TEST(test_simple_blacklist) { | |
63 | ck_assert_int_eq(pattern_match("eth0", "!eth0", 0), 0); | |
64 | ck_assert_int_eq(pattern_match("eth0", "!eth0", 1), 0); | |
65 | ck_assert_int_eq(pattern_match("eth1", "!eth0", 0), 0); | |
66 | ck_assert_int_eq(pattern_match("eth1", "!eth0", 1), 1); | |
67 | } | |
68 | END_TEST | |
69 | ||
70 | START_TEST(test_match_and_blacklist) { | |
71 | ck_assert_int_eq(pattern_match("eth0", "eth0,!eth0", 0), 0); | |
72 | ck_assert_int_eq(pattern_match("eth0", "eth0,!eth0", 1), 0); | |
73 | ck_assert_int_eq(pattern_match("eth1", "eth0,!eth0", 0), 0); | |
74 | ck_assert_int_eq(pattern_match("eth1", "eth0,!eth0", 1), 1); | |
75 | } | |
76 | END_TEST | |
77 | ||
78 | START_TEST(test_blacklist_wildcard) { | |
79 | ck_assert_int_eq(pattern_match("eth0", "!eth*", 0), 0); | |
80 | ck_assert_int_eq(pattern_match("eth0", "!eth*", 1), 0); | |
81 | ck_assert_int_eq(pattern_match("eth1", "!eth*", 0), 0); | |
82 | ck_assert_int_eq(pattern_match("eth1", "!eth*", 1), 0); | |
83 | ck_assert_int_eq(pattern_match("eth1", "eth*,!eth1", 0), 0); | |
84 | ck_assert_int_eq(pattern_match("eth1", "eth*,!eth1", 1), 0); | |
85 | ck_assert_int_eq(pattern_match("eth0", "eth*,!eth1", 0), 1); | |
86 | ck_assert_int_eq(pattern_match("eth0", "eth*,!eth1", 1), 1); | |
87 | } | |
88 | END_TEST | |
89 | ||
8b53fc2c | 90 | START_TEST(test_whitelist) { |
627e31c4 VB |
91 | ck_assert_int_eq(pattern_match("eth0", "!!eth0", 0), 2); |
92 | ck_assert_int_eq(pattern_match("eth0", "!!eth0", 1), 2); | |
93 | ck_assert_int_eq(pattern_match("eth1", "!!eth0", 1), 1); | |
94 | ck_assert_int_eq(pattern_match("eth0", "!eth*,!!eth0", 0), 2); | |
95 | ck_assert_int_eq(pattern_match("eth0", "!eth*,!!eth0", 1), 2); | |
8b53fc2c VB |
96 | ck_assert_int_eq(pattern_match("eth1", "!eth*,!!eth0", 0), 0); |
97 | ck_assert_int_eq(pattern_match("eth1", "!eth*,!!eth0", 1), 0); | |
98 | ck_assert_int_eq(pattern_match("vlan0", "*,!eth*,!!eth0", 0), 1); | |
99 | ck_assert_int_eq(pattern_match("vlan0", "*,!eth*,!!eth0", 1), 1); | |
627e31c4 VB |
100 | ck_assert_int_eq(pattern_match("eth0", "*,!eth*,!!eth0", 0), 2); |
101 | ck_assert_int_eq(pattern_match("eth0", "*,!eth*,!!eth0", 1), 2); | |
8b53fc2c VB |
102 | ck_assert_int_eq(pattern_match("eth1", "*,!eth*,!!eth0", 0), 0); |
103 | ck_assert_int_eq(pattern_match("eth1", "*,!!eth0,!eth*", 1), 0); | |
627e31c4 VB |
104 | ck_assert_int_eq(pattern_match("eth0", "*,!!eth0,!eth*", 0), 2); |
105 | ck_assert_int_eq(pattern_match("eth0", "*,!!eth0,!eth*", 1), 2); | |
8b53fc2c VB |
106 | } |
107 | END_TEST | |
108 | ||
d5e69431 VB |
109 | Suite * |
110 | pattern_suite(void) | |
111 | { | |
112 | Suite *s = suite_create("Pattern matching"); | |
113 | ||
114 | TCase *tc_pattern = tcase_create("Pattern matching"); | |
115 | tcase_add_test(tc_pattern, test_empty); | |
116 | tcase_add_test(tc_pattern, test_simple_match); | |
117 | tcase_add_test(tc_pattern, test_wildcard); | |
118 | tcase_add_test(tc_pattern, test_match_list); | |
119 | tcase_add_test(tc_pattern, test_match_list_with_wildcards); | |
120 | tcase_add_test(tc_pattern, test_simple_blacklist); | |
121 | tcase_add_test(tc_pattern, test_match_and_blacklist); | |
122 | tcase_add_test(tc_pattern, test_blacklist_wildcard); | |
8b53fc2c | 123 | tcase_add_test(tc_pattern, test_whitelist); |
d5e69431 VB |
124 | suite_add_tcase(s, tc_pattern); |
125 | ||
126 | return s; | |
127 | } | |
128 | ||
129 | int | |
130 | main() | |
131 | { | |
132 | int number_failed; | |
133 | Suite *s = pattern_suite(); | |
134 | SRunner *sr = srunner_create(s); | |
135 | srunner_run_all(sr, CK_ENV); | |
136 | number_failed = srunner_ntests_failed(sr); | |
137 | srunner_free(sr); | |
138 | return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | |
139 | } |