예제 #1
0
        public void Should_return_Null_for_Null_Object()
        {
            object test = null;

            var actual = BencodeConvert.Serialize(test);

            actual.Should().BeNull();
        }
예제 #2
0
        public static string GetSHA1Hash(this Info info)
        {
            var bencoded = BencodeConvert.Serialize(info);

            using (var sha1 = new SHA1Managed())
            {
                var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(bencoded));
                return(Encoding.UTF8.GetString(hash));
            }
        }
예제 #3
0
        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            IServiceProvider serviceProvider = context.HttpContext.RequestServices;
            var logger = serviceProvider.GetService(typeof(ILogger <BencodeOutputFormatter>)) as ILogger;

            var response = context.HttpContext.Response;

            var bencoded = BencodeConvert.Serialize(context.Object);

            await response.WriteAsync(bencoded, selectedEncoding);
        }
예제 #4
0
        public void Should_respect_BencodeNameAttribute()
        {
            var baz = new Baz()
            {
                FirstName = "abc"
            };
            var expected = "d10:First Name3:abce";

            var actual = BencodeConvert.Serialize(baz);

            actual.Should().Be(expected);
        }
예제 #5
0
        public void Should_convert_Class_with_primitive_fields()
        {
            var dummyDate = new DateTime(2002, 10, 15);
            var test      = new Foo()
            {
                DateTime = dummyDate, Integer = 10, Long = 100000000000, String = "bar"
            };
            var dummyDateFromEpoch = (new DateTimeOffset(dummyDate)).ToUnixTimeSeconds();

            var expectedDateTime = $"8:datetime10:{dummyDateFromEpoch}";
            var expectedInteger  = "7:integeri10e";
            var expectedLong     = "4:longi100000000000e";
            var expectedString   = "6:string3:bar";

            string actual = BencodeConvert.Serialize(test);

            actual.Should().ContainAll(expectedDateTime, expectedInteger, expectedLong, expectedString);
            actual.Should().StartWith("d");
            actual.Should().EndWith("e");
        }
예제 #6
0
        public void Should_convert_Class_with_complex_fields()
        {
            var dummyDate = new DateTime(2002, 10, 15);
            var foo       = new Foo()
            {
                DateTime = dummyDate, Integer = 10, Long = 100000000000, String = "bar"
            };
            var bar = new Bar()
            {
                BarIntegers = new List <int> {
                    20, 30
                }, Foo = foo, BarStrings = new List <string> {
                    "s1", "s2"
                }
            };
            var dummyDateFromEpoch = (new DateTimeOffset(dummyDate)).ToUnixTimeSeconds();

            var expectedFooDateTime   = $"8:datetime10:{dummyDateFromEpoch}";
            var expectedFooInteger    = "7:integeri10e";
            var expectedFooLong       = "4:longi100000000000e";
            var expectedFooString     = "6:string3:bar";
            var expectedBarIntegers   = "11:barintegersli20ei30ee";
            var expectedBarStrings    = "10:barstringsl2:s12:s2e";
            var expectedFooDictionary = "3:food";

            string actual = BencodeConvert.Serialize(bar);

            actual.Should().ContainAll(
                expectedFooDateTime,
                expectedFooInteger,
                expectedFooLong,
                expectedFooString,
                expectedBarIntegers,
                expectedBarStrings,
                expectedFooDictionary);
            actual.Should().StartWith("d");
            actual.Should().EndWith("e");
        }
예제 #7
0
        public void Should_Bencode_MultipleFileMetaInfo_with_required_fields()
        {
            var expectedInfo        = "4:infod";
            var expectedPieceLength = "12:piece lengthi";
            var expectedPieces      = "6:pieces";
            var expectedName        = "4:name0:";
            var expectedLength      = "6:lengthi";
            var expectedAnnounce    = "8:announce";
            var expectedPath        = "4:path";

            var actual = BencodeConvert.Serialize(multipleFileMetainfo);

            actual.Should().ContainAll(
                expectedInfo,
                expectedPieceLength,
                expectedPieces,
                expectedName,
                expectedLength,
                expectedAnnounce,
                expectedPath);

            actual.Should().StartWith("d");
            actual.Should().EndWith("e");
        }