View Javadoc
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  
44  package org.eclipse.jgit.internal.storage.pack;
45  
46  import static org.junit.Assert.assertArrayEquals;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertTrue;
50  
51  import java.io.ByteArrayOutputStream;
52  import java.io.IOException;
53  
54  import org.eclipse.jgit.junit.JGitTestUtil;
55  import org.eclipse.jgit.junit.TestRng;
56  import org.eclipse.jgit.lib.Constants;
57  import org.junit.Before;
58  import org.junit.Test;
59  
60  public class DeltaIndexTest {
61  	private TestRng rng;
62  
63  	private ByteArrayOutputStream actDeltaBuf;
64  
65  	private ByteArrayOutputStream expDeltaBuf;
66  
67  	private DeltaEncoder expDeltaEnc;
68  
69  	private byte[] src;
70  
71  	private byte[] dst;
72  
73  	private ByteArrayOutputStream dstBuf;
74  
75  	private TestRng getRng() {
76  		if (rng == null)
77  			rng = new TestRng(JGitTestUtil.getName());
78  		return rng;
79  	}
80  
81  	@Before
82  	public void setUp() throws Exception {
83  		actDeltaBuf = new ByteArrayOutputStream();
84  		expDeltaBuf = new ByteArrayOutputStream();
85  		expDeltaEnc = new DeltaEncoder(expDeltaBuf, 0, 0);
86  		dstBuf = new ByteArrayOutputStream();
87  	}
88  
89  	@Test
90  	public void testInsertWholeObject_Length12() throws IOException {
91  		src = getRng().nextBytes(12);
92  		insert(src);
93  		doTest();
94  	}
95  
96  	@Test
97  	public void testCopyWholeObject_Length128() throws IOException {
98  		src = getRng().nextBytes(128);
99  		copy(0, 128);
100 		doTest();
101 	}
102 
103 	@Test
104 	public void testCopyWholeObject_Length123() throws IOException {
105 		src = getRng().nextBytes(123);
106 		copy(0, 123);
107 		doTest();
108 	}
109 
110 	@Test
111 	public void testCopyZeros_Length128() throws IOException {
112 		src = new byte[2048];
113 		copy(0, src.length);
114 		doTest();
115 
116 		// The index should be smaller than expected due to the chain
117 		// being truncated. Without truncation we would expect to have
118 		// more than 3584 bytes used.
119 		//
120 		assertEquals(2636, new DeltaIndex(src).getIndexSize());
121 	}
122 
123 	@Test
124 	public void testShuffleSegments() throws IOException {
125 		src = getRng().nextBytes(128);
126 		copy(64, 64);
127 		copy(0, 64);
128 		doTest();
129 	}
130 
131 	@Test
132 	public void testInsertHeadMiddle() throws IOException {
133 		src = getRng().nextBytes(1024);
134 		insert("foo");
135 		copy(0, 512);
136 		insert("yet more fooery");
137 		copy(0, 512);
138 		doTest();
139 	}
140 
141 	@Test
142 	public void testInsertTail() throws IOException {
143 		src = getRng().nextBytes(1024);
144 		copy(0, 512);
145 		insert("bar");
146 		doTest();
147 	}
148 
149 	@Test
150 	public void testIndexSize() {
151 		src = getRng().nextBytes(1024);
152 		DeltaIndex di = new DeltaIndex(src);
153 		assertEquals(1860, di.getIndexSize());
154 		assertEquals("DeltaIndex[2 KiB]", di.toString());
155 	}
156 
157 	@Test
158 	public void testLimitObjectSize_Length12InsertFails() throws IOException {
159 		src = getRng().nextBytes(12);
160 		dst = src;
161 
162 		DeltaIndex di = new DeltaIndex(src);
163 		assertFalse(di.encode(actDeltaBuf, dst, src.length));
164 	}
165 
166 	@Test
167 	public void testLimitObjectSize_Length130InsertFails() throws IOException {
168 		src = getRng().nextBytes(130);
169 		dst = getRng().nextBytes(130);
170 
171 		DeltaIndex di = new DeltaIndex(src);
172 		assertFalse(di.encode(actDeltaBuf, dst, src.length));
173 	}
174 
175 	@Test
176 	public void testLimitObjectSize_Length130CopyOk() throws IOException {
177 		src = getRng().nextBytes(130);
178 		copy(0, 130);
179 		dst = dstBuf.toByteArray();
180 
181 		DeltaIndex di = new DeltaIndex(src);
182 		assertTrue(di.encode(actDeltaBuf, dst, dst.length));
183 
184 		byte[] actDelta = actDeltaBuf.toByteArray();
185 		byte[] expDelta = expDeltaBuf.toByteArray();
186 
187 		assertEquals(BinaryDelta.format(expDelta, false), //
188 				BinaryDelta.format(actDelta, false));
189 	}
190 
191 	@Test
192 	public void testLimitObjectSize_Length130CopyFails() throws IOException {
193 		src = getRng().nextBytes(130);
194 		copy(0, 130);
195 		dst = dstBuf.toByteArray();
196 
197 		// The header requires 4 bytes for these objects, so a target length
198 		// of 5 is bigger than the copy instruction and should cause an abort.
199 		//
200 		DeltaIndex di = new DeltaIndex(src);
201 		assertFalse(di.encode(actDeltaBuf, dst, 5));
202 		assertEquals(4, actDeltaBuf.size());
203 	}
204 
205 	@Test
206 	public void testLimitObjectSize_InsertFrontFails() throws IOException {
207 		src = getRng().nextBytes(130);
208 		insert("eight");
209 		copy(0, 130);
210 		dst = dstBuf.toByteArray();
211 
212 		// The header requires 4 bytes for these objects, so a target length
213 		// of 5 is bigger than the copy instruction and should cause an abort.
214 		//
215 		DeltaIndex di = new DeltaIndex(src);
216 		assertFalse(di.encode(actDeltaBuf, dst, 5));
217 		assertEquals(4, actDeltaBuf.size());
218 	}
219 
220 	private void copy(int offset, int len) throws IOException {
221 		dstBuf.write(src, offset, len);
222 		expDeltaEnc.copy(offset, len);
223 	}
224 
225 	private void insert(String text) throws IOException {
226 		insert(Constants.encode(text));
227 	}
228 
229 	private void insert(byte[] text) throws IOException {
230 		dstBuf.write(text);
231 		expDeltaEnc.insert(text);
232 	}
233 
234 	private void doTest() throws IOException {
235 		dst = dstBuf.toByteArray();
236 
237 		DeltaIndex di = new DeltaIndex(src);
238 		di.encode(actDeltaBuf, dst);
239 
240 		byte[] actDelta = actDeltaBuf.toByteArray();
241 		byte[] expDelta = expDeltaBuf.toByteArray();
242 
243 		assertEquals(BinaryDelta.format(expDelta, false), //
244 				BinaryDelta.format(actDelta, false));
245 
246 		assertTrue("delta is not empty", actDelta.length > 0);
247 		assertEquals(src.length, BinaryDelta.getBaseSize(actDelta));
248 		assertEquals(dst.length, BinaryDelta.getResultSize(actDelta));
249 		assertArrayEquals(dst, BinaryDelta.apply(src, actDelta));
250 	}
251 }