Exemplo n.º 1
0
        public void TestEncodeList1()
        {
            //Test1
            ListNode list1   = new ListNode();
            string   source1 = BEncodingFactory.StringEncode(list1);

            Assert.AreEqual(source1, "le");

            //Test2
            ListNode list2 = new ListNode();

            for (int i = 1; i <= 3; i++)
            {
                list2.Add(i);
            }
            string source2 = BEncodingFactory.StringEncode(list2);

            Assert.AreEqual(source2, "li1ei2ei3ee");

            //Test3
            ListNode lh31 = new ListNode();

            lh31.Add("Alice");
            lh31.Add("Bob");
            ListNode lh32 = new ListNode();

            lh32.Add(2);
            lh32.Add(3);
            ListNode list3   = new ListNode(new List <BEncodedNode>(new BEncodedNode[] { lh31, lh32 }));
            string   source3 = BEncodingFactory.StringEncode(list3);

            Assert.AreEqual(source3, "ll5:Alice3:Bobeli2ei3eee");
        }
Exemplo n.º 2
0
        public void TestEncodeDictionary1()
        {
            //Test1
            DictNode dict1   = new DictNode();
            string   source1 = BEncodingFactory.StringEncode(dict1);

            Assert.AreEqual(source1, "de");

            //Test2
            DictNode dict2 = new DictNode();

            dict2.Add("age", 25);
            dict2.Add("eyes", "blue");
            string source2 = BEncodingFactory.StringEncode(dict2);

            Assert.AreEqual(source2, "d3:agei25e4:eyes4:bluee");


            //Test3
            DictNode dh31 = new DictNode();

            dh31.Add("author", "Alice");
            dh31.Add("length", 1048576);
            DictNode dict3 = new DictNode();

            dict3.Add("spam.mp3", dh31);
            string source3 = BEncodingFactory.StringEncode(dict3);

            Assert.AreEqual(source3, "d8:spam.mp3d6:author5:Alice6:lengthi1048576eee");
            Assert.AreEqual(dict3.ToString(), "d8:spam.mp3d6:author5:Alice6:lengthi1048576eee");
        }
