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.lfs.lib;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotEquals;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51
52 import java.io.ByteArrayOutputStream;
53 import java.io.IOException;
54 import java.io.OutputStreamWriter;
55 import java.nio.ByteBuffer;
56 import java.nio.charset.Charset;
57 import java.nio.charset.StandardCharsets;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.util.Locale;
61
62 import org.eclipse.jgit.junit.JGitTestUtil;
63 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
64 import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
65 import org.eclipse.jgit.lib.Constants;
66 import org.eclipse.jgit.util.FileUtils;
67 import org.junit.AfterClass;
68 import org.junit.BeforeClass;
69 import org.junit.Test;
70
71
72
73
74 public class LongObjectIdTest {
75 private static Path tmp;
76
77 @BeforeClass
78 public static void setup() throws IOException {
79 tmp = Files.createTempDirectory("jgit_test_");
80 }
81
82 @AfterClass
83 public static void tearDown() throws IOException {
84 FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
85 }
86
87 @Test
88 public void test001_toString() {
89 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
90 final LongObjectId oid = LongObjectId.fromString(x);
91 assertEquals(x, oid.name());
92 }
93
94 @Test
95 public void test002_toString() {
96 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
97 final LongObjectId oid = LongObjectId.fromString(x);
98 assertEquals(x, oid.name());
99 }
100
101 @Test
102 public void test003_equals() {
103 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
104 final LongObjectId a = LongObjectId.fromString(x);
105 final LongObjectId b = LongObjectId.fromString(x);
106 assertEquals(a.hashCode(), b.hashCode());
107 assertEquals("a and b should be equal", b, a);
108 }
109
110 @Test
111 public void test004_isId() {
112 assertTrue("valid id", LongObjectId.isId(
113 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
114 }
115
116 @Test
117 public void test005_notIsId() {
118 assertFalse("bob is not an id", LongObjectId.isId("bob"));
119 }
120
121 @Test
122 public void test006_notIsId() {
123 assertFalse("63 digits is not an id", LongObjectId.isId(
124 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0"));
125 }
126
127 @Test
128 public void test007_isId() {
129 assertTrue("uppercase is accepted", LongObjectId.isId(
130 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2dEb7ab0A"));
131 }
132
133 @Test
134 public void test008_notIsId() {
135 assertFalse("g is not a valid hex digit", LongObjectId.isId(
136 "g367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
137 }
138
139 @Test
140 public void test009_toString() {
141 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
142 final LongObjectId oid = LongObjectId.fromString(x);
143 assertEquals(x, LongObjectId.toString(oid));
144 }
145
146 @Test
147 public void test010_toString() {
148 final String x = "0000000000000000000000000000000000000000000000000000000000000000";
149 assertEquals(x, LongObjectId.toString(null));
150 }
151
152 @Test
153 public void test011_toString() {
154 final String x = "0123456789ABCDEFabcdef01234567890123456789ABCDEFabcdef0123456789";
155 final LongObjectId oid = LongObjectId.fromString(x);
156 assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
157 }
158
159 @Test
160 public void testGetByte() {
161 byte[] raw = new byte[32];
162 for (int i = 0; i < 32; i++)
163 raw[i] = (byte) (0xa0 + i);
164 LongObjectId id = LongObjectId.fromRaw(raw);
165
166 assertEquals(raw[0] & 0xff, id.getFirstByte());
167 assertEquals(raw[0] & 0xff, id.getByte(0));
168 assertEquals(raw[1] & 0xff, id.getByte(1));
169 assertEquals(raw[1] & 0xff, id.getSecondByte());
170
171 for (int i = 2; i < 32; i++) {
172 assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
173 }
174 try {
175 id.getByte(32);
176 fail("LongObjectId has 32 byte only");
177 } catch (ArrayIndexOutOfBoundsException e) {
178
179 }
180 }
181
182 @Test
183 public void testSetByte() {
184 byte[] exp = new byte[32];
185 for (int i = 0; i < 32; i++) {
186 exp[i] = (byte) (0xa0 + i);
187 }
188
189 MutableLongObjectId id = new MutableLongObjectId();
190 id.fromRaw(exp);
191 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
192
193 id.setByte(0, 0x10);
194 assertEquals(0x10, id.getByte(0));
195 exp[0] = 0x10;
196 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
197
198 for (int p = 1; p < 32; p++) {
199 id.setByte(p, 0x10 + p);
200 assertEquals(0x10 + p, id.getByte(p));
201 exp[p] = (byte) (0x10 + p);
202 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
203 }
204
205 for (int p = 0; p < 32; p++) {
206 id.setByte(p, 0x80 + p);
207 assertEquals(0x80 + p, id.getByte(p));
208 exp[p] = (byte) (0x80 + p);
209 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
210 }
211 }
212
213 @Test
214 public void testZeroId() {
215 AnyLongObjectId zero = new LongObjectId(0L, 0L, 0L, 0L);
216 assertEquals(zero, LongObjectId.zeroId());
217 assertEquals(
218 "0000000000000000000000000000000000000000000000000000000000000000",
219 LongObjectId.zeroId().name());
220 }
221
222 @Test
223 public void testEquals() {
224 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
225 assertTrue("id should equal itself", id1.equals(id1));
226 AnyLongObjectId id2 = new LongObjectId(id1);
227 assertEquals("objects should be equals", id1, id2);
228
229 id2 = LongObjectIdTestUtils.hash("other");
230 assertNotEquals("objects should be not equal", id1, id2);
231 }
232
233 @Test
234 public void testCopyRawBytes() {
235 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
236 AnyLongObjectId id2 = new LongObjectId(id1);
237
238 byte[] buf = new byte[64];
239 id1.copyRawTo(buf, 0);
240 id2.copyRawTo(buf, 32);
241 assertTrue("objects should be equals",
242 LongObjectId.equals(buf, 0, buf, 32));
243 }
244
245 @Test
246 public void testCopyRawLongs() {
247 long[] a = new long[4];
248 a[0] = 1L;
249 a[1] = 2L;
250 a[2] = 3L;
251 a[3] = 4L;
252 AnyLongObjectId id1 = new LongObjectId(a[0], a[1], a[2], a[3]);
253 AnyLongObjectId id2 = LongObjectId.fromRaw(a);
254 assertEquals("objects should be equals", id1, id2);
255 }
256
257 @Test
258 public void testCopyFromStringInvalid() {
259 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
260 try {
261 LongObjectId.fromString(id1.name() + "01234");
262 fail("expected InvalidLongObjectIdException");
263 } catch (InvalidLongObjectIdException e) {
264 assertEquals("Invalid id: " + id1.name() + "01234",
265 e.getMessage());
266 }
267 }
268
269 @Test
270 public void testCopyFromStringByte() {
271 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
272 byte[] buf = new byte[64];
273 Charset cs = StandardCharsets.US_ASCII;
274 cs.encode(id1.name()).get(buf);
275 AnyLongObjectId id2 = LongObjectId.fromString(buf, 0);
276 assertEquals("objects should be equals", id1, id2);
277 }
278
279 @Test
280 public void testHashFile() throws IOException {
281 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
282 Path f = tmp.resolve("test");
283 JGitTestUtil.write(f.toFile(), "test");
284 AnyLongObjectId id2 = LongObjectIdTestUtils.hash(f);
285 assertEquals("objects should be equals", id1, id2);
286 }
287
288 @Test
289 public void testCompareTo() {
290 AnyLongObjectId id1 = LongObjectId.fromString(
291 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
292 assertEquals(0, id1.compareTo(LongObjectId.fromString(
293 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
294
295 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
296 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
297 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
298 "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef")));
299 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
300 "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef")));
301 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
302 "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef")));
303
304 assertEquals(1, id1.compareTo(LongObjectId.fromString(
305 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
306 assertEquals(1, id1.compareTo(LongObjectId.fromString(
307 "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef")));
308 assertEquals(1, id1.compareTo(LongObjectId.fromString(
309 "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef")));
310 assertEquals(1, id1.compareTo(LongObjectId.fromString(
311 "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef")));
312 }
313
314 @Test
315 public void testCompareToByte() {
316 AnyLongObjectId id1 = LongObjectId.fromString(
317 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
318 byte[] buf = new byte[32];
319 id1.copyRawTo(buf, 0);
320 assertEquals(0, id1.compareTo(buf, 0));
321
322 LongObjectId
323 .fromString(
324 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
325 .copyRawTo(buf, 0);
326 assertEquals(-1, id1.compareTo(buf, 0));
327
328 LongObjectId
329 .fromString(
330 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
331 .copyRawTo(buf, 0);
332 assertEquals(1, id1.compareTo(buf, 0));
333 }
334
335 @Test
336 public void testCompareToLong() {
337 AnyLongObjectId id1 = new LongObjectId(1L, 2L, 3L, 4L);
338 long[] buf = new long[4];
339 id1.copyRawTo(buf, 0);
340 assertEquals(0, id1.compareTo(buf, 0));
341
342 new LongObjectId(2L, 2L, 3L, 4L).copyRawTo(buf, 0);
343 assertEquals(-1, id1.compareTo(buf, 0));
344
345 new LongObjectId(0L, 2L, 3L, 4L).copyRawTo(buf, 0);
346 assertEquals(1, id1.compareTo(buf, 0));
347 }
348
349 @Test
350 public void testCopyToByte() {
351 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
352 byte[] buf = new byte[64];
353 id1.copyTo(buf, 0);
354 assertEquals(id1, LongObjectId.fromString(buf, 0));
355 }
356
357 @Test
358 public void testCopyRawToByteBuffer() {
359 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
360 ByteBuffer buf = ByteBuffer.allocate(32);
361 id1.copyRawTo(buf);
362 assertEquals(id1, LongObjectId.fromRaw(buf.array(), 0));
363 }
364
365 @Test
366 public void testCopyToByteBuffer() {
367 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
368 ByteBuffer buf = ByteBuffer.allocate(64);
369 id1.copyTo(buf);
370 assertEquals(id1, LongObjectId.fromString(buf.array(), 0));
371 }
372
373 @Test
374 public void testCopyRawToOutputStream() throws IOException {
375 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
376 ByteArrayOutputStream os = new ByteArrayOutputStream(32);
377 id1.copyRawTo(os);
378 assertEquals(id1, LongObjectId.fromRaw(os.toByteArray(), 0));
379 }
380
381 @Test
382 public void testCopyToOutputStream() throws IOException {
383 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
384 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
385 id1.copyTo(os);
386 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
387 }
388
389 @Test
390 public void testCopyToWriter() throws IOException {
391 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
392 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
393 OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
394 id1.copyTo(w);
395 w.close();
396 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
397 }
398
399 @Test
400 public void testCopyToWriterWithBuf() throws IOException {
401 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
402 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
403 OutputStreamWriter w = new OutputStreamWriter(os, Constants.CHARSET);
404 char[] buf = new char[64];
405 id1.copyTo(buf, w);
406 w.close();
407 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
408 }
409
410 @Test
411 public void testCopyToStringBuilder() {
412 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
413 StringBuilder sb = new StringBuilder();
414 char[] buf = new char[64];
415 id1.copyTo(buf, sb);
416 assertEquals(id1, LongObjectId.fromString(sb.toString()));
417 }
418
419 @Test
420 public void testCopy() {
421 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
422 assertEquals(id1.copy(), id1);
423 MutableLongObjectId id2 = new MutableLongObjectId();
424 id2.fromObjectId(id1);
425 assertEquals(id1, id2.copy());
426 }
427 }