Пример #1
0
        public void TestDecodeDictionaryEmpty()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("depostscript");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDictionary, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.EndDictionary, d.Next());
            Assert.AreEqual(2, d.NextOffset);
        }
Пример #2
0
        public void TestDecodeDictionary()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("d3:foo3:barepostscript");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDictionary, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("foo"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("bar"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.EndDictionary, d.Next());
            Assert.AreEqual(12, d.NextOffset);
        }
Пример #3
0
        public void TestDecodeBytestringPrematureEof2()
        {
            byte[] encoded = Utils.Bs("12");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.Eof, d.Next());
        }
Пример #4
0
        public void TestDecodeBytestringSizeGarbage()
        {
            byte[] encoded = Utils.Bs("1%ab#2:interference");
            Bdecoder d = new Bdecoder(encoded);

            d.Next();
        }
Пример #5
0
        public void TestDecodeBytestring()
        {
            byte[] encoded = Utils.Bs("5:magicgarbage");
            byte[] expected = Utils.Bs("magic");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(expected, d.ByteStringValue);
            Assert.AreEqual(7, d.NextOffset);
        }
Пример #6
0
        public void TestDecodeDictionaryHalfOpen()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("d5:sushii85e");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDictionary, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("sushi"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.Integer, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.Eof, d.Next());
        }
Пример #7
0
        public void TestDecodeMetadataEmpty()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("me");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartMetadata, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.EndMetadata, d.Next());
            Assert.AreEqual(2, d.NextOffset);
        }
Пример #8
0
        public void TestDecodeMetadata()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("m4:data4:metae");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartMetadata, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("data"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("meta"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.EndMetadata, d.Next());
            Assert.AreEqual(14, d.NextOffset);
        }
Пример #9
0
        public void TestDecodeIntegerHalfopen()
        {
            byte[] encoded = Utils.Bs("i123");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.Eof, d.Next());
        }
Пример #10
0
        public void TestDecodeIntegerGarbage()
        {
            byte[] encoded = Utils.Bs("isawthesign");
            Bdecoder d = new Bdecoder(encoded);

            d.Next();
        }
Пример #11
0
        public void TestDecodeInteger()
        {
            byte[] encoded = Utils.Bs("i427effffff");
            int expected = 427;
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.Integer, d.Next());
            Assert.AreEqual(expected, d.IntegerValue);
            Assert.AreEqual(5, d.NextOffset);
        }
Пример #12
0
        public void TestDecodeDStringEmpty()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("S0:e");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDString, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs(""), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.EndDString, d.Next());
            Assert.AreEqual(4, d.NextOffset);
        }
Пример #13
0
        public void TestDecodeDRecordEmpty()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("Re");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDRecord, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.EndDRecord, d.Next());
            Assert.AreEqual(2, d.NextOffset);
        }
Пример #14
0
        public void TestDecodeDRecord()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("R4:safe6:secureeoffthe");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDRecord, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("safe"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.ByteString, d.Next());
            Utils.AssertBytewiseIdentical(Utils.Bs("secure"), d.ByteStringValue);

            Assert.AreEqual(Bdecoder.NodeType.EndDRecord, d.Next());
            Assert.AreEqual(16, d.NextOffset);
        }
Пример #15
0
        public void TestDecodeDNumber()
        {
            byte[] encoded = Encoding.ASCII.GetBytes("Nn123.45eecomplex");
            Bdecoder d = new Bdecoder(encoded);

            Assert.AreEqual(Bdecoder.NodeType.StartDNumber, d.Next());

            Assert.AreEqual(Bdecoder.NodeType.Number, d.Next());
            Assert.AreEqual(123.45m, d.NumberValue);

            Assert.AreEqual(Bdecoder.NodeType.EndDNumber, d.Next());
            Assert.AreEqual(10, d.NextOffset);
        }
Пример #16
0
        /// <see cref="IMessageFormat.Decode"/>
        public Message Decode(byte[] payload, out int bytesConsumed)
        {
            Bdecoder decoder = new Bdecoder(payload);
            Stack<DecodingFrame> frames = new Stack<DecodingFrame>();

            while (true)
            {
                var nodeType = decoder.Next();
                DecodingFrame top = frames.Count > 0 ? frames.Peek() : null;

                switch (nodeType)
                {
                    case Bdecoder.NodeType.ByteString:
                        top.InsertBytestring(decoder.ByteStringValue);
                        break;

                    case Bdecoder.NodeType.EndDictionary:
                    case Bdecoder.NodeType.EndDNumber:
                    case Bdecoder.NodeType.EndDRecord:
                    case Bdecoder.NodeType.EndDString:
                        DecodingFrame child = frames.Pop();
                        DecodingFrame parent = frames.Count > 0 ? frames.Peek() : null;

                        if (parent == null)
                        {
                            Debug.Assert(child is MessageToplevelFrame);

                            bytesConsumed = decoder.NextOffset;
                            return (Message)child.Realize();
                        }
                        else
                        {
                            parent.InsertObject((DObject)child.Realize());
                        }

                        break;

                    case Bdecoder.NodeType.EndList:
                        Debug.Fail("whaaaaaaaaat");
                        break;

                    case Bdecoder.NodeType.Integer:
                        top.InsertInteger(decoder.IntegerValue);
                        break;

                    case Bdecoder.NodeType.Number:
                        top.InsertNumber(decoder.NumberValue);
                        break;

                    case Bdecoder.NodeType.StartDictionary:
                        if (frames.Count == 0)
                        {
                            frames.Push(new MessageToplevelFrame());
                        }
                        else
                        {
                            Debug.Assert(false);
                            //frames.Push(new DRecordFrame());
                        }

                        break;

                    case Bdecoder.NodeType.StartList:
                        Debug.Fail("oh nooooo");
                        break;

                    case Bdecoder.NodeType.StartDNumber:
                        frames.Push(new DNumberFrame());
                        break;

                    case Bdecoder.NodeType.StartDRecord:
                        frames.Push(new DRecordFrame());
                        break;

                    case Bdecoder.NodeType.StartDString:
                        frames.Push(new DStringFrame());
                        break;

                    case Bdecoder.NodeType.Eof:
                        // expected a node here, but didn't get one. return "premature eof".
                        bytesConsumed = 0;
                        return null;
                }
            }
        }