コード例 #1
0
        public static MessageType TryParseMessage(byte[] messageBytes, out BDictionary message)
        {
            if (BObject.TryParse(messageBytes, out BObject bObject))
            {
                if (bObject is BDictionary dictionary)
                {
                    message = dictionary;
                    if (message.TryGetValue("y", out BString type))
                    {
                        if (!message.ContainsKey <BString>("t"))
                        {
                            return(MessageType.Invalid);
                        }

                        switch (type.Text)
                        {
                        case "q": return(MessageType.Query);

                        case "r": return(MessageType.Response);

                        case "e": return(MessageType.Error);

                        default: return(MessageType.Unknown);
                        }
                    }
                }
            }

            message = null;
            return(MessageType.Invalid);
        }
コード例 #2
0
ファイル: BDictionaryTests.cs プロジェクト: lecaotri/Torrent
 public void BDictionary_EnforcedSorting_Unsorted()
 {
     // { "b", 1 }, { "a", 2 }
     byte[] encoded = Encoding.UTF8.GetBytes("d1:bi1e1:ai2ee");
     BDictionary.EnforceDictionarySorting = true;
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
     BDictionary.EnforceDictionarySorting = false;
 }
コード例 #3
0
ファイル: BDictionaryTests.cs プロジェクト: lecaotri/Torrent
        private void TestBDictionary(Dictionary <BString, BObject> testDictionary)
        {
            BDictionary bDictionary = new BDictionary(testDictionary);

            byte[] encoded = bDictionary.Encode();

            Assert.True(BObject.TryParse(encoded, out BDictionary bObject));
            Assert.True(bDictionary.Equals(bObject));

            using (MemoryStream ms = new MemoryStream())
            {
                bDictionary.Encode(ms);
                encoded = ms.ToArray();
            }

            Assert.True(BObject.TryParse(encoded, out bObject));
            Assert.True(bDictionary.Equals(bObject));

            Assert.Equal(encoded.Length, bDictionary.GetEncodedSize());
        }
コード例 #4
0
ファイル: BListTests.cs プロジェクト: lecaotri/Torrent
        private void TestBList(List <BObject> testList)
        {
            BList bList = new BList(testList);

            byte[] encoded = bList.Encode();

            Assert.True(BObject.TryParse(encoded, out BList bObject));
            Assert.True(bList.Equals(bObject));

            using (MemoryStream ms = new MemoryStream())
            {
                bList.Encode(ms);
                encoded = ms.ToArray();
            }

            Assert.True(BObject.TryParse(encoded, out bObject));
            Assert.True(bList.Equals(bObject));

            Assert.Equal(encoded.Length, bList.GetEncodedSize());
        }
コード例 #5
0
        private void TestBInteger(long testInteger)
        {
            BInteger bInteger = new BInteger(testInteger);

            byte[] encoded = bInteger.Encode();

            Assert.True(BObject.TryParse(encoded, out BInteger bObject));
            Assert.Equal(testInteger, bObject.Value);

            using (MemoryStream ms = new MemoryStream())
            {
                bInteger.Encode(ms);
                encoded = ms.ToArray();
            }

            Assert.True(BObject.TryParse(encoded, out bObject));
            Assert.Equal(testInteger, bObject.Value);

            Assert.Equal(encoded.Length, bInteger.EncodedSize);
        }
コード例 #6
0
ファイル: BDictionaryTests.cs プロジェクト: lecaotri/Torrent
 public void BDictionary_MissingTerminator()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("d4:testi1e");
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
 }
コード例 #7
0
ファイル: BListTests.cs プロジェクト: lecaotri/Torrent
 public void BList_MissingTerminator()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("li1e");
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
 }
コード例 #8
0
 public void BInteger_MisplacedNegative()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("i123-4e");
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
 }
コード例 #9
0
 public void BInteger_MultipleNegative()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("i--1e");
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
 }
コード例 #10
0
 public void BInteger_NegativeZero()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("i-0e");
     Assert.False(BObject.TryParse(encoded, out BObject bObject));
 }
コード例 #11
0
 public void BInteger_EmptyIsValidAndZero()
 {
     byte[] encoded = Encoding.UTF8.GetBytes("ie");
     Assert.True(BObject.TryParse(encoded, out BInteger bObject));
     Assert.Equal(0L, bObject.Value);
 }