You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsiExtraFieldTest.java 6.8 kB

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