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

DeltaIndexTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.pack;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.IOException;
  50. import org.eclipse.jgit.junit.JGitTestUtil;
  51. import org.eclipse.jgit.junit.TestRng;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.junit.Before;
  54. import org.junit.Test;
  55. public class DeltaIndexTest {
  56. private TestRng rng;
  57. private ByteArrayOutputStream actDeltaBuf;
  58. private ByteArrayOutputStream expDeltaBuf;
  59. private DeltaEncoder expDeltaEnc;
  60. private byte[] src;
  61. private byte[] dst;
  62. private ByteArrayOutputStream dstBuf;
  63. private TestRng getRng() {
  64. if (rng == null)
  65. rng = new TestRng(JGitTestUtil.getName());
  66. return rng;
  67. }
  68. @Before
  69. public void setUp() throws Exception {
  70. actDeltaBuf = new ByteArrayOutputStream();
  71. expDeltaBuf = new ByteArrayOutputStream();
  72. expDeltaEnc = new DeltaEncoder(expDeltaBuf, 0, 0);
  73. dstBuf = new ByteArrayOutputStream();
  74. }
  75. @Test
  76. public void testInsertWholeObject_Length12() throws IOException {
  77. src = getRng().nextBytes(12);
  78. insert(src);
  79. doTest();
  80. }
  81. @Test
  82. public void testCopyWholeObject_Length128() throws IOException {
  83. src = getRng().nextBytes(128);
  84. copy(0, 128);
  85. doTest();
  86. }
  87. @Test
  88. public void testCopyWholeObject_Length123() throws IOException {
  89. src = getRng().nextBytes(123);
  90. copy(0, 123);
  91. doTest();
  92. }
  93. @Test
  94. public void testCopyZeros_Length128() throws IOException {
  95. src = new byte[2048];
  96. copy(0, src.length);
  97. doTest();
  98. // The index should be smaller than expected due to the chain
  99. // being truncated. Without truncation we would expect to have
  100. // more than 3584 bytes used.
  101. //
  102. assertEquals(2636, new DeltaIndex(src).getIndexSize());
  103. }
  104. @Test
  105. public void testShuffleSegments() throws IOException {
  106. src = getRng().nextBytes(128);
  107. copy(64, 64);
  108. copy(0, 64);
  109. doTest();
  110. }
  111. @Test
  112. public void testInsertHeadMiddle() throws IOException {
  113. src = getRng().nextBytes(1024);
  114. insert("foo");
  115. copy(0, 512);
  116. insert("yet more fooery");
  117. copy(0, 512);
  118. doTest();
  119. }
  120. @Test
  121. public void testInsertTail() throws IOException {
  122. src = getRng().nextBytes(1024);
  123. copy(0, 512);
  124. insert("bar");
  125. doTest();
  126. }
  127. @Test
  128. public void testIndexSize() {
  129. src = getRng().nextBytes(1024);
  130. DeltaIndex di = new DeltaIndex(src);
  131. assertEquals(1860, di.getIndexSize());
  132. assertEquals("DeltaIndex[2 KiB]", di.toString());
  133. }
  134. @Test
  135. public void testLimitObjectSize_Length12InsertFails() throws IOException {
  136. src = getRng().nextBytes(12);
  137. dst = src;
  138. DeltaIndex di = new DeltaIndex(src);
  139. assertFalse(di.encode(actDeltaBuf, dst, src.length));
  140. }
  141. @Test
  142. public void testLimitObjectSize_Length130InsertFails() throws IOException {
  143. src = getRng().nextBytes(130);
  144. dst = getRng().nextBytes(130);
  145. DeltaIndex di = new DeltaIndex(src);
  146. assertFalse(di.encode(actDeltaBuf, dst, src.length));
  147. }
  148. @Test
  149. public void testLimitObjectSize_Length130CopyOk() throws IOException {
  150. src = getRng().nextBytes(130);
  151. copy(0, 130);
  152. dst = dstBuf.toByteArray();
  153. DeltaIndex di = new DeltaIndex(src);
  154. assertTrue(di.encode(actDeltaBuf, dst, dst.length));
  155. byte[] actDelta = actDeltaBuf.toByteArray();
  156. byte[] expDelta = expDeltaBuf.toByteArray();
  157. assertEquals(BinaryDelta.format(expDelta, false), //
  158. BinaryDelta.format(actDelta, false));
  159. }
  160. @Test
  161. public void testLimitObjectSize_Length130CopyFails() throws IOException {
  162. src = getRng().nextBytes(130);
  163. copy(0, 130);
  164. dst = dstBuf.toByteArray();
  165. // The header requires 4 bytes for these objects, so a target length
  166. // of 5 is bigger than the copy instruction and should cause an abort.
  167. //
  168. DeltaIndex di = new DeltaIndex(src);
  169. assertFalse(di.encode(actDeltaBuf, dst, 5));
  170. assertEquals(4, actDeltaBuf.size());
  171. }
  172. @Test
  173. public void testLimitObjectSize_InsertFrontFails() throws IOException {
  174. src = getRng().nextBytes(130);
  175. insert("eight");
  176. copy(0, 130);
  177. dst = dstBuf.toByteArray();
  178. // The header requires 4 bytes for these objects, so a target length
  179. // of 5 is bigger than the copy instruction and should cause an abort.
  180. //
  181. DeltaIndex di = new DeltaIndex(src);
  182. assertFalse(di.encode(actDeltaBuf, dst, 5));
  183. assertEquals(4, actDeltaBuf.size());
  184. }
  185. private void copy(int offset, int len) throws IOException {
  186. dstBuf.write(src, offset, len);
  187. expDeltaEnc.copy(offset, len);
  188. }
  189. private void insert(String text) throws IOException {
  190. insert(Constants.encode(text));
  191. }
  192. private void insert(byte[] text) throws IOException {
  193. dstBuf.write(text);
  194. expDeltaEnc.insert(text);
  195. }
  196. private void doTest() throws IOException {
  197. dst = dstBuf.toByteArray();
  198. DeltaIndex di = new DeltaIndex(src);
  199. di.encode(actDeltaBuf, dst);
  200. byte[] actDelta = actDeltaBuf.toByteArray();
  201. byte[] expDelta = expDeltaBuf.toByteArray();
  202. assertEquals(BinaryDelta.format(expDelta, false), //
  203. BinaryDelta.format(actDelta, false));
  204. assertTrue("delta is not empty", actDelta.length > 0);
  205. assertEquals(src.length, BinaryDelta.getBaseSize(actDelta));
  206. assertEquals(dst.length, BinaryDelta.getResultSize(actDelta));
  207. assertArrayEquals(dst, BinaryDelta.apply(src, actDelta));
  208. }
  209. }