@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());
  }
Beispiel #3
0
  @Test
  public void testAbbreviatePackedBlob() throws Exception {
    RevBlob id = test.blob("test");
    test.branch("master").commit().add("test", id).child();
    test.packAndPrune();
    assertTrue(reader.has(id));

    assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
    assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
    assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
    assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));

    Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
    assertNotNull(matches);
    assertEquals(1, matches.size());
    assertEquals(id, matches.iterator().next());

    assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
  }
  @Test
  public void testPush_UnpackError_TruncatedPack() throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.append(ObjectId.zeroId().name());
    sb.append(' ');
    sb.append(a_blob.name());
    sb.append(' ');
    sb.append("refs/objects/A");
    sb.append('\0');
    sb.append("report-status");

    ByteArrayOutputStream reqbuf = new ByteArrayOutputStream();
    PacketLineOut reqpck = new PacketLineOut(reqbuf);
    reqpck.writeString(sb.toString());
    reqpck.end();

    packHeader(reqbuf, 1);

    byte[] reqbin = reqbuf.toByteArray();

    URL u = new URL(remoteURI.toString() + "/git-receive-pack");
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    try {
      c.setRequestMethod("POST");
      c.setDoOutput(true);
      c.setRequestProperty("Content-Type", GitSmartHttpTools.RECEIVE_PACK_REQUEST_TYPE);
      c.setFixedLengthStreamingMode(reqbin.length);
      OutputStream out = c.getOutputStream();
      try {
        out.write(reqbin);
      } finally {
        out.close();
      }

      assertEquals(200, c.getResponseCode());
      assertEquals(GitSmartHttpTools.RECEIVE_PACK_RESULT_TYPE, c.getContentType());

      InputStream rawin = c.getInputStream();
      try {
        PacketLineIn pckin = new PacketLineIn(rawin);
        assertEquals("unpack error " + JGitText.get().packfileIsTruncated, pckin.readString());
        assertEquals("ng refs/objects/A n/a (unpacker error)", pckin.readString());
        assertSame(PacketLineIn.END, pckin.readString());
      } finally {
        rawin.close();
      }
    } finally {
      c.disconnect();
    }
  }
  @Test
  public void testSuccess() throws Exception {
    // Manually force a delta of an object so we reuse it later.
    //
    TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);

    packHeader(pack, 2);
    pack.write((Constants.OBJ_BLOB) << 4 | 1);
    deflate(pack, new byte[] {'a'});

    pack.write((Constants.OBJ_REF_DELTA) << 4 | 4);
    a.copyRawTo(pack);
    deflate(pack, new byte[] {0x1, 0x1, 0x1, 'b'});

    digest(pack);
    openPack(pack);

    // Verify the only storage of b is our packed delta above.
    //
    ObjectDirectory od = (ObjectDirectory) src.getObjectDatabase();
    assertTrue("has b", src.hasObject(b));
    assertFalse("b not loose", od.fileFor(b).exists());

    // Now use b but in a different commit than what is hidden.
    //
    TestRepository<Repository> s = new TestRepository<Repository>(src);
    RevCommit N = s.commit().parent(B).add("q", b).create();
    s.update(R_MASTER, N);

    // Push this new content to the remote, doing strict validation.
    //
    TransportLocal t =
        new TransportLocal(src, uriOf(dst), dst.getDirectory()) {
          @Override
          ReceivePack createReceivePack(final Repository db) {
            db.close();
            dst.incrementOpen();

            final ReceivePack rp = super.createReceivePack(dst);
            rp.setCheckReceivedObjects(true);
            rp.setCheckReferencedObjectsAreReachable(true);
            rp.setAdvertiseRefsHook(new HidePrivateHook());
            return rp;
          }
        };
    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
            );
    PushResult r;
    try {
      t.setPushThin(true);
      r = t.push(PM, Collections.singleton(u));
    } finally {
      t.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));
  }