View Javadoc
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  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.util.Locale;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.commons.compress.AbstractTestCase;
30  
31  public class TarArchiveEntryTest extends TestCase implements TarConstants {
32  
33      private static final String OS =
34          System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
35      private static final String ROOT =
36          OS.startsWith("windows") || OS.startsWith("netware") ? "C:\\" : "/";
37  
38      /**
39       * JIRA issue SANDBOX-284
40       *
41       * @see "https://issues.apache.org/jira/browse/SANDBOX-284"
42       */
43      public void testFileSystemRoot() {
44          TarArchiveEntry t = new TarArchiveEntry(new File(ROOT));
45          assertEquals("/", t.getName());
46      }
47  
48      public void testTarFileWithFSRoot() throws IOException {
49          File f = File.createTempFile("taetest", ".tar");
50          f.deleteOnExit();
51          TarArchiveOutputStream tout = null;
52          TarArchiveInputStream tin = null;
53          try {
54              tout = new TarArchiveOutputStream(new FileOutputStream(f));
55              TarArchiveEntry t = new TarArchiveEntry(new File(ROOT));
56              tout.putArchiveEntry(t);
57              tout.closeArchiveEntry();
58              t = new TarArchiveEntry(new File(new File(ROOT), "foo.txt"));
59              t.setSize(6);
60              tout.putArchiveEntry(t);
61              tout.write(new byte[] {'h', 'e', 'l', 'l', 'o', ' '});
62              tout.closeArchiveEntry();
63              t = new TarArchiveEntry(new File(new File(ROOT), "bar.txt")
64                                      .getAbsolutePath());
65              t.setSize(5);
66              tout.putArchiveEntry(t);
67              tout.write(new byte[] {'w', 'o', 'r', 'l', 'd'});
68              tout.closeArchiveEntry();
69              t = new TarArchiveEntry("dummy");
70              t.setName(new File(new File(ROOT), "baz.txt").getAbsolutePath());
71              t.setSize(1);
72              tout.putArchiveEntry(t);
73              tout.write(new byte[] {'!'});
74              tout.closeArchiveEntry();
75              tout.close();
76              tout = null;
77  
78              tin = new TarArchiveInputStream(new FileInputStream(f));
79              //tin.setDebug(true);
80              t = tin.getNextTarEntry();
81              assertNotNull(t);
82              assertEquals("/", t.getName());
83              assertTrue(t.isCheckSumOK());
84              t = tin.getNextTarEntry();
85              assertNotNull(t);
86              assertEquals("foo.txt", t.getName());
87              assertTrue(t.isCheckSumOK());
88              t = tin.getNextTarEntry();
89              assertNotNull(t);
90              assertEquals("bar.txt", t.getName());
91              assertTrue(t.isCheckSumOK());
92              t = tin.getNextTarEntry();
93              assertNotNull(t);
94              assertEquals("baz.txt", t.getName());
95              assertTrue(t.isCheckSumOK());
96          } finally {
97              if (tin != null) {
98                  tin.close();
99              }
100             if (tout != null) {
101                 tout.close();
102             }
103             AbstractTestCase.tryHardToDelete(f);
104         }
105     }
106 
107     public void testMaxFileSize(){
108         TarArchiveEntry t = new TarArchiveEntry("");
109         t.setSize(0);
110         t.setSize(1);
111         try {
112             t.setSize(-1);
113             fail("Should have generated IllegalArgumentException");
114         } catch (IllegalArgumentException expected) {
115         }
116         t.setSize(077777777777L);
117         t.setSize(0100000000000L);
118     }
119 
120     public void testLinkFlagConstructor() {
121         TarArchiveEntry t = new TarArchiveEntry("/foo", LF_GNUTYPE_LONGNAME);
122         assertGnuMagic(t);
123         assertEquals("foo", t.getName());
124     }
125 
126     public void testLinkFlagConstructorWithFileFlag() {
127         TarArchiveEntry t = new TarArchiveEntry("/foo", LF_NORMAL);
128         assertPosixMagic(t);
129         assertEquals("foo", t.getName());
130     }
131 
132     public void testLinkFlagConstructorWithPreserve() {
133         TarArchiveEntry t = new TarArchiveEntry("/foo", LF_GNUTYPE_LONGNAME,
134                                                 true);
135         assertGnuMagic(t);
136         assertEquals("/foo", t.getName());
137     }
138 
139     private void assertGnuMagic(TarArchiveEntry t) {
140         assertEquals(MAGIC_GNU + VERSION_GNU_SPACE, readMagic(t));
141     }
142 
143     private void assertPosixMagic(TarArchiveEntry t) {
144         assertEquals(MAGIC_POSIX + VERSION_POSIX, readMagic(t));
145     }
146 
147     private String readMagic(TarArchiveEntry t) {
148         byte[] buf = new byte[512];
149         t.writeEntryHeader(buf);
150         return new String(buf, MAGIC_OFFSET, MAGICLEN + VERSIONLEN);
151     }
152 }