1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
117
118
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
198
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
213
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 }