public virtual void TestUsingUnknownTreeFails() { TestRepository<Repository> s = new TestRepository<Repository>(src); RevCommit N = s.Commit().Parent(B).Add("q", s.Blob("a")).Create(); RevTree t = s.ParseBody(N).Tree; // Don't include the tree in the pack. // TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); Copy(pack, src.Open(N)); Digest(pack); TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); 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); TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); ReceivePack rp = new ReceivePack(dst); rp.SetCheckReceivedObjects(true); rp.SetCheckReferencedObjectsAreReachable(true); rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook()); try { Receive(rp, inBuf, outBuf); NUnit.Framework.Assert.Fail("Expected UnpackException"); } catch (UnpackException failed) { Exception err = failed.InnerException; NUnit.Framework.Assert.IsTrue(err is MissingObjectException); MissingObjectException moe = (MissingObjectException)err; NUnit.Framework.Assert.AreEqual(t, moe.GetObjectId()); } PacketLineIn r = AsPacketLineIn(outBuf); string master = r.ReadString(); int nul = master.IndexOf('\0'); NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list"); NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring (master, 0, nul)); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); NUnit.Framework.Assert.AreEqual("unpack error Missing tree " + t.Name, r.ReadString ()); NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString ()); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); }
public virtual void TestDelta_SmallObjectChain() { ObjectInserter.Formatter fmt = new ObjectInserter.Formatter(); byte[] data0 = new byte[512]; Arrays.Fill(data0, unchecked((byte)unchecked((int)(0xf3)))); ObjectId id0 = fmt.IdFor(Constants.OBJ_BLOB, data0); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64 * 1024); PackHeader(pack, 4); ObjectHeader(pack, Constants.OBJ_BLOB, data0.Length); Deflate(pack, data0); byte[] data1 = Clone(unchecked((int)(0x01)), data0); byte[] delta1 = Delta(data0, data1); ObjectId id1 = fmt.IdFor(Constants.OBJ_BLOB, data1); ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta1.Length); id0.CopyRawTo(pack); Deflate(pack, delta1); byte[] data2 = Clone(unchecked((int)(0x02)), data1); byte[] delta2 = Delta(data1, data2); ObjectId id2 = fmt.IdFor(Constants.OBJ_BLOB, data2); ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta2.Length); id1.CopyRawTo(pack); Deflate(pack, delta2); byte[] data3 = Clone(unchecked((int)(0x03)), data2); byte[] delta3 = Delta(data2, data3); ObjectId id3 = fmt.IdFor(Constants.OBJ_BLOB, data3); ObjectHeader(pack, Constants.OBJ_REF_DELTA, delta3.Length); id2.CopyRawTo(pack); Deflate(pack, delta3); Digest(pack); PackParser ip = Index(pack.ToByteArray()); ip.SetAllowThin(true); ip.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.IsTrue(wc.Has(id3), "has blob"); ObjectLoader ol = wc.Open(id3); NUnit.Framework.Assert.IsNotNull(ol, "created loader"); NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, ol.GetType()); NUnit.Framework.Assert.AreEqual(data3.Length, ol.GetSize()); NUnit.Framework.Assert.IsFalse(ol.IsLarge(), "is large"); NUnit.Framework.Assert.IsNotNull(ol.GetCachedBytes()); NUnit.Framework.Assert.IsTrue(Arrays.Equals(data3, ol.GetCachedBytes())); ObjectStream @in = ol.OpenStream(); NUnit.Framework.Assert.IsNotNull(@in, "have stream"); NUnit.Framework.Assert.AreEqual(Constants.OBJ_BLOB, @in.GetType()); NUnit.Framework.Assert.AreEqual(data3.Length, @in.GetSize()); byte[] act = new byte[data3.Length]; IOUtil.ReadFully(@in, act, 0, data3.Length); NUnit.Framework.Assert.IsTrue(Arrays.Equals(act, data3), "same content"); NUnit.Framework.Assert.AreEqual(-1, @in.Read(), "stream at EOF"); @in.Close(); }
// ignore any close errors, this was a read only stream /// <summary>Read an entire input stream into memory as a ByteBuffer.</summary> /// <remarks> /// Read an entire input stream into memory as a ByteBuffer. /// Note: The stream is read to its end and is not usable after calling this /// method. The caller is responsible for closing the stream. /// </remarks> /// <param name="in">input stream to be read.</param> /// <param name="sizeHint"> /// a hint on the approximate number of bytes contained in the /// stream, used to allocate temporary buffers more efficiently /// </param> /// <returns> /// complete contents of the input stream. The ByteBuffer always has /// a writable backing array, with /// <code>position() == 0</code> /// and /// <code>limit()</code> /// equal to the actual length read. Callers may rely /// on obtaining the underlying array for efficient data access. If /// <code>sizeHint</code> /// was too large, the array may be over-allocated, /// resulting in /// <code>limit() < array().length</code> /// . /// </returns> /// <exception cref="System.IO.IOException">there was an error reading from the stream. /// </exception> public static ByteBuffer ReadWholeStream(InputStream @in, int sizeHint) { byte[] @out = new byte[sizeHint]; int pos = 0; while (pos < @out.Length) { int read = @in.Read(@out, pos, @out.Length - pos); if (read < 0) { return ByteBuffer.Wrap(@out, 0, pos); } pos += read; } int last = @in.Read(); if (last < 0) { return ByteBuffer.Wrap(@out, 0, pos); } TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(int.MaxValue); tmp.Write(@out); tmp.Write(last); tmp.Copy(@in); return ByteBuffer.Wrap(tmp.ToByteArray()); }
public virtual void TestUsingHiddenDeltaBaseFails() { byte[] delta = new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('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(); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 3); Copy(pack, src.Open(N)); Copy(pack, src.Open(s.ParseBody(N).Tree)); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); b.CopyRawTo(pack); Deflate(pack, delta); Digest(pack); TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(1024); 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); TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); ReceivePack rp = new ReceivePack(dst); rp.SetCheckReceivedObjects(true); rp.SetCheckReferencedObjectsAreReachable(true); rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook()); try { Receive(rp, inBuf, outBuf); NUnit.Framework.Assert.Fail("Expected UnpackException"); } catch (UnpackException failed) { Exception err = failed.InnerException; NUnit.Framework.Assert.IsTrue(err is MissingObjectException); MissingObjectException moe = (MissingObjectException)err; NUnit.Framework.Assert.AreEqual(b, moe.GetObjectId()); } PacketLineIn r = AsPacketLineIn(outBuf); string master = r.ReadString(); int nul = master.IndexOf('\0'); NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list"); NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring (master, 0, nul)); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); NUnit.Framework.Assert.AreEqual("unpack error Missing blob " + b.Name, r.ReadString ()); NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString ()); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); }
public virtual void TestMaxObjectSizeFullBlob() { TestRepository d = new TestRepository(db); byte[] data = Constants.Encode("0123456789"); d.Blob(data); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_BLOB) << 4 | 10); Deflate(pack, data); Digest(pack); PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetMaxObjectSizeLimit(11); p.Parse(NullProgressMonitor.INSTANCE); p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetMaxObjectSizeLimit(10); p.Parse(NullProgressMonitor.INSTANCE); p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetMaxObjectSizeLimit(9); try { p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.Fail("PackParser should have failed"); } catch (TooLargeObjectInPackException e) { NUnit.Framework.Assert.IsTrue(e.Message.Contains("10")); // obj size NUnit.Framework.Assert.IsTrue(e.Message.Contains("9")); } }
public virtual void TestPackWithTrailingGarbage() { TestRepository d = new TestRepository(db); RevBlob a = d.Blob("a"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); Digest(pack); PackParser p = Index(new UnionInputStream(new ByteArrayInputStream(pack.ToByteArray ()), new ByteArrayInputStream(new byte[] { unchecked((int)(0x7e)) }))); p.SetAllowThin(true); p.SetCheckEofAfterPackFooter(true); try { p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.Fail("Pack with trailing garbage was accepted"); } catch (IOException err) { NUnit.Framework.Assert.AreEqual(MessageFormat.Format(JGitText.Get().expectedEOFReceived , "\\x7e"), err.Message); } }
public virtual void TestDataAfterPackFooterSingleRead() { TestRepository d = new TestRepository<FileRepository>(db); RevBlob a = d.Blob("a"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32 * 1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); Digest(pack); byte[] packData = pack.ToByteArray(); byte[] streamData = new byte[packData.Length + 1]; System.Array.Copy(packData, 0, streamData, 0, packData.Length); streamData[packData.Length] = unchecked((int)(0x7e)); InputStream @in = new ByteArrayInputStream(streamData); PackParser p = Index(@in); p.SetAllowThin(true); p.SetCheckEofAfterPackFooter(false); p.SetExpectDataAfterPackFooter(true); p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.AreEqual(unchecked((int)(0x7e)), @in.Read()); }
/// <exception cref="System.IO.IOException"></exception> private void Search() { // TODO(spearce) If the object is used as a base for other // objects in this pack we should limit the depth we create // for ourselves to be the remainder of our longest dependent // chain and the configured maximum depth. This can happen // when the dependents are being reused out a pack, but we // cannot be because we are near the edge of a thin pack. // resMaxDepth = maxDepth; // Loop through the window backwards, considering every entry. // This lets us look at the bigger objects that came before. // for (int srcSlot = Prior(resSlot); srcSlot != resSlot; srcSlot = Prior(srcSlot)) { DeltaWindowEntry src = window[srcSlot]; if (src.Empty()) { break; } if (Delta(src, srcSlot) == NEXT_RES) { bestDelta = null; return; } } // We couldn't find a suitable delta for this object, but it may // still be able to act as a base for another one. // if (bestDelta == null) { KeepInWindow(); return; } // Select this best matching delta as the base for the object. // ObjectToPack srcObj = window[bestSlot].@object; ObjectToPack resObj = res.@object; if (srcObj.IsEdge()) { // The source (the delta base) is an edge object outside of the // pack. Its part of the common base set that the peer already // has on hand, so we don't want to send it. We have to store // an ObjectId and *NOT* an ObjectToPack for the base to ensure // the base isn't included in the outgoing pack file. // resObj.SetDeltaBase(srcObj.Copy()); } else { // The base is part of the pack we are sending, so it should be // a direct pointer to the base. // resObj.SetDeltaBase(srcObj); } resObj.SetDeltaDepth(srcObj.GetDeltaDepth() + 1); resObj.ClearReuseAsIs(); CacheDelta(srcObj, resObj); // Discard the cached best result, otherwise it leaks. // bestDelta = null; // If this should be the end of a chain, don't keep // it in the window. Just move on to the next object. // if (resObj.GetDeltaDepth() == maxDepth) { return; } ShuffleBaseUpInPriority(); KeepInWindow(); }
public virtual void TestMaxObjectSizeDeltaBlock() { TestRepository d = new TestRepository<FileRepository>(db); RevBlob a = d.Blob("a"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 14); a.CopyRawTo(pack); Deflate(pack, new byte[] { 1, 11, 11, (byte)('a'), (byte)('0'), (byte)('1'), (byte )('2'), (byte)('3'), (byte)('4'), (byte)('5'), (byte)('6'), (byte)('7'), (byte)( '8'), (byte)('9') }); Digest(pack); PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(true); p.SetMaxObjectSizeLimit(14); p.Parse(NullProgressMonitor.INSTANCE); p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(true); p.SetMaxObjectSizeLimit(13); try { p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.Fail("PackParser should have failed"); } catch (TooLargeObjectInPackException e) { NUnit.Framework.Assert.IsTrue(e.Message.Contains("13")); // max obj size NUnit.Framework.Assert.IsFalse(e.Message.Contains("14")); } }
public virtual void TestNonMarkingInputStream() { TestRepository d = new TestRepository<FileRepository>(db); RevBlob a = d.Blob("a"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); Digest(pack); InputStream @in = new _ByteArrayInputStream_319(pack.ToByteArray()); PackParser p = Index(@in); p.SetAllowThin(true); p.SetCheckEofAfterPackFooter(false); p.SetExpectDataAfterPackFooter(true); try { p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.Fail("PackParser should have failed"); } catch (IOException e) { NUnit.Framework.Assert.AreEqual(e.Message, JGitText.Get().inputStreamMustSupportMark ); } }
public virtual void TestHeap() { TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024); byte[] r = new byte[8 * 1024]; b.Write(r); b.Write(r); try { b.Write(1); NUnit.Framework.Assert.Fail("accepted too many bytes of data"); } catch (IOException e) { NUnit.Framework.Assert.AreEqual("In-memory buffer limit exceeded", e.Message); } }
public virtual void TestInCoreInputStream() { int cnt = 256; byte[] test = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(cnt); TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4); b.Write(test); b.Close(); InputStream @in = b.OpenInputStream(); byte[] act = new byte[cnt]; IOUtil.ReadFully(@in, act, 0, cnt); for (int i = 0; i < test.Length; i ++) Assert.AreEqual (test[i], act[i]); }
/// <exception cref="System.IO.IOException"></exception> private void FmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen, FileMode mode) { if (buf != null) { overflowBuffer = new TemporaryBuffer.Heap(int.MaxValue); overflowBuffer.Write(buf, 0, ptr); buf = null; } mode.CopyTo(overflowBuffer); overflowBuffer.Write(unchecked((byte)' ')); overflowBuffer.Write(nameBuf, namePos, nameLen); overflowBuffer.Write(unchecked((byte)0)); }
private void CacheDelta(ObjectToPack srcObj, ObjectToPack resObj) { if (int.MaxValue < bestDelta.Length()) { return; } int rawsz = (int)bestDelta.Length(); if (deltaCache.CanCache(rawsz, srcObj, resObj)) { try { byte[] zbuf = new byte[DeflateBound(rawsz)]; DeltaWindow.ZipStream zs = new DeltaWindow.ZipStream(Deflater(), zbuf); bestDelta.WriteTo(zs, null); bestDelta = null; int len = zs.Finish(); resObj.SetCachedDelta(deltaCache.Cache(zbuf, len, rawsz)); resObj.SetCachedSize(rawsz); } catch (IOException) { deltaCache.Credit(rawsz); } catch (OutOfMemoryException) { deltaCache.Credit(rawsz); } } }
public virtual void TestDataAfterPackFooterSplitObjectRead() { byte[] data = Constants.Encode("0123456789"); // Build a pack ~17k int objects = 900; TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32 * 1024); PackHeader(pack, objects); for (int i = 0; i < objects; i++) { pack.Write((Constants.OBJ_BLOB) << 4 | 10); Deflate(pack, data); } Digest(pack); byte[] packData = pack.ToByteArray(); byte[] streamData = new byte[packData.Length + 1]; System.Array.Copy(packData, 0, streamData, 0, packData.Length); streamData[packData.Length] = unchecked((int)(0x7e)); InputStream @in = new ByteArrayInputStream(streamData); PackParser p = Index(@in); p.SetAllowThin(true); p.SetCheckEofAfterPackFooter(false); p.SetExpectDataAfterPackFooter(true); p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.AreEqual(unchecked((int)(0x7e)), @in.Read()); }
/// <exception cref="System.IO.IOException"></exception> private int Delta(DeltaWindowEntry src, int srcSlot) { // Objects must use only the same type as their delta base. // If we are looking at something where that isn't true we // have exhausted everything of the correct type and should // move on to the next thing to examine. // if (src.Type() != res.Type()) { KeepInWindow(); return NEXT_RES; } // Only consider a source with a short enough delta chain. if (src.Depth() > resMaxDepth) { return NEXT_SRC; } // Estimate a reasonable upper limit on delta size. int msz = DeltaSizeLimit(res, resMaxDepth, src); if (msz <= 8) { return NEXT_SRC; } // If we have to insert a lot to make this work, find another. if (res.Size() - src.Size() > msz) { return NEXT_SRC; } // If the sizes are radically different, this is a bad pairing. if (res.Size() < src.Size() / 16) { return NEXT_SRC; } DeltaIndex srcIndex; try { srcIndex = Index(src); } catch (LargeObjectException) { // If the source is too big to work on, skip it. DropFromWindow(srcSlot); return NEXT_SRC; } catch (IOException notAvailable) { if ([email protected]()) { // This is an edge that is suddenly not available. DropFromWindow(srcSlot); return NEXT_SRC; } else { throw; } } byte[] resBuf; try { resBuf = Buffer(res); } catch (LargeObjectException) { // If its too big, move on to another item. return NEXT_RES; } // If we already have a delta for the current object, abort // encoding early if this new pairing produces a larger delta. if (bestDelta != null && bestDelta.Length() < msz) { msz = (int)bestDelta.Length(); } TemporaryBuffer.Heap delta = new TemporaryBuffer.Heap(msz); try { if (!srcIndex.Encode(delta, resBuf, msz)) { return NEXT_SRC; } } catch (IOException) { // This only happens when the heap overflows our limit. return NEXT_SRC; } if (IsBetterDelta(src, delta)) { bestDelta = delta; bestSlot = srcSlot; } return NEXT_SRC; }
public virtual void TestDataAfterPackFooterSplitHeaderRead() { TestRepository d = new TestRepository<FileRepository>(db); byte[] data = Constants.Encode("a"); RevBlob b = d.Blob(data); int objects = 248; TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(32 * 1024); PackHeader(pack, objects + 1); int offset = 13; StringBuilder sb = new StringBuilder(); for (int i = 0; i < offset; i++) { sb.Append(i); } offset = sb.ToString().Length; int lenByte = (Constants.OBJ_BLOB) << 4 | (offset & unchecked((int)(0x0F))); offset >>= 4; if (offset > 0) { lenByte |= 1 << 7; } pack.Write(lenByte); while (offset > 0) { lenByte = offset & unchecked((int)(0x7F)); offset >>= 6; if (offset > 0) { lenByte |= 1 << 7; } pack.Write(lenByte); } Deflate(pack, Constants.Encode(sb.ToString())); for (int i_1 = 0; i_1 < objects; i_1++) { // The last pack header written falls across the 8192 byte boundary // between [8189:8210] pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); b.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); } Digest(pack); byte[] packData = pack.ToByteArray(); byte[] streamData = new byte[packData.Length + 1]; System.Array.Copy(packData, 0, streamData, 0, packData.Length); streamData[packData.Length] = unchecked((int)(0x7e)); InputStream @in = new ByteArrayInputStream(streamData); PackParser p = Index(@in); p.SetAllowThin(true); p.SetCheckEofAfterPackFooter(false); p.SetExpectDataAfterPackFooter(true); p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.AreEqual(unchecked((int)(0x7e)), @in.Read()); }
public virtual void TestMaxObjectSizeDeltaResultSize() { TestRepository d = new TestRepository(db); RevBlob a = d.Blob("0123456789"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { 10, 11, 1, (byte)('a') }); Digest(pack); PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(true); p.SetMaxObjectSizeLimit(11); p.Parse(NullProgressMonitor.INSTANCE); p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(true); p.SetMaxObjectSizeLimit(10); try { p.Parse(NullProgressMonitor.INSTANCE); NUnit.Framework.Assert.Fail("PackParser should have failed"); } catch (TooLargeObjectInPackException e) { NUnit.Framework.Assert.IsTrue(e.Message.Contains("11")); // result obj size NUnit.Framework.Assert.IsTrue(e.Message.Contains("10")); } }
public virtual void TestSuccess() { // 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[] { (byte)('a') }); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); Digest(pack); OpenPack(pack); // Verify the only storage of b is our packed delta above. // ObjectDirectory od = (ObjectDirectory)src.ObjectDatabase; NUnit.Framework.Assert.IsTrue(src.HasObject(b), "has b"); NUnit.Framework.Assert.IsFalse(od.FileFor(b).Exists(), "b not loose"); // Now use b but in a different commit than what is hidden. // TestRepository 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_210(this, src, UriOf(dst), dst.Directory); RemoteRefUpdate u = new RemoteRefUpdate(src, R_MASTER, R_MASTER, false, null, null ); // // // src name // dst name // do not force update // local tracking branch // expected id PushResult r; try { t.SetPushThin(true); r = t.Push(PM, Sharpen.Collections.Singleton(u)); } finally { t.Close(); } NUnit.Framework.Assert.IsNotNull(r, "have result"); NUnit.Framework.Assert.IsNull(r.GetAdvertisedRef(R_PRIVATE), "private not advertised" ); NUnit.Framework.Assert.AreEqual(RemoteRefUpdate.Status.OK, u.GetStatus(), "master updated" ); NUnit.Framework.Assert.AreEqual(N, dst.Resolve(R_MASTER)); }
public virtual void TestPackWithDuplicateBlob() { byte[] data = Constants.Encode("0123456789abcdefg"); TestRepository<Repository> d = new TestRepository<Repository>(db); NUnit.Framework.Assert.IsTrue(db.HasObject(d.Blob(data))); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_BLOB) << 4 | unchecked((int)(0x80)) | 1); pack.Write(1); Deflate(pack, data); Digest(pack); PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(false); p.Parse(NullProgressMonitor.INSTANCE); }
public virtual void TestCreateBranchAtHiddenCommitFails() { TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64); PackHeader(pack, 0); Digest(pack); TemporaryBuffer.Heap inBuf = new TemporaryBuffer.Heap(256); 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); TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024); ReceivePack rp = new ReceivePack(dst); rp.SetCheckReceivedObjects(true); rp.SetCheckReferencedObjectsAreReachable(true); rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook()); try { Receive(rp, inBuf, outBuf); NUnit.Framework.Assert.Fail("Expected UnpackException"); } catch (UnpackException failed) { Exception err = failed.InnerException; NUnit.Framework.Assert.IsTrue(err is MissingObjectException); MissingObjectException moe = (MissingObjectException)err; NUnit.Framework.Assert.AreEqual(P, moe.GetObjectId()); } PacketLineIn r = AsPacketLineIn(outBuf); string master = r.ReadString(); int nul = master.IndexOf('\0'); NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list"); NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring (master, 0, nul)); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); NUnit.Framework.Assert.AreEqual("unpack error Missing commit " + P.Name, r.ReadString ()); NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString ()); NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString()); }
public virtual void TestTinyThinPack() { TestRepository d = new TestRepository(db); RevBlob a = d.Blob("a"); TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024); PackHeader(pack, 1); pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4); a.CopyRawTo(pack); Deflate(pack, new byte[] { unchecked((int)(0x1)), unchecked((int)(0x1)), unchecked( (int)(0x1)), (byte)('b') }); Digest(pack); PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray())); p.SetAllowThin(true); p.Parse(NullProgressMonitor.INSTANCE); }
public virtual void TestInCoreInputStream() { int cnt = 256; byte[] test = new TestRng(Sharpen.Extensions.GetTestName()).NextBytes(cnt); TemporaryBuffer.Heap b = new TemporaryBuffer.Heap(cnt + 4); b.Write(test); b.Close(); InputStream @in = b.OpenInputStream(); byte[] act = new byte[cnt]; IOUtil.ReadFully(@in, act, 0, cnt); NUnit.Framework.Assert.IsTrue(Arrays.Equals(test, act)); }