Beispiel #1
0
        public void ReadsDictionaries()
        {
            var reader = ReaderTests.GetReader("d3:foo3:bar3:bari-5ee");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string key));
            Assert.Equal("foo", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string value));
            Assert.Equal("bar", value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out key));
            Assert.Equal("bar", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out int intValue));
            Assert.Equal(-5, intValue);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
Beispiel #2
0
        public void ReadsNestedDictionaryContent()
        {
            var reader = ReaderTests.GetReader("d3:food3:bari3eee");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string key));
            Assert.Equal("foo", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out key));
            Assert.Equal("bar", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out int intValue));
            Assert.Equal(3, intValue);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
Beispiel #3
0
        public void ReadsNestedListContent()
        {
            var reader = ReaderTests.GetReader("li1eli2ei22el5:Helloei4e6: worldei5ee");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out int value));
            Assert.Equal(1, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out value));
            Assert.Equal(2, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out value));
            Assert.Equal(22, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string stringValue));
            Assert.Equal("Hello", stringValue);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out value));
            Assert.Equal(4, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out stringValue));
            Assert.Equal(" world", stringValue);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out value));
            Assert.Equal(5, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
Beispiel #4
0
        public void ReadsStrings(string value)
        {
            var reader = ReaderTests.GetReader($"{Encoding.UTF8.GetByteCount(value)}:{value}");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string readValue));
            Assert.Equal(value, readValue);
        }
Beispiel #5
0
        public void ReadsEmptyDictionaries()
        {
            var reader = ReaderTests.GetReader("de");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
Beispiel #6
0
        public void ReadsEmptyList()
        {
            var reader = ReaderTests.GetReader("le");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
Beispiel #7
0
        public void ReadsNestedDictionariesAndLists()
        {
            var reader = ReaderTests.GetReader("d4: foold4:bar leeiee0:0:e");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string key));
            Assert.Equal(" foo", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartDictionary, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out key));
            Assert.Equal("bar ", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out int intValue));
            Assert.Equal(0, intValue);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out key));
            Assert.Equal("", key);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.String, reader.TokenType);
            Assert.True(reader.TryGet(out string value));
            Assert.Equal("", value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }
        private static Utf8BEncodingReader GetNumberReader(string number)
        {
            string bencoded = 'i' + number + 'e';

            var reader = ReaderTests.GetReader(bencoded);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.Equal(number, Encoding.UTF8.GetString(reader.ValueSpan));

            return(reader);
        }
Beispiel #9
0
        public void ReadsList()
        {
            var reader = ReaderTests.GetReader("li1ei-2ee");

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.StartList, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out int value));
            Assert.Equal(1, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.Integer, reader.TokenType);
            Assert.True(reader.TryGet(out value));
            Assert.Equal(-2, value);

            Assert.True(reader.Read());
            Assert.Equal(BEncodingTokenType.End, reader.TokenType);
        }