tests: Add basic ms_fnmatch unit test
[metze/samba/wip.git] / lib / util / tests / test_ms_fnmatch.c
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 <setjmp.h>
23 #include <cmocka.h>
24
25 #include <errno.h>
26
27 #include "lib/replace/replace.h"
28 #include "lib/util/samba_util.h"
29 #include "libcli/smb/smb_constants.h"
30
31 static void test_ms_fn_match_protocol_no_wildcard(void **state)
32 {
33         int cmp;
34
35         /* no wildcards in pattern, a simple strcasecmp_m */
36         cmp = ms_fnmatch_protocol("pattern", "string", PROTOCOL_COREPLUS,
37                                   true);        /* case sensitive */
38         assert_int_equal(cmp, -3);
39 }
40
41 static void test_ms_fn_match_protocol_pattern_upgraded(void **state)
42 {
43         int cmp;
44
45         /* protocol < PROTOCOL_NT1 pattern is "upgraded" */
46         cmp = ms_fnmatch_protocol("??????", "string", PROTOCOL_COREPLUS,
47                                   false);
48         assert_int_equal(cmp, 0);
49 }
50
51 static void test_ms_fn_match_protocol_match_zero_or_more(void **state)
52 {
53         int cmp;
54
55         /* '*' matches zero or more characters. handled via recursive calls */
56         cmp = ms_fnmatch_protocol("********", "string", PROTOCOL_COREPLUS,
57                                   true);
58         assert_int_equal(cmp, 0);
59 }
60
61 static void test_ms_fn_match_protocol_mapped_char(void **state)
62 {
63         int cmp;
64
65         /* '?' is mapped to '>', which matches any char or a '\0' */
66         cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_COREPLUS,
67                                     false);
68         assert_int_equal(cmp, 0);
69 }
70
71 static void test_ms_fn_match_protocol_nt1_any_char(void **state)
72 {
73         int cmp;
74
75         /* PROTOCOL_NT1 '?' matches any char, '\0' is not included */
76         cmp = ms_fnmatch_protocol("???????", "string", PROTOCOL_NT1,
77                                   false);
78         assert_int_equal(cmp, -1);
79 }
80
81 static void test_ms_fn_match_protocol_nt1_case_sensitive(void **state)
82 {
83         int cmp;
84
85         cmp = ms_fnmatch_protocol("StRinG", "string", PROTOCOL_NT1,
86                                   true);        /* case sensitive */
87         assert_int_equal(cmp, 0);
88
89         cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
90                                   true);        /* case sensitive */
91         assert_int_equal(cmp, -1);
92
93         cmp = ms_fnmatch_protocol("StRin?", "string", PROTOCOL_NT1,
94                                   false);
95         assert_int_equal(cmp, 0);
96         cmp = ms_fnmatch_protocol("strin?", "string", PROTOCOL_NT1,
97                                   true);        /* case sensitive */
98         assert_int_equal(cmp, 0);
99 }
100
101 int main(void) {
102         const struct CMUnitTest tests[] = {
103                 cmocka_unit_test(test_ms_fn_match_protocol_no_wildcard),
104                 cmocka_unit_test(test_ms_fn_match_protocol_pattern_upgraded),
105                 cmocka_unit_test(test_ms_fn_match_protocol_match_zero_or_more),
106                 cmocka_unit_test(test_ms_fn_match_protocol_mapped_char),
107                 cmocka_unit_test(test_ms_fn_match_protocol_nt1_any_char),
108                 cmocka_unit_test(test_ms_fn_match_protocol_nt1_case_sensitive),
109         };
110
111         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
112         return cmocka_run_group_tests(tests, NULL, NULL);
113 }