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