Skip to content

Commit

Permalink
unit tests for PeerWirePacket, plus a small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed Oct 17, 2023
1 parent 68d179d commit 0064f8c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 11 deletions.
106 changes: 106 additions & 0 deletions bzTorrent.Tests/Data/PeerWirePacketTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using FluentAssertions;
using System;
using System.Diagnostics;
using bzTorrent.Helpers;
using Xunit;
using bzTorrent.Data;

namespace bzTorrent.Tests.Data
{
public class PeerWirePacketTests
{
[Fact]
public void KeepAlivePacketHasZeroCommandLength()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.KeepAlive);
pwp.CommandLength.Should().Be(0);
pwp.PacketByteLength.Should().Be(4);
}

[Fact]
public void HavePacketHas5ByteCommandLength()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 5, 4, 0, 0, 0, 1 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.Have);
pwp.CommandLength.Should().Be(5);
pwp.PacketByteLength.Should().Be(9);
}

[Fact]
public void ParseShouldReturnFalseIfNotEnoughData()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 5, 4, 0, 0 }).Should().BeFalse();
}

[Fact]
public void OutputShouldEqualValidInput()
{
var inputOutput = new byte[] { 0, 0, 0, 5, 4, 0, 0, 0, 1 };
var pwp = new PeerWirePacket();
pwp.Parse(inputOutput).Should().BeTrue();

pwp.GetBytes().Should().BeEquivalentTo(inputOutput);
}

[Fact]
public void CommandPacketShouldBe5Bytes()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 1, 1 }).Should().BeTrue();

pwp.CommandLength.Should().Be(1);
pwp.PacketByteLength.Should().Be(5);
}

[Fact]
public void Command0IsChoke()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 1, 0 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.Choke);
}

[Fact]
public void Command1IsUnchoke()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 1, 1 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.Unchoke);
}

[Fact]
public void Command2IsUnchoke()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 1, 2 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.Interested);
}

[Fact]
public void Command3IsUnchoke()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 1, 3 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.NotInterested);
}

[Fact]
public void Command4IsHave()
{
var pwp = new PeerWirePacket();
pwp.Parse(new byte[] { 0, 0, 0, 5, 4, 0, 0, 0, 1 }).Should().BeTrue();

pwp.Command.Should().Be(PeerClientCommands.Have);
}
}
}
16 changes: 5 additions & 11 deletions bzTorrent/Data/PeerWirePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,21 @@ public class PeerWirePacket
public bool Parse(byte[] currentPacketBuffer)
{
var commandLength = UnpackHelper.UInt32(currentPacketBuffer, 0, UnpackHelper.Endianness.Big);
if (commandLength == 0)
CommandLength = commandLength;

if (CommandLength == 0)
{
//keep-alive message
Command = PeerClientCommands.KeepAlive;
return true;
}

if (commandLength > (currentPacketBuffer.Length - 4))
if (CommandLength > (currentPacketBuffer.Length - 4))
{
//need more data first
return false;
}

CommandLength = commandLength;

if(CommandLength == 0)
{
Command = PeerClientCommands.KeepAlive;
}

if (CommandLength > 0)
{
Command = (PeerClientCommands)currentPacketBuffer[4];
Expand All @@ -71,8 +66,7 @@ public bool Parse(byte[] currentPacketBuffer)
if(CommandLength > 1)
{
Payload = currentPacketBuffer.GetBytes(5, (int)CommandLength - 1);
}

}

return true;
}
Expand Down

0 comments on commit 0064f8c

Please sign in to comment.