blob: d687ce40e2b150e4244af8fae5ef93d1597ff4d6 [file] [log] [blame]
Stefan Bodewig5c136a32011-12-07 15:01:27 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package org.apache.commons.compress.archivers.tar;
20
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000021import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertTrue;
23
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000024import java.io.ByteArrayInputStream;
Stefan Bodewig40f2e632012-03-02 19:59:50 +000025import java.io.File;
26import java.io.FileInputStream;
Stefan Bodewig40f2e632012-03-02 19:59:50 +000027import java.net.URI;
28import java.net.URL;
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +000029import java.util.Calendar;
Stefan Bodewig40f2e632012-03-02 19:59:50 +000030import java.util.Date;
Stefan Bodewig5c136a32011-12-07 15:01:27 +000031import java.util.Map;
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +000032import java.util.TimeZone;
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000033
34import org.apache.commons.compress.utils.CharsetNames;
Stefan Bodewig5c136a32011-12-07 15:01:27 +000035import org.junit.Test;
Stefan Bodewig5c136a32011-12-07 15:01:27 +000036
37public class TarArchiveInputStreamTest {
38
39 @Test
40 public void readSimplePaxHeader() throws Exception {
41 Map<String, String> headers = new TarArchiveInputStream(null)
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000042 .parsePaxHeaders(new ByteArrayInputStream("30 atime=1321711775.972059463\n"
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000043 .getBytes(CharsetNames.UTF_8)));
Stefan Bodewig5c136a32011-12-07 15:01:27 +000044 assertEquals(1, headers.size());
45 assertEquals("1321711775.972059463", headers.get("atime"));
46 }
47
48 @Test
49 public void readPaxHeaderWithEmbeddedNewline() throws Exception {
50 Map<String, String> headers = new TarArchiveInputStream(null)
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000051 .parsePaxHeaders(new ByteArrayInputStream("28 comment=line1\nline2\nand3\n"
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000052 .getBytes(CharsetNames.UTF_8)));
Stefan Bodewig5c136a32011-12-07 15:01:27 +000053 assertEquals(1, headers.size());
54 assertEquals("line1\nline2\nand3", headers.get("comment"));
55 }
Stefan Bodewig40f2e632012-03-02 19:59:50 +000056
57 @Test
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000058 public void readNonAsciiPaxHeader() throws Exception {
59 String ae = "\u00e4";
60 String line = "11 path="+ ae + "\n";
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000061 assertEquals(11, line.getBytes(CharsetNames.UTF_8).length);
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000062 Map<String, String> headers = new TarArchiveInputStream(null)
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000063 .parsePaxHeaders(new ByteArrayInputStream(line.getBytes(CharsetNames.UTF_8)));
Stefan Bodewigc2bc7a42012-03-23 13:47:59 +000064 assertEquals(1, headers.size());
65 assertEquals(ae, headers.get("path"));
66 }
67
68 @Test
Stefan Bodewig40f2e632012-03-02 19:59:50 +000069 public void workaroundForBrokenTimeHeader() throws Exception {
70 URL tar = getClass().getResource("/simple-aix-native-tar.tar");
71 TarArchiveInputStream in = null;
72 try {
73 in = new TarArchiveInputStream(new FileInputStream(new File(new URI(tar.toString()))));
74 TarArchiveEntry tae = in.getNextTarEntry();
75 tae = in.getNextTarEntry();
76 assertEquals("sample/link-to-txt-file.lnk", tae.getName());
77 assertEquals(new Date(0), tae.getLastModifiedDate());
78 assertTrue(tae.isSymbolicLink());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +000079 assertTrue(tae.isCheckSumOK());
Stefan Bodewig40f2e632012-03-02 19:59:50 +000080 } finally {
81 if (in != null) {
82 in.close();
83 }
84 }
Sebastian Bazley39c93f42012-03-31 12:30:09 +000085 }
Stefan Bodewig40f2e632012-03-02 19:59:50 +000086
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +000087 @Test
88 public void datePriorToEpochInGNUFormat() throws Exception {
Stefan Bodewigbe286da2012-03-05 19:34:12 +000089 datePriorToEpoch("/preepoch-star.tar");
Stefan Bodewig1ffe1b12012-03-04 07:46:48 +000090 }
91
92
93 @Test
94 public void datePriorToEpochInPAXFormat() throws Exception {
95 datePriorToEpoch("/preepoch-posix.tar");
96 }
97
98 private void datePriorToEpoch(String archive) throws Exception {
99 URL tar = getClass().getResource(archive);
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +0000100 TarArchiveInputStream in = null;
101 try {
102 in = new TarArchiveInputStream(new FileInputStream(new File(new URI(tar.toString()))));
103 TarArchiveEntry tae = in.getNextTarEntry();
104 assertEquals("foo", tae.getName());
105 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
106 cal.set(1969, 11, 31, 23, 59, 59);
107 cal.set(Calendar.MILLISECOND, 0);
108 assertEquals(cal.getTime(), tae.getLastModifiedDate());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +0000109 assertTrue(tae.isCheckSumOK());
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +0000110 } finally {
111 if (in != null) {
112 in.close();
113 }
114 }
Sebastian Bazley39c93f42012-03-31 12:30:09 +0000115 }
Stefan Bodewig6c71a2b2012-03-04 07:28:12 +0000116
Stefan Bodewig5c136a32011-12-07 15:01:27 +0000117}