Exemplo n.º 3
0
        public void TestEncodeByteArray1()
        {
            //Test1标点符号
            BytesNode bah1    = new BytesNode("~!@#$%^&*()_+|`-=\\{}:\"<>?[];',./");
            string    source1 = BEncodingFactory.StringEncode(bah1);

            Assert.AreEqual(source1, "32:~!@#$%^&*()_+|`-=\\{}:\"<>?[];',./");

            //Test2空字符
            BytesNode bah2    = new BytesNode("");
            string    source2 = BEncodingFactory.StringEncode(bah2);

            Assert.AreEqual(source2, "0:");

            //Test3英文字母与数字
            BytesNode bah3    = new BytesNode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            string    source3 = BEncodingFactory.StringEncode(bah3);

            Assert.AreEqual(source3, "62:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

            //Test4中文字体与全角标点符号
            BytesNode bah4    = new BytesNode("微软公司,广州大学");
            string    source4 = BEncodingFactory.StringEncode(bah4);

            Assert.AreEqual(source4, "27:微软公司,广州大学");

            //Test5全角的数字与英文字母
            BytesNode bah5    = new BytesNode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            string    source5 = BEncodingFactory.StringEncode(bah5);

            Assert.AreEqual(source5, "186:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
        }
Exemplo n.º 4
0
        public void TestDecodeByteArray1()
        {
            //Test1
            BytesNode bah1 = (BytesNode)BEncodingFactory.Decode("10:0123456789");

            Assert.AreEqual(bah1.ByteArray, _encoding.GetBytes("0123456789"));
            Assert.AreEqual(bah1.StringText, "0123456789");

            //Test2
            BytesNode bah2 = (BytesNode)BEncodingFactory.Decode("26:abcdefghijklmnopqrstuvwxyz");

            Assert.AreEqual(bah2.ByteArray, _encoding.GetBytes("abcdefghijklmnopqrstuvwxyz"));
            Assert.AreEqual(bah2.StringText, "abcdefghijklmnopqrstuvwxyz");

            //Test3
            BytesNode bah3 = (BytesNode)BEncodingFactory.Decode("186:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

            Assert.AreEqual(bah3.ByteArray, _encoding.GetBytes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
            Assert.AreEqual(bah3.StringText, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

            //Test4
            BytesNode bah4 = (BytesNode)BEncodingFactory.Decode("0:");

            Assert.AreEqual(bah4.ByteArray, _encoding.GetBytes(string.Empty));
            Assert.AreEqual(bah4.StringText, string.Empty);
        }
Exemplo n.º 5
0
        public void TestDecodeDictionary1()
        {
            //Test1整数
            DictNode dh1 = (DictNode)BEncodingFactory.Decode("d3:agei25ee");

            Assert.AreEqual(((IntNode)dh1["age"]).Value, 25);

            //Test2字节数组
            DictNode dh2 = (DictNode)BEncodingFactory.Decode("d3:agei25e5:color4:bluee");

            Assert.AreEqual(((IntNode)dh2["age"]).Value, 25);

            Assert.AreEqual((dh2["color"] as BytesNode).ByteArray, _encoding.GetBytes("blue"));
            Assert.AreEqual((dh2["color"] as BytesNode).StringText, "blue");

            //Test3字节数组与整数
            DictNode dh3        = (DictNode)BEncodingFactory.Decode("d8:spam.mp3d6:author5:Alice6:lengthi1048576eee");
            DictNode dHandler31 = (DictNode)dh3["spam.mp3"];

            Assert.AreEqual((dHandler31["author"] as BytesNode).ByteArray, _encoding.GetBytes("Alice"));
            Assert.AreEqual((dHandler31["author"] as BytesNode).StringText, "Alice");
            Assert.AreEqual(((IntNode)dHandler31["length"]).Value, 1048576);

            //Test4空字典
            DictNode dh4 = (DictNode)BEncodingFactory.Decode("de");

            Assert.AreEqual(dh4.Count, 0);
        }
Exemplo n.º 6
0
        public void TestEncodeInteger1()
        {
            //Test1测试用例为4
            IntNode ih1     = new IntNode(4);
            string  source1 = BEncodingFactory.StringEncode(ih1);

            Assert.AreEqual(source1, "i4e");
            //Assert.AreEqual(ih1.OutputBufferSize, 3);

            //Test2测试用例为1234567890
            IntNode ih2     = new IntNode(1234567890);
            string  source2 = BEncodingFactory.StringEncode(ih2);

            Assert.AreEqual(source2, "i1234567890e");

            //Test3测试用例为0
            IntNode ih3     = new IntNode(0);
            string  source3 = BEncodingFactory.StringEncode(ih3);

            Assert.AreEqual(source3, "i0e");

            //Test4测试用例为-10
            IntNode ih4     = new IntNode(-10);
            string  source4 = BEncodingFactory.StringEncode(ih4);

            Assert.AreEqual(source4, "i-10e");
        }
Exemplo n.º 7
0
        public void TestDecodeHandler1()
        {
            byte[]   source = File.ReadAllBytes(@"E:\Bittorrent\Torrents\winedt70.exe.torrent");
            DictNode dh     = (DictNode)BEncodingFactory.Decode(source);

            Assert.AreEqual("http://192.168.1.150:8080/announce", (dh["announce"] as BytesNode).StringText);
            Assert.AreEqual("http://192.168.1.150:8080/announce", _encoding.GetString((dh["announce"] as BytesNode).ByteArray));
        }
Exemplo n.º 8
0
        public void TestDecodeList1()
        {
            //Test1整数
            ListNode lh1 = (ListNode)BEncodingFactory.Decode("li0ei1ei2ee");

            Assert.AreEqual(((IntNode)lh1[0]).Value, 0);
            Assert.AreEqual(((IntNode)lh1[1]).Value, 1);
            Assert.AreEqual(((IntNode)lh1[2]).Value, 2);

            //Test2字节数组
            ListNode lh2 = (ListNode)BEncodingFactory.Decode("l3:abc2:xye");

            Assert.AreEqual((lh2[0] as BytesNode).ByteArray, _encoding.GetBytes("abc"));
            Assert.AreEqual((lh2[0] as BytesNode).StringText, "abc");

            Assert.AreEqual((lh2[1] as BytesNode).ByteArray, _encoding.GetBytes("xy"));
            Assert.AreEqual((lh2[1] as BytesNode).StringText, "xy");

            //Test3空字节数组
            ListNode lh3 = (ListNode)BEncodingFactory.Decode("l0:0:0:e");

            Assert.AreEqual((lh3[0] as BytesNode).ByteArray, _encoding.GetBytes(string.Empty));
            Assert.AreEqual((lh3[0] as BytesNode).StringText, string.Empty);

            Assert.AreEqual((lh3[1] as BytesNode).ByteArray, _encoding.GetBytes(string.Empty));
            Assert.AreEqual((lh3[1] as BytesNode).StringText, string.Empty);

            Assert.AreEqual((lh3[2] as BytesNode).ByteArray, _encoding.GetBytes(string.Empty));
            Assert.AreEqual((lh3[2] as BytesNode).StringText, string.Empty);

            //Test4字节数组与整数
            ListNode lh4        = (ListNode)BEncodingFactory.Decode("ll5:Alice3:Bobeli2ei3eee");
            ListNode lHandler40 = (ListNode)lh4[0];
            ListNode lHandler41 = (ListNode)lh4[1];

            Assert.AreEqual((lHandler40[0] as BytesNode).ByteArray, _encoding.GetBytes("Alice"));
            Assert.AreEqual((lHandler40[0] as BytesNode).StringText, "Alice");

            Assert.AreEqual((lHandler40[1] as BytesNode).ByteArray, _encoding.GetBytes("Bob"));
            Assert.AreEqual((lHandler40[1] as BytesNode).StringText, "Bob");

            Assert.AreEqual(((IntNode)lHandler41[0]).Value, 2);

            Assert.AreEqual(((IntNode)lHandler41[1]).Value, 3);

            //Test5空列表
            ListNode lh5 = (ListNode)BEncodingFactory.Decode("le");

            Assert.AreEqual(lh5.Count, 0);
        }
Exemplo n.º 9
0
        public void TestDecodeInteger1()
        {
            //Test1正整数
            IntNode ih1 = (IntNode)BEncodingFactory.Decode("i10e");

            Assert.AreEqual(ih1.Value, 10);

            //Test2零
            IntNode ih2 = (IntNode)BEncodingFactory.Decode("i0e");

            Assert.AreEqual(ih2.Value, 0);

            //Test3负整数
            IntNode ih3 = (IntNode)BEncodingFactory.Decode("i-55e");

            Assert.AreEqual(ih3.Value, -55);

            //Test4所有的数字
            IntNode ih4 = (IntNode)BEncodingFactory.Decode("i1234567890e");

            Assert.AreEqual(ih4.Value, 1234567890);
        }
Exemplo n.º 10
0
        public void TestEncodeHandler1()
        {
            FileStream sourceFile = File.OpenRead(@"E:\Bittorrent\Torrents\winedt70.exe.torrent");

            byte[] source = new byte[sourceFile.Length];
            sourceFile.Read(source, 0, (int)sourceFile.Length);
            sourceFile.Close();
            DictNode dh = (DictNode)BEncodingFactory.Decode(source);

            byte[]     destion    = BEncodingFactory.ByteArrayEncode(dh);
            FileStream targetFile = File.OpenWrite(@"E:\Bittorrent\Torrents\test.torrent");

            targetFile.Write(destion, 0, destion.Length);

            int i;

            for (i = 0; i < source.Length; i++)
            {
                Assert.AreEqual(source[i], destion[i]);
            }

            targetFile.Close();
        }
Exemplo n.º 11
0
 public void TestDecodeInteger2()
 {
     BEncodingFactory.Decode("ie");
 }
Exemplo n.º 12
0
 public void TestDecodeDictionary9()
 {
     BEncodingFactory.Decode("d01:x0:e");
 }
Exemplo n.º 13
0
 public void TestDecodeHandler3()
 {
     BEncodingFactory.Decode("35208734823ljdahflajhdf");
 }
Exemplo n.º 14
0
 public void TestDecodeDictionary7()
 {
     BEncodingFactory.Decode("d0:1:ae");
 }
Exemplo n.º 15
0
 public void TestDecodeDictionary8()
 {
     BEncodingFactory.Decode("d0:");
 }
Exemplo n.º 16
0
 public void TestDecodeDictionary5()
 {
     BEncodingFactory.Decode("d3:fooe");
 }
Exemplo n.º 17
0
 public void TestDecodeDictionary6()
 {
     BEncodingFactory.Decode("di1e0:e");
 }
Exemplo n.º 18
0
 public void TestDecodeByteArray3()
 {
     BEncodingFactory.Decode("02:ab");
 }
Exemplo n.º 19
0
 public void TestDecodeByteArray4()
 {
     BEncodingFactory.Decode("0:0:");
 }
Exemplo n.º 20
0
 public void TestDecodeByteArray5()
 {
     BEncodingFactory.Decode("9:abc");
 }
Exemplo n.º 21
0
 public void TestDecodeList2()
 {
     BEncodingFactory.Decode("lezeral");
 }
Exemplo n.º 22
0
 public void TestDecodeList5()
 {
     BEncodingFactory.Decode("l01:xe");
 }
Exemplo n.º 23
0
 public void TestDecodeList4()
 {
     BEncodingFactory.Decode("l0:");
 }
Exemplo n.º 24
0
 public void TestDecodeList3()
 {
     BEncodingFactory.Decode("l");
 }
Exemplo n.º 25
0
 public void TestDecodeHandler2()
 {
     BEncodingFactory.Decode("");
 }
Exemplo n.º 26
0
 public void TestDecodeByteArray2()
 {
     BEncodingFactory.Decode("2:abcedefg");
 }
Exemplo n.º 27
0
 public void TestDecodeDictionary2()
 {
     BEncodingFactory.Decode("d3:agei25e3:agei50ee");
 }
Exemplo n.º 28
0
 public void TestDecodeDictionary4()
 {
     BEncodingFactory.Decode("de0564adf");
 }
Exemplo n.º 29
0
 public void TestDecodeInteger3()
 {
     BEncodingFactory.Decode("i341foo382e");
 }
Exemplo n.º 30
0
 public void TestDecodeInteger6()
 {
     BEncodingFactory.Decode("i0345e");
 }