001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018 019package org.apache.commons.compress.archivers.tar; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.util.Locale; 026 027import junit.framework.TestCase; 028 029import org.apache.commons.compress.AbstractTestCase; 030 031public class TarArchiveEntryTest extends TestCase implements TarConstants { 032 033 private static final String OS = 034 System.getProperty("os.name").toLowerCase(Locale.ENGLISH); 035 private static final String ROOT = 036 OS.startsWith("windows") || OS.startsWith("netware") ? "C:\\" : "/"; 037 038 /** 039 * JIRA issue SANDBOX-284 040 * 041 * @see "https://issues.apache.org/jira/browse/SANDBOX-284" 042 */ 043 public void testFileSystemRoot() { 044 TarArchiveEntry t = new TarArchiveEntry(new File(ROOT)); 045 assertEquals("/", t.getName()); 046 } 047 048 public void testTarFileWithFSRoot() throws IOException { 049 File f = File.createTempFile("taetest", ".tar"); 050 f.deleteOnExit(); 051 TarArchiveOutputStream tout = null; 052 TarArchiveInputStream tin = null; 053 try { 054 tout = new TarArchiveOutputStream(new FileOutputStream(f)); 055 TarArchiveEntry t = new TarArchiveEntry(new File(ROOT)); 056 tout.putArchiveEntry(t); 057 tout.closeArchiveEntry(); 058 t = new TarArchiveEntry(new File(new File(ROOT), "foo.txt")); 059 t.setSize(6); 060 tout.putArchiveEntry(t); 061 tout.write(new byte[] {'h', 'e', 'l', 'l', 'o', ' '}); 062 tout.closeArchiveEntry(); 063 t = new TarArchiveEntry(new File(new File(ROOT), "bar.txt") 064 .getAbsolutePath()); 065 t.setSize(5); 066 tout.putArchiveEntry(t); 067 tout.write(new byte[] {'w', 'o', 'r', 'l', 'd'}); 068 tout.closeArchiveEntry(); 069 t = new TarArchiveEntry("dummy"); 070 t.setName(new File(new File(ROOT), "baz.txt").getAbsolutePath()); 071 t.setSize(1); 072 tout.putArchiveEntry(t); 073 tout.write(new byte[] {'!'}); 074 tout.closeArchiveEntry(); 075 tout.close(); 076 tout = null; 077 078 tin = new TarArchiveInputStream(new FileInputStream(f)); 079 //tin.setDebug(true); 080 t = tin.getNextTarEntry(); 081 assertNotNull(t); 082 assertEquals("/", t.getName()); 083 assertTrue(t.isCheckSumOK()); 084 t = tin.getNextTarEntry(); 085 assertNotNull(t); 086 assertEquals("foo.txt", t.getName()); 087 assertTrue(t.isCheckSumOK()); 088 t = tin.getNextTarEntry(); 089 assertNotNull(t); 090 assertEquals("bar.txt", t.getName()); 091 assertTrue(t.isCheckSumOK()); 092 t = tin.getNextTarEntry(); 093 assertNotNull(t); 094 assertEquals("baz.txt", t.getName()); 095 assertTrue(t.isCheckSumOK()); 096 } finally { 097 if (tin != null) { 098 tin.close(); 099 } 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}