Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | package org.apache.commons.compress.archivers.tar; |
| 20 | |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
Stefan Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 24 | import java.io.ByteArrayInputStream; |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 25 | import java.io.File; |
| 26 | import java.io.FileInputStream; |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 27 | import java.net.URI; |
| 28 | import java.net.URL; |
Stefan Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 29 | import java.util.Calendar; |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 30 | import java.util.Date; |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 31 | import java.util.Map; |
Stefan Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 32 | import java.util.TimeZone; |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 33 | |
| 34 | import org.apache.commons.compress.utils.CharsetNames; |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 35 | import org.junit.Test; |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 36 | |
| 37 | public class TarArchiveInputStreamTest { |
| 38 | |
| 39 | @Test |
| 40 | public void readSimplePaxHeader() throws Exception { |
| 41 | Map<String, String> headers = new TarArchiveInputStream(null) |
Stefan Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 42 | .parsePaxHeaders(new ByteArrayInputStream("30 atime=1321711775.972059463\n" |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 43 | .getBytes(CharsetNames.UTF_8))); |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 44 | 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 Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 51 | .parsePaxHeaders(new ByteArrayInputStream("28 comment=line1\nline2\nand3\n" |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 52 | .getBytes(CharsetNames.UTF_8))); |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 53 | assertEquals(1, headers.size()); |
| 54 | assertEquals("line1\nline2\nand3", headers.get("comment")); |
| 55 | } |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 56 | |
| 57 | @Test |
Stefan Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 58 | public void readNonAsciiPaxHeader() throws Exception { |
| 59 | String ae = "\u00e4"; |
| 60 | String line = "11 path="+ ae + "\n"; |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 61 | assertEquals(11, line.getBytes(CharsetNames.UTF_8).length); |
Stefan Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 62 | Map<String, String> headers = new TarArchiveInputStream(null) |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 63 | .parsePaxHeaders(new ByteArrayInputStream(line.getBytes(CharsetNames.UTF_8))); |
Stefan Bodewig | c2bc7a4 | 2012-03-23 13:47:59 +0000 | [diff] [blame] | 64 | assertEquals(1, headers.size()); |
| 65 | assertEquals(ae, headers.get("path")); |
| 66 | } |
| 67 | |
| 68 | @Test |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 69 | 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 Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 79 | assertTrue(tae.isCheckSumOK()); |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 80 | } finally { |
| 81 | if (in != null) { |
| 82 | in.close(); |
| 83 | } |
| 84 | } |
Sebastian Bazley | 39c93f4 | 2012-03-31 12:30:09 +0000 | [diff] [blame] | 85 | } |
Stefan Bodewig | 40f2e63 | 2012-03-02 19:59:50 +0000 | [diff] [blame] | 86 | |
Stefan Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 87 | @Test |
| 88 | public void datePriorToEpochInGNUFormat() throws Exception { |
Stefan Bodewig | be286da | 2012-03-05 19:34:12 +0000 | [diff] [blame] | 89 | datePriorToEpoch("/preepoch-star.tar"); |
Stefan Bodewig | 1ffe1b1 | 2012-03-04 07:46:48 +0000 | [diff] [blame] | 90 | } |
| 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 Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 100 | 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 Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 109 | assertTrue(tae.isCheckSumOK()); |
Stefan Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 110 | } finally { |
| 111 | if (in != null) { |
| 112 | in.close(); |
| 113 | } |
| 114 | } |
Sebastian Bazley | 39c93f4 | 2012-03-31 12:30:09 +0000 | [diff] [blame] | 115 | } |
Stefan Bodewig | 6c71a2b | 2012-03-04 07:28:12 +0000 | [diff] [blame] | 116 | |
Stefan Bodewig | 5c136a3 | 2011-12-07 15:01:27 +0000 | [diff] [blame] | 117 | } |