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