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
|
// SPDX-License-Identifier: BSL-1.0
#include <string.h>
#ifndef BUNDLED_CATCH2
#ifdef CATCH3
#include "catch2/catch_all.hpp"
#else
#include "catch2/catch.hpp"
#endif
#else
#include "../third-party/catch.hpp"
#endif
typedef bool _Bool;
#include <termpaint_utf8.h>
template <typename X>
static const unsigned char* u8p(X); // intentionally undefined
template <>
const unsigned char* u8p(const char *str) {
return (const unsigned char*)str;
}
TEST_CASE("misc invalid single byte utf8") {
unsigned char ch;
for (int i = 0x80; i < 0xc0; i++) {
INFO(i);
ch = i;
REQUIRE(termpaintp_check_valid_sequence(&ch, 1) == false);
}
{
INFO(0xfe);
ch = 0xfe;
REQUIRE(termpaintp_check_valid_sequence(&ch, 1) == false);
}
{
INFO(0xff);
ch = 0xff;
REQUIRE(termpaintp_check_valid_sequence(&ch, 1) == false);
}
}
TEST_CASE("short utf8 sequences") {
}
static inline int create_encoding_with_length(int codepoint, unsigned char *buf, int length) {
#define STORE_AND_SHIFT(index) \
buf[index] = (codepoint & 0b00111111) | 0x80; \
codepoint = codepoint >> 6;
if (length == 6) {
//This is implied by the range of int:
//REQUIRE(codepoint < (1u << (6*5+1)));
STORE_AND_SHIFT(5)
STORE_AND_SHIFT(4)
STORE_AND_SHIFT(3)
STORE_AND_SHIFT(2)
STORE_AND_SHIFT(1)
buf[0] = 0b11111100 | codepoint;
return 6;
} else if (length == 5) {
REQUIRE(codepoint < (1 << (6*4+2)));
STORE_AND_SHIFT(4)
STORE_AND_SHIFT(3)
STORE_AND_SHIFT(2)
STORE_AND_SHIFT(1)
buf[0] = 0b11111000 | codepoint;
return 5;
} else if (length == 4) {
REQUIRE(codepoint < (1 << (6*3+3)));
STORE_AND_SHIFT(3)
STORE_AND_SHIFT(2)
STORE_AND_SHIFT(1)
buf[0] = 0b11110000 | codepoint;
return 4;
} else if (length == 3) {
REQUIRE(codepoint < (1 << (6*2+4)));
STORE_AND_SHIFT(2)
STORE_AND_SHIFT(1)
buf[0] = 0b11100000 | codepoint;
return 3;
} else if (length == 2) {
REQUIRE(codepoint < (1 << (6*1+5)));
STORE_AND_SHIFT(1)
buf[0] = 0b11000000 | codepoint;
return 2;
} else {
REQUIRE(codepoint < 0x80);
REQUIRE(length == 1);
buf[0] = codepoint;
return 1;
}
#undef STORE_AND_SHIFT
}
TEST_CASE("Non shortest form") {
SECTION ("2 byte /") {
REQUIRE(termpaintp_check_valid_sequence(u8p("\xc1\x9c"), 2) == false);
}
SECTION ("2 byte A") {
REQUIRE(termpaintp_check_valid_sequence(u8p("\xc1\x81"), 2) == false);
}
SECTION ("3 byte A") {
REQUIRE(termpaintp_check_valid_sequence(u8p("\xe0\x81\x81"), 3) == false);
}
SECTION ("overlong nul") {
unsigned char buffer[7];
for (int i = 2; i < 7; i++) {
INFO(i);
create_encoding_with_length(0, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x7f") {
unsigned char buffer[7];
for (int i = 2; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x7f, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x80") {
unsigned char buffer[7];
for (int i = 3; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x80, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x7ff") {
unsigned char buffer[7];
for (int i = 3; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x7ff, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x800") {
unsigned char buffer[7];
for (int i = 4; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x800, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0xffff") {
unsigned char buffer[7];
for (int i = 4; i < 7; i++) {
INFO(i);
create_encoding_with_length(0xffff, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x10000") {
unsigned char buffer[7];
for (int i = 5; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x10000, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x1fffff") {
unsigned char buffer[7];
for (int i = 5; i < 7; i++) {
INFO(i);
create_encoding_with_length(0x1fffff, buffer, i);
REQUIRE(termpaintp_check_valid_sequence(buffer, i) == false);
}
}
SECTION ("overlong 0x200000") {
unsigned char buffer[7];
create_encoding_with_length(0x200000, buffer, 6);
REQUIRE(termpaintp_check_valid_sequence(buffer, 6) == false);
}
SECTION ("overlong 0x3ffffff") {
unsigned char buffer[7];
create_encoding_with_length(0x3ffffff, buffer, 6);
REQUIRE(termpaintp_check_valid_sequence(buffer, 6) == false);
}
}
static void codepoint_test(unsigned lower, unsigned upper) {
unsigned char buffer[7];
for (unsigned codepoint = lower; codepoint <= upper; codepoint++) {
INFO(codepoint);
memset(buffer, 42, 7);
int len = termpaintp_encode_to_utf8(codepoint, buffer);
REQUIRE(buffer[len] == 42);
REQUIRE(len == termpaintp_utf8_len(buffer[0]));
INFO("buffer " << std::hex << (unsigned)buffer[0] << " " << (unsigned)buffer[1]
<< " " << (unsigned)buffer[2] << " " << (unsigned)buffer[3]
<< " " << (unsigned)buffer[4] << " " << (unsigned)buffer[5]);
if (codepoint > 0xd7ff && codepoint < 0xe000) {
REQUIRE(termpaintp_check_valid_sequence(buffer, len) == false);
} else {
REQUIRE(termpaintp_check_valid_sequence(buffer, len) == true);
}
}
}
TEST_CASE( "utf8 brute force unicode", "[utf8]" ) {
codepoint_test(1, 0x10ffff);
}
TEST_CASE( "utf8 brute force", "[.utf8slow]" ) {
codepoint_test(1, 0x7fffffff);
}
|