Exemple #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadSmartHeaders(InputStream @in, string service)
        {
            // A smart reply will have a '#' after the first 4 bytes, but
            // a dumb reply cannot contain a '#' until after byte 41. Do a
            // quick check to make sure its a smart reply before we parse
            // as a pkt-line stream.
            //
            byte[] magic = new byte[5];
            IOUtil.ReadFully(@in, magic, 0, magic.Length);
            if (magic[4] != '#')
            {
                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().expectedPktLineWithService
                                                                       , RawParseUtils.Decode(magic)));
            }
            PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(new ByteArrayInputStream
                                                                           (magic), @in));
            string exp = "# service=" + service;
            //$NON-NLS-1$
            string act = pckIn.ReadString();

            if (!exp.Equals(act))
            {
                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().expectedGot
                                                                       , exp, act));
            }
            while (pckIn.ReadString() != PacketLineIn.END)
            {
            }
        }
        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 TestUsingUnknownBlobFails()
        {
            // 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.
            //
            TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
            PackHeader(pack, 2);
            Copy(pack, src.Open(N));
            Copy(pack, src.Open(s.ParseBody(N).Tree));
            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(n, 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 " + n.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());
        }
Exemple #4
0
        /// <exception cref="System.IO.IOException"></exception>
        private void RecvWants()
        {
            bool isFirst = true;

            for (; ;)
            {
                string line;
                try
                {
                    line = pckIn.ReadString();
                }
                catch (EOFException eof)
                {
                    if (isFirst)
                    {
                        break;
                    }
                    throw;
                }
                if (line == PacketLineIn.END)
                {
                    break;
                }
                if (!line.StartsWith("want ") || line.Length < 45)
                {
                    throw new PackProtocolException(MessageFormat.Format(JGitText.Get().expectedGot,
                                                                         "want", line));
                }
                if (isFirst && line.Length > 45)
                {
                    string opt = Sharpen.Runtime.Substring(line, 45);
                    if (opt.StartsWith(" "))
                    {
                        opt = Sharpen.Runtime.Substring(opt, 1);
                    }
                    foreach (string c in opt.Split(" "))
                    {
                        options.AddItem(c);
                    }
                    line = Sharpen.Runtime.Substring(line, 0, 45);
                }
                wantIds.AddItem(ObjectId.FromString(Sharpen.Runtime.Substring(line, 5)));
                isFirst = false;
            }
        }
        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());
        }
Exemple #6
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadAdvertisedRefsImpl()
        {
            LinkedHashMap <string, Ref> avail = new LinkedHashMap <string, Ref>();

            for (; ;)
            {
                string line;
                try
                {
                    line = pckIn.ReadString();
                }
                catch (EOFException eof)
                {
                    if (avail.IsEmpty())
                    {
                        throw NoRepository();
                    }
                    throw;
                }
                if (line == PacketLineIn.END)
                {
                    break;
                }
                if (line.StartsWith("ERR "))
                {
                    // This is a customized remote service error.
                    // Users should be informed about it.
                    throw new RemoteRepositoryException(uri, Sharpen.Runtime.Substring(line, 4));
                }
                if (avail.IsEmpty())
                {
                    int nul = line.IndexOf('\0');
                    if (nul >= 0)
                    {
                        // The first line (if any) may contain "hidden"
                        // capability values after a NUL byte.
                        foreach (string c in Sharpen.Runtime.Substring(line, nul + 1).Split(" "))
                        {
                            remoteCapablities.AddItem(c);
                        }
                        line = Sharpen.Runtime.Substring(line, 0, nul);
                    }
                }
                string name = Sharpen.Runtime.Substring(line, 41, line.Length);
                if (avail.IsEmpty() && name.Equals("capabilities^{}"))
                {
                    // special line from git-receive-pack to show
                    // capabilities when there are no refs to advertise
                    continue;
                }
                ObjectId id = ObjectId.FromString(Sharpen.Runtime.Substring(line, 0, 40));
                if (name.Equals(".have"))
                {
                    additionalHaves.AddItem(id);
                }
                else
                {
                    if (name.EndsWith("^{}"))
                    {
                        name = Sharpen.Runtime.Substring(name, 0, name.Length - 3);
                        Ref prior = avail.Get(name);
                        if (prior == null)
                        {
                            throw new PackProtocolException(uri, MessageFormat.Format(JGitText.Get().advertisementCameBefore
                                                                                      , name, name));
                        }
                        if (prior.GetPeeledObjectId() != null)
                        {
                            throw DuplicateAdvertisement(name + "^{}");
                        }
                        avail.Put(name, new ObjectIdRef.PeeledTag(RefStorage.NETWORK, name, prior.GetObjectId
                                                                      (), id));
                    }
                    else
                    {
                        Ref prior = avail.Put(name, new ObjectIdRef.PeeledNonTag(RefStorage.NETWORK, name
                                                                                 , id));
                        if (prior != null)
                        {
                            throw DuplicateAdvertisement(name);
                        }
                    }
                }
            }
            Available(avail);
        }
Exemple #7
0
 public virtual void TestReadString1()
 {
     Init("0006a\n0007bc\n");
     NUnit.Framework.Assert.AreEqual("a", @in.ReadString());
     NUnit.Framework.Assert.AreEqual("bc", @in.ReadString());
     AssertEOF();
 }