Пример #1
0
        public void BenNumberEncoding()
        {
            byte[]         data   = Encoding.UTF8.GetBytes("i12345e");
            BEncodedNumber number = 12345;

            Assert.True(Toolbox.ByteMatch(data, number.Encode()));
        }
Пример #2
0
        public void BenStringEncoding2()
        {
            byte[] data = Encoding.UTF8.GetBytes("0:");

            BEncodedString benString = new BEncodedString("");

            Assert.True(Toolbox.ByteMatch(data, benString.Encode()));
        }
Пример #3
0
        public void BenStringEncoding()
        {
            byte[] data = Encoding.UTF8.GetBytes("22:this is my test string");

            BEncodedString benString = new BEncodedString("this is my test string");

            Assert.True(Toolbox.ByteMatch(data, benString.Encode()));
        }
Пример #4
0
        public void BenNumberEncoding5()
        {
            byte[]         data   = Encoding.UTF8.GetBytes("i-123e");
            BEncodedNumber number = -123;

            Assert.Equal(6, number.LengthInBytes());
            Assert.True(Toolbox.ByteMatch(data, number.Encode()));
        }
Пример #5
0
        public void FromCompactNode()
        {
            byte[] buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 21, 131, 3, 1, 255 };
            Node   n      = Node.FromCompactNode(buffer, 0);

            Assert.True(Toolbox.ByteMatch(buffer, 0, n.Id.Bytes, 0, 20));
            Assert.Equal(IPAddress.Parse("1.21.131.3"), n.EndPoint.Address);
            Assert.Equal(511, n.EndPoint.Port);
        }
Пример #6
0
        public void BenNumberEncodingBuffered()
        {
            byte[]         data   = Encoding.UTF8.GetBytes("i12345e");
            BEncodedNumber number = 12345;

            byte[] result = new byte[number.LengthInBytes()];
            number.Encode(result, 0);
            Assert.True(Toolbox.ByteMatch(data, result));
        }
Пример #7
0
        public void BenStringEncodingBuffered()
        {
            byte[] data = Encoding.UTF8.GetBytes("22:this is my test string");

            BEncodedString benString = new BEncodedString("this is my test string");

            byte[] result = new byte[benString.LengthInBytes()];
            benString.Encode(result, 0);
            Assert.True(Toolbox.ByteMatch(data, result));
        }
Пример #8
0
        public void BenListEncoding()
        {
            byte[]       data = Encoding.UTF8.GetBytes("l4:test5:tests6:testede");
            BEncodedList list = new BEncodedList();

            list.Add(new BEncodedString("test"));
            list.Add(new BEncodedString("tests"));
            list.Add(new BEncodedString("tested"));

            Assert.True(Toolbox.ByteMatch(data, list.Encode()));
        }
Пример #9
0
        public void BenListEncodingBuffered()
        {
            byte[]       data = Encoding.UTF8.GetBytes("l4:test5:tests6:testede");
            BEncodedList list = new BEncodedList();

            list.Add(new BEncodedString("test"));
            list.Add(new BEncodedString("tests"));
            list.Add(new BEncodedString("tested"));
            byte[] result = new byte[list.LengthInBytes()];
            list.Encode(result, 0);
            Assert.True(Toolbox.ByteMatch(data, result));
        }
Пример #10
0
        public void CompactNode()
        {
            Node           n    = new Node(NodeId.Create(), new IPEndPoint(IPAddress.Parse("1.21.121.3"), 511));
            BEncodedString port = n.CompactNode();

            Assert.True(Toolbox.ByteMatch(n.Id.Bytes, 0, port.TextBytes, 0, 20), "#A");
            Assert.Equal(1, port.TextBytes[20]);
            Assert.Equal(21, port.TextBytes[21]);
            Assert.Equal(121, port.TextBytes[22]);
            Assert.Equal(3, port.TextBytes[23]);
            Assert.Equal(1, port.TextBytes[24]);
            Assert.Equal(255, port.TextBytes[25]);
        }
Пример #11
0
        public void BenDictionaryEncodingBuffered()
        {
            byte[]             data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");
            BEncodedDictionary dict = new BEncodedDictionary();
            BEncodedList       list = new BEncodedList();

            list.Add(new BEncodedString("a"));
            list.Add(new BEncodedString("b"));
            dict.Add("spam", list);
            byte[] result = new byte[dict.LengthInBytes()];
            dict.Encode(result, 0);
            Assert.True(Toolbox.ByteMatch(data, result));
        }
Пример #12
0
        public void BenDictionaryEncoding()
        {
            byte[] data = Encoding.UTF8.GetBytes("d4:spaml1:a1:bee");

            BEncodedDictionary dict = new BEncodedDictionary();
            BEncodedList       list = new BEncodedList();

            list.Add(new BEncodedString("a"));
            list.Add(new BEncodedString("b"));
            dict.Add("spam", list);
            Assert.Equal(Encoding.UTF8.GetString(data), Encoding.UTF8.GetString(dict.Encode()));
            Assert.True(Toolbox.ByteMatch(data, dict.Encode()));
        }