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  
58              final JarFile jarFile2 = new JarFile(new File(Segment.class.getResource("/pack200/hw.jar").toURI()));
59              final JarEntry entry2 = jarFile2.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
60              assertNotNull(entry2);
61  
62              final InputStream expected = jarFile2.getInputStream(entry2);
63  
64              try (BufferedReader reader1 = new BufferedReader(new InputStreamReader(ours));
65                      BufferedReader reader2 = new BufferedReader(new InputStreamReader(expected))) {
66                  String line1 = reader1.readLine();
67                  String line2 = reader2.readLine();
68                  int i = 1;
69                  while (line1 != null || line2 != null) {
70                      assertEquals(line2, line1, "Unpacked class files differ ar line " + i);
71                      line1 = reader1.readLine();
72                      line2 = reader2.readLine();
73                      i++;
74                  }
75              }
76          }
77      }
78  
79      @Test
80      public 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      public 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     public 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     public 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 }