// 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 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); NUnit.Framework.Assert.IsTrue(Arrays.Equals(test, act)); }
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]); } }
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 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 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()); }
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()); }
// 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 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 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 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)); }
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 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 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 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 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 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); }