1 /* Copyright 2017 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.dec; 8 9 import static org.junit.Assert.assertEquals; 10 11 import org.brotli.integration.BundleHelper; 12 import java.io.ByteArrayInputStream; 13 import java.io.FileInputStream; 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.util.List; 17 import junit.framework.TestCase; 18 import junit.framework.TestSuite; 19 import org.junit.runner.RunWith; 20 import org.junit.runners.AllTests; 21 22 /** Tests for {@link org.brotli.wrapper.dec.BrotliInputStream}. */ 23 @RunWith(AllTests.class) 24 public class BrotliInputStreamTest { 25 26 // TODO: remove when Bazel get JNI support. 27 static { 28 System.load(new java.io.File(new java.io.File(System.getProperty("java.library.path")), 29 "liblibjni.so").getAbsolutePath()); 30 } 31 32 static InputStream getBundle() throws IOException { 33 return new FileInputStream(System.getProperty("TEST_BUNDLE")); 34 } 35 36 /** Creates a test suite. */ 37 public static TestSuite suite() throws IOException { 38 TestSuite suite = new TestSuite(); 39 InputStream bundle = getBundle(); 40 try { 41 List<String> entries = BundleHelper.listEntries(bundle); 42 for (String entry : entries) { 43 suite.addTest(new StreamTestCase(entry)); 44 } 45 } finally { 46 bundle.close(); 47 } 48 return suite; 49 } 50 51 /** Test case with a unique name. */ 52 static class StreamTestCase extends TestCase { 53 final String entryName; 54 StreamTestCase(String entryName) { 55 super("BrotliInputStreamTest." + entryName); 56 this.entryName = entryName; 57 } 58 59 @Override 60 protected void runTest() throws Throwable { 61 BrotliInputStreamTest.run(entryName); 62 } 63 } 64 65 private static void run(String entryName) throws Throwable { 66 InputStream bundle = getBundle(); 67 byte[] compressed; 68 try { 69 compressed = BundleHelper.readEntry(bundle, entryName); 70 } finally { 71 bundle.close(); 72 } 73 if (compressed == null) { 74 throw new RuntimeException("Can't read bundle entry: " + entryName); 75 } 76 77 InputStream src = new ByteArrayInputStream(compressed); 78 InputStream decoder = new BrotliInputStream(src); 79 long crc; 80 try { 81 crc = BundleHelper.fingerprintStream(decoder); 82 } finally { 83 decoder.close(); 84 } 85 assertEquals(BundleHelper.getExpectedFingerprint(entryName), crc); 86 } 87 } 88