Home | History | Annotate | Download | only in cpio
      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 java.io.ByteArrayInputStream;
     22 import java.io.ByteArrayOutputStream;
     23 import java.util.Arrays;
     24 import java.util.Collection;
     25 import org.apache.commons.compress.utils.IOUtils;
     26 import org.junit.Assert;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.junit.runners.Parameterized.Parameters;
     30 import org.junit.runners.Parameterized;
     31 
     32 @RunWith(Parameterized.class)
     33 public class CpioArchiveTest {
     34 
     35     @Parameters(name = "using {0}")
     36     public static Collection<Object[]> factory() {
     37         return Arrays.asList(new Object[][] {
     38                 new Object[]  { CpioConstants.FORMAT_NEW },
     39                 new Object[]  { CpioConstants.FORMAT_NEW_CRC },
     40                 new Object[]  { CpioConstants.FORMAT_OLD_ASCII },
     41                 new Object[]  { CpioConstants.FORMAT_OLD_BINARY },
     42             });
     43     }
     44 
     45     private final short format;
     46 
     47     public CpioArchiveTest(short format) {
     48         this.format = format;
     49     }
     50 
     51     @Test
     52     public void utf18RoundtripTest() throws Exception {
     53         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
     54             try (CpioArchiveOutputStream os = new CpioArchiveOutputStream(baos, format, CpioConstants.BLOCK_SIZE,
     55                 "UTF-16LE")) {
     56                 CpioArchiveEntry entry = new CpioArchiveEntry(format, "T\u00e4st.txt", 4);
     57                 if (format == CpioConstants.FORMAT_NEW_CRC) {
     58                     entry.setChksum(10);
     59                 }
     60                 os.putArchiveEntry(entry);
     61                 os.write(new byte[] { 1, 2, 3, 4 });
     62                 os.closeArchiveEntry();
     63             }
     64             baos.close();
     65             try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
     66                  CpioArchiveInputStream in = new CpioArchiveInputStream(bin, "UTF-16LE")) {
     67                 CpioArchiveEntry entry = (CpioArchiveEntry) in.getNextEntry();
     68                 Assert.assertNotNull(entry);
     69                 Assert.assertEquals("T\u00e4st.txt", entry.getName());
     70                 Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, IOUtils.toByteArray(in));
     71             }
     72         }
     73     }
     74 }
     75