2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2018 Andreas Schneider <asn@samba.org>
5 * Copyright (C) 2024 Douglas Bagnall <dbagnall@samba.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "ldb_private.h"
29 #include "ldb_handlers.h"
30 #include "util/tsort.h"
31 #include "ldb-samba/ldb_wrap.h"
34 #define debug_message(...) do { \
36 print_message(__VA_ARGS__); \
41 * We use sets of string values paired with integer rankings and make every
42 * pair-wise comparison (both ways: cmp(a,b) and cmp(b,a)). The strings should
43 * be consistently ordered in the same way as the integers.
45 * There are separate sets for the default ldb ASCII comparison, and for
46 * Samba's utf-8 aware comparisons, and a common set that both of them should
55 #define STR_VAL(s, r) { { discard_const(s), sizeof(s) - 1 }, r}
57 static const struct ranked_value values_common
[] = {
61 STR_VAL(" A\0 ignored-post-zero", 10),
66 STR_VAL("a\xc2\xfe", 30),
67 STR_VAL("a\xc2\xfe a", 32),
68 STR_VAL("a\xc2\xfe A", 32),
69 STR_VAL("a\xc2\xfe Ā", 35),
70 STR_VAL("A\xc2\xfe Ā", 35),
71 STR_VAL("a\xc2\xfe ā", 37),
72 STR_VAL("a\xff\xfe ā", 40),
76 STR_VAL("\xff\xfe", 1000),
79 static const struct ranked_value values_default_ascii
[] = {
89 STR_VAL("\xc8\xfe", 500),
92 STR_VAL("\xff\xfe", 1000),
95 static const struct ranked_value values_utf8
[] = {
106 STR_VAL("\xc8\xfe", 900),
108 STR_VAL("\xff\xfe", 1000),
116 static void _test_ldb_comparison_fold_set(struct ldb_context
*ldb
,
117 const struct ranked_value
*values
,
123 for (i
= 0; i
< n
; i
++) {
124 struct ranked_value a
= values
[i
];
125 for (j
= 0; j
< n
; j
++) {
126 struct ranked_value b
= values
[j
];
127 int ret
= ldb_comparison_fold(ldb
, NULL
, &a
.val
, &b
.val
);
128 if ((ret
< 0 && a
.rank
< b
.rank
) ||
129 (ret
== 0 && a
.rank
== b
.rank
) ||
130 (ret
> 0 && a
.rank
> b
.rank
)) {
133 debug_message("A {'%s', %zu} vs B {'%s', %zu} returned %d,"
134 "should be %d (%d - %d)\n",
135 a
.val
.data
, a
.val
.length
, b
.val
.data
, b
.val
.length
, ret
,
136 NUMERIC_CMP(a
.rank
, b
.rank
), a
.rank
, b
.rank
);
141 debug_message("%zu errors out of %zu\n", n_errors
, n
* n
);
143 assert_int_equal(n_errors
, 0);
148 * These tests are for the specific behaviour of the default ASCII-only
151 static void test_ldb_comparison_fold_default_ascii(void **state
)
153 struct ldb_context
*ldb
= ldb_init(NULL
, NULL
);
154 _test_ldb_comparison_fold_set(ldb
,
155 values_default_ascii
,
156 ARRAY_SIZE(values_default_ascii
));
161 * These tests are for behaviour with the default comparison, that should work
162 * the same with the Samba utf-8 comparison.
164 static void test_ldb_comparison_fold_default_common(void **state
)
166 struct ldb_context
*ldb
= ldb_init(NULL
, NULL
);
167 _test_ldb_comparison_fold_set(ldb
,
169 ARRAY_SIZE(values_common
));
174 * These tests are for behaviour with the Samba utf-8 comparison, that should
175 * work the same with the default ASCII comparison.
177 static void test_ldb_comparison_fold_utf8_common(void **state
)
179 struct ldb_context
*ldb
= ldb_init(NULL
, NULL
);
180 ldb_set_utf8_functions(ldb
, NULL
, wrap_casefold
, ldb_comparison_fold_utf8
);
181 _test_ldb_comparison_fold_set(ldb
,
183 ARRAY_SIZE(values_common
));
187 * These tests are for the specific behaviour of the default ASCII-only
190 static void test_ldb_comparison_fold_utf8(void **state
)
192 struct ldb_context
*ldb
= ldb_init(NULL
, NULL
);
193 ldb_set_utf8_functions(ldb
, NULL
, wrap_casefold
, ldb_comparison_fold_utf8
);
194 _test_ldb_comparison_fold_set(ldb
,
196 ARRAY_SIZE(values_utf8
));
203 const struct CMUnitTest tests
[] = {
204 cmocka_unit_test(test_ldb_comparison_fold_default_common
),
205 cmocka_unit_test(test_ldb_comparison_fold_default_ascii
),
206 cmocka_unit_test(test_ldb_comparison_fold_utf8_common
),
207 cmocka_unit_test(test_ldb_comparison_fold_utf8
),
210 cmocka_set_message_output(CM_OUTPUT_SUBUNIT
);
212 return cmocka_run_group_tests(tests
, NULL
, NULL
);