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.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.params.ParameterizedTest;
32  import org.junit.jupiter.params.provider.Arguments;
33  import org.junit.jupiter.params.provider.MethodSource;
34  
35  public class CpioArchiveTest {
36  
37      public static Stream<Arguments> factory() {
38          return Stream.of(Arguments.of(CpioConstants.FORMAT_NEW), Arguments.of(CpioConstants.FORMAT_NEW_CRC), Arguments.of(CpioConstants.FORMAT_OLD_ASCII),
39                  Arguments.of(CpioConstants.FORMAT_OLD_BINARY));
40      }
41  
42      @ParameterizedTest
43      @MethodSource("factory")
44      public void utf18RoundtripTest(final short format) throws Exception {
45          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
46              try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE, StandardCharsets.UTF_16LE.name())) {
47                  final CpioArchiveEntry entry = new CpioArchiveEntry(format, "T\u00e4st.txt", 4);
48                  if (format == CpioConstants.FORMAT_NEW_CRC) {
49                      entry.setChksum(10);
50                  }
51                  os.putArchiveEntry(entry);
52                  os.write(new byte[] { 1, 2, 3, 4 });
53                  os.closeArchiveEntry();
54              }
55              baos.close();
56              try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
57                      CpioArchiveInputStream in = new CpioArchiveInputStream(bin, StandardCharsets.UTF_16LE.name())) {
58                  final CpioArchiveEntry entry = in.getNextEntry();
59                  assertNotNull(entry);
60                  assertEquals("T\u00e4st.txt", entry.getName());
61                  assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in));
62              }
63          }
64      }
65  }