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   *   https://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.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.nio.charset.StandardCharsets;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.Arguments;
34  import org.junit.jupiter.params.provider.MethodSource;
35  
36  class CpioArchiveTest {
37  
38      public static Stream<Arguments> factory() {
39          return Stream.of(Arguments.of(CpioConstants.FORMAT_NEW), Arguments.of(CpioConstants.FORMAT_NEW_CRC), Arguments.of(CpioConstants.FORMAT_OLD_ASCII),
40                  Arguments.of(CpioConstants.FORMAT_OLD_BINARY));
41      }
42  
43      @Test
44      void utf18RoundtripTestCtor2() throws Exception {
45          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
46              try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, StandardCharsets.UTF_8.name())) {
47                  final CpioArchiveEntry entry = new CpioArchiveEntry("Test.txt", 4);
48                  os.putArchiveEntry(entry);
49                  os.write(new byte[] { 1, 2, 3, 4 });
50                  os.closeArchiveEntry();
51              }
52              baos.close();
53              try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
54                      CpioArchiveInputStream in = new CpioArchiveInputStream(bin, StandardCharsets.UTF_8.name())) {
55                  final CpioArchiveEntry entry = in.getNextEntry();
56                  assertNotNull(entry);
57                  assertEquals("Test.txt", entry.getName());
58                  assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in));
59              }
60          }
61      }
62  
63      @ParameterizedTest
64      @MethodSource("factory")
65      public void utf18RoundtripTestCtor3(final short format) throws Exception {
66          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
67              try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE)) {
68                  final CpioArchiveEntry entry = new CpioArchiveEntry(format, "T\u00e4st.txt", 4);
69                  if (format == CpioConstants.FORMAT_NEW_CRC) {
70                      entry.setChksum(10);
71                  }
72                  os.putArchiveEntry(entry);
73                  os.write(new byte[] { 1, 2, 3, 4 });
74                  os.closeArchiveEntry();
75              }
76              baos.close();
77              try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
78                      CpioArchiveInputStream in = new CpioArchiveInputStream(bin)) {
79                  final CpioArchiveEntry entry = in.getNextEntry();
80                  assertNotNull(entry);
81                  assertEquals("T%U00E4st.txt", entry.getName());
82                  assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in));
83              }
84          }
85      }
86  
87      @ParameterizedTest
88      @MethodSource("factory")
89      public void utf18RoundtripTestCtor4(final short format) throws Exception {
90          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
91              try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE, StandardCharsets.UTF_16LE.name())) {
92                  final CpioArchiveEntry entry = new CpioArchiveEntry(format, "T\u00e4st.txt", 4);
93                  if (format == CpioConstants.FORMAT_NEW_CRC) {
94                      entry.setChksum(10);
95                  }
96                  os.putArchiveEntry(entry);
97                  os.write(new byte[] { 1, 2, 3, 4 });
98                  os.closeArchiveEntry();
99              }
100             baos.close();
101             try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
102                     CpioArchiveInputStream in = new CpioArchiveInputStream(bin, StandardCharsets.UTF_16LE.name())) {
103                 final CpioArchiveEntry entry = in.getNextEntry();
104                 assertNotNull(entry);
105                 assertEquals("T\u00e4st.txt", entry.getName());
106                 assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in));
107             }
108         }
109     }
110 }