aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/InvalidPathCheckoutTest.java
blob: e3bc85a512602b830e56f9691b6381f62eaa5fdb (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
/*
 * Copyright (C) 2023 Thomas Wolf <twolf@apache.org> 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 org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;

import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;

/**
 * Tests for checking out with invalid paths.
 */
public class InvalidPathCheckoutTest extends RepositoryTestCase {

	private DirCacheEntry brokenEntry(String fileName, RevBlob blob) {
		DirCacheEntry entry = new DirCacheEntry("XXXX/" + fileName);
		entry.path[0] = '.';
		entry.path[1] = 'g';
		entry.path[2] = 'i';
		entry.path[3] = 't';
		entry.setFileMode(FileMode.REGULAR_FILE);
		entry.setObjectId(blob);
		return entry;
	}

	@Test
	public void testCheckoutIntoDotGit() throws Exception {
		try (TestRepository<Repository> repo = new TestRepository<>(db)) {
			db.incrementOpen();
			// DirCacheEntry does not allow any path component to contain
			// ".git". C git also forbids this. But what if somebody creates
			// such an entry explicitly?
			RevCommit base = repo
					.commit(repo.tree(brokenEntry("b", repo.blob("test"))));
			try (Git git = new Git(db)) {
				assertThrows(InvalidPathException.class, () -> git.reset()
						.setMode(ResetType.HARD).setRef(base.name()).call());
				File b = new File(new File(trash, ".git"), "b");
				assertFalse(".git/b should not exist", b.exists());
			}
		}
	}

}