1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
|
/*
* Copyright 2024-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mongocrypt.h"
#include "test-mongocrypt-assert.h"
#include "test-mongocrypt.h"
#include "mc-fle2-encryption-placeholder-private.h"
#include "mc-str-encode-string-sets-private.h"
#include "mc-text-search-str-encode-private.h"
#include "unicode/fold.h"
#include <stdint.h>
#include <string.h>
static uint32_t get_utf8_codepoint_length(const char *buf, uint32_t len) {
const char *cur = buf;
const char *end = buf + len;
uint32_t codepoint_len = 0;
while (cur < end) {
cur = bson_utf8_next_char(cur);
codepoint_len++;
}
return codepoint_len;
}
static void test_nofold_suffix_prefix_case(_mongocrypt_tester_t *tester,
const char *str,
uint32_t lb,
uint32_t ub,
bool casef,
bool diacf,
int foldable_codepoints) {
TEST_PRINTF("Testing nofold suffix/prefix case: str=\"%s\", lb=%u, ub=%u, casef=%d, diacf=%d\n",
str,
lb,
ub,
casef,
diacf);
uint32_t byte_len = (uint32_t)strlen(str);
uint32_t unfolded_codepoint_len = byte_len == 0 ? 1 : get_utf8_codepoint_length(str, byte_len);
uint32_t folded_codepoint_len = byte_len == 0 ? 0 : unfolded_codepoint_len - foldable_codepoints;
uint32_t padded_len = 16 * (uint32_t)((byte_len + 5 + 15) / 16) - 5;
uint32_t max_affix_len = BSON_MIN(ub, folded_codepoint_len);
uint32_t n_real_affixes = max_affix_len >= lb ? max_affix_len - lb + 1 : 0;
uint32_t n_affixes = BSON_MIN(ub, padded_len) - lb + 1;
uint32_t n_padding = n_affixes - n_real_affixes;
mc_str_encode_sets_t *sets;
mongocrypt_status_t *status = mongocrypt_status_new();
for (int suffix = 0; suffix <= 1; suffix++) {
if (suffix) {
mc_FLE2TextSearchInsertSpec_t spec = {.v = str,
.len = byte_len,
.suffix = {{lb, ub}, true},
.casef = casef,
.diacf = diacf};
sets = mc_text_search_str_encode(&spec, status);
} else {
mc_FLE2TextSearchInsertSpec_t spec = {.v = str,
.len = byte_len,
.prefix = {{lb, ub}, true},
.casef = casef,
.diacf = diacf};
sets = mc_text_search_str_encode(&spec, status);
}
ASSERT_OR_PRINT(sets, status);
ASSERT_CMPUINT32(sets->base_string->codepoint_len, ==, folded_codepoint_len + 1);
if (!casef && !diacf) {
ASSERT_CMPUINT32(sets->base_string->buf.len, ==, byte_len + 1);
ASSERT_CMPINT(0, ==, memcmp(sets->base_string->buf.data, str, byte_len));
}
ASSERT_CMPUINT8(sets->base_string->buf.data[sets->base_string->buf.len - 1], ==, (uint8_t)0xFF);
ASSERT(sets->substring_set == NULL);
ASSERT_CMPUINT32(sets->exact.len, ==, sets->base_string->buf.len - 1);
ASSERT_CMPINT(0, ==, memcmp(sets->exact.data, sets->base_string->buf.data, sets->exact.len));
if (lb > padded_len) {
ASSERT(sets->suffix_set == NULL);
ASSERT(sets->prefix_set == NULL);
ASSERT_CMPUINT32(sets->msize, ==, 1 /* for exact string */);
goto CONTINUE;
}
ASSERT_CMPUINT32(sets->msize, ==, n_affixes + 1 /* for exact string */);
TEST_PRINTF("Expecting: n_real_affixes: %u, n_affixes: %u, n_padding: %u\n",
n_real_affixes,
n_affixes,
n_padding);
mc_affix_set_t *set;
if (suffix) {
ASSERT(sets->prefix_set == NULL);
set = sets->suffix_set;
} else {
ASSERT(sets->suffix_set == NULL);
set = sets->prefix_set;
}
ASSERT(set != NULL);
mc_affix_set_iter_t it;
mc_affix_set_iter_init(&it, set);
const char *affix;
uint32_t idx = 0;
uint32_t affix_len = 0;
uint32_t affix_count = 0;
uint32_t total_real_affix_count = 0;
while (mc_affix_set_iter_next(&it, &affix, &affix_len, &affix_count)) {
// Since all substrings are just views on the base string, we can use pointer math to find our start and
// end indices.
TEST_PRINTF("Affix starting %lld, ending %lld, count %u\n",
(long long)((uint8_t *)affix - sets->base_string->buf.data),
(long long)((uint8_t *)affix - sets->base_string->buf.data + affix_len),
affix_count);
if (affix_len == sets->base_string->buf.len) {
// This is padding, so there should be no more entries due to how we ordered them
ASSERT(!mc_affix_set_iter_next(&it, NULL, NULL, NULL));
break;
}
ASSERT_CMPUINT32(affix_len, <=, sets->base_string->buf.len - 1);
ASSERT_CMPUINT32(0, <, affix_len);
// We happen to always order from smallest to largest in the suffix/prefix algorithm, which makes our
// life slightly easier when testing.
if (suffix) {
uint32_t start_offset = sets->base_string->codepoint_offsets[folded_codepoint_len - (lb + idx)];
ASSERT_CMPPTR((uint8_t *)affix, ==, sets->base_string->buf.data + start_offset);
ASSERT_CMPUINT32(affix_len,
==,
sets->base_string->codepoint_offsets[folded_codepoint_len] - start_offset);
} else {
uint32_t end_offset = sets->base_string->codepoint_offsets[lb + idx];
ASSERT_CMPPTR((uint8_t *)affix, ==, sets->base_string->buf.data);
ASSERT_CMPUINT32(affix_len, ==, end_offset);
}
// The count should always be 1, except for padding.
ASSERT_CMPUINT32(1, ==, affix_count);
total_real_affix_count++;
idx++;
}
ASSERT_CMPUINT32(total_real_affix_count, ==, n_real_affixes);
if (affix_len == sets->base_string->buf.len) {
// Padding
ASSERT_CMPPTR((uint8_t *)affix, ==, sets->base_string->buf.data);
ASSERT_CMPUINT32(affix_count, ==, n_padding);
} else {
// No padding found
ASSERT_CMPUINT32(n_padding, ==, 0);
}
CONTINUE:
mc_str_encode_sets_destroy(sets);
}
mongocrypt_status_destroy(status);
}
static uint32_t calc_number_of_substrings(uint32_t len, uint32_t lb, uint32_t ub) {
uint32_t ret = 0;
// Calculate the long way to make sure our math in calc_number_of_substrings is correct
for (uint32_t i = 0; i < len; i++) {
uint32_t max_sublen = BSON_MIN(ub, len - i);
uint32_t n_substrings = max_sublen < lb ? 0 : max_sublen - lb + 1;
ret += n_substrings;
}
return ret;
}
static uint32_t calc_unique_substrings(const mc_utf8_string_with_bad_char_t *str, uint32_t lb, uint32_t ub) {
uint32_t len = str->codepoint_len - 1; // eliminate last 0xff CP
if (len < lb) {
return 0;
}
// Bruteforce to make sure our hashset is working as expected.
uint8_t *idx_is_dupe = bson_malloc0(len);
uint32_t dupes = 0;
for (uint32_t ss_len = lb; ss_len <= BSON_MIN(len, ub); ss_len++) {
for (uint32_t i = 0; i < len - ss_len; i++) {
// Already checked
if (idx_is_dupe[i]) {
continue;
}
for (uint32_t j = i + 1; j <= len - ss_len; j++) {
// Already counted
if (idx_is_dupe[j]) {
continue;
}
uint32_t i_start_byte = str->codepoint_offsets[i];
uint32_t i_end_byte = str->codepoint_offsets[i + ss_len];
uint32_t j_start_byte = str->codepoint_offsets[j];
uint32_t j_end_byte = str->codepoint_offsets[j + ss_len];
if (i_end_byte - i_start_byte == j_end_byte - j_start_byte
&& memcmp(&str->buf.data[i_start_byte], &str->buf.data[j_start_byte], i_end_byte - i_start_byte)
== 0) {
idx_is_dupe[j] = 1;
dupes++;
}
}
}
memset(idx_is_dupe, 0, len);
}
bson_free(idx_is_dupe);
return calc_number_of_substrings(len, lb, ub) - dupes;
}
static void test_nofold_substring_case(_mongocrypt_tester_t *tester,
const char *str,
uint32_t lb,
uint32_t ub,
uint32_t mlen,
bool casef,
bool diacf,
int foldable_codepoints) {
TEST_PRINTF("Testing nofold substring case: str=\"%s\", lb=%u, ub=%u, mlen=%u, casef=%d, diacf=%d\n",
str,
lb,
ub,
mlen,
casef,
diacf);
uint32_t byte_len = (uint32_t)strlen(str);
uint32_t unfolded_codepoint_len = byte_len == 0 ? 1 : get_utf8_codepoint_length(str, byte_len);
uint32_t folded_codepoint_len = byte_len == 0 ? 0 : unfolded_codepoint_len - foldable_codepoints;
uint32_t padded_len = 16 * (uint32_t)((byte_len + 5 + 15) / 16) - 5;
uint32_t n_substrings = calc_number_of_substrings(BSON_MIN(padded_len, mlen), lb, ub);
mongocrypt_status_t *status = mongocrypt_status_new();
mc_str_encode_sets_t *sets;
mc_FLE2TextSearchInsertSpec_t spec = {.v = str,
.len = byte_len,
.substr = {{mlen, lb, ub}, true},
.casef = casef,
.diacf = diacf};
sets = mc_text_search_str_encode(&spec, status);
if (unfolded_codepoint_len > mlen) {
ASSERT_FAILS_STATUS(sets, status, "longer than the maximum length");
mongocrypt_status_destroy(status);
return;
}
ASSERT_OR_PRINT(sets, status);
mongocrypt_status_destroy(status);
ASSERT_CMPUINT32(sets->base_string->codepoint_len, ==, folded_codepoint_len + 1);
if (!casef && !diacf) {
ASSERT_CMPUINT32(sets->base_string->buf.len, ==, byte_len + 1);
ASSERT_CMPINT(0, ==, memcmp(sets->base_string->buf.data, str, byte_len));
}
ASSERT_CMPUINT8(sets->base_string->buf.data[sets->base_string->buf.len - 1], ==, (uint8_t)0xFF);
ASSERT(sets->suffix_set == NULL);
ASSERT(sets->prefix_set == NULL);
ASSERT_CMPUINT32(sets->exact.len, ==, sets->base_string->buf.len - 1);
ASSERT_CMPINT(0, ==, memcmp(sets->exact.data, sets->base_string->buf.data, sets->base_string->buf.len - 1));
if (lb > padded_len) {
ASSERT(sets->substring_set == NULL);
ASSERT_CMPUINT32(sets->msize, ==, 1 /* for exact string */);
goto cleanup;
} else {
ASSERT(sets->substring_set != NULL);
}
ASSERT_CMPUINT32(sets->msize, ==, n_substrings + 1 /* for exact string */);
uint32_t n_real_substrings = calc_unique_substrings(sets->base_string, lb, ub);
uint32_t n_padding = n_substrings - n_real_substrings;
TEST_PRINTF("Expecting: n_real_substrings: %u, n_substrings: %u, n_padding: %u\n",
n_real_substrings,
n_substrings,
n_padding);
mc_substring_set_t *set = sets->substring_set;
mc_substring_set_iter_t it;
mc_substring_set_iter_init(&it, set);
const char *substring;
uint32_t substring_len = 0;
uint32_t substring_count = 0;
uint32_t total_real_substring_count = 0;
while (mc_substring_set_iter_next(&it, &substring, &substring_len, &substring_count)) {
TEST_PRINTF("Substring starting %lld, ending %lld, count %u: \"%.*s\"\n",
(long long)((uint8_t *)substring - sets->base_string->buf.data),
(long long)((uint8_t *)substring - sets->base_string->buf.data + substring_len),
substring_count,
substring_len,
substring);
if (substring_len == sets->base_string->buf.len) {
// This is padding, so there should be no more entries due to how we ordered them
ASSERT(!mc_substring_set_iter_next(&it, NULL, NULL, NULL));
break;
}
ASSERT_CMPPTR((uint8_t *)substring + substring_len,
<=,
sets->base_string->buf.data + sets->base_string->buf.len);
ASSERT_CMPUINT32(substring_len, <=, sets->base_string->buf.len - 1);
ASSERT_CMPUINT32(0, <, substring_len);
ASSERT_CMPUINT32(1, ==, substring_count);
total_real_substring_count++;
}
ASSERT_CMPUINT32(total_real_substring_count, ==, n_real_substrings);
if (substring_len == sets->base_string->buf.len) {
// Padding
ASSERT_CMPPTR((uint8_t *)substring, ==, sets->base_string->buf.data);
ASSERT_CMPUINT32(substring_count, ==, n_padding);
} else {
// No padding found
ASSERT_CMPUINT32(n_padding, ==, 0);
}
cleanup:
mc_str_encode_sets_destroy(sets);
}
static void test_nofold_substring_case_multiple_mlen(_mongocrypt_tester_t *tester,
const char *str,
uint32_t lb,
uint32_t ub,
uint32_t unfolded_codepoint_len,
bool casef,
bool diacf,
int foldable_codepoints) {
if (unfolded_codepoint_len > 1) {
// mlen < unfolded_codepoint_len
test_nofold_substring_case(tester, str, lb, ub, unfolded_codepoint_len - 1, casef, diacf, foldable_codepoints);
}
// mlen = unfolded_codepoint_len
test_nofold_substring_case(tester, str, lb, ub, unfolded_codepoint_len, casef, diacf, foldable_codepoints);
// mlen > unfolded_codepoint_len
test_nofold_substring_case(tester, str, lb, ub, unfolded_codepoint_len + 1, casef, diacf, foldable_codepoints);
// mlen >> unfolded_codepoint_len
test_nofold_substring_case(tester, str, lb, ub, unfolded_codepoint_len + 64, casef, diacf, foldable_codepoints);
uint32_t byte_len = (uint32_t)strlen(str);
if (byte_len > 1) {
// mlen < byte_len
test_nofold_substring_case(tester, str, lb, ub, byte_len - 1, casef, diacf, foldable_codepoints);
}
if (byte_len > 0) {
// mlen = byte_len
test_nofold_substring_case(tester, str, lb, ub, byte_len, casef, diacf, foldable_codepoints);
}
// mlen > byte_len
test_nofold_substring_case(tester, str, lb, ub, byte_len + 1, casef, diacf, foldable_codepoints);
// mlen = padded_len
test_nofold_substring_case(tester,
str,
lb,
ub,
16 * (uint32_t)((byte_len + 5 + 15) / 16) - 5,
casef,
diacf,
foldable_codepoints);
// mlen >> byte_len
test_nofold_substring_case(tester, str, lb, ub, byte_len + 64, casef, diacf, foldable_codepoints);
}
static const char *normal_ascii_strings[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
static const char *ascii_diacritics[] = {"^", "`"};
static const char *normal_unicode_strings[] = {"ぁ", "あ", "ぃ", "い", "ぅ", "う", "ぇ", "え", "ぉ", "お", "か", "が",
"き", "ぎ", "く", "け", "Ѐ", "Ё", "Ђ", "Ѓ", "Є", "Ѕ", "І", "Ї",
"Ј", "Љ", "Њ", "Ћ", "Ќ", "Ѝ", "Ў", "Џ", "𓀀", "𓀁", "𓀂", "𓀃",
"𓀄", "𓀅", "𓀆", "𓀇", "𓀈", "𓀉", "𓀊", "𓀋", "𓀌", "𓀍", "𓀎", "𓀏"};
static const char *unicode_diacritics[] = {"̀", "́", "̂", "̃", "̄", "̅", "̆", "̇", "̈", "̉", "̊", "̋", "̌", "̍", "̎",
"̏", "᷄", "᷅", "᷆", "᷇", "᷈", "᷉", "᷊", "᷋", "᷌", "᷍", "᷎", "᷏", "︠", "︡",
"︢", "︣", "︤", "︥", "︦", "︧", "︨", "︩", "︪", "︫", "︬", "︭", "︮", "︯"};
// Build a random string which has unfolded_len codepoints, but folds to folded_len codepoints after diacritic folding.
static char *build_random_string_to_fold(uint32_t folded_len, uint32_t unfolded_len) {
// 1/3 to generate all unicode, 1/3 to be half and half, 1/3 to be all ascii.
int ascii_ratio = rand() % 3;
ASSERT_CMPUINT32(unfolded_len, >=, folded_len);
// Max size in bytes is # unicode characters * 4 bytes for each character + 1 null terminator.
char *str = malloc(unfolded_len * 4 + 1);
char *ptr = str;
uint32_t folded_size = 0;
uint32_t diacritics = unfolded_len - folded_len;
int dia_prob = (diacritics * 1000) / unfolded_len;
for (uint32_t n_codepoints = 0; n_codepoints < unfolded_len; n_codepoints++) {
const char *src_ptr;
bool must_add_diacritic = folded_size == folded_len;
bool must_add_normal = n_codepoints - folded_size == diacritics;
if (must_add_diacritic || (!must_add_normal && (rand() % 1000 < dia_prob))) {
// Add diacritic.
if (rand() % 2 < ascii_ratio) {
int i = rand() % (sizeof(ascii_diacritics) / sizeof(char *));
src_ptr = ascii_diacritics[i];
} else {
int i = rand() % (sizeof(unicode_diacritics) / sizeof(char *));
src_ptr = unicode_diacritics[i];
}
} else {
// Add normal character.
if (rand() % 2 < ascii_ratio) {
int i = rand() % (sizeof(normal_ascii_strings) / sizeof(char *));
src_ptr = normal_ascii_strings[i];
} else {
int i = rand() % (sizeof(normal_unicode_strings) / sizeof(char *));
src_ptr = normal_unicode_strings[i];
}
folded_size++;
}
strcpy(ptr, src_ptr);
ptr += strlen(src_ptr);
}
uint32_t len = (uint32_t)(ptr - str);
// ptr points to the final null character, include that in the final string.
str = realloc(str, len + 1);
// Make sure we did everything right.
ASSERT_CMPUINT32(unfolded_len, ==, get_utf8_codepoint_length(str, len));
mongocrypt_status_t *status = mongocrypt_status_new();
char *out_str;
size_t out_len;
ASSERT_OK_STATUS(unicode_fold(str, len, kUnicodeFoldRemoveDiacritics, &out_str, &out_len, status), status);
ASSERT_CMPUINT32(folded_len, ==, get_utf8_codepoint_length(out_str, (uint32_t)out_len));
bson_free(out_str);
mongocrypt_status_destroy(status);
return str;
}
static void suffix_prefix_run_folding_case(_mongocrypt_tester_t *tester,
const char *short_s,
const char *medium_s,
const char *long_s,
bool casef,
bool diacf,
int foldable_codepoints) {
// LB > 16
test_nofold_suffix_prefix_case(tester, short_s, 17, 19, casef, diacf, foldable_codepoints);
// Simple cases
test_nofold_suffix_prefix_case(tester, short_s, 2, 4, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 3, 6, casef, diacf, foldable_codepoints);
// LB = UB
test_nofold_suffix_prefix_case(tester, short_s, 2, 2, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 9, 9, casef, diacf, foldable_codepoints);
// UB = len
test_nofold_suffix_prefix_case(tester, short_s, 2, 9, casef, diacf, foldable_codepoints);
// 16 > UB > len
test_nofold_suffix_prefix_case(tester, short_s, 2, 14, casef, diacf, foldable_codepoints);
// UB = 16
test_nofold_suffix_prefix_case(tester, short_s, 2, 16, casef, diacf, foldable_codepoints);
// UB > 16
test_nofold_suffix_prefix_case(tester, short_s, 2, 19, casef, diacf, foldable_codepoints);
// UB > 32
test_nofold_suffix_prefix_case(tester, short_s, 2, 35, casef, diacf, foldable_codepoints);
// 16 >= LB > len
test_nofold_suffix_prefix_case(tester, short_s, 12, 19, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 12, 16, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 16, 19, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 12, 35, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, short_s, 16, 35, casef, diacf, foldable_codepoints);
// len = 16 cases
// LB > 16
test_nofold_suffix_prefix_case(tester, medium_s, 17, 19, casef, diacf, foldable_codepoints);
// Simple cases
test_nofold_suffix_prefix_case(tester, medium_s, 2, 4, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, medium_s, 3, 6, casef, diacf, foldable_codepoints);
// LB = UB
test_nofold_suffix_prefix_case(tester, medium_s, 2, 2, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, medium_s, 16, 16, casef, diacf, foldable_codepoints);
// UB = len
test_nofold_suffix_prefix_case(tester, medium_s, 2, 16, casef, diacf, foldable_codepoints);
// UB > len
test_nofold_suffix_prefix_case(tester, medium_s, 2, 19, casef, diacf, foldable_codepoints);
// UB = 32
test_nofold_suffix_prefix_case(tester, medium_s, 2, 32, casef, diacf, foldable_codepoints);
// UB > 32
test_nofold_suffix_prefix_case(tester, medium_s, 2, 35, casef, diacf, foldable_codepoints);
// LB = len
test_nofold_suffix_prefix_case(tester, medium_s, 16, 19, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, medium_s, 16, 35, casef, diacf, foldable_codepoints);
// len > 16 cases
// LB > 32
test_nofold_suffix_prefix_case(tester, long_s, 33, 38, casef, diacf, foldable_codepoints);
// Simple cases
test_nofold_suffix_prefix_case(tester, long_s, 2, 4, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 3, 6, casef, diacf, foldable_codepoints);
// LB < 16 <= UB <= len
test_nofold_suffix_prefix_case(tester, long_s, 3, 18, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 3, 16, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 3, 27, casef, diacf, foldable_codepoints);
// 16 <= LB < UB <= len
test_nofold_suffix_prefix_case(tester, long_s, 18, 24, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 16, 24, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 18, 27, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 16, 27, casef, diacf, foldable_codepoints);
// LB = UB
test_nofold_suffix_prefix_case(tester, long_s, 3, 3, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 16, 16, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 27, 27, casef, diacf, foldable_codepoints);
// 32 > UB > len
test_nofold_suffix_prefix_case(tester, long_s, 3, 29, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 18, 29, casef, diacf, foldable_codepoints);
// UB = 32
test_nofold_suffix_prefix_case(tester, long_s, 3, 32, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 18, 32, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 27, 32, casef, diacf, foldable_codepoints);
// UB > 32
test_nofold_suffix_prefix_case(tester, long_s, 3, 35, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 18, 35, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 27, 32, casef, diacf, foldable_codepoints);
// UB > 48
test_nofold_suffix_prefix_case(tester, long_s, 3, 49, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 18, 49, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 27, 32, casef, diacf, foldable_codepoints);
// 32 >= LB > len
test_nofold_suffix_prefix_case(tester, long_s, 28, 30, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 28, 28, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 28, 32, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 28, 34, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 28, 49, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 32, 32, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 32, 34, casef, diacf, foldable_codepoints);
test_nofold_suffix_prefix_case(tester, long_s, 32, 49, casef, diacf, foldable_codepoints);
}
static const uint32_t UNFOLDED_CASES[] = {0, 1, 3, 16};
// Predefined lengths to test a variety of cases
static const uint32_t SHORT_LEN = 9;
static const uint32_t MEDIUM_LEN = 16;
static const uint32_t LONG_LEN = 27;
static void _test_text_search_str_encode_suffix_prefix(_mongocrypt_tester_t *tester) {
unsigned int seed = (unsigned int)time(0);
TEST_PRINTF("Testing with seed: %u", seed);
srand(seed);
// Run diacritic folding and case+diacritic folding for a variety of folded/unfolded sizes.
for (uint32_t i = 0; i < sizeof(UNFOLDED_CASES) / sizeof(UNFOLDED_CASES[0]); i++) {
char *short_s = build_random_string_to_fold(SHORT_LEN, SHORT_LEN + UNFOLDED_CASES[i]);
char *medium_s = build_random_string_to_fold(MEDIUM_LEN, MEDIUM_LEN + UNFOLDED_CASES[i]);
char *long_s = build_random_string_to_fold(LONG_LEN, LONG_LEN + UNFOLDED_CASES[i]);
for (int casef = 0; casef <= 1; casef++) {
suffix_prefix_run_folding_case(tester,
short_s,
medium_s,
long_s,
casef,
true /* diacf */,
UNFOLDED_CASES[i]);
}
bson_free(short_s);
bson_free(medium_s);
bson_free(long_s);
}
// Run case folding and no folding for different sizes. Only unfolded size matters.
char *short_s = build_random_string_to_fold(SHORT_LEN, SHORT_LEN);
char *medium_s = build_random_string_to_fold(MEDIUM_LEN, MEDIUM_LEN);
char *long_s = build_random_string_to_fold(LONG_LEN, LONG_LEN);
for (int casef = 0; casef <= 1; casef++) {
suffix_prefix_run_folding_case(tester, short_s, medium_s, long_s, casef, false /* diacf*/, 0);
}
bson_free(short_s);
bson_free(medium_s);
bson_free(long_s);
}
static void substring_run_folding_case(_mongocrypt_tester_t *tester,
const char *short_s,
uint32_t short_unfolded_codepoint_len,
const char *medium_s,
uint32_t medium_unfolded_codepoint_len,
const char *long_s,
uint32_t long_unfolded_codepoint_len,
bool casef,
bool diacf,
int foldable_codepoints) {
// LB > 16
test_nofold_substring_case_multiple_mlen(tester,
short_s,
17,
19,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// Simple cases
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
4,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
3,
6,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// LB = UB
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
2,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
9,
9,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB = len
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
9,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// 16 > UB > len
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
14,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB = 16
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
16,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > 16
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
19,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > 32
test_nofold_substring_case_multiple_mlen(tester,
short_s,
2,
35,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// 16 >= LB > len
test_nofold_substring_case_multiple_mlen(tester,
short_s,
12,
19,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
12,
16,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
16,
19,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
12,
35,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
short_s,
16,
35,
short_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// len = 16 cases
// LB > 16
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
17,
19,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// Simple cases
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
4,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
3,
6,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// LB = UB
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
2,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
16,
16,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB = len
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
16,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > len
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
19,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB = 32
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
32,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > 32
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
2,
35,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// LB = len
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
16,
19,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
medium_s,
16,
35,
medium_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// len > 16 cases
// LB > 32
test_nofold_substring_case_multiple_mlen(tester,
long_s,
33,
38,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// Simple cases
test_nofold_substring_case_multiple_mlen(tester,
long_s,
2,
4,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
6,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// LB < 16 <= UB <= len
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
18,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
16,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
27,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// 16 <= LB < UB <= len
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
24,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
16,
24,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
27,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
16,
27,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// LB = UB
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
3,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
16,
16,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
27,
27,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// 32 > UB > len
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
29,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
29,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB = 32
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
27,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > 32
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
35,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
35,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
27,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// UB > 48
test_nofold_substring_case_multiple_mlen(tester,
long_s,
3,
49,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
18,
49,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
27,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
// 32 >= LB > len
test_nofold_substring_case_multiple_mlen(tester,
long_s,
28,
30,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
28,
28,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
28,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
28,
34,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
28,
49,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
32,
32,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
32,
34,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
test_nofold_substring_case_multiple_mlen(tester,
long_s,
32,
49,
long_unfolded_codepoint_len,
casef,
diacf,
foldable_codepoints);
}
static void _test_text_search_str_encode_substring(_mongocrypt_tester_t *tester) {
unsigned int seed = (unsigned int)time(0);
TEST_PRINTF("Testing with seed: %u", seed);
srand(seed);
// Run diacritic folding and case+diacritic folding for a variety of folded/unfolded sizes.
for (uint32_t i = 0; i < sizeof(UNFOLDED_CASES) / sizeof(UNFOLDED_CASES[0]); i++) {
char *short_s = build_random_string_to_fold(SHORT_LEN, SHORT_LEN + UNFOLDED_CASES[i]);
char *medium_s = build_random_string_to_fold(MEDIUM_LEN, MEDIUM_LEN + UNFOLDED_CASES[i]);
char *long_s = build_random_string_to_fold(LONG_LEN, LONG_LEN + UNFOLDED_CASES[i]);
for (int casef = 0; casef <= 1; casef++) {
substring_run_folding_case(tester,
short_s,
SHORT_LEN + UNFOLDED_CASES[i],
medium_s,
MEDIUM_LEN + UNFOLDED_CASES[i],
long_s,
LONG_LEN + UNFOLDED_CASES[i],
casef,
true /* diacf */,
UNFOLDED_CASES[i]);
}
bson_free(short_s);
bson_free(medium_s);
bson_free(long_s);
}
// Run case folding and no folding for different sizes. Only unfolded size matters.
char *short_s = build_random_string_to_fold(SHORT_LEN, SHORT_LEN);
char *medium_s = build_random_string_to_fold(MEDIUM_LEN, MEDIUM_LEN);
char *long_s = build_random_string_to_fold(LONG_LEN, LONG_LEN);
for (int casef = 0; casef <= 1; casef++) {
substring_run_folding_case(tester,
short_s,
SHORT_LEN,
medium_s,
MEDIUM_LEN,
long_s,
LONG_LEN,
casef,
false /* diacf */,
0);
}
bson_free(short_s);
bson_free(medium_s);
bson_free(long_s);
}
static void _test_text_search_str_encode_multiple(_mongocrypt_tester_t *tester) {
mc_FLE2TextSearchInsertSpec_t spec = {.v = "123456789",
.len = 9,
.substr = {{20, 9, 9}, true},
.suffix = {{1, 5}, true},
.prefix = {{6, 8}, true}};
mongocrypt_status_t *status = mongocrypt_status_new();
mc_str_encode_sets_t *sets = mc_text_search_str_encode(&spec, status);
// Ensure that we ran tree generation for suffix, prefix, and substring successfully by checking the first entry of
// each.
const char *str;
uint32_t len, count;
ASSERT_OR_PRINT(sets, status);
mongocrypt_status_destroy(status);
ASSERT(sets->suffix_set != NULL);
mc_affix_set_iter_t it;
mc_affix_set_iter_init(&it, sets->suffix_set);
ASSERT(mc_affix_set_iter_next(&it, &str, &len, &count));
ASSERT_CMPUINT32(len, ==, 1);
ASSERT_CMPUINT8((uint8_t)*str, ==, (uint8_t)'9');
ASSERT_CMPUINT32(count, ==, 1);
ASSERT(sets->prefix_set != NULL);
mc_affix_set_iter_init(&it, sets->prefix_set);
ASSERT(mc_affix_set_iter_next(&it, &str, &len, &count));
ASSERT_CMPUINT32(len, ==, 6);
ASSERT_CMPINT(0, ==, memcmp("123456", str, 6));
ASSERT_CMPUINT32(count, ==, 1);
ASSERT(sets->substring_set != NULL);
mc_substring_set_iter_t ss_it;
mc_substring_set_iter_init(&ss_it, sets->substring_set);
ASSERT(mc_substring_set_iter_next(&ss_it, &str, &len, &count));
ASSERT_CMPUINT32(len, ==, 9);
ASSERT_CMPINT(0, ==, memcmp("123456789", str, 9));
ASSERT_CMPUINT32(count, ==, 1);
ASSERT_CMPUINT32(sets->exact.len, ==, 9);
ASSERT_CMPINT(0, ==, memcmp(sets->exact.data, str, 9));
ASSERT_CMPUINT32(sets->msize, ==, 1 + 3 + 5 + 3); /* exact + substring + suffix + prefix */
mc_str_encode_sets_destroy(sets);
}
static void _test_text_search_str_encode_bad_string(_mongocrypt_tester_t *tester) {
mongocrypt_status_t *status = mongocrypt_status_new();
mc_FLE2TextSearchInsertSpec_t spec = {.v = "\xff\xff\xff\xff\xff\xff\xff\xff\xff",
.len = 9,
.substr = {{20, 4, 7}, true},
.suffix = {{1, 5}, true},
.prefix = {{6, 8}, true}};
mc_str_encode_sets_t *sets = mc_text_search_str_encode(&spec, status);
ASSERT_FAILS_STATUS(sets, status, "not valid UTF-8");
mc_str_encode_sets_destroy(sets);
mongocrypt_status_destroy(status);
}
static void _test_text_search_str_encode_empty_string(_mongocrypt_tester_t *tester) {
for (int casef = 0; casef <= 1; casef++) {
for (int diacf = 0; diacf <= 1; diacf++) {
test_nofold_suffix_prefix_case(tester, "", 1, 1, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 1, 2, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 2, 3, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 1, 16, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 1, 17, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 2, 16, casef, diacf, 0);
test_nofold_suffix_prefix_case(tester, "", 2, 17, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 1, 1, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 1, 2, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 2, 3, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 1, 16, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 1, 17, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 2, 16, 1, casef, diacf, 0);
test_nofold_substring_case_multiple_mlen(tester, "", 2, 17, 1, casef, diacf, 0);
}
}
}
// Tests mc_text_search_str_query() fails on invalid utf-8.
static void _test_text_search_str_query_bad_string(_mongocrypt_tester_t *tester) {
mongocrypt_status_t *status = mongocrypt_status_new();
_mongocrypt_buffer_t out;
mc_FLE2TextSearchInsertSpec_t spec = {.v = "\xff\xff\xff\xff\xff\xff\xff\xff\xff", .len = 9};
bool res = mc_text_search_str_query(&spec, &out, status);
ASSERT_FAILS_STATUS(res, status, "not valid UTF-8");
mongocrypt_status_destroy(status);
}
// Tests mc_text_search_str_query() checks the input string codepoint length against lb and ub
// for substring, suffix, and prefix queries, and not for exact queres.
static void _test_text_search_str_query_bounds_checking(_mongocrypt_tester_t *tester) {
mc_FLE2TextSearchInsertSpec_t substrSpec = {.substr = {{.mlen = 20, .lb = 4, .ub = 7}, .set = true}};
mc_FLE2TextSearchInsertSpec_t suffixSpec = {.suffix = {{.lb = 4, .ub = 7}, .set = true}};
mc_FLE2TextSearchInsertSpec_t prefixSpec = {.prefix = {{.lb = 4, .ub = 7}, .set = true}};
mc_FLE2TextSearchInsertSpec_t *specs[3] = {&substrSpec, &suffixSpec, &prefixSpec};
char *short_str = build_random_string_to_fold(3, 5);
char *long_str = build_random_string_to_fold(8, 10);
mongocrypt_status_t *status = mongocrypt_status_new();
_mongocrypt_buffer_t out;
for (int i = 0; i < 3; i++) {
// long_str always fails regardless of folding
for (int d = 0; d < 2; d++) {
specs[i]->v = long_str;
specs[i]->len = (uint32_t)strlen(specs[i]->v);
specs[i]->diacf = d;
ASSERT_FAILS_STATUS(mc_text_search_str_query(specs[i], &out, status),
status,
"longer than the maximum query length");
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
}
// short_str only fails if diacritic folding is on
specs[i]->v = short_str;
specs[i]->len = (uint32_t)strlen(specs[i]->v);
specs[i]->diacf = true;
ASSERT_FAILS_STATUS(mc_text_search_str_query(specs[i], &out, status),
status,
"shorter than the minimum query length");
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
specs[i]->diacf = false;
ASSERT_OK_STATUS(mc_text_search_str_query(specs[i], &out, status), status);
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
}
// test no bounds checking performed if no substr/suffix/prefix specs
mc_FLE2TextSearchInsertSpec_t exactSpecShort = {.diacf = true, .v = short_str, .len = (uint32_t)strlen(short_str)};
mc_FLE2TextSearchInsertSpec_t exactSpecLong = {.diacf = true, .v = long_str, .len = (uint32_t)strlen(long_str)};
ASSERT_OK_STATUS(mc_text_search_str_query(&exactSpecShort, &out, status), status);
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
ASSERT_OK_STATUS(mc_text_search_str_query(&exactSpecLong, &out, status), status);
_mongocrypt_buffer_cleanup(&out);
mongocrypt_status_destroy(status);
bson_free(short_str);
bson_free(long_str);
}
// Tests mc_text_search_str_query() rejects empty string input for substr/suffix/prefix queries,
// but not for exact match queries.
static void _test_text_search_str_query_empty_string(_mongocrypt_tester_t *tester) {
mc_FLE2TextSearchInsertSpec_t substrSpec = {.substr = {{20, 4, 7}, true}, .v = "", .len = 0};
mc_FLE2TextSearchInsertSpec_t suffixSpec = {.suffix = {{4, 7}, true}, .v = "", .len = 0};
mc_FLE2TextSearchInsertSpec_t prefixSpec = {.prefix = {{4, 7}, true}, .v = "", .len = 0};
mc_FLE2TextSearchInsertSpec_t exactSpec = {.v = "", .len = 0};
mc_FLE2TextSearchInsertSpec_t *specs[3] = {&substrSpec, &suffixSpec, &prefixSpec};
mongocrypt_status_t *status = mongocrypt_status_new();
_mongocrypt_buffer_t out;
for (int i = 0; i < 3; i++) {
ASSERT_FAILS_STATUS(mc_text_search_str_query(specs[i], &out, status), status, "string value cannot be empty");
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
}
ASSERT_OK_STATUS(mc_text_search_str_query(&exactSpec, &out, status), status);
_mongocrypt_buffer_cleanup(&out);
mongocrypt_status_destroy(status);
}
// Tests mc_text_search_str_query() performs folding per the diacf and casef parameters.
static void _test_text_search_str_query_folding(_mongocrypt_tester_t *tester) {
const char *testStr = "Düsseldorf";
const char *diacFoldStr = "Dusseldorf";
const char *caseFoldStr = "düsseldorf";
const char *bothFoldStr = "dusseldorf";
mc_FLE2TextSearchInsertSpec_t spec = {.v = testStr, .len = (uint32_t)strlen(testStr)};
mongocrypt_status_t *status = mongocrypt_status_new();
_mongocrypt_buffer_t out;
spec.diacf = true;
ASSERT_OK_STATUS(mc_text_search_str_query(&spec, &out, status), status);
ASSERT_CMPUINT32((uint32_t)strlen(diacFoldStr) + 5, ==, out.len); // +5 for BSON overhead
ASSERT_STREQUAL(diacFoldStr, (const char *)(out.data + 4)); // +4 skips past 32-bit size field
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
spec.casef = true;
spec.diacf = false;
ASSERT_OK_STATUS(mc_text_search_str_query(&spec, &out, status), status);
ASSERT_CMPUINT32((uint32_t)strlen(caseFoldStr) + 5, ==, out.len);
ASSERT_STREQUAL(caseFoldStr, (const char *)(out.data + 4));
_mongocrypt_buffer_cleanup(&out);
_mongocrypt_status_reset(status);
spec.diacf = true;
ASSERT_OK_STATUS(mc_text_search_str_query(&spec, &out, status), status);
ASSERT_CMPUINT32((uint32_t)strlen(bothFoldStr) + 5, ==, out.len);
ASSERT_STREQUAL(bothFoldStr, (const char *)(out.data + 4));
_mongocrypt_buffer_cleanup(&out);
mongocrypt_status_destroy(status);
}
void _mongocrypt_tester_install_text_search_str_encode(_mongocrypt_tester_t *tester) {
INSTALL_TEST(_test_text_search_str_encode_suffix_prefix);
INSTALL_TEST(_test_text_search_str_encode_substring);
INSTALL_TEST(_test_text_search_str_encode_multiple);
INSTALL_TEST(_test_text_search_str_encode_bad_string);
INSTALL_TEST(_test_text_search_str_encode_empty_string);
INSTALL_TEST(_test_text_search_str_query_bad_string);
INSTALL_TEST(_test_text_search_str_query_bounds_checking);
INSTALL_TEST(_test_text_search_str_query_empty_string);
INSTALL_TEST(_test_text_search_str_query_folding);
}
|