1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
110 })
111
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
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
133 })
134
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 }