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  package org.apache.commons.compress.archivers.cpio;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.nio.charset.StandardCharsets;
28  
29  import org.apache.commons.compress.AbstractTest;
30  import org.apache.commons.io.IOUtils;
31  import org.junit.jupiter.api.Test;
32  
33  public class CpioArchiveInputStreamTest extends AbstractTest {
34  
35      private long consumeEntries(final CpioArchiveInputStream in) throws IOException {
36          long count = 0;
37          CpioArchiveEntry entry;
38          while ((entry = in.getNextEntry()) != null) {
39              count++;
40              assertNotNull(entry);
41          }
42          return count;
43      }
44  
45      @Test
46      public void testCpioUnarchive() throws Exception {
47          final StringBuilder expected = new StringBuilder();
48          expected.append("./test1.xml<?xml version=\"1.0\"?>\n");
49          expected.append("<empty/>./test2.xml<?xml version=\"1.0\"?>\n");
50          expected.append("<empty/>\n");
51          final StringBuilder result = new StringBuilder();
52          try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("bla.cpio"))) {
53              CpioArchiveEntry entry;
54              while ((entry = in.getNextEntry()) != null) {
55                  result.append(entry.getName());
56                  int tmp;
57                  while ((tmp = in.read()) != -1) {
58                      result.append((char) tmp);
59                  }
60              }
61          }
62          assertEquals(result.toString(), expected.toString());
63      }
64  
65      @Test
66      public void testCpioUnarchiveCreatedByRedlineRpm() throws Exception {
67          long count = 0;
68          try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("redline.cpio"))) {
69              count = consumeEntries(in);
70          }
71          assertEquals(count, 1);
72      }
73  
74      @Test
75      public void testCpioUnarchiveMultibyteCharName() throws Exception {
76          long count = 0;
77          try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("COMPRESS-459.cpio"), StandardCharsets.UTF_8.name())) {
78              count = consumeEntries(in);
79          }
80          assertEquals(2, count);
81      }
82  
83      @Test
84      public void testInvalidLongValueInMetadata() throws Exception {
85          try (InputStream in = newInputStream("org/apache/commons/compress/cpio/bad_long_value.cpio");
86               CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
87              assertThrows(IOException.class, archive::getNextEntry);
88          }
89      }
90  
91      @Test
92      public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
93          final byte[] buf = new byte[2];
94          try (InputStream in = newInputStream("bla.cpio");
95                  CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
96              assertNotNull(archive.getNextEntry());
97              IOUtils.toByteArray(archive);
98              assertEquals(-1, archive.read(buf));
99              assertEquals(-1, archive.read(buf));
100         }
101     }
102 
103     @Test
104     public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
105         try (InputStream in = newInputStream("bla.cpio");
106                 CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
107             assertNotNull(archive.getNextEntry());
108             IOUtils.toByteArray(archive);
109             assertEquals(-1, archive.read());
110             assertEquals(-1, archive.read());
111         }
112     }
113 
114 }