KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > zip > AsiExtraFieldTest


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.zip;
19
20 import junit.framework.TestCase;
21
22 /**
23  * JUnit 3 testcases for org.apache.tools.zip.AsiExtraField.
24  *
25  */

26 public class AsiExtraFieldTest extends TestCase implements UnixStat {
27     public AsiExtraFieldTest(String JavaDoc name) {
28         super(name);
29     }
30
31     /**
32      * Test file mode magic.
33      */

34     public void testModes() {
35         AsiExtraField a = new AsiExtraField();
36         a.setMode(0123);
37         assertEquals("plain file", 0100123, a.getMode());
38         a.setDirectory(true);
39         assertEquals("directory", 040123, a.getMode());
40         a.setLinkedFile("test");
41         assertEquals("symbolic link", 0120123, a.getMode());
42     }
43
44     /**
45      * Test content.
46      */

47     public void testContent() {
48         AsiExtraField a = new AsiExtraField();
49         a.setMode(0123);
50         a.setUserId(5);
51         a.setGroupId(6);
52         byte[] b = a.getLocalFileDataData();
53
54         // CRC manually calculated, sorry
55
byte[] expect = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC
56
0123, (byte)0x80, // mode
57
0, 0, 0, 0, // link length
58
5, 0, 6, 0}; // uid, gid
59
assertEquals("no link", expect.length, b.length);
60         for (int i=0; i<expect.length; i++) {
61             assertEquals("no link, byte "+i, expect[i], b[i]);
62         }
63
64         a.setLinkedFile("test");
65         expect = new byte[] {0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC
66
0123, (byte)0xA0, // mode
67
4, 0, 0, 0, // link length
68
5, 0, 6, 0, // uid, gid
69
(byte)'t', (byte)'e', (byte)'s', (byte)'t'};
70         b = a.getLocalFileDataData();
71         assertEquals("no link", expect.length, b.length);
72         for (int i=0; i<expect.length; i++) {
73             assertEquals("no link, byte "+i, expect[i], b[i]);
74         }
75
76     }
77
78     /**
79      * Test reparse
80      */

81     public void testReparse() throws Exception JavaDoc {
82         // CRC manually calculated, sorry
83
byte[] data = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC
84
0123, (byte)0x80, // mode
85
0, 0, 0, 0, // link length
86
5, 0, 6, 0}; // uid, gid
87
AsiExtraField a = new AsiExtraField();
88         a.parseFromLocalFileData(data, 0, data.length);
89         assertEquals("length plain file", data.length,
90                      a.getLocalFileDataLength().getValue());
91         assertTrue("plain file, no link", !a.isLink());
92         assertTrue("plain file, no dir", !a.isDirectory());
93         assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode());
94         assertEquals("uid plain file", 5, a.getUserId());
95         assertEquals("gid plain file", 6, a.getGroupId());
96
97         data = new byte[] {0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC
98
0123, (byte)0xA0, // mode
99
4, 0, 0, 0, // link length
100
5, 0, 6, 0, // uid, gid
101
(byte)'t', (byte)'e', (byte)'s', (byte)'t'};
102         a = new AsiExtraField();
103         a.parseFromLocalFileData(data, 0, data.length);
104         assertEquals("length link", data.length,
105                      a.getLocalFileDataLength().getValue());
106         assertTrue("link, is link", a.isLink());
107         assertTrue("link, no dir", !a.isDirectory());
108         assertEquals("mode link", LINK_FLAG | 0123, a.getMode());
109         assertEquals("uid link", 5, a.getUserId());
110         assertEquals("gid link", 6, a.getGroupId());
111         assertEquals("test", a.getLinkedFile());
112
113         data = new byte[] {(byte)0x8E, 0x01, (byte)0xBF, (byte)0x0E, // CRC
114
0123, (byte)0x40, // mode
115
0, 0, 0, 0, // link
116
5, 0, 6, 0}; // uid, gid
117
a = new AsiExtraField();
118         a.parseFromLocalFileData(data, 0, data.length);
119         assertEquals("length dir", data.length,
120                      a.getLocalFileDataLength().getValue());
121         assertTrue("dir, no link", !a.isLink());
122         assertTrue("dir, is dir", a.isDirectory());
123         assertEquals("mode dir", DIR_FLAG | 0123, a.getMode());
124         assertEquals("uid dir", 5, a.getUserId());
125         assertEquals("gid dir", 6, a.getGroupId());
126
127         data = new byte[] {0, 0, 0, 0, // bad CRC
128
0123, (byte)0x40, // mode
129
0, 0, 0, 0, // link
130
5, 0, 6, 0}; // uid, gid
131
a = new AsiExtraField();
132         try {
133             a.parseFromLocalFileData(data, 0, data.length);
134             fail("should raise bad CRC exception");
135         } catch (Exception JavaDoc e) {
136             assertEquals("bad CRC checksum 0 instead of ebf018e",
137                          e.getMessage());
138         }
139     }
140 }
141
Popular Tags