private void needDataPacket() throws IOException { if (eof || (channel == CH_DATA && available > 0)) return; for (; ; ) { available = pckIn.readLength(); if (available == 0) { eof = true; return; } channel = rawIn.read() & 0xff; available -= HDR_SIZE; // length header plus channel indicator if (available == 0) continue; switch (channel) { case CH_DATA: return; case CH_PROGRESS: progress(readString(available)); continue; case CH_ERROR: eof = true; throw new TransportException(PFX_REMOTE + readString(available)); default: throw new PackProtocolException( MessageFormat.format(JGitText.get().invalidChannel, channel)); } } }
@Test public void testUsingHiddenDeltaBaseFails() throws Exception { byte[] delta = {0x1, 0x1, 0x1, 'c'}; TestRepository<Repository> s = new TestRepository<Repository>(src); RevCommit N = s.commit() .parent(B) .add("q", s.blob(BinaryDelta.apply(dst.open(b).getCachedBytes(), delta))) .create(); final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); packHeader(pack, 3); copy(pack, src.open(N)); copy(pack, src.open(s.parseBody(N).getTree())); pack.write((Constants.OBJ_REF_DELTA) << 4 | 4); b.copyRawTo(pack); deflate(pack, delta); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + N.name() + ' ' + "refs/heads/s" + '\0' + 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 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 public void testUsingUnknownBlobFails() throws Exception { // Try to use the 'n' blob that is not on the server. // TestRepository<Repository> s = new TestRepository<Repository>(src); RevBlob n = s.blob("n"); RevCommit N = s.commit().parent(B).add("q", n).create(); // But don't include it in the pack. // final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); packHeader(pack, 2); copy(pack, src.open(N)); copy(pack, src.open(s.parseBody(N).getTree())); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + N.name() + ' ' + "refs/heads/s" + '\0' + 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(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 public void testCreateBranchAtHiddenCommitFails() throws Exception { final TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); packHeader(pack, 0); digest(pack); final TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); final PacketLineOut inPckLine = new PacketLineOut(inBuf); inPckLine.writeString( ObjectId.zeroId().name() + ' ' + P.name() + ' ' + "refs/heads/s" + '\0' + 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(P, 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 commit " + P.name(), r.readString()); assertEquals("ng refs/heads/s n/a (unpacker error)", r.readString()); assertSame(PacketLineIn.END, r.readString()); }
private void recvCommands() throws IOException { for (; ; ) { String line; try { line = pckIn.readStringRaw(); } catch (EOFException eof) { if (commands.isEmpty()) return; throw eof; } if (line == PacketLineIn.END) break; if (commands.isEmpty()) { final int nul = line.indexOf('\0'); if (nul >= 0) { for (String c : line.substring(nul + 1).split(" ")) enabledCapablities.add(c); line = line.substring(0, nul); } } if (line.length() < 83) { final String m = JGitText.get().errorInvalidProtocolWantedOldNewRef; sendError(m); throw new PackProtocolException(m); } final ObjectId oldId = ObjectId.fromString(line.substring(0, 40)); final ObjectId newId = ObjectId.fromString(line.substring(41, 81)); final String name = line.substring(82); final ReceiveCommand cmd = new ReceiveCommand(oldId, newId, name); if (name.equals(Constants.HEAD)) { cmd.setResult(Result.REJECTED_CURRENT_BRANCH); } else { cmd.setRef(refs.get(cmd.getRefName())); } commands.add(cmd); } }