1 /* Copyright 2015 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 */ 6 7 package org.brotli.wrapper.common; 8 9 import static org.junit.Assert.assertArrayEquals; 10 import static org.junit.Assert.assertEquals; 11 import static org.junit.Assert.assertTrue; 12 13 import org.brotli.dec.Dictionary; 14 import org.brotli.integration.BrotliJniTestBase; 15 import org.brotli.wrapper.dec.BrotliInputStream; 16 import java.io.ByteArrayInputStream; 17 import java.io.IOException; 18 import java.security.MessageDigest; 19 import java.security.NoSuchAlgorithmException; 20 import org.junit.Test; 21 import org.junit.runner.RunWith; 22 import org.junit.runners.JUnit4; 23 24 /** 25 * Tests for {@link BrotliCommon}. 26 */ 27 @RunWith(JUnit4.class) 28 public class SetRfcDictionaryTest extends BrotliJniTestBase { 29 30 @Test testRfcDictionaryChecksums()31 public void testRfcDictionaryChecksums() throws NoSuchAlgorithmException { 32 System.err.println(Dictionary.getData().slice().remaining()); 33 MessageDigest md5 = MessageDigest.getInstance("MD5"); 34 md5.update(Dictionary.getData().slice()); 35 assertTrue(BrotliCommon.checkDictionaryDataMd5(md5.digest())); 36 37 MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); 38 sha1.update(Dictionary.getData().slice()); 39 assertTrue(BrotliCommon.checkDictionaryDataSha1(sha1.digest())); 40 41 MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); 42 sha256.update(Dictionary.getData().slice()); 43 assertTrue(BrotliCommon.checkDictionaryDataSha256(sha256.digest())); 44 } 45 46 @Test testSetRfcDictionary()47 public void testSetRfcDictionary() throws IOException { 48 /* "leftdatadataleft" encoded with dictionary words. */ 49 byte[] data = {27, 15, 0, 0, 0, 0, -128, -29, -76, 13, 0, 0, 7, 91, 38, 49, 64, 2, 0, -32, 78, 50 27, 65, -128, 32, 80, 16, 36, 8, 6}; 51 BrotliCommon.setDictionaryData(Dictionary.getData()); 52 53 BrotliInputStream decoder = new BrotliInputStream(new ByteArrayInputStream(data)); 54 byte[] output = new byte[17]; 55 int offset = 0; 56 try { 57 int bytesRead; 58 while ((bytesRead = decoder.read(output, offset, 17 - offset)) != -1) { 59 offset += bytesRead; 60 } 61 } finally { 62 decoder.close(); 63 } 64 assertEquals(16, offset); 65 byte[] expected = { 66 'l', 'e', 'f', 't', 67 'd', 'a', 't', 'a', 68 'd', 'a', 't', 'a', 69 'l', 'e', 'f', 't', 70 0 71 }; 72 assertArrayEquals(expected, output); 73 } 74 } 75