diff options
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java | 221 |
1 files changed, 211 insertions, 10 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java index 133ecd01eb..ef083da183 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java @@ -15,6 +15,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringWriter; +import org.eclipse.jgit.errors.PackProtocolException; import org.eclipse.jgit.errors.TransportException; import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector; import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; @@ -335,12 +336,12 @@ public class UploadPackTest { } /* - * Invokes UploadPack with protocol v2 and sends it the given lines. - * Returns UploadPack's output stream, not including the capability - * advertisement by the server. + * Invokes UploadPack with protocol v2 and sends it the given lines, + * and returns UploadPack's output stream. */ - private ByteArrayInputStream uploadPackV2(RequestPolicy requestPolicy, + private ByteArrayInputStream uploadPackV2Setup(RequestPolicy requestPolicy, RefFilter refFilter, String... inputLines) throws Exception { + ByteArrayOutputStream send = new ByteArrayOutputStream(); PacketLineOut pckOut = new PacketLineOut(send); for (String line : inputLines) { @@ -364,10 +365,37 @@ public class UploadPackTest { ByteArrayOutputStream recv = new ByteArrayOutputStream(); up.upload(new ByteArrayInputStream(send.toByteArray()), recv, null); - ByteArrayInputStream recvStream = new ByteArrayInputStream(recv.toByteArray()); + return new ByteArrayInputStream(recv.toByteArray()); + } + + /* + * Invokes UploadPack with protocol v2 and sends it the given lines. + * Returns UploadPack's output stream, not including the capability + * advertisement by the server. + */ + private ByteArrayInputStream uploadPackV2(RequestPolicy requestPolicy, + RefFilter refFilter, String... inputLines) throws Exception { + ByteArrayInputStream recvStream = + uploadPackV2Setup(requestPolicy, refFilter, inputLines); + PacketLineIn pckIn = new PacketLineIn(recvStream); + + // drain capabilities + while (pckIn.readString() != PacketLineIn.END) { + // do nothing + } + return recvStream; + } + + private ByteArrayInputStream uploadPackV2(String... inputLines) throws Exception { + return uploadPackV2(null, null, inputLines); + } + + @Test + public void testV2Capabilities() throws Exception { + ByteArrayInputStream recvStream = + uploadPackV2Setup(null, null, PacketLineIn.END); PacketLineIn pckIn = new PacketLineIn(recvStream); - // capability advertisement (always sent) assertThat(pckIn.readString(), is("version 2")); assertThat( Arrays.asList(pckIn.readString(), pckIn.readString()), @@ -377,13 +405,24 @@ public class UploadPackTest { // allow additional commands to be added to the list, // and additional capabilities to be added to existing // commands without requiring test changes. - hasItems("ls-refs", "fetch")); + hasItems("ls-refs", "fetch=shallow")); assertTrue(pckIn.readString() == PacketLineIn.END); - return recvStream; } - private ByteArrayInputStream uploadPackV2(String... inputLines) throws Exception { - return uploadPackV2(null, null, inputLines); + @Test + public void testV2CapabilitiesAllowFilter() throws Exception { + server.getConfig().setBoolean("uploadpack", null, "allowfilter", true); + ByteArrayInputStream recvStream = + uploadPackV2Setup(null, null, PacketLineIn.END); + PacketLineIn pckIn = new PacketLineIn(recvStream); + + assertThat(pckIn.readString(), is("version 2")); + assertThat( + Arrays.asList(pckIn.readString(), pckIn.readString()), + // TODO(jonathantanmy) This check overspecifies the + // order of the capabilities of "fetch". + hasItems("ls-refs", "fetch=filter shallow")); + assertTrue(pckIn.readString() == PacketLineIn.END); } @Test @@ -514,6 +553,17 @@ public class UploadPackTest { assertTrue(pckIn.readString() == PacketLineIn.END); } + @Test + public void testV2LsRefsUnrecognizedArgument() throws Exception { + thrown.expect(PackProtocolException.class); + thrown.expectMessage("unexpected invalid-argument"); + uploadPackV2( + "command=ls-refs\n", + PacketLineIn.DELIM, + "invalid-argument\n", + PacketLineIn.END); + } + /* * Parse multiplexed packfile output from upload-pack using protocol V2 * into the client repository. @@ -877,6 +927,157 @@ public class UploadPackTest { assertTrue(stats.getNumOfsDelta() != 0); } + @Test + public void testV2FetchShallow() throws Exception { + RevCommit commonParent = remote.commit().message("parent").create(); + RevCommit fooChild = remote.commit().message("x").parent(commonParent).create(); + RevCommit barChild = remote.commit().message("y").parent(commonParent).create(); + remote.update("branch1", barChild); + + // Without shallow, the server thinks that we have + // commonParent, so it doesn't send it. + ByteArrayInputStream recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + barChild.toObjectId().getName() + "\n", + "have " + fooChild.toObjectId().getName() + "\n", + "done\n", + PacketLineIn.END); + PacketLineIn pckIn = new PacketLineIn(recvStream); + assertThat(pckIn.readString(), is("packfile")); + parsePack(recvStream); + assertTrue(client.hasObject(barChild.toObjectId())); + assertFalse(client.hasObject(commonParent.toObjectId())); + + // With shallow, the server knows that we don't have + // commonParent, so it sends it. + recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + barChild.toObjectId().getName() + "\n", + "have " + fooChild.toObjectId().getName() + "\n", + "shallow " + fooChild.toObjectId().getName() + "\n", + "done\n", + PacketLineIn.END); + pckIn = new PacketLineIn(recvStream); + assertThat(pckIn.readString(), is("packfile")); + parsePack(recvStream); + assertTrue(client.hasObject(commonParent.toObjectId())); + } + + @Test + public void testV2FetchDeepenAndDone() throws Exception { + RevCommit parent = remote.commit().message("parent").create(); + RevCommit child = remote.commit().message("x").parent(parent).create(); + remote.update("branch1", child); + + // "deepen 1" sends only the child. + ByteArrayInputStream recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + child.toObjectId().getName() + "\n", + "deepen 1\n", + "done\n", + PacketLineIn.END); + PacketLineIn pckIn = new PacketLineIn(recvStream); + assertThat(pckIn.readString(), is("shallow-info")); + assertThat(pckIn.readString(), is("shallow " + child.toObjectId().getName())); + assertThat(pckIn.readString(), theInstance(PacketLineIn.DELIM)); + assertThat(pckIn.readString(), is("packfile")); + parsePack(recvStream); + assertTrue(client.hasObject(child.toObjectId())); + assertFalse(client.hasObject(parent.toObjectId())); + + // Without that, the parent is sent too. + recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + child.toObjectId().getName() + "\n", + "done\n", + PacketLineIn.END); + pckIn = new PacketLineIn(recvStream); + assertThat(pckIn.readString(), is("packfile")); + parsePack(recvStream); + assertTrue(client.hasObject(parent.toObjectId())); + } + + @Test + public void testV2FetchDeepenWithoutDone() throws Exception { + RevCommit parent = remote.commit().message("parent").create(); + RevCommit child = remote.commit().message("x").parent(parent).create(); + remote.update("branch1", child); + + ByteArrayInputStream recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + child.toObjectId().getName() + "\n", + "deepen 1\n", + PacketLineIn.END); + PacketLineIn pckIn = new PacketLineIn(recvStream); + + // Verify that only the correct section is sent. "shallow-info" + // is not sent because, according to the specification, it is + // sent only if a packfile is sent. + assertThat(pckIn.readString(), is("acknowledgments")); + assertThat(pckIn.readString(), is("NAK")); + assertThat(pckIn.readString(), theInstance(PacketLineIn.END)); + } + + @Test + public void testV2FetchUnrecognizedArgument() throws Exception { + thrown.expect(PackProtocolException.class); + thrown.expectMessage("unexpected invalid-argument"); + uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "invalid-argument\n", + PacketLineIn.END); + } + + @Test + public void testV2FetchFilter() throws Exception { + RevBlob big = remote.blob("foobar"); + RevBlob small = remote.blob("fooba"); + RevTree tree = remote.tree(remote.file("1", big), + remote.file("2", small)); + RevCommit commit = remote.commit(tree); + remote.update("master", commit); + + server.getConfig().setBoolean("uploadpack", null, "allowfilter", true); + + ByteArrayInputStream recvStream = uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + commit.toObjectId().getName() + "\n", + "filter blob:limit=5\n", + "done\n", + PacketLineIn.END); + PacketLineIn pckIn = new PacketLineIn(recvStream); + assertThat(pckIn.readString(), is("packfile")); + parsePack(recvStream); + + assertFalse(client.hasObject(big.toObjectId())); + assertTrue(client.hasObject(small.toObjectId())); + } + + @Test + public void testV2FetchFilterWhenNotAllowed() throws Exception { + RevCommit commit = remote.commit().message("0").create(); + remote.update("master", commit); + + server.getConfig().setBoolean("uploadpack", null, "allowfilter", false); + + thrown.expect(PackProtocolException.class); + thrown.expectMessage("unexpected filter blob:limit=5"); + uploadPackV2( + "command=fetch\n", + PacketLineIn.DELIM, + "want " + commit.toObjectId().getName() + "\n", + "filter blob:limit=5\n", + "done\n", + PacketLineIn.END); + } + private static class RejectAllRefFilter implements RefFilter { @Override public Map<String, Ref> filter(Map<String, Ref> refs) { |