Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Pursehouse2016-02-15 03:14:09 -0500
committerDavid Pursehouse2016-02-15 03:14:09 -0500
commit259dca06ec5b3371106f0981c21ae63b95fbcff2 (patch)
tree6aadb425a0a9313a1cd5e9c6b7bbc9a4ebe4284f
parent30c9ec88d18a50b543c5c5d94c6f5f956d538773 (diff)
downloadjgit-259dca06ec5b3371106f0981c21ae63b95fbcff2.tar.gz
jgit-259dca06ec5b3371106f0981c21ae63b95fbcff2.tar.xz
jgit-259dca06ec5b3371106f0981c21ae63b95fbcff2.zip
DirCacheCheckoutMaliciousPathTest: Open Git and RevWalk in t-w-r
Change-Id: Iacb4e25f0ada74b1a01e448216cb02c7ec18b2d7 Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java121
1 files changed, 61 insertions, 60 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java
index ca3e0666ea..59a4699bcd 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutMaliciousPathTest.java
@@ -338,68 +338,69 @@ public class DirCacheCheckoutMaliciousPathTest extends RepositoryTestCase {
*/
private void testMaliciousPath(boolean good, boolean secondCheckout,
String... path) throws GitAPIException, IOException {
- Git git = new Git(db);
- ObjectInserter newObjectInserter;
- newObjectInserter = git.getRepository().newObjectInserter();
- ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
- "data".getBytes());
- newObjectInserter = git.getRepository().newObjectInserter();
- FileMode mode = FileMode.REGULAR_FILE;
- ObjectId insertId = blobId;
- for (int i = path.length - 1; i >= 0; --i) {
- TreeFormatter treeFormatter = new TreeFormatter();
- treeFormatter.append("goodpath", mode, insertId);
- insertId = newObjectInserter.insert(treeFormatter);
- mode = FileMode.TREE;
- }
- newObjectInserter = git.getRepository().newObjectInserter();
- CommitBuilder commitBuilder = new CommitBuilder();
- commitBuilder.setAuthor(author);
- commitBuilder.setCommitter(committer);
- commitBuilder.setMessage("foo#1");
- commitBuilder.setTreeId(insertId);
- ObjectId firstCommitId = newObjectInserter.insert(commitBuilder);
-
- newObjectInserter = git.getRepository().newObjectInserter();
- mode = FileMode.REGULAR_FILE;
- insertId = blobId;
- for (int i = path.length - 1; i >= 0; --i) {
- TreeFormatter treeFormatter = new TreeFormatter();
- treeFormatter.append(path[i], mode, insertId);
- insertId = newObjectInserter.insert(treeFormatter);
- mode = FileMode.TREE;
- }
+ try (Git git = new Git(db);
+ RevWalk revWalk = new RevWalk(git.getRepository())) {
+ ObjectInserter newObjectInserter;
+ newObjectInserter = git.getRepository().newObjectInserter();
+ ObjectId blobId = newObjectInserter.insert(Constants.OBJ_BLOB,
+ "data".getBytes());
+ newObjectInserter = git.getRepository().newObjectInserter();
+ FileMode mode = FileMode.REGULAR_FILE;
+ ObjectId insertId = blobId;
+ for (int i = path.length - 1; i >= 0; --i) {
+ TreeFormatter treeFormatter = new TreeFormatter();
+ treeFormatter.append("goodpath", mode, insertId);
+ insertId = newObjectInserter.insert(treeFormatter);
+ mode = FileMode.TREE;
+ }
+ newObjectInserter = git.getRepository().newObjectInserter();
+ CommitBuilder commitBuilder = new CommitBuilder();
+ commitBuilder.setAuthor(author);
+ commitBuilder.setCommitter(committer);
+ commitBuilder.setMessage("foo#1");
+ commitBuilder.setTreeId(insertId);
+ ObjectId firstCommitId = newObjectInserter.insert(commitBuilder);
+
+ newObjectInserter = git.getRepository().newObjectInserter();
+ mode = FileMode.REGULAR_FILE;
+ insertId = blobId;
+ for (int i = path.length - 1; i >= 0; --i) {
+ TreeFormatter treeFormatter = new TreeFormatter();
+ treeFormatter.append(path[i], mode, insertId);
+ insertId = newObjectInserter.insert(treeFormatter);
+ mode = FileMode.TREE;
+ }
- // Create another commit
- commitBuilder = new CommitBuilder();
- commitBuilder.setAuthor(author);
- commitBuilder.setCommitter(committer);
- commitBuilder.setMessage("foo#2");
- commitBuilder.setTreeId(insertId);
- commitBuilder.setParentId(firstCommitId);
- ObjectId commitId = newObjectInserter.insert(commitBuilder);
-
- RevWalk revWalk = new RevWalk(git.getRepository());
- if (!secondCheckout)
- git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
- .setName("refs/heads/master").setCreateBranch(true).call();
- try {
- if (secondCheckout) {
- git.checkout().setStartPoint(revWalk.parseCommit(commitId))
- .setName("refs/heads/master").setCreateBranch(true)
- .call();
- } else {
- git.branchCreate().setName("refs/heads/next")
- .setStartPoint(commitId.name()).call();
- git.checkout().setName("refs/heads/next")
- .call();
+ // Create another commit
+ commitBuilder = new CommitBuilder();
+ commitBuilder.setAuthor(author);
+ commitBuilder.setCommitter(committer);
+ commitBuilder.setMessage("foo#2");
+ commitBuilder.setTreeId(insertId);
+ commitBuilder.setParentId(firstCommitId);
+ ObjectId commitId = newObjectInserter.insert(commitBuilder);
+
+ if (!secondCheckout)
+ git.checkout().setStartPoint(revWalk.parseCommit(firstCommitId))
+ .setName("refs/heads/master").setCreateBranch(true).call();
+ try {
+ if (secondCheckout) {
+ git.checkout().setStartPoint(revWalk.parseCommit(commitId))
+ .setName("refs/heads/master").setCreateBranch(true)
+ .call();
+ } else {
+ git.branchCreate().setName("refs/heads/next")
+ .setStartPoint(commitId.name()).call();
+ git.checkout().setName("refs/heads/next")
+ .call();
+ }
+ if (!good)
+ fail("Checkout of Tree " + Arrays.asList(path) + " should fail");
+ } catch (InvalidPathException e) {
+ if (good)
+ throw e;
+ assertTrue(e.getMessage().startsWith("Invalid path"));
}
- if (!good)
- fail("Checkout of Tree " + Arrays.asList(path) + " should fail");
- } catch (InvalidPathException e) {
- if (good)
- throw e;
- assertTrue(e.getMessage().startsWith("Invalid path"));
}
}

Back to the top