blob: 2f8d6f4dffa9364122fa446f69933a57cc85de4c [file] [log] [blame]
Stefan Bodewig32eea1e2009-03-17 12:53:22 +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
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.util.Locale;
Gary D. Gregoryd7119be2012-03-31 22:36:04 +000026
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000027import junit.framework.TestCase;
28
Stefan Bodewig6daf5852011-08-12 14:21:44 +000029import org.apache.commons.compress.AbstractTestCase;
30
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000031public class TarArchiveEntryTest extends TestCase {
32
33 private static final String OS =
34 System.getProperty("os.name").toLowerCase(Locale.US);
35 private static final String ROOT =
36 OS.startsWith("windows") || OS.startsWith("netware") ? "C:\\" : "/";
37
38 /**
39 * JIRA issue SANDBOX-284
40 *
Sebastian Bazley8143cde2009-03-23 21:36:23 +000041 * @see "https://issues.apache.org/jira/browse/SANDBOX-284"
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000042 */
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));
Sebastian Bazley421e3792009-03-30 16:45:13 +000056 tout.putArchiveEntry(t);
57 tout.closeArchiveEntry();
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000058 t = new TarArchiveEntry(new File(new File(ROOT), "foo.txt"));
59 t.setSize(6);
Sebastian Bazley421e3792009-03-30 16:45:13 +000060 tout.putArchiveEntry(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000061 tout.write(new byte[] {'h', 'e', 'l', 'l', 'o', ' '});
Sebastian Bazley421e3792009-03-30 16:45:13 +000062 tout.closeArchiveEntry();
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000063 t = new TarArchiveEntry(new File(new File(ROOT), "bar.txt")
64 .getAbsolutePath());
65 t.setSize(5);
Sebastian Bazley421e3792009-03-30 16:45:13 +000066 tout.putArchiveEntry(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000067 tout.write(new byte[] {'w', 'o', 'r', 'l', 'd'});
Sebastian Bazley421e3792009-03-30 16:45:13 +000068 tout.closeArchiveEntry();
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000069 t = new TarArchiveEntry("dummy");
70 t.setName(new File(new File(ROOT), "baz.txt").getAbsolutePath());
71 t.setSize(1);
Sebastian Bazley421e3792009-03-30 16:45:13 +000072 tout.putArchiveEntry(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000073 tout.write(new byte[] {'!'});
Sebastian Bazley421e3792009-03-30 16:45:13 +000074 tout.closeArchiveEntry();
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000075 tout.close();
76 tout = null;
77
78 tin = new TarArchiveInputStream(new FileInputStream(f));
Stefan Bodewig82594a22009-03-17 19:56:57 +000079 //tin.setDebug(true);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000080 t = tin.getNextTarEntry();
Stefan Bodewig82594a22009-03-17 19:56:57 +000081 assertNotNull(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000082 assertEquals("/", t.getName());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +000083 assertTrue(t.isCheckSumOK());
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000084 t = tin.getNextTarEntry();
Stefan Bodewig82594a22009-03-17 19:56:57 +000085 assertNotNull(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000086 assertEquals("foo.txt", t.getName());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +000087 assertTrue(t.isCheckSumOK());
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000088 t = tin.getNextTarEntry();
Stefan Bodewig82594a22009-03-17 19:56:57 +000089 assertNotNull(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000090 assertEquals("bar.txt", t.getName());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +000091 assertTrue(t.isCheckSumOK());
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000092 t = tin.getNextTarEntry();
Stefan Bodewig82594a22009-03-17 19:56:57 +000093 assertNotNull(t);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000094 assertEquals("baz.txt", t.getName());
Stefan Bodewig811fb4e2012-07-07 05:19:39 +000095 assertTrue(t.isCheckSumOK());
Stefan Bodewig32eea1e2009-03-17 12:53:22 +000096 } finally {
97 if (tin != null) {
98 tin.close();
99 }
100 if (tout != null) {
101 tout.close();
102 }
Stefan Bodewig6daf5852011-08-12 14:21:44 +0000103 AbstractTestCase.tryHardToDelete(f);
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000104 }
105 }
Sebastian Bazley39c93f42012-03-31 12:30:09 +0000106
Sebastian Bazley26d12c52009-03-31 18:45:14 +0000107 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);
Stefan Bodewiga6fbdae2011-12-08 14:03:57 +0000117 t.setSize(0100000000000L);
Stefan Bodewig5dad1122011-12-07 11:34:34 +0000118 }
Stefan Bodewig32eea1e2009-03-17 12:53:22 +0000119}