aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/wrapper/common/SetRfcDictionaryTest.java
blob: 857780013dd1551d4d6565d53bffc6fcbbfe6c5a (plain)
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
/* Copyright 2015 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

package org.brotli.wrapper.common;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.brotli.wrapper.dec.BrotliInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link BrotliCommon}.
 */
@RunWith(JUnit4.class)
public class SetRfcDictionaryTest {

  // TODO: remove when Bazel get JNI support.
  static {
    System.load(new java.io.File(new java.io.File(System.getProperty("java.library.path")),
        "liblibjni_Uno_Udictionary_Udata.so").getAbsolutePath());
  }

  @Test
  public void testRfcDictionaryChecksums() throws IOException, NoSuchAlgorithmException {
    FileInputStream dictionary = new FileInputStream(System.getProperty("RFC_DICTIONARY"));
    byte[] data = new byte[BrotliCommon.RFC_DICTIONARY_SIZE + 1];
    int offset = 0;
    try {
      int readBytes;
      while ((readBytes = dictionary.read(data, offset, data.length - offset)) != -1) {
        offset += readBytes;
        if (offset > BrotliCommon.RFC_DICTIONARY_SIZE) {
          break;
        }
      }
    } finally {
      dictionary.close();
    }
    if (offset != BrotliCommon.RFC_DICTIONARY_SIZE) {
      fail("dictionary size mismatch");
    }

    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(data, 0, offset);
    assertTrue(BrotliCommon.checkDictionaryDataMd5(md5.digest()));

    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    sha1.update(data, 0, offset);
    assertTrue(BrotliCommon.checkDictionaryDataSha1(sha1.digest()));

    MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
    sha256.update(data, 0, offset);
    assertTrue(BrotliCommon.checkDictionaryDataSha256(sha256.digest()));
  }

  @Test
  public void testSetRfcDictionary() throws IOException {
    /* "leftdatadataleft" encoded with dictionary words. */
    byte[] data = {27, 15, 0, 0, 0, 0, -128, -29, -76, 13, 0, 0, 7, 91, 38, 49, 64, 2, 0, -32, 78,
        27, 65, -128, 32, 80, 16, 36, 8, 6};
    FileInputStream dictionary = new FileInputStream(System.getProperty("RFC_DICTIONARY"));
    try {
      BrotliCommon.setDictionaryData(dictionary);
    } finally {
      dictionary.close();
    }

    BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(data));
    byte[] output = new byte[17];
    int offset = 0;
    try {
      int bytesRead;
      while ((bytesRead = decoder.read(output, offset, 17 - offset)) != -1) {
        offset += bytesRead;
      }
    } finally {
      decoder.close();
    }
    assertEquals(16, offset);
    byte[] expected = {
      'l', 'e', 'f', 't',
      'd', 'a', 't', 'a',
      'd', 'a', 't', 'a',
      'l', 'e', 'f', 't',
      0
    };
    assertArrayEquals(expected, output);
  }
}