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.dec; 8 9 import static org.junit.Assert.assertArrayEquals; 10 import static org.junit.Assert.assertEquals; 11 12 import java.nio.charset.StandardCharsets; 13 import java.security.MessageDigest; 14 import java.security.NoSuchAlgorithmException; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 /** 20 * Tests for {@link Transform}. 21 */ 22 @RunWith(JUnit4.class) 23 public class TransformTest { 24 25 @Test 26 public void testTrimAll() { 27 byte[] output = new byte[2]; 28 byte[] input = "word".getBytes(StandardCharsets.UTF_8); 29 Transform transform = new Transform("[", WordTransformType.OMIT_FIRST_5, "]"); 30 Transform.transformDictionaryWord(output, 0, input, 0, input.length, transform); 31 assertArrayEquals(output, "[]".getBytes(StandardCharsets.UTF_8)); 32 } 33 34 @Test 35 public void testCapitalize() { 36 byte[] output = new byte[8]; 37 byte[] input = "q".getBytes(StandardCharsets.UTF_8); 38 Transform transform = new Transform("[", WordTransformType.UPPERCASE_ALL, "]"); 39 Transform.transformDictionaryWord(output, 0, input, 0, input.length, transform); 40 assertArrayEquals(output, "[Q]".getBytes(StandardCharsets.UTF_8)); 41 } 42 43 @Test 44 public void testAllTransforms() throws NoSuchAlgorithmException { 45 /* This string allows to apply all transforms: head and tail cutting, capitalization and 46 turning to upper case; all results will be mutually different. */ 47 byte[] testWord = Transform.readUniBytes("o123456789abcdef"); 48 byte[] output = new byte[2259]; 49 int offset = 0; 50 for (int i = 0; i < Transform.TRANSFORMS.length; ++i) { 51 offset += Transform.transformDictionaryWord( 52 output, offset, testWord, 0, testWord.length, Transform.TRANSFORMS[i]); 53 output[offset++] = -1; 54 } 55 assertEquals(output.length, offset); 56 57 MessageDigest md = MessageDigest.getInstance("SHA-256"); 58 md.update(output); 59 byte[] digest = md.digest(); 60 String sha256 = String.format("%064x", new java.math.BigInteger(1, digest)); 61 assertEquals("60f1c7e45d788e24938c5a3919aaf41a7d8ad474d0ced6b9e4c0079f4d1da8c4", sha256); 62 } 63 } 64