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