1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.archivers.arj;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Calendar;
31 import java.util.TimeZone;
32
33 import org.apache.commons.compress.AbstractTest;
34 import org.apache.commons.compress.archivers.ArchiveException;
35 import org.apache.commons.io.IOUtils;
36 import org.junit.jupiter.api.Test;
37
38 public class ArjArchiveInputStreamTest extends AbstractTest {
39
40 @Test
41 public void testArjUnarchive() throws Exception {
42 final StringBuilder expected = new StringBuilder();
43 expected.append("test1.xml<?xml version=\"1.0\"?>\n");
44 expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
45 expected.append("<empty/>\n");
46
47 final StringBuilder result = new StringBuilder();
48 try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
49 ArjArchiveEntry entry;
50
51 while ((entry = in.getNextEntry()) != null) {
52 result.append(entry.getName());
53 int tmp;
54 while ((tmp = in.read()) != -1) {
55 result.append((char) tmp);
56 }
57 assertFalse(entry.isDirectory());
58 }
59 }
60 assertEquals(result.toString(), expected.toString());
61 }
62
63 @Test
64 public void testFirstHeaderSizeSetToZero() throws Exception {
65 try (InputStream in = newInputStream("org/apache/commons/compress/arj/zero_sized_headers-fail.arj")) {
66 final ArchiveException ex = assertThrows(ArchiveException.class, () -> {
67 try (ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
68
69 }
70 });
71 assertTrue(ex.getCause() instanceof IOException);
72 }
73 }
74
75 @Test
76 public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
77 final byte[] buf = new byte[2];
78 try (InputStream in = newInputStream("bla.arj");
79 ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
80 assertNotNull(archive.getNextEntry());
81 IOUtils.toByteArray(archive);
82 assertEquals(-1, archive.read(buf));
83 assertEquals(-1, archive.read(buf));
84 }
85 }
86
87 @Test
88 public void testReadingOfAttributesDosVersion() throws Exception {
89 try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
90 final ArjArchiveEntry entry = in.getNextEntry();
91 assertEquals("test1.xml", entry.getName());
92 assertEquals(30, entry.getSize());
93 assertEquals(0, entry.getUnixMode());
94 final Calendar cal = Calendar.getInstance();
95 cal.set(2008, 9, 6, 23, 50, 52);
96 cal.set(Calendar.MILLISECOND, 0);
97 assertEquals(cal.getTime(), entry.getLastModifiedDate());
98 }
99 }
100
101 @Test
102 public void testReadingOfAttributesUnixVersion() throws Exception {
103 try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.unix.arj"))) {
104 final ArjArchiveEntry entry = in.getNextEntry();
105 assertEquals("test1.xml", entry.getName());
106 assertEquals(30, entry.getSize());
107 assertEquals(0664, entry.getUnixMode() & 07777 );
108 final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0000"));
109 cal.set(2008, 9, 6, 21, 50, 52);
110 cal.set(Calendar.MILLISECOND, 0);
111 assertEquals(cal.getTime(), entry.getLastModifiedDate());
112 }
113 }
114
115 @Test
116 public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
117 try (InputStream in = newInputStream("bla.arj");
118 ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
119 assertNotNull(archive.getNextEntry());
120 IOUtils.toByteArray(archive);
121 assertEquals(-1, archive.read());
122 assertEquals(-1, archive.read());
123 }
124 }
125
126 }