Skip to content

Commit ac4f478

Browse files
committed
Fix and add tests for Pieces and PiecesAsHexString
1 parent bd7bd49 commit ac4f478

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

BencodeNET.Tests/Parsing/TorrentParserTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,19 @@ public void Info_PieceLength_IsParsed(long pieceSize)
173173

174174
[Theory]
175175
[AutoMockedData]
176-
public void Info_Pieces_IsParsed(string pieces)
176+
public void Info_Pieces_IsParsed(byte[] pieces)
177177
{
178178
// Arrange
179179
ParsedData = ValidSingleFileTorrentData;
180180
var info = ParsedData.Get<BDictionary>(TorrentFields.Info);
181-
info[TorrentInfoFields.Pieces] = (BString) pieces;
181+
info[TorrentInfoFields.Pieces] = new BString(pieces);
182182

183183
// Act
184184
var parser = new TorrentParser(BencodeParser);
185185
var torrent = parser.Parse((BencodeStream)null);
186186

187187
// Assert
188-
torrent.Pieces.Should().Be(pieces);
188+
torrent.Pieces.Should().Equal(pieces);
189189
}
190190

191191
[Theory]

BencodeNET.Tests/Torrents/TorrentTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,65 @@ public void DisplayName_UnknownFileMode_ThrowsBencodeException()
9393
act.Invoking(x => x()).ShouldThrow<BencodeException>();
9494
}
9595

96+
[Fact]
97+
public void PiecesAsHexString_Get_ReturnsHexUppercaseWithoutDashes()
98+
{
99+
var pieces = new byte[] { 66, 115, 135, 19, 149, 125, 229, 85, 68, 117, 252, 185, 243, 247, 139, 38, 11, 37, 60, 112 };
100+
var torrent = new Torrent
101+
{
102+
Pieces = pieces
103+
};
104+
105+
torrent.PiecesAsHexString.Should().Be("42738713957DE5554475FCB9F3F78B260B253C70");
106+
}
107+
108+
[Fact]
109+
public void PiecesAsHexString_Set_ValidUppercaseHexSetsPiecesProperty()
110+
{
111+
var pieces = new byte[] { 66, 115, 135, 19, 149, 125, 229, 85, 68, 117, 252, 185, 243, 247, 139, 38, 11, 37, 60, 112 };
112+
var torrent = new Torrent();
113+
114+
torrent.PiecesAsHexString = "42738713957DE5554475FCB9F3F78B260B253C70";
115+
116+
torrent.Pieces.Should().Equal(pieces);
117+
}
118+
119+
[Fact]
120+
public void PiecesAsHexString_Set_NullThrowsArgumentException()
121+
{
122+
var torrent = new Torrent();
123+
Action action = () => torrent.PiecesAsHexString = null;
124+
action.ShouldThrow<ArgumentException>();
125+
}
126+
127+
[Theory]
128+
[InlineAutoMockedData("qwer")]
129+
[InlineAutoMockedData("123456789012345678901234567890123456789")]
130+
[InlineAutoMockedData("12345678901234567890123456789012345678901")]
131+
public void PiecesAsHexString_Set_LengthNotMultipleOf40ShouldThrowArgumentException(string value)
132+
{
133+
var torrent = new Torrent();
134+
Action action = () => torrent.PiecesAsHexString = value;
135+
action.ShouldThrow<ArgumentException>();
136+
}
137+
138+
[Theory]
139+
[InlineAutoMockedData("a")]
140+
[InlineAutoMockedData("b")]
141+
[InlineAutoMockedData("c")]
142+
[InlineAutoMockedData("G")]
143+
[InlineAutoMockedData("H")]
144+
[InlineAutoMockedData("T")]
145+
[InlineAutoMockedData(".")]
146+
[InlineAutoMockedData("*")]
147+
[InlineAutoMockedData("[")]
148+
public void PiecesAsHexString_Set_ValueWithNonUppercaseHexCharacterShouldThrowArgumentException(string value)
149+
{
150+
var torrent = new Torrent();
151+
Action action = () => torrent.PiecesAsHexString = value;
152+
action.ShouldThrow<ArgumentException>();
153+
}
154+
96155
[Theory]
97156
[AutoMockedData]
98157
public void TotalSize_SingleFile_ShouldBeFileSize(long fileSize)

0 commit comments

Comments
 (0)