|
6 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; |
7 | 7 | import static org.junit.jupiter.api.Assertions.assertThrows; |
8 | 8 |
|
9 | | -import java.io.File; |
10 | 9 | import java.io.IOException; |
11 | | -import java.io.RandomAccessFile; |
12 | 10 | import java.math.BigInteger; |
13 | 11 | import java.nio.ByteBuffer; |
14 | | -import java.nio.MappedByteBuffer; |
15 | | -import java.nio.channels.FileChannel; |
16 | | -import java.nio.channels.FileChannel.MapMode; |
17 | 12 | import java.nio.charset.StandardCharsets; |
18 | 13 | import java.util.ArrayList; |
19 | 14 | import java.util.HashMap; |
20 | 15 | import java.util.List; |
21 | 16 | import java.util.Map; |
22 | | -import java.util.UUID; |
23 | 17 | import org.junit.jupiter.api.Test; |
24 | 18 |
|
25 | 19 | @SuppressWarnings({"boxing", "static-method"}) |
@@ -404,94 +398,77 @@ public void testArrays() throws IOException { |
404 | 398 | } |
405 | 399 |
|
406 | 400 | @Test |
407 | | - public void testInvalidControlByte() throws IOException { |
408 | | - try (FileChannel fc = DecoderTest.getFileChannel(new byte[] {0x0, 0xF})) { |
409 | | - MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size()); |
| 401 | + public void testInvalidControlByte() { |
| 402 | + ByteBuffer buffer = DecoderTest.getByteBuffer(new byte[] {0x0, 0xF}); |
410 | 403 |
|
411 | | - Decoder decoder = new Decoder(new CHMCache(), mmap, 0); |
412 | | - InvalidDatabaseException ex = assertThrows( |
| 404 | + Decoder decoder = new Decoder(new CHMCache(), buffer, 0); |
| 405 | + InvalidDatabaseException ex = assertThrows( |
413 | 406 | InvalidDatabaseException.class, |
414 | 407 | () -> decoder.decode(0, String.class)); |
415 | | - assertThat(ex.getMessage(), |
| 408 | + assertThat(ex.getMessage(), |
416 | 409 | containsString("The MaxMind DB file's data section contains bad data")); |
417 | | - } |
418 | 410 | } |
419 | 411 |
|
420 | 412 | private static <T> void testTypeDecoding(Type type, Map<T, byte[]> tests) |
421 | | - throws IOException { |
| 413 | + throws IOException { |
422 | 414 | NodeCache cache = new CHMCache(); |
423 | 415 |
|
424 | 416 | for (Map.Entry<T, byte[]> entry : tests.entrySet()) { |
425 | 417 | T expect = entry.getKey(); |
426 | 418 | byte[] input = entry.getValue(); |
427 | 419 |
|
428 | 420 | String desc = "decoded " + type.name() + " - " + expect; |
429 | | - try (FileChannel fc = DecoderTest.getFileChannel(input)) { |
430 | | - MappedByteBuffer mmap = fc.map(MapMode.READ_ONLY, 0, fc.size()); |
431 | | - |
432 | | - Decoder decoder = new Decoder(cache, mmap, 0); |
433 | | - decoder.pointerTestHack = true; |
434 | | - |
435 | | - // XXX - this could be streamlined |
436 | | - if (type.equals(Type.BYTES)) { |
437 | | - assertArrayEquals((byte[]) expect, decoder.decode(0, byte[].class), desc); |
438 | | - } else if (type.equals(Type.ARRAY)) { |
439 | | - assertEquals(expect, decoder.decode(0, List.class), desc); |
440 | | - } else if (type.equals(Type.UINT16) |
| 421 | + ByteBuffer buffer = DecoderTest.getByteBuffer(input); |
| 422 | + |
| 423 | + Decoder decoder = new Decoder(cache, buffer, 0); |
| 424 | + decoder.pointerTestHack = true; |
| 425 | + |
| 426 | + // XXX - this could be streamlined |
| 427 | + if (type.equals(Type.BYTES)) { |
| 428 | + assertArrayEquals((byte[]) expect, decoder.decode(0, byte[].class), desc); |
| 429 | + } else if (type.equals(Type.ARRAY)) { |
| 430 | + assertEquals(expect, decoder.decode(0, List.class), desc); |
| 431 | + } else if (type.equals(Type.UINT16) |
441 | 432 | || type.equals(Type.INT32)) { |
442 | | - assertEquals(expect, decoder.decode(0, Integer.class), desc); |
443 | | - } else if (type.equals(Type.UINT32) |
| 433 | + assertEquals(expect, decoder.decode(0, Integer.class), desc); |
| 434 | + } else if (type.equals(Type.UINT32) |
444 | 435 | || type.equals(Type.POINTER)) { |
445 | | - assertEquals(expect, decoder.decode(0, Long.class), desc); |
446 | | - } else if (type.equals(Type.UINT64) |
| 436 | + assertEquals(expect, decoder.decode(0, Long.class), desc); |
| 437 | + } else if (type.equals(Type.UINT64) |
447 | 438 | || type.equals(Type.UINT128)) { |
448 | | - assertEquals(expect, decoder.decode(0, BigInteger.class), desc); |
449 | | - } else if (type.equals(Type.DOUBLE)) { |
450 | | - assertEquals(expect, decoder.decode(0, Double.class), desc); |
451 | | - } else if (type.equals(Type.FLOAT)) { |
452 | | - assertEquals(expect, decoder.decode(0, Float.class), desc); |
453 | | - } else if (type.equals(Type.UTF8_STRING)) { |
454 | | - assertEquals(expect, decoder.decode(0, String.class), desc); |
455 | | - } else if (type.equals(Type.BOOLEAN)) { |
456 | | - assertEquals(expect, decoder.decode(0, Boolean.class), desc); |
457 | | - } else { |
458 | | - // We hit this for Type.MAP. |
459 | | - |
460 | | - Map got = decoder.decode(0, Map.class); |
461 | | - Map expectMap = (Map) expect; |
462 | | - |
463 | | - assertEquals(expectMap.size(), got.size(), desc); |
464 | | - |
465 | | - for (Object keyObject : expectMap.keySet()) { |
466 | | - String key = (String) keyObject; |
467 | | - Object value = expectMap.get(key); |
468 | | - |
469 | | - if (value instanceof Object[]) { |
470 | | - assertArrayEquals((Object[]) value, (Object[]) got.get(key), desc); |
471 | | - } else { |
472 | | - assertEquals(value, got.get(key), desc); |
473 | | - } |
| 439 | + assertEquals(expect, decoder.decode(0, BigInteger.class), desc); |
| 440 | + } else if (type.equals(Type.DOUBLE)) { |
| 441 | + assertEquals(expect, decoder.decode(0, Double.class), desc); |
| 442 | + } else if (type.equals(Type.FLOAT)) { |
| 443 | + assertEquals(expect, decoder.decode(0, Float.class), desc); |
| 444 | + } else if (type.equals(Type.UTF8_STRING)) { |
| 445 | + assertEquals(expect, decoder.decode(0, String.class), desc); |
| 446 | + } else if (type.equals(Type.BOOLEAN)) { |
| 447 | + assertEquals(expect, decoder.decode(0, Boolean.class), desc); |
| 448 | + } else { |
| 449 | + // We hit this for Type.MAP. |
| 450 | + |
| 451 | + Map got = decoder.decode(0, Map.class); |
| 452 | + Map expectMap = (Map) expect; |
| 453 | + |
| 454 | + assertEquals(expectMap.size(), got.size(), desc); |
| 455 | + |
| 456 | + for (Object keyObject : expectMap.keySet()) { |
| 457 | + String key = (String) keyObject; |
| 458 | + Object value = expectMap.get(key); |
| 459 | + |
| 460 | + if (value instanceof Object[]) { |
| 461 | + assertArrayEquals((Object[]) value, (Object[]) got.get(key), desc); |
| 462 | + } else { |
| 463 | + assertEquals(value, got.get(key), desc); |
474 | 464 | } |
475 | 465 | } |
476 | 466 | } |
477 | 467 | } |
478 | 468 | } |
479 | 469 |
|
480 | | - /* |
481 | | - * I really didn't want to create temporary files for these tests, but it is |
482 | | - * pretty hard to abstract away from the file io system in a way that is |
483 | | - * Java 6 compatible |
484 | | - */ |
485 | | - private static FileChannel getFileChannel(byte[] data) throws IOException { |
486 | | - File file = File.createTempFile(UUID.randomUUID().toString(), "tmp"); |
487 | | - file.deleteOnExit(); |
488 | | - RandomAccessFile raf = new RandomAccessFile(file, "rw"); |
489 | | - FileChannel fc = raf.getChannel(); |
490 | | - fc.write(ByteBuffer.wrap(data)); |
491 | | - raf.close(); |
492 | | - fc.close(); |
493 | | - raf = new RandomAccessFile(file, "r"); |
494 | | - return raf.getChannel(); |
| 470 | + private static ByteBuffer getByteBuffer(byte[] data) { |
| 471 | + return ByteBuffer.wrap(data); |
495 | 472 | } |
496 | 473 |
|
497 | 474 | } |
0 commit comments