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());
        }
 /// <exception cref="System.IO.IOException"></exception>
 private void PackHeader(TemporaryBuffer.Heap tinyPack, int cnt)
 {
     byte[] hdr = new byte[8];
     NB.EncodeInt32(hdr, 0, 2);
     NB.EncodeInt32(hdr, 4, cnt);
     tinyPack.Write(Constants.PACK_SIGNATURE);
     tinyPack.Write(hdr, 0, 8);
 }
        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 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 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"));
            }
        }
        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);
            }
        }
        /// <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 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 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());
        }
Example #13
0
 /// <summary>Append any entry to the tree.</summary>
 /// <remarks>Append any entry to the tree.</remarks>
 /// <param name="nameBuf">
 /// buffer holding the name of the entry. The name should be UTF-8
 /// encoded, but file name encoding is not a well defined concept
 /// in Git.
 /// </param>
 /// <param name="namePos">
 /// first position within
 /// <code>nameBuf</code>
 /// of the name data.
 /// </param>
 /// <param name="nameLen">
 /// number of bytes from
 /// <code>nameBuf</code>
 /// to use as the name.
 /// </param>
 /// <param name="mode">
 /// mode describing the treatment of
 /// <code>id</code>
 /// .
 /// </param>
 /// <param name="idBuf">buffer holding the raw ObjectId of the entry.</param>
 /// <param name="idPos">
 /// first position within
 /// <code>idBuf</code>
 /// to copy the id from.
 /// </param>
 public virtual void Append(byte[] nameBuf, int namePos, int nameLen, FileMode mode
                            , byte[] idBuf, int idPos)
 {
     if (FmtBuf(nameBuf, namePos, nameLen, mode))
     {
         System.Array.Copy(idBuf, idPos, buf, ptr, Constants.OBJECT_ID_LENGTH);
         ptr += Constants.OBJECT_ID_LENGTH;
     }
     else
     {
         try
         {
             FmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
             overflowBuffer.Write(idBuf, idPos, Constants.OBJECT_ID_LENGTH);
         }
         catch (IOException badBuffer)
         {
             // This should never occur.
             throw new RuntimeException(badBuffer);
         }
     }
 }
Example #14
0
 /// <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));
 }
        /// <exception cref="System.IO.IOException"></exception>
        private void Deflate(TemporaryBuffer.Heap tinyPack, byte[] content)
        {
            Deflater deflater = new Deflater();

            byte[] buf = new byte[128];
            deflater.SetInput(content, 0, content.Length);
            deflater.Finish();
            do
            {
                int n = deflater.Deflate(buf, 0, buf.Length);
                if (n > 0)
                {
                    tinyPack.Write(buf, 0, n);
                }
            }while (!deflater.IsFinished);
        }
        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);
        }
Example #17
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ObjectHeader(TemporaryBuffer.Heap pack, int type, int sz)
        {
            byte[] buf        = new byte[8];
            int    nextLength = (int)(((uint)sz) >> 4);

            buf[0] = unchecked ((byte)((nextLength > 0 ? unchecked ((int)(0x80)) : unchecked ((int
                                                                                               )(0x00))) | (type << 4) | (sz & unchecked ((int)(0x0F)))));
            sz = nextLength;
            int n = 1;

            while (sz > 0)
            {
                nextLength = (int)(((uint)nextLength) >> 7);
                buf[n++]   = unchecked ((byte)((nextLength > 0 ? unchecked ((int)(0x80)) : unchecked (
                                                    (int)(0x00))) | (sz & unchecked ((int)(0x7F)))));
                sz = nextLength;
            }
            pack.Write(buf, 0, n);
        }
Example #18
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 void Copy(TemporaryBuffer.Heap tinyPack, ObjectLoader ldr)
        {
            byte[] buf        = new byte[64];
            byte[] content    = ldr.GetCachedBytes();
            int    dataLength = content.Length;
            int    nextLength = (int)(((uint)dataLength) >> 4);
            int    size       = 0;

            buf[size++] = unchecked ((byte)((nextLength > 0 ? unchecked ((int)(0x80)) : unchecked (
                                                 (int)(0x00))) | (ldr.GetType() << 4) | (dataLength & unchecked ((int)(0x0F)))));
            dataLength = nextLength;
            while (dataLength > 0)
            {
                nextLength  = (int)(((uint)nextLength) >> 7);
                buf[size++] = unchecked ((byte)((nextLength > 0 ? unchecked ((int)(0x80)) : unchecked (
                                                     (int)(0x00))) | (dataLength & unchecked ((int)(0x7F)))));
                dataLength = nextLength;
            }
            tinyPack.Write(buf, 0, size);
            Deflate(tinyPack, content);
        }