View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotSame;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * JUnit tests for org.apache.commons.compress.archivers.zip.AsiExtraField.
32   */
33  class AsiExtraFieldTest implements UnixStat {
34  
35      @Test
36      void testClone() {
37          final AsiExtraField s1 = new AsiExtraField();
38          s1.setUserId(42);
39          s1.setGroupId(12);
40          s1.setLinkedFile("foo");
41          s1.setMode(0644);
42          s1.setDirectory(true);
43          final AsiExtraField s2 = (AsiExtraField) s1.clone();
44          assertNotSame(s1, s2);
45          assertEquals(s1.getUserId(), s2.getUserId());
46          assertEquals(s1.getGroupId(), s2.getGroupId());
47          assertEquals(s1.getLinkedFile(), s2.getLinkedFile());
48          assertEquals(s1.getMode(), s2.getMode());
49          assertEquals(s1.isDirectory(), s2.isDirectory());
50      }
51  
52      /**
53       * Test content.
54       */
55      @Test
56      void testContent() {
57          final AsiExtraField a = new AsiExtraField();
58          a.setMode(0123);
59          a.setUserId(5);
60          a.setGroupId(6);
61          byte[] b = a.getLocalFileDataData();
62  
63          // CRC manually calculated, sorry
64          byte[] expect = { (byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC
65                  0123, (byte) 0x80, // mode
66                  0, 0, 0, 0, // link length
67                  5, 0, 6, 0 }; // uid, gid
68          assertEquals(expect.length, b.length, "no link");
69          for (int i = 0; i < expect.length; i++) {
70              assertEquals(expect[i], b[i], "no link, byte " + i);
71          }
72  
73          a.setLinkedFile("test");
74          expect = new byte[] { 0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC
75                  0123, (byte) 0xA0, // mode
76                  4, 0, 0, 0, // link length
77                  5, 0, 6, 0, // uid, gid
78                  (byte) 't', (byte) 'e', (byte) 's', (byte) 't' };
79          b = a.getLocalFileDataData();
80          assertEquals(expect.length, b.length, "no link");
81          for (int i = 0; i < expect.length; i++) {
82              assertEquals(expect[i], b[i], "no link, byte " + i);
83          }
84  
85      }
86  
87      /**
88       * Test file mode magic.
89       */
90      @Test
91      void testModes() {
92          final AsiExtraField a = new AsiExtraField();
93          a.setMode(0123);
94          assertEquals(0100123, a.getMode(), "plain file");
95          a.setDirectory(true);
96          assertEquals(040123, a.getMode(), "directory");
97          a.setLinkedFile("test");
98          assertEquals(0120123, a.getMode(), "symbolic link");
99      }
100 
101     /**
102      * Test reparse
103      */
104     @Test
105     void testReparse() throws Exception {
106         // CRC manually calculated, sorry
107         final byte[] data1 = { (byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC
108                 0123, (byte) 0x80, // mode
109                 0, 0, 0, 0, // link length
110                 5, 0, 6, 0 }; // uid, gid
111         final AsiExtraField a1 = new AsiExtraField();
112         a1.parseFromLocalFileData(data1, 0, data1.length);
113         assertEquals(data1.length, a1.getLocalFileDataLength().getValue(), "length plain file");
114         assertFalse(a1.isLink(), "plain file, no link");
115         assertFalse(a1.isDirectory(), "plain file, no dir");
116         assertEquals(FILE_FLAG | 0123, a1.getMode(), "mode plain file");
117         assertEquals(5, a1.getUserId(), "uid plain file");
118         assertEquals(6, a1.getGroupId(), "gid plain file");
119 
120         final byte[] data2 = { 0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC
121                 0123, (byte) 0xA0, // mode
122                 4, 0, 0, 0, // link length
123                 5, 0, 6, 0, // uid, gid
124                 (byte) 't', (byte) 'e', (byte) 's', (byte) 't' };
125         final AsiExtraField a2 = new AsiExtraField();
126         a2.parseFromLocalFileData(data2, 0, data2.length);
127         assertEquals(data2.length, a2.getLocalFileDataLength().getValue(), "length link");
128         assertTrue(a2.isLink(), "link, is link");
129         assertFalse(a2.isDirectory(), "link, no dir");
130         assertEquals(LINK_FLAG | 0123, a2.getMode(), "mode link");
131         assertEquals(5, a2.getUserId(), "uid link");
132         assertEquals(6, a2.getGroupId(), "gid link");
133         assertEquals("test", a2.getLinkedFile());
134 
135         final byte[] data3 = { (byte) 0x8E, 0x01, (byte) 0xBF, (byte) 0x0E, // CRC
136                 0123, (byte) 0x40, // mode
137                 0, 0, 0, 0, // link
138                 5, 0, 6, 0 }; // uid, gid
139         final AsiExtraField a3 = new AsiExtraField();
140         a3.parseFromLocalFileData(data3, 0, data3.length);
141         assertEquals(data3.length, a3.getLocalFileDataLength().getValue(), "length dir");
142         assertFalse(a3.isLink(), "dir, no link");
143         assertTrue(a3.isDirectory(), "dir, is dir");
144         assertEquals(DIR_FLAG | 0123, a3.getMode(), "mode dir");
145         assertEquals(5, a3.getUserId(), "uid dir");
146         assertEquals(6, a3.getGroupId(), "gid dir");
147 
148         final byte[] data4 = { 0, 0, 0, 0, // bad CRC
149                 0123, (byte) 0x40, // mode
150                 0, 0, 0, 0, // link
151                 5, 0, 6, 0 }; // uid, gid
152         final AsiExtraField a4 = new AsiExtraField();
153         final Exception e = assertThrows(Exception.class, () -> a4.parseFromLocalFileData(data4, 0, data4.length), "should raise bad CRC exception");
154         assertEquals("Bad CRC checksum, expected 0 instead of ebf018e", e.getMessage());
155     }
156 }