public virtual void TestMaxObjectSizeDeltaResultSize()
        {
            TestRepository d = new TestRepository <FileRepository>(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 TestMaxObjectSizeFullBlob()
        {
            TestRepository d = new TestRepository <FileRepository>(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"));
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        private void Digest(TemporaryBuffer.Heap buf)
        {
            MessageDigest md = Constants.NewMessageDigest();

            md.Update(buf.ToByteArray());
            buf.Write(md.Digest());
        }
        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());
        }
        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 TestPackWithTrailingGarbage()
        {
            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);
            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);
            }
        }
Exemple #9
0
        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());
            CollectionAssert.AreEquivalent(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();
        }
        /// <exception cref="System.IO.IOException"></exception>
        private void OpenPack(TemporaryBuffer.Heap buf)
        {
            if (inserter == null)
            {
                inserter = src.NewObjectInserter();
            }
            byte[]     raw = buf.ToByteArray();
            PackParser p   = inserter.NewPackParser(new ByteArrayInputStream(raw));

            p.SetAllowThin(true);
            p.Parse(PM);
        }
        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 TestTinyThinPack()
        {
            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);
            PackParser p = Index(new ByteArrayInputStream(pack.ToByteArray()));

            p.SetAllowThin(true);
            p.Parse(NullProgressMonitor.INSTANCE);
        }
Exemple #13
0
 /// <summary>Copy this formatter's buffer into a byte array.</summary>
 /// <remarks>
 /// Copy this formatter's buffer into a byte array.
 /// This method is not efficient, as it needs to create a copy of the
 /// internal buffer in order to supply an array of the correct size to the
 /// caller. If the buffer is just to pass to an ObjectInserter, consider
 /// using
 /// <see cref="ObjectInserter.Insert(TreeFormatter)">ObjectInserter.Insert(TreeFormatter)
 ///     </see>
 /// instead.
 /// </remarks>
 /// <returns>a copy of this formatter's buffer.</returns>
 public virtual byte[] ToByteArray()
 {
     if (buf != null)
     {
         byte[] r = new byte[ptr];
         System.Array.Copy(buf, 0, r, 0, ptr);
         return(r);
     }
     try
     {
         return(overflowBuffer.ToByteArray());
     }
     catch (IOException err)
     {
         // This should never happen, its read failure on a byte array.
         throw new RuntimeException(err);
     }
 }
Exemple #14
0
        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);
            byte[]    raw = pack.ToByteArray();
            IndexPack ip  = IndexPack.Create(db, new ByteArrayInputStream(raw));

            ip.SetFixThin(true);
            ip.Index(NullProgressMonitor.INSTANCE);
            ip.RenameAndOpenPack();
        }
 /// <exception cref="System.IO.IOException"></exception>
 private static PacketLineIn AsPacketLineIn(TemporaryBuffer.Heap buf)
 {
     return(new PacketLineIn(new ByteArrayInputStream(buf.ToByteArray())));
 }
 /// <exception cref="System.IO.IOException"></exception>
 private void Receive(ReceivePack rp, TemporaryBuffer.Heap inBuf, TemporaryBuffer.Heap
                      outBuf)
 {
     rp.Receive(new ByteArrayInputStream(inBuf.ToByteArray()), outBuf, null);
 }