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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/*
* Copyright (C) 2008-2009, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.treewalk;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
import static org.eclipse.jgit.lib.FileMode.SYMLINK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.TreeFormatter;
import org.eclipse.jgit.util.RawParseUtils;
import org.junit.Before;
import org.junit.Test;
public class CanonicalTreeParserTest {
private final CanonicalTreeParser ctp = new CanonicalTreeParser();
private final FileMode m644 = FileMode.REGULAR_FILE;
private final FileMode mt = FileMode.TREE;
private final ObjectId hash_a = ObjectId
.fromString("6b9c715d21d5486e59083fb6071566aa6ecd4d42");
private final ObjectId hash_foo = ObjectId
.fromString("a213e8e25bb2442326e86cbfb9ef56319f482869");
private final ObjectId hash_sometree = ObjectId
.fromString("daf4bdb0d7bb24319810fe0e73aa317663448c93");
private byte[] tree1;
private byte[] tree2;
private byte[] tree3;
@Before
public void setUp() throws Exception {
tree1 = mktree(entry(m644, "a", hash_a));
tree2 = mktree(entry(m644, "a", hash_a), entry(m644, "foo", hash_foo));
tree3 = mktree(entry(m644, "a", hash_a), entry(mt, "b_sometree",
hash_sometree), entry(m644, "foo", hash_foo));
}
private static byte[] mktree(byte[]... data) throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
for (byte[] e : data)
out.write(e);
return out.toByteArray();
}
private static byte[] entry(final FileMode mode, final String name,
final ObjectId id) throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
mode.copyTo(out);
out.write(' ');
out.write(Constants.encode(name));
out.write(0);
id.copyRawTo(out);
return out.toByteArray();
}
private String path() {
return RawParseUtils.decode(UTF_8, ctp.path,
ctp.pathOffset, ctp.pathLen);
}
@Test
public void testEmptyTree_AtEOF() throws Exception {
ctp.reset(new byte[0]);
assertTrue(ctp.eof());
}
@Test
public void testOneEntry_Forward() throws Exception {
ctp.reset(tree1);
assertTrue(ctp.first());
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
ctp.next(1);
assertFalse(ctp.first());
assertTrue(ctp.eof());
}
@Test
public void testTwoEntries_ForwardOneAtATime() throws Exception {
ctp.reset(tree2);
assertTrue(ctp.first());
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
ctp.next(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("foo", path());
assertEquals(hash_foo, ctp.getEntryObjectId());
ctp.next(1);
assertFalse(ctp.first());
assertTrue(ctp.eof());
}
@Test
public void testOneEntry_Seek1IsEOF() throws Exception {
ctp.reset(tree1);
ctp.next(1);
assertTrue(ctp.eof());
}
@Test
public void testTwoEntries_Seek2IsEOF() throws Exception {
ctp.reset(tree2);
ctp.next(2);
assertTrue(ctp.eof());
}
@Test
public void testThreeEntries_Seek3IsEOF() throws Exception {
ctp.reset(tree3);
ctp.next(3);
assertTrue(ctp.eof());
}
@Test
public void testThreeEntries_Seek2() throws Exception {
ctp.reset(tree3);
ctp.next(2);
assertFalse(ctp.eof());
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("foo", path());
assertEquals(hash_foo, ctp.getEntryObjectId());
ctp.next(1);
assertTrue(ctp.eof());
}
@Test
public void testOneEntry_Backwards() throws Exception {
ctp.reset(tree1);
ctp.next(1);
assertFalse(ctp.first());
assertTrue(ctp.eof());
ctp.back(1);
assertTrue(ctp.first());
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
}
@Test
public void testTwoEntries_BackwardsOneAtATime() throws Exception {
ctp.reset(tree2);
ctp.next(2);
assertTrue(ctp.eof());
ctp.back(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("foo", path());
assertEquals(hash_foo, ctp.getEntryObjectId());
ctp.back(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
}
@Test
public void testTwoEntries_BackwardsTwo() throws Exception {
ctp.reset(tree2);
ctp.next(2);
assertTrue(ctp.eof());
ctp.back(2);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
ctp.next(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("foo", path());
assertEquals(hash_foo, ctp.getEntryObjectId());
ctp.next(1);
assertTrue(ctp.eof());
}
@Test
public void testThreeEntries_BackwardsTwo() throws Exception {
ctp.reset(tree3);
ctp.next(3);
assertTrue(ctp.eof());
ctp.back(2);
assertFalse(ctp.eof());
assertEquals(mt.getBits(), ctp.mode);
assertEquals("b_sometree", path());
assertEquals(hash_sometree, ctp.getEntryObjectId());
ctp.next(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("foo", path());
assertEquals(hash_foo, ctp.getEntryObjectId());
ctp.next(1);
assertTrue(ctp.eof());
}
@Test
public void testBackwards_ConfusingPathName() throws Exception {
final String aVeryConfusingName = "confusing 644 entry 755 and others";
ctp.reset(mktree(entry(m644, "a", hash_a), entry(mt, aVeryConfusingName,
hash_sometree), entry(m644, "foo", hash_foo)));
ctp.next(3);
assertTrue(ctp.eof());
ctp.back(2);
assertFalse(ctp.eof());
assertEquals(mt.getBits(), ctp.mode);
assertEquals(aVeryConfusingName, path());
assertEquals(hash_sometree, ctp.getEntryObjectId());
ctp.back(1);
assertFalse(ctp.eof());
assertEquals(m644.getBits(), ctp.mode);
assertEquals("a", path());
assertEquals(hash_a, ctp.getEntryObjectId());
}
@Test
public void testBackwords_Prebuilts1() throws Exception {
// What is interesting about this test is the ObjectId for the
// "darwin-x86" path entry ends in an octal digit (37 == '7').
// Thus when scanning backwards we could over scan and consume
// part of the SHA-1, and miss the path terminator.
//
final ObjectId common = ObjectId
.fromString("af7bf97cb9bce3f60f1d651a0ef862e9447dd8bc");
final ObjectId darwinx86 = ObjectId
.fromString("e927f7398240f78face99e1a738dac54ef738e37");
final ObjectId linuxx86 = ObjectId
.fromString("ac08dd97120c7cb7d06e98cd5b152011183baf21");
final ObjectId windows = ObjectId
.fromString("6c4c64c221a022bb973165192cca4812033479df");
ctp.reset(mktree(entry(mt, "common", common), entry(mt, "darwin-x86",
darwinx86), entry(mt, "linux-x86", linuxx86), entry(mt,
"windows", windows)));
ctp.next(3);
assertEquals("windows", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(windows, ctp.getEntryObjectId());
ctp.back(1);
assertEquals("linux-x86", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(linuxx86, ctp.getEntryObjectId());
ctp.next(1);
assertEquals("windows", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(windows, ctp.getEntryObjectId());
}
@Test
public void testBackwords_Prebuilts2() throws Exception {
// What is interesting about this test is the ObjectId for the
// "darwin-x86" path entry ends in an octal digit (37 == '7').
// Thus when scanning backwards we could over scan and consume
// part of the SHA-1, and miss the path terminator.
//
final ObjectId common = ObjectId
.fromString("af7bf97cb9bce3f60f1d651a0ef862e9447dd8bc");
final ObjectId darwinx86 = ObjectId
.fromString("0000000000000000000000000000000000000037");
final ObjectId linuxx86 = ObjectId
.fromString("ac08dd97120c7cb7d06e98cd5b152011183baf21");
final ObjectId windows = ObjectId
.fromString("6c4c64c221a022bb973165192cca4812033479df");
ctp.reset(mktree(entry(mt, "common", common), entry(mt, "darwin-x86",
darwinx86), entry(mt, "linux-x86", linuxx86), entry(mt,
"windows", windows)));
ctp.next(3);
assertEquals("windows", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(windows, ctp.getEntryObjectId());
ctp.back(1);
assertEquals("linux-x86", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(linuxx86, ctp.getEntryObjectId());
ctp.next(1);
assertEquals("windows", ctp.getEntryPathString());
assertSame(mt, ctp.getEntryFileMode());
assertEquals(windows, ctp.getEntryObjectId());
}
@Test
public void testFreakingHugePathName() throws Exception {
final int n = AbstractTreeIterator.DEFAULT_PATH_SIZE * 4;
final StringBuilder b = new StringBuilder(n);
for (int i = 0; i < n; i++)
b.append('q');
final String name = b.toString();
ctp.reset(entry(m644, name, hash_a));
assertFalse(ctp.eof());
assertEquals(name, RawParseUtils.decode(UTF_8, ctp.path,
ctp.pathOffset, ctp.pathLen));
}
@Test
public void testFindAttributesWhenFirst() throws CorruptObjectException {
TreeFormatter tree = new TreeFormatter();
tree.append(".gitattributes", REGULAR_FILE, hash_a);
ctp.reset(tree.toByteArray());
assertTrue(ctp.findFile(".gitattributes"));
assertEquals(REGULAR_FILE.getBits(), ctp.getEntryRawMode());
assertEquals(".gitattributes", ctp.getEntryPathString());
assertEquals(hash_a, ctp.getEntryObjectId());
}
@Test
public void testFindAttributesWhenSecond() throws CorruptObjectException {
TreeFormatter tree = new TreeFormatter();
tree.append(".config", SYMLINK, hash_a);
tree.append(".gitattributes", REGULAR_FILE, hash_foo);
ctp.reset(tree.toByteArray());
assertTrue(ctp.findFile(".gitattributes"));
assertEquals(REGULAR_FILE.getBits(), ctp.getEntryRawMode());
assertEquals(".gitattributes", ctp.getEntryPathString());
assertEquals(hash_foo, ctp.getEntryObjectId());
}
@Test
public void testFindAttributesWhenMissing() throws CorruptObjectException {
TreeFormatter tree = new TreeFormatter();
tree.append("src", REGULAR_FILE, hash_a);
tree.append("zoo", REGULAR_FILE, hash_foo);
ctp.reset(tree.toByteArray());
assertFalse(ctp.findFile(".gitattributes"));
assertEquals(11, ctp.idOffset()); // Did not walk the entire tree.
assertEquals("src", ctp.getEntryPathString());
}
}
|