1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2014 Vincent Bernat <bernat@luffy.cx>
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.
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.
20 #include "../src/daemon/lldpd.h"
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);
28 START_TEST(test_simple_match
) {
29 ck_assert_int_eq(pattern_match("eth0", "eth0", 0), 2);
30 ck_assert_int_eq(pattern_match("eth0", "eth0", 1), 2);
31 ck_assert_int_eq(pattern_match("eth0", "eth1", 0), 0);
32 ck_assert_int_eq(pattern_match("eth0", "eth1", 1), 1);
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);
44 START_TEST(test_match_list
) {
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);
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);
52 START_TEST(test_match_list_with_wildcards
) {
53 ck_assert_int_eq(pattern_match("eth0", "eth0,eth*,eth2", 0), 2);
54 ck_assert_int_eq(pattern_match("eth1", "eth0,eth*,eth2", 0), 1);
55 ck_assert_int_eq(pattern_match("eth2", "eth0,eth*,eth2", 0), 2);
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);
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);
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);
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);
90 START_TEST(test_whitelist
) {
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);
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);
100 ck_assert_int_eq(pattern_match("eth0", "*,!eth*,!!eth0", 0), 2);
101 ck_assert_int_eq(pattern_match("eth0", "*,!eth*,!!eth0", 1), 2);
102 ck_assert_int_eq(pattern_match("eth1", "*,!eth*,!!eth0", 0), 0);
103 ck_assert_int_eq(pattern_match("eth1", "*,!!eth0,!eth*", 1), 0);
104 ck_assert_int_eq(pattern_match("eth0", "*,!!eth0,!eth*", 0), 2);
105 ck_assert_int_eq(pattern_match("eth0", "*,!!eth0,!eth*", 1), 2);
112 Suite
*s
= suite_create("Pattern matching");
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
);
123 tcase_add_test(tc_pattern
, test_whitelist
);
124 suite_add_tcase(s
, tc_pattern
);
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
);
138 return (number_failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;