forked from simdjson/simdjson-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtf8ValidatorTest.java
More file actions
496 lines (408 loc) · 19.6 KB
/
Utf8ValidatorTest.java
File metadata and controls
496 lines (408 loc) · 19.6 KB
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
package org.simdjson;
import jdk.incubator.vector.VectorSpecies;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.*;
class Utf8ValidatorTest {
private static final VectorSpecies<Byte> VECTOR_SPECIES = StructuralIndexer.BYTE_SPECIES;
/* ASCII / 1 BYTE TESTS */
@Test
void validate_allEightBitValues_invalidAscii() {
byte[] invalidAscii = new byte[128];
int index = 0;
for (int eightBitVal = 255; eightBitVal >= 128; eightBitVal--) {
invalidAscii[index++] = (byte) eightBitVal;
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < 128; i += VECTOR_SPECIES.vectorByteSize()) {
byte[] vectorChunk = Arrays.copyOfRange(invalidAscii, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* CONTINUATION BYTE TESTS */
// continuation byte is never valid without a preceding leader byte
@Test
void validate_continuationByteOutOfOrder_invalid() {
byte minContinuationByte = (byte) 0b10_000000;
byte maxContinuationByte = (byte) 0b10_111111;
byte[] inputBytes = new byte[64];
int index = 0;
byte continuationByte = minContinuationByte;
while (continuationByte <= maxContinuationByte) {
inputBytes[index++] = continuationByte;
continuationByte++;
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.length()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
@Test
void validate_extraContinuationByte_2Byte_invalid() {
byte[] inputBytes = new byte[3];
inputBytes[0] = (byte) 0b110_00010;
inputBytes[1] = (byte) 0b10_000000;
inputBytes[2] = (byte) 0b10_000000; // two byte lead should only have one continuation byte
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationOneByteTooShort_2Byte_invalid() {
byte[] inputBytes = new byte[1];
inputBytes[0] = (byte) 0b110_00010;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_extraContinuationByte_3Byte_invalid() {
byte[] inputBytes = new byte[4];
inputBytes[0] = (byte) 0b1110_0000;
inputBytes[1] = (byte) 0b10_100000;
inputBytes[2] = (byte) 0b10_000000;
inputBytes[3] = (byte) 0b10_000000; // three byte lead should only have two continuation bytes
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationOneByteTooShort_3Byte_invalid() {
byte[] inputBytes = new byte[2];
inputBytes[0] = (byte) 0b1110_0000;
inputBytes[1] = (byte) 0b10_100000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationTwoBytesTooShort_3Byte_invalid() {
byte[] inputBytes = new byte[1];
inputBytes[0] = (byte) 0b1110_0000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_extraContinuationByte_4Byte_invalid() {
byte[] inputBytes = new byte[5];
inputBytes[0] = (byte) 0b11110_000;
inputBytes[1] = (byte) 0b10_010000;
inputBytes[2] = (byte) 0b10_000000;
inputBytes[3] = (byte) 0b10_000000;
inputBytes[4] = (byte) 0b10_000000; // four byte lead should only have three continuation bytes
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationOneByteTooShort_4Byte_invalid() {
byte[] inputBytes = new byte[3];
inputBytes[0] = (byte) 0b11110_000;
inputBytes[1] = (byte) 0b10_010000;
inputBytes[2] = (byte) 0b10_000000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationTwoBytesTooShort_4Byte_invalid() {
byte[] inputBytes = new byte[2];
inputBytes[0] = (byte) 0b11110_000;
inputBytes[1] = (byte) 0b10_010000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationThreeBytesTooShort_4Byte_invalid() {
byte[] inputBytes = new byte[1];
inputBytes[0] = (byte) 0b11110_000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
/* 2 BYTE / LATIN TESTS */
@Test
void validate_overlong_2byte_invalid() {
byte minLeaderByte = (byte) 0b110_00000;
byte maxLeaderByte = (byte) 0b110_00001;
byte minContinuationByte = (byte) 0b10_000000;
byte maxContinuationByte = (byte) 0b10_111111;
/* 7 bit code points in 2 byte utf8 is invalid
2 to the power of 7 = 128 code points * 2 bytes = 256 bytes */
byte[] inputBytes = new byte[256];
int index = 0;
byte leaderByte = minLeaderByte;
byte continuationByte = minContinuationByte;
while (leaderByte <= maxLeaderByte) {
inputBytes[index++] = leaderByte;
inputBytes[index++] = continuationByte;
if (continuationByte == maxContinuationByte) {
leaderByte++;
continuationByte = minContinuationByte;
} else {
continuationByte++;
}
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.length()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* 3 BYTE / Asiatic TESTS */
/* first valid three byte character: 1110_0000 10_100000 10_000000
anything smaller is invalid as it would fit into 11 bits (two byte utf8) */
@Test
void validate_overlong_3Byte_allInvalid() {
byte minLeaderByte = (byte) 0b1110_0000;
byte firstValidContinuationByte = (byte) 0b10_100000;
byte minContinuationByte = (byte) 0b10_000000;
byte maxContinuationByte = (byte) 0b10_111111;
// 2 to the power of 11 = 2048 code points * 3 bytes = 6144
byte[] inputBytes = new byte[6144];
int index = 0;
byte firstContinuationByte = minContinuationByte;
byte secondContinuationByte = minContinuationByte;
while (firstContinuationByte < firstValidContinuationByte) {
inputBytes[index++] = minLeaderByte;
inputBytes[index++] = firstContinuationByte;
inputBytes[index++] = secondContinuationByte;
if (secondContinuationByte == maxContinuationByte) {
secondContinuationByte = minContinuationByte;
firstContinuationByte++;
} else {
secondContinuationByte++;
}
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.length()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* code points in the range of U+D800 - U+DFFF (inclusive) are the surrogates for UTF-16.
These 2048 code points that are reserved for UTF-16 are disallowed in UTF-8
1101 1000 0000 0000 -> 1101 1111 1111 1111 */
@Test
void validate_surrogateCodePoints_invalid() {
final byte leaderByte = (byte) 0b1101_1110;
final byte minContinuationByte = (byte) 0b10_000000;
final byte maxContinuationByte = (byte) 0b10_111111;
final byte minFirstContinuationByte = (byte) 0b10_100000;
byte firstContinuationByte = minFirstContinuationByte;
byte secondContinuationByte = minContinuationByte;
// 2048 invalid code points * 3 bytes = 6144 bytes
byte[] inputBytes = new byte[6144];
int index = 0;
while (firstContinuationByte <= maxContinuationByte) {
inputBytes[index++] = leaderByte;
inputBytes[index++] = firstContinuationByte;
inputBytes[index++] = secondContinuationByte;
if (secondContinuationByte == maxContinuationByte) {
firstContinuationByte++;
secondContinuationByte = minContinuationByte;
} else {
secondContinuationByte++;
}
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.vectorByteSize()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* 4 BYTE / Supplementary TESTS */
/* Overlong Test, the decoded character must be above U+FFFF / 11110_000 10_001111 10_111111 10_111111 */
@Test
void validate_overlong_4Byte_allInvalid() {
byte leaderByte = (byte) 0b11110_000;
byte minContinuationByte = (byte) 0b10_000000;
byte maxContinuationByte = (byte) 0b10_111111;
byte maxFirstContinuationByte = (byte) 0b10_001111;
// 2 to the power of 16 = 65536 valid code points * 4 bytes = 262144 bytes
byte[] inputBytes = new byte[262144];
int index = 0;
byte firstContinuationByte = minContinuationByte;
byte secondContinuationByte = minContinuationByte;
byte thirdContinuationByte = minContinuationByte;
while (firstContinuationByte <= maxFirstContinuationByte) {
inputBytes[index++] = leaderByte;
inputBytes[index++] = firstContinuationByte;
inputBytes[index++] = secondContinuationByte;
inputBytes[index++] = thirdContinuationByte;
if (thirdContinuationByte == maxContinuationByte) {
if (secondContinuationByte == maxContinuationByte) {
firstContinuationByte++;
secondContinuationByte = minContinuationByte;
} else {
secondContinuationByte++;
}
thirdContinuationByte = minContinuationByte;
} else {
thirdContinuationByte++;
}
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.vectorByteSize()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* last valid four byte character: 11110_100 10_001111 10_111111 10_111111
Any code point greater than U+10FFFF will result in a TOO_LARGE error */
@Test
void validate_tooLarge_4Byte_allInvalid() {
byte minLeaderByte = (byte) 0b11110_100;
byte maxLeaderByte = (byte) 0b11111_111;
byte minContinuationByte = (byte) 0b10_000000;
byte maxContinuationByte = (byte) 0b10_111111;
byte minFirstContinuationByte = (byte) 0b10_010000;
byte leaderByte = minLeaderByte;
byte firstContinuationByte = minFirstContinuationByte;
byte secondContinuationByte = minContinuationByte;
byte thirdContinuationByte = minContinuationByte;
int codePoints = 0x3FFFFF - 0x110000 + 1;
byte[] inputBytes = new byte[codePoints * 4];
int index = 0;
while (leaderByte <= maxLeaderByte) {
inputBytes[index++] = leaderByte;
inputBytes[index++] = firstContinuationByte;
inputBytes[index++] = secondContinuationByte;
inputBytes[index++] = thirdContinuationByte;
if (thirdContinuationByte == maxContinuationByte) {
if (secondContinuationByte == maxContinuationByte) {
if (firstContinuationByte == maxContinuationByte) {
leaderByte++;
firstContinuationByte = minContinuationByte;
} else {
firstContinuationByte++;
}
secondContinuationByte = minContinuationByte;
} else {
secondContinuationByte++;
}
thirdContinuationByte = minContinuationByte;
} else {
thirdContinuationByte++;
}
}
SimdJsonParser parser = new SimdJsonParser();
for (int i = 0; i < inputBytes.length; i += VECTOR_SPECIES.vectorByteSize()) {
byte[] vectorChunk = Arrays.copyOfRange(inputBytes, i, i + VECTOR_SPECIES.vectorByteSize());
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(vectorChunk, vectorChunk.length))
.withMessage("Invalid UTF8");
}
}
/* check that the data stream does not terminate with an incomplete code point
We just have to check that the last byte in the last vector is strictly smaller than 0xC0 (using an unsigned comparison)
that the second last byte is strictly smaller than 0xE0
the third last byte is strictly smaller than 0xF0 */
@Test
void validate_continuationOneByteTooShort_2Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 1] = (byte) 0b110_00010;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationOneByteTooShort_3Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 2] = (byte) 0b1110_0000;
inputBytes[vectorBytes - 1] = (byte) 0b10_100000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationTwoBytesTooShort_3Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 1] = (byte) 0b1110_0000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationOneByteTooShort_4Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 3] = (byte) 0b11110_000;
inputBytes[vectorBytes - 2] = (byte) 0b10_010000;
inputBytes[vectorBytes - 1] = (byte) 0b10_000000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationTwoBytesTooShort_4Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 2] = (byte) 0b11110_000;
inputBytes[vectorBytes - 1] = (byte) 0b10_010000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
@Test
void validate_continuationThreeBytesTooShort_4Byte_eof_invalid() {
int vectorBytes = VECTOR_SPECIES.vectorByteSize();
byte[] inputBytes = new byte[vectorBytes];
inputBytes[vectorBytes - 1] = (byte) 0b11110_000;
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
/* file tests */
@ParameterizedTest
@ValueSource(strings = {"/twitter.json", "/nhkworld.json"})
void validate_utf8InputFiles_valid(String inputFilePath) throws IOException {
byte[] inputBytes = TestUtils.loadTestFile(inputFilePath);
SimdJsonParser parser = new SimdJsonParser();
assertThatCode(() -> parser.parse(inputBytes, inputBytes.length)).doesNotThrowAnyException();
}
@Test
void validate_utf8InputFile_invalid() throws IOException {
byte[] inputBytes = TestUtils.loadTestFile("/malformed.txt");
SimdJsonParser parser = new SimdJsonParser();
assertThatExceptionOfType(JsonParsingException.class)
.isThrownBy(() -> parser.parse(inputBytes, inputBytes.length))
.withMessage("Invalid UTF8");
}
}