forked from Krusen/BencodeNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBStringParserTests.cs
More file actions
190 lines (170 loc) · 6.65 KB
/
Copy pathBStringParserTests.cs
File metadata and controls
190 lines (170 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Text;
using BencodeNET.Exceptions;
using BencodeNET.Objects;
using BencodeNET.Parsing;
using FluentAssertions;
using Xunit;
namespace BencodeNET.Tests.Parsing
{
public partial class BStringParserTests
{
private BStringParser Parser { get; }
public BStringParserTests()
{
Parser = new BStringParser();
}
[Theory]
[InlineData("4:spam")]
[InlineData("8:spameggs")]
[InlineData("9:spam eggs")]
[InlineData("9:spam:eggs")]
[InlineData("14:!@#¤%&/()=?$|")]
public void CanParseSimple(string bencode)
{
var parts = bencode.Split(new[] {':'}, 2);
var length = int.Parse(parts[0]);
var value = parts[1];
var bstring = Parser.ParseString(bencode);
bstring.Length.Should().Be(length);
bstring.Should().Be(value);
}
[Fact]
public void CanParse_EmptyString()
{
var bstring = Parser.ParseString("0:");
bstring.Length.Should().Be(0);
bstring.Should().Be("");
}
[Theory]
[InlineData("5:spam", 4)]
[InlineData("6:spam", 4)]
[InlineData("100:spam", 4)]
public void LessCharsThanSpecified_ThrowsInvalidBencodeException(string bencode, int expectedReadBytes)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage($"*but could only read {expectedReadBytes} bytes*")
.Which.StreamPosition.Should().Be(0);
}
[Theory]
[InlineData("4spam", 1)]
[InlineData("10spam", 2)]
[InlineData("4-spam", 1)]
[InlineData("4.spam", 1)]
[InlineData("4;spam", 1)]
[InlineData("4,spam", 1)]
[InlineData("4|spam", 1)]
public void MissingDelimiter_ThrowsInvalidBencodeException(string bencode, int errorIndex)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage("*Unexpected character. Expected ':'*")
.Which.StreamPosition.Should().Be(errorIndex);
}
[Theory]
[InlineData("spam")]
[InlineData("-spam")]
[InlineData(".spam")]
[InlineData(",spam")]
[InlineData(";spam")]
[InlineData("?spam")]
[InlineData("!spam")]
[InlineData("#spam")]
public void NonDigitFirstChar_ThrowsInvalidBencodeException(string bencode)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage($"*Unexpected character. Expected ':' but found '{bencode[0]}'*")
.Which.StreamPosition.Should().Be(0);
}
[Theory]
[InlineData("0")]
[InlineData("4")]
public void LessThanMinimumLength2_ThrowsInvalidBencodeException(string bencode)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage("*Invalid length*")
.Which.StreamPosition.Should().Be(0);
}
[Theory]
[InlineData("12345678901:spam")]
[InlineData("123456789012:spam")]
[InlineData("1234567890123:spam")]
[InlineData("12345678901234:spam")]
public void LengthAboveMaxDigits10_ThrowsUnsupportedException(string bencode)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<UnsupportedBencodeException<BString>>()
.WithMessage("*Length of string is more than * digits*")
.Which.StreamPosition.Should().Be(0);
}
[Theory]
[InlineData("1:spam")]
[InlineData("12:spam")]
[InlineData("123:spam")]
[InlineData("1234:spam")]
[InlineData("12345:spam")]
[InlineData("123456:spam")]
[InlineData("1234567:spam")]
[InlineData("12345678:spam")]
[InlineData("123456789:spam")]
[InlineData("1234567890:spam")]
public void LengthAtOrBelowMaxDigits10_DoesNotThrowUnsupportedException(string bencode)
{
Action action = () => Parser.ParseString(bencode);
action.Should().NotThrow<UnsupportedBencodeException<BString>>();
}
[Fact]
public void LengthAboveInt32MaxValue_ThrowsUnsupportedException()
{
var bencode = "2147483648:spam";
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<UnsupportedBencodeException<BString>>()
.WithMessage("*Length of string is * but maximum supported length is *")
.Which.StreamPosition.Should().Be(0);
}
[Fact]
public void LengthBelowInt32MaxValue_DoesNotThrowUnsupportedException()
{
var bencode = "2147483647:spam";
Action action = () => Parser.ParseString(bencode);
action.Should().NotThrow<UnsupportedBencodeException<BString>>();
}
[Fact]
public void CanParseEncodedAsLatin1()
{
var encoding = Encoding.GetEncoding("LATIN1");
var expected = new BString("æøå", encoding);
var parser = new BStringParser(encoding);
// "3:æøå"
var bytes = new byte[] {51, 58, 230, 248, 229};
var bstring = parser.Parse(bytes);
bstring.Should().Be(expected);
}
[Theory]
[InlineData("1-:a", 1)]
[InlineData("1abc:a", 1)]
[InlineData("123?:asdf", 3)]
[InlineData("3abc:abc", 1)]
public void InvalidLengthString_ThrowsInvalidException(string bencode, int errorIndex)
{
Action action = () => Parser.ParseString(bencode);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage("*Unexpected character. Expected ':'*")
.Which.StreamPosition.Should().Be(errorIndex);
}
[Theory]
[InlineData("")]
[InlineData("0")]
public void BelowMinimumLength_WhenStreamWithoutLengthSupport_ThrowsInvalidException(string bencode)
{
var stream = new LengthNotSupportedStream(bencode);
Action action = () => Parser.Parse(stream);
action.Should().Throw<InvalidBencodeException<BString>>()
.WithMessage("*Unexpected character. Expected ':' but reached end of stream*")
.Which.StreamPosition.Should().Be(0);
}
}
}