Browse Source

ReceivePackAdvertiseRefsHookTest: Open TestRepository in try-with-resource

Change-Id: I7ab1de3516ac542821275add908ee70c427d781b
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.4
David Pursehouse 6 years ago
parent
commit
6efe3612a7
  1. 483
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackAdvertiseRefsHookTest.java

483
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackAdvertiseRefsHookTest.java

@ -110,24 +110,26 @@ public class ReceivePackAdvertiseRefsHookTest extends LocalDiskRepositoryTestCas
// Fill dst with a some common history. // Fill dst with a some common history.
// //
TestRepository<Repository> d = new TestRepository<>(dst); try (TestRepository<Repository> d = new TestRepository<>(dst)) {
a = d.blob("a"); a = d.blob("a");
A = d.commit(d.tree(d.file("a", a))); A = d.commit(d.tree(d.file("a", a)));
B = d.commit().parent(A).create(); B = d.commit().parent(A).create();
d.update(R_MASTER, B); d.update(R_MASTER, B);
// Clone from dst into src
//
try (Transport t = Transport.open(src, uriOf(dst))) {
t.fetch(PM,
Collections.singleton(new RefSpec("+refs/*:refs/*")));
assertEquals(B, src.resolve(R_MASTER));
}
// Clone from dst into src // Now put private stuff into dst.
// //
try (Transport t = Transport.open(src, uriOf(dst))) { b = d.blob("b");
t.fetch(PM, Collections.singleton(new RefSpec("+refs/*:refs/*"))); P = d.commit(d.tree(d.file("b", b)), A);
assertEquals(B, src.resolve(R_MASTER)); d.update(R_PRIVATE, P);
} }
// Now put private stuff into dst.
//
b = d.blob("b");
P = d.commit(d.tree(d.file("b", b)), A);
d.update(R_PRIVATE, P);
} }
@Test @Test
@ -241,31 +243,33 @@ public class ReceivePackAdvertiseRefsHookTest extends LocalDiskRepositoryTestCas
// Now use b but in a different commit than what is hidden. // Now use b but in a different commit than what is hidden.
// //
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevCommit N = s.commit().parent(B).add("q", b).create(); RevCommit N = s.commit().parent(B).add("q", b).create();
s.update(R_MASTER, N); s.update(R_MASTER, N);
// Push this new content to the remote, doing strict validation.
//
PushResult r;
RemoteRefUpdate u = new RemoteRefUpdate( //
src, //
R_MASTER, // src name
R_MASTER, // dst name
false, // do not force update
null, // local tracking branch
null // expected id
);
try (TransportLocal t = newTransportLocalWithStrictValidation()) {
t.setPushThin(true);
r = t.push(PM, Collections.singleton(u));
dst.close();
}
// Push this new content to the remote, doing strict validation. assertNotNull("have result", r);
// assertNull("private not advertised", r.getAdvertisedRef(R_PRIVATE));
PushResult r; assertSame("master updated", RemoteRefUpdate.Status.OK,
RemoteRefUpdate u = new RemoteRefUpdate( // u.getStatus());
src, // assertEquals(N, dst.resolve(R_MASTER));
R_MASTER, // src name
R_MASTER, // dst name
false, // do not force update
null, // local tracking branch
null // expected id
);
try (TransportLocal t = newTransportLocalWithStrictValidation()) {
t.setPushThin(true);
r = t.push(PM, Collections.singleton(u));
dst.close();
} }
assertNotNull("have result", r);
assertNull("private not advertised", r.getAdvertisedRef(R_PRIVATE));
assertSame("master updated", RemoteRefUpdate.Status.OK, u.getStatus());
assertEquals(N, dst.resolve(R_MASTER));
} }
@Test @Test
@ -318,154 +322,165 @@ public class ReceivePackAdvertiseRefsHookTest extends LocalDiskRepositoryTestCas
@Test @Test
public void testUsingHiddenDeltaBaseFails() throws Exception { public void testUsingHiddenDeltaBaseFails() throws Exception {
byte[] delta = { 0x1, 0x1, 0x1, 'c' }; byte[] delta = { 0x1, 0x1, 0x1, 'c' };
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevCommit N = s.commit().parent(B).add("q", RevCommit N = s.commit().parent(B)
s.blob(BinaryDelta.apply(dst.open(b).getCachedBytes(), delta))) .add("q",
.create(); s.blob(BinaryDelta.apply(
dst.open(b).getCachedBytes(), delta)))
final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); .create();
packHeader(pack, 3);
copy(pack, src.open(N)); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
copy(pack, src.open(s.parseBody(N).getTree())); packHeader(pack, 3);
pack.write((Constants.OBJ_REF_DELTA) << 4 | 4); copy(pack, src.open(N));
b.copyRawTo(pack); copy(pack, src.open(s.parseBody(N).getTree()));
deflate(pack, delta); pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
digest(pack); b.copyRawTo(pack);
deflate(pack, delta);
final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); digest(pack);
final PacketLineOut inPckLine = new PacketLineOut(inBuf);
inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' ' final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
+ "refs/heads/s" + '\0' final PacketLineOut inPckLine = new PacketLineOut(inBuf);
+ BasePackPushConnection.CAPABILITY_REPORT_STATUS); inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name()
inPckLine.end(); + ' ' + "refs/heads/s" + '\0'
pack.writeTo(inBuf, PM); + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
inPckLine.end();
pack.writeTo(inBuf, PM);
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
final ReceivePack rp = new ReceivePack(dst);
rp.setCheckReceivedObjects(true);
rp.setCheckReferencedObjectsAreReachable(true);
rp.setAdvertiseRefsHook(new HidePrivateHook());
try {
receive(rp, inBuf, outBuf);
fail("Expected UnpackException");
} catch (UnpackException failed) {
Throwable err = failed.getCause();
assertTrue(err instanceof MissingObjectException);
MissingObjectException moe = (MissingObjectException) err;
assertEquals(b, moe.getObjectId());
}
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final PacketLineIn r = asPacketLineIn(outBuf);
final ReceivePack rp = new ReceivePack(dst); String master = r.readString();
rp.setCheckReceivedObjects(true); int nul = master.indexOf('\0');
rp.setCheckReferencedObjectsAreReachable(true); assertTrue("has capability list", nul > 0);
rp.setAdvertiseRefsHook(new HidePrivateHook()); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
try { assertSame(PacketLineIn.END, r.readString());
receive(rp, inBuf, outBuf);
fail("Expected UnpackException"); assertEquals("unpack error Missing blob " + b.name(),
} catch (UnpackException failed) { r.readString());
Throwable err = failed.getCause(); assertEquals("ng refs/heads/s n/a (unpacker error)",
assertTrue(err instanceof MissingObjectException); r.readString());
MissingObjectException moe = (MissingObjectException) err; assertSame(PacketLineIn.END, r.readString());
assertEquals(b, moe.getObjectId());
} }
final PacketLineIn r = asPacketLineIn(outBuf);
String master = r.readString();
int nul = master.indexOf('\0');
assertTrue("has capability list", nul > 0);
assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
assertSame(PacketLineIn.END, r.readString());
assertEquals("unpack error Missing blob " + b.name(), r.readString());
assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
assertSame(PacketLineIn.END, r.readString());
} }
@Test @Test
public void testUsingHiddenCommonBlobFails() throws Exception { public void testUsingHiddenCommonBlobFails() throws Exception {
// Try to use the 'b' blob that is hidden. // Try to use the 'b' blob that is hidden.
// //
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create(); RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create();
// But don't include it in the pack. // But don't include it in the pack.
// //
final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
packHeader(pack, 2); packHeader(pack, 2);
copy(pack, src.open(N)); copy(pack, src.open(N));
copy(pack,src.open(s.parseBody(N).getTree())); copy(pack, src.open(s.parseBody(N).getTree()));
digest(pack); digest(pack);
final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
final PacketLineOut inPckLine = new PacketLineOut(inBuf); final PacketLineOut inPckLine = new PacketLineOut(inBuf);
inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' ' inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name()
+ "refs/heads/s" + '\0' + ' ' + "refs/heads/s" + '\0'
+ BasePackPushConnection.CAPABILITY_REPORT_STATUS); + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
inPckLine.end(); inPckLine.end();
pack.writeTo(inBuf, PM); pack.writeTo(inBuf, PM);
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
final ReceivePack rp = new ReceivePack(dst);
rp.setCheckReceivedObjects(true);
rp.setCheckReferencedObjectsAreReachable(true);
rp.setAdvertiseRefsHook(new HidePrivateHook());
try {
receive(rp, inBuf, outBuf);
fail("Expected UnpackException");
} catch (UnpackException failed) {
Throwable err = failed.getCause();
assertTrue(err instanceof MissingObjectException);
MissingObjectException moe = (MissingObjectException) err;
assertEquals(b, moe.getObjectId());
}
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final PacketLineIn r = asPacketLineIn(outBuf);
final ReceivePack rp = new ReceivePack(dst); String master = r.readString();
rp.setCheckReceivedObjects(true); int nul = master.indexOf('\0');
rp.setCheckReferencedObjectsAreReachable(true); assertTrue("has capability list", nul > 0);
rp.setAdvertiseRefsHook(new HidePrivateHook()); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
try { assertSame(PacketLineIn.END, r.readString());
receive(rp, inBuf, outBuf);
fail("Expected UnpackException"); assertEquals("unpack error Missing blob " + b.name(),
} catch (UnpackException failed) { r.readString());
Throwable err = failed.getCause(); assertEquals("ng refs/heads/s n/a (unpacker error)",
assertTrue(err instanceof MissingObjectException); r.readString());
MissingObjectException moe = (MissingObjectException) err; assertSame(PacketLineIn.END, r.readString());
assertEquals(b, moe.getObjectId());
} }
final PacketLineIn r = asPacketLineIn(outBuf);
String master = r.readString();
int nul = master.indexOf('\0');
assertTrue("has capability list", nul > 0);
assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
assertSame(PacketLineIn.END, r.readString());
assertEquals("unpack error Missing blob " + b.name(), r.readString());
assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
assertSame(PacketLineIn.END, r.readString());
} }
@Test @Test
public void testUsingUnknownBlobFails() throws Exception { public void testUsingUnknownBlobFails() throws Exception {
// Try to use the 'n' blob that is not on the server. // Try to use the 'n' blob that is not on the server.
// //
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevBlob n = s.blob("n"); RevBlob n = s.blob("n");
RevCommit N = s.commit().parent(B).add("q", n).create(); RevCommit N = s.commit().parent(B).add("q", n).create();
// But don't include it in the pack. // But don't include it in the pack.
// //
final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
packHeader(pack, 2); packHeader(pack, 2);
copy(pack, src.open(N)); copy(pack, src.open(N));
copy(pack,src.open(s.parseBody(N).getTree())); copy(pack, src.open(s.parseBody(N).getTree()));
digest(pack); digest(pack);
final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
final PacketLineOut inPckLine = new PacketLineOut(inBuf); final PacketLineOut inPckLine = new PacketLineOut(inBuf);
inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' ' inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name()
+ "refs/heads/s" + '\0' + ' ' + "refs/heads/s" + '\0'
+ BasePackPushConnection.CAPABILITY_REPORT_STATUS); + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
inPckLine.end(); inPckLine.end();
pack.writeTo(inBuf, PM); pack.writeTo(inBuf, PM);
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
final ReceivePack rp = new ReceivePack(dst);
rp.setCheckReceivedObjects(true);
rp.setCheckReferencedObjectsAreReachable(true);
rp.setAdvertiseRefsHook(new HidePrivateHook());
try {
receive(rp, inBuf, outBuf);
fail("Expected UnpackException");
} catch (UnpackException failed) {
Throwable err = failed.getCause();
assertTrue(err instanceof MissingObjectException);
MissingObjectException moe = (MissingObjectException) err;
assertEquals(n, moe.getObjectId());
}
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final PacketLineIn r = asPacketLineIn(outBuf);
final ReceivePack rp = new ReceivePack(dst); String master = r.readString();
rp.setCheckReceivedObjects(true); int nul = master.indexOf('\0');
rp.setCheckReferencedObjectsAreReachable(true); assertTrue("has capability list", nul > 0);
rp.setAdvertiseRefsHook(new HidePrivateHook()); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
try { assertSame(PacketLineIn.END, r.readString());
receive(rp, inBuf, outBuf);
fail("Expected UnpackException"); assertEquals("unpack error Missing blob " + n.name(),
} catch (UnpackException failed) { r.readString());
Throwable err = failed.getCause(); assertEquals("ng refs/heads/s n/a (unpacker error)",
assertTrue(err instanceof MissingObjectException); r.readString());
MissingObjectException moe = (MissingObjectException) err; assertSame(PacketLineIn.END, r.readString());
assertEquals(n, moe.getObjectId());
} }
final PacketLineIn r = asPacketLineIn(outBuf);
String master = r.readString();
int nul = master.indexOf('\0');
assertTrue("has capability list", nul > 0);
assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
assertSame(PacketLineIn.END, r.readString());
assertEquals("unpack error Missing blob " + n.name(), r.readString());
assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
assertSame(PacketLineIn.END, r.readString());
} }
@Test @Test
@ -509,75 +524,79 @@ public class ReceivePackAdvertiseRefsHookTest extends LocalDiskRepositoryTestCas
.append(" url = -upayload.sh\n") .append(" url = -upayload.sh\n")
.toString(); .toString();
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevBlob blob = s.blob(fakeGitmodules); RevBlob blob = s.blob(fakeGitmodules);
RevCommit N = s.commit().parent(B) RevCommit N = s.commit().parent(B).add(".gitmodules", blob)
.add(".gitmodules", blob).create(); .create();
RevTree t = s.parseBody(N).getTree(); RevTree t = s.parseBody(N).getTree();
final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
packHeader(pack, 3); packHeader(pack, 3);
copy(pack, src.open(N)); copy(pack, src.open(N));
copy(pack, src.open(t)); copy(pack, src.open(t));
copy(pack, src.open(blob)); copy(pack, src.open(blob));
digest(pack); digest(pack);
final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
final PacketLineOut inPckLine = new PacketLineOut(inBuf); final PacketLineOut inPckLine = new PacketLineOut(inBuf);
inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' ' inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name()
+ "refs/heads/s" + '\0' + ' ' + "refs/heads/s" + '\0'
+ BasePackPushConnection.CAPABILITY_REPORT_STATUS); + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
inPckLine.end(); inPckLine.end();
pack.writeTo(inBuf, PM); pack.writeTo(inBuf, PM);
return inBuf; return inBuf;
}
} }
@Test @Test
public void testUsingUnknownTreeFails() throws Exception { public void testUsingUnknownTreeFails() throws Exception {
TestRepository<Repository> s = new TestRepository<>(src); try (TestRepository<Repository> s = new TestRepository<>(src)) {
RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create(); RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create();
RevTree t = s.parseBody(N).getTree(); RevTree t = s.parseBody(N).getTree();
// Don't include the tree in the pack. // Don't include the tree in the pack.
// //
final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
packHeader(pack, 1); packHeader(pack, 1);
copy(pack, src.open(N)); copy(pack, src.open(N));
digest(pack); digest(pack);
final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024);
final PacketLineOut inPckLine = new PacketLineOut(inBuf); final PacketLineOut inPckLine = new PacketLineOut(inBuf);
inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name() + ' ' inPckLine.writeString(ObjectId.zeroId().name() + ' ' + N.name()
+ "refs/heads/s" + '\0' + ' ' + "refs/heads/s" + '\0'
+ BasePackPushConnection.CAPABILITY_REPORT_STATUS); + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
inPckLine.end(); inPckLine.end();
pack.writeTo(inBuf, PM); pack.writeTo(inBuf, PM);
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
final ReceivePack rp = new ReceivePack(dst);
rp.setCheckReceivedObjects(true);
rp.setCheckReferencedObjectsAreReachable(true);
rp.setAdvertiseRefsHook(new HidePrivateHook());
try {
receive(rp, inBuf, outBuf);
fail("Expected UnpackException");
} catch (UnpackException failed) {
Throwable err = failed.getCause();
assertTrue(err instanceof MissingObjectException);
MissingObjectException moe = (MissingObjectException) err;
assertEquals(t, moe.getObjectId());
}
final TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); final PacketLineIn r = asPacketLineIn(outBuf);
final ReceivePack rp = new ReceivePack(dst); String master = r.readString();
rp.setCheckReceivedObjects(true); int nul = master.indexOf('\0');
rp.setCheckReferencedObjectsAreReachable(true); assertTrue("has capability list", nul > 0);
rp.setAdvertiseRefsHook(new HidePrivateHook()); assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
try { assertSame(PacketLineIn.END, r.readString());
receive(rp, inBuf, outBuf);
fail("Expected UnpackException"); assertEquals("unpack error Missing tree " + t.name(),
} catch (UnpackException failed) { r.readString());
Throwable err = failed.getCause(); assertEquals("ng refs/heads/s n/a (unpacker error)",
assertTrue(err instanceof MissingObjectException); r.readString());
MissingObjectException moe = (MissingObjectException) err; assertSame(PacketLineIn.END, r.readString());
assertEquals(t, moe.getObjectId());
} }
final PacketLineIn r = asPacketLineIn(outBuf);
String master = r.readString();
int nul = master.indexOf('\0');
assertTrue("has capability list", nul > 0);
assertEquals(B.name() + ' ' + R_MASTER, master.substring(0, nul));
assertSame(PacketLineIn.END, r.readString());
assertEquals("unpack error Missing tree " + t.name(), r.readString());
assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString());
assertSame(PacketLineIn.END, r.readString());
} }
private static void packHeader(TemporaryBuffer.Heap tinyPack, int cnt) private static void packHeader(TemporaryBuffer.Heap tinyPack, int cnt)

Loading…
Cancel
Save