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.zip;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * JUnit 3 testcases for org.apache.commons.compress.archivers.zip.AsiExtraField.
25   *
26   */
27  public class AsiExtraFieldTest extends TestCase implements UnixStat {
28      public AsiExtraFieldTest(String name) {
29          super(name);
30      }
31  
32      /**
33       * Test file mode magic.
34       */
35      public void testModes() {
36          AsiExtraField a = new AsiExtraField();
37          a.setMode(0123);
38          assertEquals("plain file", 0100123, a.getMode());
39          a.setDirectory(true);
40          assertEquals("directory", 040123, a.getMode());
41          a.setLinkedFile("test");
42          assertEquals("symbolic link", 0120123, a.getMode());
43      }
44  
45      /**
46       * Test content.
47       */
48      public void testContent() {
49          AsiExtraField a = new AsiExtraField();
50          a.setMode(0123);
51          a.setUserId(5);
52          a.setGroupId(6);
53          byte[] b = a.getLocalFileDataData();
54  
55          // CRC manually calculated, sorry
56          byte[] expect = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC
57                           0123, (byte)0x80,                   // mode
58                           0, 0, 0, 0,                         // link length
59                           5, 0, 6, 0};                        // uid, gid
60          assertEquals("no link", expect.length, b.length);
61          for (int i=0; i<expect.length; i++) {
62              assertEquals("no link, byte "+i, expect[i], b[i]);
63          }
64  
65          a.setLinkedFile("test");
66          expect = new byte[] {0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC
67                               0123, (byte)0xA0,                   // mode
68                               4, 0, 0, 0,                         // link length
69                               5, 0, 6, 0,                         // uid, gid
70                               (byte)'t', (byte)'e', (byte)'s', (byte)'t'};
71          b = a.getLocalFileDataData();
72          assertEquals("no link", expect.length, b.length);
73          for (int i=0; i<expect.length; i++) {
74              assertEquals("no link, byte "+i, expect[i], b[i]);
75          }
76  
77      }
78  
79      /**
80       * Test reparse
81       */
82      public void testReparse() throws Exception {
83          // CRC manually calculated, sorry
84          byte[] data = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC
85                         0123, (byte)0x80,                   // mode
86                         0, 0, 0, 0,                         // link length
87                         5, 0, 6, 0};                        // uid, gid
88          AsiExtraField a = new AsiExtraField();
89          a.parseFromLocalFileData(data, 0, data.length);
90          assertEquals("length plain file", data.length,
91                       a.getLocalFileDataLength().getValue());
92          assertTrue("plain file, no link", !a.isLink());
93          assertTrue("plain file, no dir", !a.isDirectory());
94          assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode());
95          assertEquals("uid plain file", 5, a.getUserId());
96          assertEquals("gid plain file", 6, a.getGroupId());
97  
98          data = new byte[] {0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC
99                             0123, (byte)0xA0,                   // mode
100                            4, 0, 0, 0,                         // link length
101                            5, 0, 6, 0,                         // uid, gid
102                            (byte)'t', (byte)'e', (byte)'s', (byte)'t'};
103         a = new AsiExtraField();
104         a.parseFromLocalFileData(data, 0, data.length);
105         assertEquals("length link", data.length,
106                      a.getLocalFileDataLength().getValue());
107         assertTrue("link, is link", a.isLink());
108         assertTrue("link, no dir", !a.isDirectory());
109         assertEquals("mode link", LINK_FLAG | 0123, a.getMode());
110         assertEquals("uid link", 5, a.getUserId());
111         assertEquals("gid link", 6, a.getGroupId());
112         assertEquals("test", a.getLinkedFile());
113 
114         data = new byte[] {(byte)0x8E, 0x01, (byte)0xBF, (byte)0x0E, // CRC
115                            0123, (byte)0x40,                         // mode
116                            0, 0, 0, 0,                               // link
117                            5, 0, 6, 0};                          // uid, gid
118         a = new AsiExtraField();
119         a.parseFromLocalFileData(data, 0, data.length);
120         assertEquals("length dir", data.length,
121                      a.getLocalFileDataLength().getValue());
122         assertTrue("dir, no link", !a.isLink());
123         assertTrue("dir, is dir", a.isDirectory());
124         assertEquals("mode dir", DIR_FLAG | 0123, a.getMode());
125         assertEquals("uid dir", 5, a.getUserId());
126         assertEquals("gid dir", 6, a.getGroupId());
127 
128         data = new byte[] {0, 0, 0, 0,                           // bad CRC
129                            0123, (byte)0x40,                     // mode
130                            0, 0, 0, 0,                           // link
131                            5, 0, 6, 0};                          // uid, gid
132         a = new AsiExtraField();
133         try {
134             a.parseFromLocalFileData(data, 0, data.length);
135             fail("should raise bad CRC exception");
136         } catch (Exception e) {
137             assertEquals("bad CRC checksum 0 instead of ebf018e",
138                          e.getMessage());
139         }
140     }
141 
142     public void testClone() {
143         AsiExtraField s1 = new AsiExtraField();
144         s1.setUserId(42);
145         s1.setGroupId(12);
146         s1.setLinkedFile("foo");
147         s1.setMode(0644);
148         s1.setDirectory(true);
149         AsiExtraField s2 = (AsiExtraField) s1.clone();
150         assertNotSame(s1, s2);
151         assertEquals(s1.getUserId(), s2.getUserId());
152         assertEquals(s1.getGroupId(), s2.getGroupId());
153         assertEquals(s1.getLinkedFile(), s2.getLinkedFile());
154         assertEquals(s1.getMode(), s2.getMode());
155         assertEquals(s1.isDirectory(), s2.isDirectory());
156     }
157 }