ctdb-daemon: Use ctdb_parse_node_address() in ctdbd
[samba4-gss.git] / lib / ldb / tests / test_ldb_comparison_fold.c
blob601cfe6c482a52bb0079a32c5c11df9242de63ee
1 /*
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/>.
21 #include <stdarg.h>
22 #include <stddef.h>
23 #include <setjmp.h>
24 #include <cmocka.h>
25 #include <stdbool.h>
26 #include "replace.h"
27 #include "ldb.h"
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 { \
35 if (isatty(1)) { \
36 print_message(__VA_ARGS__); \
37 } \
38 } while(0)
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
47 * pass.
50 struct ranked_value {
51 struct ldb_val val;
52 int rank;
55 #define STR_VAL(s, r) { { discard_const(s), sizeof(s) - 1 }, r}
57 static const struct ranked_value values_common[] = {
58 STR_VAL("", 0),
59 STR_VAL(" ", 0),
60 STR_VAL("a", 10),
61 STR_VAL(" A\0 ignored-post-zero", 10),
62 STR_VAL("a b", 15),
63 STR_VAL("a B ", 15),
64 STR_VAL(" A b", 15),
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),
74 STR_VAL("b", 50),
76 STR_VAL("\xff\xfe", 1000),
79 static const struct ranked_value values_default_ascii[] = {
80 STR_VAL(" a", 1),
82 STR_VAL("b", 50),
83 STR_VAL("Ā", 256),
84 STR_VAL(" Ā", 256),
85 STR_VAL("ā", 257),
86 STR_VAL("ā ", 257),
88 STR_VAL("Ʊ", 433),
89 STR_VAL("\xc8\xfe", 500),
90 STR_VAL("ʊ", 650),
92 STR_VAL("\xff\xfe", 1000),
95 static const struct ranked_value values_utf8[] = {
96 STR_VAL(" a", 1),
98 STR_VAL("b", 50),
99 STR_VAL("Ā", 256),
100 STR_VAL(" Ā", 256),
101 STR_VAL("ā", 256),
102 STR_VAL("ā ", 256),
104 STR_VAL("Ʊ", 433),
105 STR_VAL("ʊ", 433),
106 STR_VAL("\xc8\xfe", 900),
108 STR_VAL("\xff\xfe", 1000),
113 #undef STR_VAL
116 static void _test_ldb_comparison_fold_set(struct ldb_context *ldb,
117 const struct ranked_value *values,
118 size_t n)
120 size_t i, j;
121 size_t n_errors = 0;
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)) {
131 continue;
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);
138 n_errors++;
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
149 * casefold.
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,
168 values_common,
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,
182 values_common,
183 ARRAY_SIZE(values_common));
187 * These tests are for the specific behaviour of the default ASCII-only
188 * casefold.
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,
195 values_utf8,
196 ARRAY_SIZE(values_utf8));
202 int main(void) {
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),
209 if (!isatty(1)) {
210 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
212 return cmocka_run_group_tests(tests, NULL, NULL);