Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Copyright (C) 2018 David Disseldorp <ddiss@samba.org>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <stdarg.h>
21 : #include <stddef.h>
22 : #include <stdint.h>
23 : #include <setjmp.h>
24 : #include <cmocka.h>
25 :
26 : #include <errno.h>
27 :
28 : #include "lib/replace/replace.h"
29 : #include "lib/util/samba_util.h"
30 : #include "libcli/smb/smb_constants.h"
31 :
32 1 : static void test_ms_fn_match_protocol_no_wildcard(void **state)
33 : {
34 1 : int cmp;
35 :
36 : /* no wildcards in pattern, a simple strcasecmp_m */
37 1 : cmp = ms_fnmatch_protocol("pattern", "string", PROTOCOL_COREPLUS,
38 : true); /* case sensitive */
39 1 : assert_true(cmp < 0);
40 1 : }
41 :
42 1 : static void test_ms_fn_match_protocol_pattern_upgraded(void **state)
43 : {
44 1 : int cmp;
45 :
46 : /* protocol < PROTOCOL_NT1 pattern is "upgraded" */
47 1 : cmp = ms_fnmatch_protocol("??????", "string", PROTOCOL_COREPLUS,
48 : false);
49 1 : assert_int_equal(cmp, 0);
50 1 : }
51 :
52 1 : static void test_ms_fn_match_protocol_match_zero_or_more(void **state)
53 : {
54 1 : int cmp;
55 :
56 : /* '*' matches zero or more characters. handled via recursive calls */
57 1 : cmp = ms_fnmatch_protocol("********", "string", PROTOCOL_COREPLUS,
58 : true);
59 1 : assert_int_equal(cmp, 0);
60 1 : }
61 :
62 1 : static void test_ms_fn_match_protocol_mapped_char(void **state)
63 : {
64 1 : int cmp;
65 :
66 : /* '?' is mapped to '>', which matches any char or a '\0' */
67 1 : cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_COREPLUS,
68 : false);
69 1 : assert_int_equal(cmp, 0);
70 1 : }
71 :
72 1 : static void test_ms_fn_match_protocol_nt1_any_char(void **state)
73 : {
74 1 : int cmp;
75 :
76 : /* PROTOCOL_NT1 '?' matches any char, '\0' is not included */
77 1 : cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_NT1,
78 : false);
79 1 : assert_int_equal(cmp, -1);
80 1 : }
81 :
82 1 : static void test_ms_fn_match_protocol_nt1_case_sensitive(void **state)
83 : {
84 1 : int cmp;
85 :
86 1 : cmp = ms_fnmatch_protocol("StRinG", "string", PROTOCOL_NT1,
87 : true); /* case sensitive */
88 1 : assert_int_equal(cmp, 0);
89 :
90 1 : cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
91 : true); /* case sensitive */
92 1 : assert_int_equal(cmp, -1);
93 :
94 1 : cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
95 : false);
96 1 : assert_int_equal(cmp, 0);
97 1 : cmp = ms_fnmatch_protocol("strin?", "string", PROTOCOL_NT1,
98 : true); /* case sensitive */
99 1 : assert_int_equal(cmp, 0);
100 1 : }
101 :
102 1 : int main(void) {
103 1 : const struct CMUnitTest tests[] = {
104 : cmocka_unit_test(test_ms_fn_match_protocol_no_wildcard),
105 : cmocka_unit_test(test_ms_fn_match_protocol_pattern_upgraded),
106 : cmocka_unit_test(test_ms_fn_match_protocol_match_zero_or_more),
107 : cmocka_unit_test(test_ms_fn_match_protocol_mapped_char),
108 : cmocka_unit_test(test_ms_fn_match_protocol_nt1_any_char),
109 : cmocka_unit_test(test_ms_fn_match_protocol_nt1_case_sensitive),
110 : };
111 :
112 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
113 1 : return cmocka_run_group_tests(tests, NULL, NULL);
114 : }
|