Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +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 junit.framework.TestCase; |
| 22 | |
| 23 | public class TarUtilsTest extends TestCase { |
| 24 | |
| 25 | |
| 26 | public void testName(){ |
| 27 | byte [] buff = new byte[20]; |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 28 | String sb1 = "abcdefghijklmnopqrstuvwxyz"; |
| 29 | int off = TarUtils.formatNameBytes(sb1, buff, 1, buff.length-1); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 30 | assertEquals(off, 20); |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 31 | String sb2 = TarUtils.parseName(buff, 1, 10); |
| 32 | assertEquals(sb2,sb1.substring(0,10)); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 33 | sb2 = TarUtils.parseName(buff, 1, 19); |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 34 | assertEquals(sb2,sb1.substring(0,19)); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 35 | buff = new byte[30]; |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 36 | off = TarUtils.formatNameBytes(sb1, buff, 1, buff.length-1); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 37 | assertEquals(off, 30); |
| 38 | sb2 = TarUtils.parseName(buff, 1, buff.length-1); |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 39 | assertEquals(sb1, sb2); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private void fillBuff(byte []buffer, String input){ |
| 43 | for(int i=0; i<buffer.length;i++){ |
| 44 | buffer[i]=0; |
| 45 | } |
| 46 | System.arraycopy(input.getBytes(),0,buffer,0,Math.min(buffer.length,input.length())); |
| 47 | } |
| 48 | |
| 49 | public void testParseOctal(){ |
| 50 | byte [] buffer = new byte[20]; |
| 51 | fillBuff(buffer,"777777777777 "); |
| 52 | long value; |
| 53 | value = TarUtils.parseOctal(buffer,0, 11); |
| 54 | assertEquals(077777777777L, value); |
| 55 | value = TarUtils.parseOctal(buffer,0, 12); |
| 56 | assertEquals(0777777777777L, value); |
| 57 | buffer[11]=' '; |
| 58 | value = TarUtils.parseOctal(buffer,0, 11); |
| 59 | assertEquals(077777777777L, value); |
| 60 | buffer[11]=0; |
| 61 | value = TarUtils.parseOctal(buffer,0, 11); |
| 62 | assertEquals(077777777777L, value); |
| 63 | fillBuff(buffer, "abcdef"); // Invalid input |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 64 | try { |
| 65 | value = TarUtils.parseOctal(buffer,0, 11); |
| 66 | fail("Expected IllegalArgumentException"); |
| 67 | } catch (IllegalArgumentException expected) { |
| 68 | } |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private void checkRoundTripOctal(final long value) { |
| 72 | byte [] buffer = new byte[12]; |
| 73 | long parseValue; |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 74 | TarUtils.formatLongOctalBytes(value, buffer, 0, buffer.length); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 75 | parseValue = TarUtils.parseOctal(buffer,0, buffer.length); |
| 76 | assertEquals(value,parseValue); |
| 77 | } |
| 78 | |
| 79 | public void testRoundTripOctal() { |
| 80 | checkRoundTripOctal(0); |
| 81 | checkRoundTripOctal(1); |
| 82 | // checkRoundTripOctal(-1); // TODO What should this do? |
| 83 | checkRoundTripOctal(077777777777L); |
| 84 | // checkRoundTripOctal(0100000000000L); // TODO What should this do? |
| 85 | } |
| 86 | |
| 87 | // Check correct trailing bytes are generated |
| 88 | public void testTrailers() { |
| 89 | byte [] buffer = new byte[12]; |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 90 | TarUtils.formatLongOctalBytes(123, buffer, 0, buffer.length); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 91 | assertEquals(' ', buffer[buffer.length-1]); |
| 92 | assertEquals('3', buffer[buffer.length-2]); // end of number |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 93 | TarUtils.formatOctalBytes(123, buffer, 0, buffer.length); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 94 | assertEquals(0 , buffer[buffer.length-1]); |
| 95 | assertEquals(' ', buffer[buffer.length-2]); |
| 96 | assertEquals('3', buffer[buffer.length-3]); // end of number |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 97 | TarUtils.formatCheckSumOctalBytes(123, buffer, 0, buffer.length); |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 98 | assertEquals(' ', buffer[buffer.length-1]); |
| 99 | assertEquals(0 , buffer[buffer.length-2]); |
| 100 | assertEquals('3', buffer[buffer.length-3]); // end of number |
| 101 | } |
Sebastian Bazley | 91401bf | 2009-04-01 00:00:49 +0000 | [diff] [blame] | 102 | |
| 103 | public void testNegative() { |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 104 | byte [] buffer = new byte[22]; |
Sebastian Bazley | 91401bf | 2009-04-01 00:00:49 +0000 | [diff] [blame] | 105 | TarUtils.formatUnsignedOctalString(-1, buffer, 0, buffer.length); |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 106 | assertEquals("1777777777777777777777", new String(buffer)); |
| 107 | } |
| 108 | |
| 109 | public void testOverflow() { |
| 110 | byte [] buffer = new byte[8-1]; // a lot of the numbers have 8-byte buffers (nul term) |
| 111 | TarUtils.formatUnsignedOctalString(07777777L, buffer, 0, buffer.length); |
| 112 | assertEquals("7777777", new String(buffer)); |
| 113 | try { |
| 114 | TarUtils.formatUnsignedOctalString(017777777L, buffer, 0, buffer.length); |
| 115 | fail("Should have cause IllegalArgumentException"); |
| 116 | } catch (IllegalArgumentException expected) { |
| 117 | } |
Sebastian Bazley | 91401bf | 2009-04-01 00:00:49 +0000 | [diff] [blame] | 118 | } |
Sebastian Bazley | 161511f | 2009-03-31 23:49:45 +0000 | [diff] [blame] | 119 | } |