aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheEntryTest.java
blob: 01d1e0282dbb7f11ada1dff5641fedfb84b6c435 (plain)
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
/*
 * Copyright (C) 2009, 2023 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.dircache;

import static java.time.Instant.EPOCH;
import static org.junit.Assert.assertArrayEquals;
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 static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

import org.eclipse.jgit.dircache.DirCache.DirCacheVersion;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.util.MutableInteger;
import org.junit.Test;

public class DirCacheEntryTest {
	@Test
	public void testIsValidPath() {
		assertTrue(isValidPath("a"));
		assertTrue(isValidPath("a/b"));
		assertTrue(isValidPath("ab/cd/ef"));

		assertFalse(isValidPath(""));
		assertFalse(isValidPath("/a"));
		assertFalse(isValidPath("a//b"));
		assertFalse(isValidPath("ab/cd//ef"));
		assertFalse(isValidPath("a/"));
		assertFalse(isValidPath("ab/cd/ef/"));
		assertFalse(isValidPath("a\u0000b"));
		assertFalse(isValidPath(".git"));
		assertFalse(isValidPath(".GIT"));
		assertFalse(isValidPath(".Git"));
		assertFalse(isValidPath(".git/b"));
		assertFalse(isValidPath(".GIT/b"));
		assertFalse(isValidPath(".Git/b"));
		assertFalse(isValidPath("x/y/.git/z/b"));
		assertFalse(isValidPath("x/y/.GIT/z/b"));
		assertFalse(isValidPath("x/y/.Git/z/b"));
		assertTrue(isValidPath("git/b"));
	}

	@SuppressWarnings("unused")
	private static boolean isValidPath(String path) {
		try {
			new DirCacheEntry(path);
			return true;
		} catch (InvalidPathException e) {
			return false;
		}
	}

	private static void checkPath(DirCacheVersion indexVersion,
			DirCacheEntry previous, String name) throws IOException {
		DirCacheEntry dce = new DirCacheEntry(name);
		long now = System.currentTimeMillis();
		long anHourAgo = now - TimeUnit.HOURS.toMillis(1);
		dce.setLastModified(Instant.ofEpochMilli(anHourAgo));
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		dce.write(out, indexVersion, previous);
		byte[] raw = out.toByteArray();
		MessageDigest md0 = Constants.newMessageDigest();
		md0.update(raw);
		ByteArrayInputStream in = new ByteArrayInputStream(raw);
		MutableInteger infoAt = new MutableInteger();
		byte[] sharedInfo = new byte[raw.length];
		MessageDigest md = Constants.newMessageDigest();
		DirCacheEntry read = new DirCacheEntry(sharedInfo, infoAt, in, md,
				Instant.ofEpochMilli(now), indexVersion, previous);
		assertEquals("Paths of length " + name.length() + " should match", name,
				read.getPathString());
		assertEquals("Should have been fully read", -1, in.read());
		assertArrayEquals("Digests should match", md0.digest(),
				md.digest());
	}

	@Test
	public void testLongPath() throws Exception {
		StringBuilder name = new StringBuilder(4094 + 16);
		for (int i = 0; i < 4094; i++) {
			name.append('a');
		}
		for (int j = 0; j < 16; j++) {
			checkPath(DirCacheVersion.DIRC_VERSION_EXTENDED, null,
					name.toString());
			name.append('b');
		}
	}

	@Test
	public void testLongPathV4() throws Exception {
		StringBuilder name = new StringBuilder(4094 + 16);
		for (int i = 0; i < 4094; i++) {
			name.append('a');
		}
		DirCacheEntry previous = new DirCacheEntry(name.toString());
		for (int j = 0; j < 16; j++) {
			checkPath(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS, previous,
					name.toString());
			name.append('b');
		}
	}

	@Test
	public void testShortPath() throws Exception {
		StringBuilder name = new StringBuilder(1 + 16);
		name.append('a');
		for (int j = 0; j < 16; j++) {
			checkPath(DirCacheVersion.DIRC_VERSION_EXTENDED, null,
					name.toString());
			name.append('b');
		}
	}

	@Test
	public void testShortPathV4() throws Exception {
		StringBuilder name = new StringBuilder(1 + 16);
		name.append('a');
		DirCacheEntry previous = new DirCacheEntry(name.toString());
		for (int j = 0; j < 16; j++) {
			checkPath(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS, previous,
					name.toString());
			name.append('b');
		}
	}

	@Test
	public void testPathV4() throws Exception {
		StringBuilder name = new StringBuilder();
		for (int i = 0; i < 20; i++) {
			name.append('a');
		}
		DirCacheEntry previous = new DirCacheEntry(name.toString());
		for (int j = 0; j < 20; j++) {
			name.setLength(name.length() - 1);
			String newName = name.toString() + "bbb";
			checkPath(DirCacheVersion.DIRC_VERSION_PATHCOMPRESS, previous,
					newName);
		}
	}

	@SuppressWarnings("unused")
	@Test
	public void testCreate_ByStringPath() {
		assertEquals("a", new DirCacheEntry("a").getPathString());
		assertEquals("a/b", new DirCacheEntry("a/b").getPathString());

		try {
			new DirCacheEntry("/a");
			fail("Incorrectly created DirCacheEntry");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid path: /a", err.getMessage());
		}
	}

	@SuppressWarnings("unused")
	@Test
	public void testCreate_ByStringPathAndStage() {
		DirCacheEntry e;

		e = new DirCacheEntry("a", 0);
		assertEquals("a", e.getPathString());
		assertEquals(0, e.getStage());

		e = new DirCacheEntry("a/b", 1);
		assertEquals("a/b", e.getPathString());
		assertEquals(1, e.getStage());

		e = new DirCacheEntry("a/c", 2);
		assertEquals("a/c", e.getPathString());
		assertEquals(2, e.getStage());

		e = new DirCacheEntry("a/d", 3);
		assertEquals("a/d", e.getPathString());
		assertEquals(3, e.getStage());

		try {
			new DirCacheEntry("/a", 1);
			fail("Incorrectly created DirCacheEntry");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid path: /a", err.getMessage());
		}

		try {
			new DirCacheEntry("a", -11);
			fail("Incorrectly created DirCacheEntry");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid stage -11 for path a", err.getMessage());
		}

		try {
			new DirCacheEntry("a", 4);
			fail("Incorrectly created DirCacheEntry");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid stage 4 for path a", err.getMessage());
		}
	}

	@Test
	public void testSetFileMode() {
		final DirCacheEntry e = new DirCacheEntry("a");

		assertEquals(0, e.getRawMode());

		e.setFileMode(FileMode.REGULAR_FILE);
		assertSame(FileMode.REGULAR_FILE, e.getFileMode());
		assertEquals(FileMode.REGULAR_FILE.getBits(), e.getRawMode());

		e.setFileMode(FileMode.EXECUTABLE_FILE);
		assertSame(FileMode.EXECUTABLE_FILE, e.getFileMode());
		assertEquals(FileMode.EXECUTABLE_FILE.getBits(), e.getRawMode());

		e.setFileMode(FileMode.SYMLINK);
		assertSame(FileMode.SYMLINK, e.getFileMode());
		assertEquals(FileMode.SYMLINK.getBits(), e.getRawMode());

		e.setFileMode(FileMode.GITLINK);
		assertSame(FileMode.GITLINK, e.getFileMode());
		assertEquals(FileMode.GITLINK.getBits(), e.getRawMode());

		try {
			e.setFileMode(FileMode.MISSING);
			fail("incorrectly accepted FileMode.MISSING");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid mode 0 for path a", err.getMessage());
		}

		try {
			e.setFileMode(FileMode.TREE);
			fail("incorrectly accepted FileMode.TREE");
		} catch (IllegalArgumentException err) {
			assertEquals("Invalid mode 40000 for path a", err.getMessage());
		}
	}

	@Test
	public void testSetStage() {
		DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_1);
		e.setAssumeValid(true);
		e.setCreationTime(2L);
		e.setFileMode(FileMode.EXECUTABLE_FILE);
		e.setLastModified(EPOCH.plusMillis(3L));
		e.setLength(100L);
		e.setObjectId(ObjectId
				.fromString("0123456789012345678901234567890123456789"));
		e.setUpdateNeeded(true);
		e.setStage(DirCacheEntry.STAGE_2);

		assertTrue(e.isAssumeValid());
		assertEquals(2L, e.getCreationTime());
		assertEquals(
				ObjectId.fromString("0123456789012345678901234567890123456789"),
				e.getObjectId());
		assertEquals(FileMode.EXECUTABLE_FILE, e.getFileMode());
		assertEquals(EPOCH.plusMillis(3L), e.getLastModifiedInstant());
		assertEquals(100L, e.getLength());
		assertEquals(DirCacheEntry.STAGE_2, e.getStage());
		assertTrue(e.isUpdateNeeded());
		assertEquals("some/path", e.getPathString());

		e.setStage(DirCacheEntry.STAGE_0);

		assertTrue(e.isAssumeValid());
		assertEquals(2L, e.getCreationTime());
		assertEquals(
				ObjectId.fromString("0123456789012345678901234567890123456789"),
				e.getObjectId());
		assertEquals(FileMode.EXECUTABLE_FILE, e.getFileMode());
		assertEquals(EPOCH.plusMillis(3L), e.getLastModifiedInstant());
		assertEquals(100L, e.getLength());
		assertEquals(DirCacheEntry.STAGE_0, e.getStage());
		assertTrue(e.isUpdateNeeded());
		assertEquals("some/path", e.getPathString());
	}

	@Test
	public void testCopyMetaDataWithStage() {
		copyMetaDataHelper(false);
	}

	@Test
	public void testCopyMetaDataWithoutStage() {
		copyMetaDataHelper(true);
	}

	private static void copyMetaDataHelper(boolean keepStage) {
		DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_2);
		e.setAssumeValid(false);
		e.setCreationTime(2L);
		e.setFileMode(FileMode.EXECUTABLE_FILE);
		e.setLastModified(EPOCH.plusMillis(3L));
		e.setLength(100L);
		e.setObjectId(ObjectId
				.fromString("0123456789012345678901234567890123456789"));
		e.setUpdateNeeded(true);

		DirCacheEntry f = new DirCacheEntry("someother/path",
				DirCacheEntry.STAGE_1);
		f.setAssumeValid(true);
		f.setCreationTime(10L);
		f.setFileMode(FileMode.SYMLINK);
		f.setLastModified(EPOCH.plusMillis(20L));
		f.setLength(100000000L);
		f.setObjectId(ObjectId
				.fromString("1234567890123456789012345678901234567890"));
		f.setUpdateNeeded(true);

		e.copyMetaData(f, keepStage);
		assertTrue(e.isAssumeValid());
		assertEquals(10L, e.getCreationTime());
		assertEquals(
				ObjectId.fromString("1234567890123456789012345678901234567890"),
				e.getObjectId());
		assertEquals(FileMode.SYMLINK, e.getFileMode());
		assertEquals(EPOCH.plusMillis(20L), e.getLastModifiedInstant());
		assertEquals(100000000L, e.getLength());
		if (keepStage)
			assertEquals(DirCacheEntry.STAGE_2, e.getStage());
		else
			assertEquals(DirCacheEntry.STAGE_1, e.getStage());
		assertTrue(e.isUpdateNeeded());
		assertEquals("some/path", e.getPathString());
	}
}