| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ctype.h> |
| 15 |
#include <inttypes.h> |
| 16 |
#include <sched.h> /* IWYU pragma: keep */ |
| 17 |
#include <setjmp.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#define UNIT_TESTING |
| 22 |
#include <cmocka.h> |
| 23 |
|
| 24 |
#include <isc/ascii.h> |
| 25 |
|
| 26 |
#include <tests/isc.h> |
| 27 |
|
| 28 |
const char *same[][2] = { |
| 29 |
{ |
| 30 |
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", |
| 31 |
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", |
| 32 |
}, |
| 33 |
{ |
| 34 |
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", |
| 35 |
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ", |
| 36 |
}, |
| 37 |
{ |
| 38 |
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ", |
| 39 |
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", |
| 40 |
}, |
| 41 |
{ |
| 42 |
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ", |
| 43 |
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", |
| 44 |
}, |
| 45 |
{ |
| 46 |
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVxXyYzZ", |
| 47 |
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvxxyyzz", |
| 48 |
}, |
| 49 |
{ |
| 50 |
"WwW.ExAmPlE.OrG", |
| 51 |
"wWw.eXaMpLe.oRg", |
| 52 |
}, |
| 53 |
{ |
| 54 |
"_SIP.tcp.example.org", |
| 55 |
"_sip.TCP.example.org", |
| 56 |
}, |
| 57 |
{ |
| 58 |
"bind-USERS.lists.example.org", |
| 59 |
"bind-users.lists.example.org", |
| 60 |
}, |
| 61 |
{ |
| 62 |
"a0123456789.example.org", |
| 63 |
"A0123456789.example.org", |
| 64 |
}, |
| 65 |
{ |
| 66 |
"\\000.example.org", |
| 67 |
"\\000.example.org", |
| 68 |
}, |
| 69 |
{ |
| 70 |
"wWw.\\000.isc.org", |
| 71 |
"www.\\000.isc.org", |
| 72 |
}, |
| 73 |
{ |
| 74 |
"\255.example.org", |
| 75 |
"\255.example.ORG", |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
struct { |
| 80 |
const char *a, *b; |
| 81 |
int cmp; |
| 82 |
} diff[] = { |
| 83 |
{ "foo", "bar", +1 }, |
| 84 |
{ "bar", "foo", -1 }, |
| 85 |
{ "foosuffix", "barsuffix", +1 }, |
| 86 |
{ "barsuffix", "foosuffix", -1 }, |
| 87 |
{ "prefixfoo", "prefixbar", +1 }, |
| 88 |
{ "prefixbar", "prefixfoo", -1 }, |
| 89 |
}; |
| 90 |
|
| 91 |
ISC_RUN_TEST_IMPL(upperlower) { |
| 92 |
UNUSED(state); |
| 93 |
|
| 94 |
for (size_t n = 0; n < ARRAY_SIZE(same); n++) { |
| 95 |
const char *a = same[n][0]; |
| 96 |
const char *b = same[n][1]; |
| 97 |
for (size_t i = 0; a[i] != '\0' && b[i] != '\0'; i++) { |
| 98 |
assert_true(isc_ascii_toupper(a[i]) == (uint8_t)a[i] || |
| 99 |
isc_ascii_tolower(a[i]) == (uint8_t)a[i]); |
| 100 |
assert_true(isc_ascii_toupper(b[i]) == (uint8_t)b[i] || |
| 101 |
isc_ascii_tolower(b[i]) == (uint8_t)b[i]); |
| 102 |
assert_true(isc_ascii_toupper(a[i]) == |
| 103 |
isc_ascii_toupper(b[i])); |
| 104 |
assert_true(isc_ascii_tolower(a[i]) == |
| 105 |
isc_ascii_tolower(b[i])); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
ISC_RUN_TEST_IMPL(lowerequal) { |
| 111 |
for (size_t n = 0; n < ARRAY_SIZE(same); n++) { |
| 112 |
const uint8_t *a = (void *)same[n][0]; |
| 113 |
const uint8_t *b = (void *)same[n][1]; |
| 114 |
unsigned int len = (unsigned int)strlen(same[n][0]); |
| 115 |
assert_true(isc_ascii_lowerequal(a, b, len)); |
| 116 |
} |
| 117 |
for (size_t n = 0; n < ARRAY_SIZE(diff); n++) { |
| 118 |
const uint8_t *a = (void *)diff[n].a; |
| 119 |
const uint8_t *b = (void *)diff[n].b; |
| 120 |
unsigned int len = (unsigned int)strlen(diff[n].a); |
| 121 |
assert_true(!isc_ascii_lowerequal(a, b, len)); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
ISC_RUN_TEST_IMPL(lowercmp) { |
| 126 |
for (size_t n = 0; n < ARRAY_SIZE(same); n++) { |
| 127 |
const uint8_t *a = (void *)same[n][0]; |
| 128 |
const uint8_t *b = (void *)same[n][1]; |
| 129 |
unsigned int len = (unsigned int)strlen(same[n][0]); |
| 130 |
assert_true(isc_ascii_lowercmp(a, b, len) == 0); |
| 131 |
} |
| 132 |
for (size_t n = 0; n < ARRAY_SIZE(diff); n++) { |
| 133 |
const uint8_t *a = (void *)diff[n].a; |
| 134 |
const uint8_t *b = (void *)diff[n].b; |
| 135 |
unsigned int len = (unsigned int)strlen(diff[n].a); |
| 136 |
assert_true(isc_ascii_lowercmp(a, b, len) == diff[n].cmp); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
ISC_RUN_TEST_IMPL(exhaustive) { |
| 141 |
for (uint64_t ab = 0; ab < (1 << 16); ab++) { |
| 142 |
uint8_t a = ab >> 8; |
| 143 |
uint8_t b = ab & 0xFF; |
| 144 |
uint64_t abc = tolower(a) << 8 | tolower(b); |
| 145 |
uint64_t abi = isc_ascii_tolower(a) << 8 | isc_ascii_tolower(b); |
| 146 |
uint64_t ab1 = isc__ascii_tolower1(a) << 8 | |
| 147 |
isc__ascii_tolower1(b); |
| 148 |
uint64_t ab8 = isc_ascii_tolower8(ab); |
| 149 |
/* each byte individually matches ctype.h */ |
| 150 |
assert_int_equal(tolower(a), isc_ascii_tolower(a)); |
| 151 |
assert_int_equal(tolower(a), isc__ascii_tolower1(a)); |
| 152 |
assert_int_equal(tolower(a), isc_ascii_tolower8(a)); |
| 153 |
assert_int_equal(tolower(b), isc_ascii_tolower(b)); |
| 154 |
assert_int_equal(tolower(b), isc__ascii_tolower1(b)); |
| 155 |
assert_int_equal(tolower(b), isc_ascii_tolower8(b)); |
| 156 |
/* two lanes of SWAR match other implementations */ |
| 157 |
assert_int_equal(ab8, abc); |
| 158 |
assert_int_equal(ab8, abi); |
| 159 |
assert_int_equal(ab8, ab1); |
| 160 |
/* check lack of overflow */ |
| 161 |
assert_int_equal(ab8 >> 16, 0); |
| 162 |
/* all lanes of SWAR work */ |
| 163 |
assert_int_equal(isc_ascii_tolower8(ab << 8), abc << 8); |
| 164 |
assert_int_equal(isc_ascii_tolower8(ab << 16), abc << 16); |
| 165 |
assert_int_equal(isc_ascii_tolower8(ab << 24), abc << 24); |
| 166 |
assert_int_equal(isc_ascii_tolower8(ab << 32), abc << 32); |
| 167 |
assert_int_equal(isc_ascii_tolower8(ab << 40), abc << 40); |
| 168 |
assert_int_equal(isc_ascii_tolower8(ab << 48), abc << 48); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
ISC_TEST_LIST_START |
| 173 |
ISC_TEST_ENTRY(upperlower) |
| 174 |
ISC_TEST_ENTRY(lowerequal) |
| 175 |
ISC_TEST_ENTRY(lowercmp) |
| 176 |
ISC_TEST_ENTRY(exhaustive) |
| 177 |
ISC_TEST_LIST_END |
| 178 |
|
| 179 |
ISC_TEST_MAIN |