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   * http://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  
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                      // empty
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 /* UnixStat.PERM_MASK */);
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 }