Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed Oct 20, 2023
1 parent aa81724 commit 42064bf
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 3 deletions.
12 changes: 12 additions & 0 deletions bzTorrent.Tests/Helpers/PackHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,17 @@ public void UInt64_ToBytes_ReturnsExpectedValue(ulong input, ulong expectedResul
// Assert
result.Should().ContainInOrder(BitConverter.GetBytes(expectedResult));
}

[Theory]
[InlineData("ABCD", new byte[] { 0xAB, 0xCD })]
[InlineData("ABC", new byte[] { 0xAB, 0xC0 })]
public void Hex_ToBytes_ReturnsExpectedValue(string input, byte[] expectedResult)
{
// Act
var result = PackHelper.Hex(input);

// Assert
result.Should().ContainInOrder(expectedResult);
}
}
}
177 changes: 177 additions & 0 deletions bzTorrent.Tests/PeerWireClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using bzTorrent.Data;
using bzTorrent.IO;
using FluentAssertions;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace bzTorrent.Tests
{
public class PeerWireClientTests
{
[Fact]
public void ConnectAndDisconnectBehave()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint))).Verifiable();
mockPeerConnection.Setup(f => f.Disconnect()).Verifiable();

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);
peerWireClient.Disconnect();

mockPeerConnection.Verify();
}

[Fact]
public void HandshakeBehaves()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var hashId = "C1463792A1FF36A237E3A0F68BADEB0D3764E9BB";
var peerId = "B1463792A1FF36A237E3";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());

mockPeerConnection.Setup(pc => pc.Handshake(It.IsAny<PeerClientHandshake>())).Callback((PeerClientHandshake hs) =>
{
hs.PeerId.Should().BeEquivalentTo(peerId);
hs.InfoHash.Should().BeEquivalentTo(hashId);
}).Verifiable();

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);
peerWireClient.Handshake(hashId, peerId).Should().BeTrue();
peerWireClient.Disconnect();

mockPeerConnection.Verify();
}

[Fact]
public void NullHashIdThrowsExceptionOnHandshake()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var peerId = "B1463792A1FF36A237E3";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);
Assert.Throws<ArgumentNullException>(() => peerWireClient.Handshake(null, peerId));
peerWireClient.Disconnect();

mockPeerConnection.Verify();
}

[Fact]
public void ShortHashIdThrowsExceptionOnHandshake()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var hashId = "C1463792A1FF36A237E3A0D3764E9BB";
var peerId = "B1463792A1FF36A237E3";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);

Assert.Throws<ArgumentOutOfRangeException>(() => peerWireClient.Handshake(hashId, peerId));

peerWireClient.Disconnect();
}

[Fact]
public void NullPeerIdThrowsExceptionOnHandshake()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var hashId = "C1463792A1FF36A237E3A0F68BADEB0D3764E9BB";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);
Assert.Throws<ArgumentNullException>(() => peerWireClient.Handshake(hashId, null));
peerWireClient.Disconnect();

mockPeerConnection.Verify();
}

[Fact]
public void ShortPeerIdThrowsExceptionOnHandshake()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var hashId = "C1463792A1FF36A237E3A0F68BADEB0D3764E9BB";
var peerId = "B1463792A1FF36";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.Connect(endPoint);

Assert.Throws<ArgumentOutOfRangeException>(() => peerWireClient.Handshake(hashId, peerId));

peerWireClient.Disconnect();
}


[Fact]
public void ProcessWithDisconnectedSocketShouldDropConnection()
{
var endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5001);
var hashId = "C1463792A1FF36A237E3A0F68BADEB0D3764E9BB";
var peerId = "B1463792A1FF36A237E3";

var mockPeerConnection = new Mock<IPeerConnection>();

mockPeerConnection.Setup(f => f.Connect(It.Is<IPEndPoint>(ip => ip == endPoint)));
mockPeerConnection.Setup(f => f.Disconnect());
mockPeerConnection.Setup(f => f.Connected).Returns(false);
mockPeerConnection.Setup(pc => pc.Process()).Returns(true).Verifiable();

var peerWireClient = new PeerWireClient(mockPeerConnection.Object);

peerWireClient.DroppedConnection += (pwc) =>
{
pwc.Should().Be(peerWireClient);
};

peerWireClient.Connect(endPoint);
peerWireClient.Handshake(hashId, peerId);

peerWireClient.Process().Should().BeFalse();

peerWireClient.Disconnect();

mockPeerConnection.Verify();
}
}
}
11 changes: 8 additions & 3 deletions bzTorrent/PeerWireClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class PeerWireClient : IPeerWireClient
private readonly IPeerConnection peerConnection;
private readonly List<IProtocolExtension> _btProtocolExtensions;

private Thread _asyncThread = null;

public int Timeout { get => peerConnection.Timeout; }
public bool[] PeerBitField { get; set; }
public bool KeepConnectionAlive { get; set; }
Expand Down Expand Up @@ -111,7 +113,7 @@ public bool Handshake(string hash, string peerId)

if (hash.Length != 40)
{
throw new ArgumentOutOfRangeException(nameof(hash), "hash must be 20 bytes exactly");
throw new ArgumentOutOfRangeException(nameof(hash), "hash must be 40 bytes exactly");
}

if (peerId.Length != 20)
Expand Down Expand Up @@ -150,18 +152,21 @@ public void ProcessAsync()
{
_asyncContinue = true;

(new Thread(o => {
_asyncThread = new Thread(o => {
var client = (PeerWireClient)o;
while (client.Process() && _asyncContinue)
{
Thread.Sleep(10);
}
})).Start(this);
});

_asyncThread.Start();
}

public void StopProcessAsync()
{
_asyncContinue = false;
_asyncThread.Join();
}

public bool Process()
Expand Down

0 comments on commit 42064bf

Please sign in to comment.