Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +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 | |
| 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; |
Gary D. Gregory | d7119be | 2012-03-31 22:36:04 +0000 | [diff] [blame] | 26 | |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 27 | import junit.framework.TestCase; |
| 28 | |
Stefan Bodewig | 6daf585 | 2011-08-12 14:21:44 +0000 | [diff] [blame] | 29 | import org.apache.commons.compress.AbstractTestCase; |
| 30 | |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 31 | public 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 Bazley | 8143cde | 2009-03-23 21:36:23 +0000 | [diff] [blame] | 41 | * @see "https://issues.apache.org/jira/browse/SANDBOX-284" |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 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)); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 56 | tout.putArchiveEntry(t); |
| 57 | tout.closeArchiveEntry(); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 58 | t = new TarArchiveEntry(new File(new File(ROOT), "foo.txt")); |
| 59 | t.setSize(6); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 60 | tout.putArchiveEntry(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 61 | tout.write(new byte[] {'h', 'e', 'l', 'l', 'o', ' '}); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 62 | tout.closeArchiveEntry(); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 63 | t = new TarArchiveEntry(new File(new File(ROOT), "bar.txt") |
| 64 | .getAbsolutePath()); |
| 65 | t.setSize(5); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 66 | tout.putArchiveEntry(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 67 | tout.write(new byte[] {'w', 'o', 'r', 'l', 'd'}); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 68 | tout.closeArchiveEntry(); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 69 | t = new TarArchiveEntry("dummy"); |
| 70 | t.setName(new File(new File(ROOT), "baz.txt").getAbsolutePath()); |
| 71 | t.setSize(1); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 72 | tout.putArchiveEntry(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 73 | tout.write(new byte[] {'!'}); |
Sebastian Bazley | 421e379 | 2009-03-30 16:45:13 +0000 | [diff] [blame] | 74 | tout.closeArchiveEntry(); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 75 | tout.close(); |
| 76 | tout = null; |
| 77 | |
| 78 | tin = new TarArchiveInputStream(new FileInputStream(f)); |
Stefan Bodewig | 82594a2 | 2009-03-17 19:56:57 +0000 | [diff] [blame] | 79 | //tin.setDebug(true); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 80 | t = tin.getNextTarEntry(); |
Stefan Bodewig | 82594a2 | 2009-03-17 19:56:57 +0000 | [diff] [blame] | 81 | assertNotNull(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 82 | assertEquals("/", t.getName()); |
Stefan Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 83 | assertTrue(t.isCheckSumOK()); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 84 | t = tin.getNextTarEntry(); |
Stefan Bodewig | 82594a2 | 2009-03-17 19:56:57 +0000 | [diff] [blame] | 85 | assertNotNull(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 86 | assertEquals("foo.txt", t.getName()); |
Stefan Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 87 | assertTrue(t.isCheckSumOK()); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 88 | t = tin.getNextTarEntry(); |
Stefan Bodewig | 82594a2 | 2009-03-17 19:56:57 +0000 | [diff] [blame] | 89 | assertNotNull(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 90 | assertEquals("bar.txt", t.getName()); |
Stefan Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 91 | assertTrue(t.isCheckSumOK()); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 92 | t = tin.getNextTarEntry(); |
Stefan Bodewig | 82594a2 | 2009-03-17 19:56:57 +0000 | [diff] [blame] | 93 | assertNotNull(t); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 94 | assertEquals("baz.txt", t.getName()); |
Stefan Bodewig | 811fb4e | 2012-07-07 05:19:39 +0000 | [diff] [blame^] | 95 | assertTrue(t.isCheckSumOK()); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 96 | } finally { |
| 97 | if (tin != null) { |
| 98 | tin.close(); |
| 99 | } |
| 100 | if (tout != null) { |
| 101 | tout.close(); |
| 102 | } |
Stefan Bodewig | 6daf585 | 2011-08-12 14:21:44 +0000 | [diff] [blame] | 103 | AbstractTestCase.tryHardToDelete(f); |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Sebastian Bazley | 39c93f4 | 2012-03-31 12:30:09 +0000 | [diff] [blame] | 106 | |
Sebastian Bazley | 26d12c5 | 2009-03-31 18:45:14 +0000 | [diff] [blame] | 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); |
Stefan Bodewig | a6fbdae | 2011-12-08 14:03:57 +0000 | [diff] [blame] | 117 | t.setSize(0100000000000L); |
Stefan Bodewig | 5dad112 | 2011-12-07 11:34:34 +0000 | [diff] [blame] | 118 | } |
Stefan Bodewig | 32eea1e | 2009-03-17 12:53:22 +0000 | [diff] [blame] | 119 | } |