View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  import java.net.URL;
32  import java.util.jar.JarEntry;
33  import java.util.jar.JarFile;
34  import java.util.jar.JarOutputStream;
35  
36  import org.apache.commons.compress.AbstractTempDirTest;
37  import org.apache.commons.io.input.BoundedInputStream;
38  import org.apache.commons.io.output.NullOutputStream;
39  import org.junit.jupiter.api.Test;
40  import org.junit.jupiter.params.ParameterizedTest;
41  import org.junit.jupiter.params.provider.ValueSource;
42  
43  /**
44   * Tests for org.apache.commons.compress.harmony.unpack200.Segment.
45   */
46  class SegmentTest extends AbstractTempDirTest {
47  
48      @Test
49      void testHelloWorld() throws Exception {
50          final File file = createTempFile("hello", "world.jar");
51          try (InputStream in = Segment.class.getResourceAsStream("/pack200/HelloWorld.pack");
52                  JarOutputStream out = new JarOutputStream(new FileOutputStream(file))) {
53              new Segment().unpack(in, out);
54          }
55          try (JarFile jarFile = new JarFile(file)) {
56              final JarEntry entry = jarFile.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
57              assertNotNull(entry);
58              final InputStream ours = jarFile.getInputStream(entry);
59              try (JarFile jarFile2 = new JarFile(new File(Segment.class.getResource("/pack200/hw.jar").toURI()))) {
60                  final JarEntry entry2 = jarFile2.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
61                  assertNotNull(entry2);
62                  final InputStream expected = jarFile2.getInputStream(entry2);
63                  try (BufferedReader reader1 = new BufferedReader(new InputStreamReader(ours));
64                          BufferedReader reader2 = new BufferedReader(new InputStreamReader(expected))) {
65                      String line1 = reader1.readLine();
66                      String line2 = reader2.readLine();
67                      int i = 1;
68                      while (line1 != null || line2 != null) {
69                          assertEquals(line2, line1, "Unpacked class files differ ar line " + i);
70                          line1 = reader1.readLine();
71                          line2 = reader2.readLine();
72                          i++;
73                      }
74                  }
75              }
76          }
77      }
78  
79      @Test
80      void testInterfaceOnly() throws Exception {
81          final File file = createTempFile("Interface", "Only.jar");
82          try (InputStream in = Segment.class.getResourceAsStream("/pack200/InterfaceOnly.pack");
83                  JarOutputStream out = new JarOutputStream(new FileOutputStream(file))) {
84              new Segment().unpack(in, out);
85          }
86      }
87  
88      @Test
89      void testJustResources() throws Exception {
90          final File file = createTempFile("just", "resources.jar");
91          try (InputStream in = Segment.class.getResourceAsStream("/pack200/JustResources.pack");
92                  JarOutputStream out = new JarOutputStream(new FileOutputStream(file))) {
93              new Segment().unpack(in, out);
94          }
95      }
96  
97      @ParameterizedTest
98      @ValueSource(strings = {
99      // @formatter:off
100             "bandint_oom.pack",
101             "cpfloat_oom.pack",
102             "cputf8_oom.pack",
103             "favoured_oom.pack",
104             "filebits_oom.pack",
105             "flags_oom.pack",
106             "references_oom.pack",
107             "segment_header_oom.pack",
108             "signatures_oom.pack"
109             // @formatter:on
110     })
111     // Tests of various files that can cause out of memory errors
112     void testParsingOOMBounded(final String testFileName) throws Exception {
113         final URL url = Segment.class.getResource("/org/apache/commons/compress/pack/" + testFileName);
114         try (BoundedInputStream in = Pack200UnpackerAdapter.newBoundedInputStream(url);
115                 JarOutputStream out = new JarOutputStream(NullOutputStream.INSTANCE)) {
116             assertThrows(IOException.class, () -> new Segment().unpack(in, out));
117         }
118     }
119 
120     @ParameterizedTest
121     @ValueSource(strings = {
122     // @formatter:off
123             "bandint_oom.pack",
124             "cpfloat_oom.pack",
125             "cputf8_oom.pack",
126             "favoured_oom.pack",
127             "filebits_oom.pack",
128             "flags_oom.pack",
129             "references_oom.pack",
130             "segment_header_oom.pack",
131             "signatures_oom.pack"
132             // @formatter:on
133     })
134     // Tests of various files that can cause out of memory errors
135     void testParsingOOMUnounded(final String testFileName) throws Exception {
136         try (InputStream in = Segment.class.getResourceAsStream("/org/apache/commons/compress/pack/" + testFileName);
137                 JarOutputStream out = new JarOutputStream(NullOutputStream.INSTANCE)) {
138             assertThrows(IOException.class, () -> new Segment().unpack(in, out));
139         }
140     }
141 
142 }