1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }