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