forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
245 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Akka.Actor; | ||
using Akka.TestKit.Xunit2; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Network.P2P; | ||
using Neo.Network.P2P.Capabilities; | ||
using Neo.Network.P2P.Payloads; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Neo.UnitTests | ||
{ | ||
[TestClass] | ||
public class UT_ProtocolHandler : TestKit | ||
{ | ||
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism | ||
|
||
private NeoSystem testBlockchain; | ||
|
||
[TestCleanup] | ||
public void Cleanup() | ||
{ | ||
Shutdown(); | ||
} | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
Akka.Actor.ActorSystem system = Sys; | ||
var config = TestKit.DefaultConfig; | ||
var akkaSettings = new Akka.Actor.Settings(system, config); | ||
testBlockchain = TestBlockchain.InitializeMockNeoSystem(); | ||
} | ||
|
||
[TestMethod] | ||
public void ProtocolHandler_Test_SendVersion_TellParent() | ||
{ | ||
var senderProbe = CreateTestProbe(); | ||
var parent = CreateTestProbe(); | ||
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(()=> new ProtocolHandler(testBlockchain, parent)); | ||
|
||
var payload = new VersionPayload() | ||
{ | ||
UserAgent = "".PadLeft(1024, '0'), | ||
Nonce = 1, | ||
Magic = 2, | ||
Timestamp = 5, | ||
Version = 6, | ||
Capabilities = new NodeCapability[] | ||
{ | ||
new ServerCapability(NodeCapabilityType.TcpServer, 25) | ||
} | ||
}; | ||
|
||
senderProbe.Send(protocolActor, Message.Create(MessageCommand.Version, payload)); | ||
parent.ExpectMsg<VersionPayload>(); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Akka.Actor; | ||
using Akka.IO; | ||
using Akka.TestKit.Xunit2; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Network.P2P; | ||
using Neo.Network.P2P.Capabilities; | ||
using Neo.Network.P2P.Payloads; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Neo.UnitTests | ||
{ | ||
[TestClass] | ||
public class UT_RemoteNode : TestKit | ||
{ | ||
private static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism | ||
|
||
private NeoSystem testBlockchain; | ||
|
||
[TestCleanup] | ||
public void Cleanup() | ||
{ | ||
Shutdown(); | ||
} | ||
|
||
[TestInitialize] | ||
public void TestSetup() | ||
{ | ||
Akka.Actor.ActorSystem system = Sys; | ||
testBlockchain = TestBlockchain.InitializeMockNeoSystem(); | ||
} | ||
|
||
[TestMethod] | ||
public void RemoteNode_Test_Abort_DifferentMagic() | ||
{ | ||
var testProbe = CreateTestProbe(); | ||
var connectionTestProbe = CreateTestProbe(); | ||
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain)); | ||
var protocolFactory = new TestProtocolFactory(protocolActor); | ||
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory)); | ||
|
||
connectionTestProbe.ExpectMsg<Tcp.Write>(); | ||
|
||
var payload = new VersionPayload() | ||
{ | ||
UserAgent = "".PadLeft(1024, '0'), | ||
Nonce = 1, | ||
Magic = 2, | ||
Timestamp = 5, | ||
Version = 6, | ||
Capabilities = new NodeCapability[] | ||
{ | ||
new ServerCapability(NodeCapabilityType.TcpServer, 25) | ||
} | ||
}; | ||
|
||
testProbe.Send(remoteNodeActor, payload); | ||
|
||
connectionTestProbe.ExpectMsg<Tcp.Abort>(); | ||
} | ||
|
||
[TestMethod] | ||
public void RemoteNode_Test_Accept_IfSameMagic() | ||
{ | ||
var testProbe = CreateTestProbe(); | ||
var connectionTestProbe = CreateTestProbe(); | ||
var protocolActor = ActorOfAsTestActorRef<ProtocolHandler>(() => new ProtocolHandler(testBlockchain)); | ||
var protocolFactory = new TestProtocolFactory(protocolActor); | ||
var remoteNodeActor = ActorOfAsTestActorRef<RemoteNode>(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null, protocolFactory)); | ||
|
||
connectionTestProbe.ExpectMsg<Tcp.Write>(); | ||
|
||
var payload = new VersionPayload() | ||
{ | ||
UserAgent = "Unit Test".PadLeft(1024, '0'), | ||
Nonce = 1, | ||
Magic = ProtocolSettings.Default.Magic, | ||
Timestamp = 5, | ||
Version = 6, | ||
Capabilities = new NodeCapability[] | ||
{ | ||
new ServerCapability(NodeCapabilityType.TcpServer, 25) | ||
} | ||
}; | ||
|
||
testProbe.Send(remoteNodeActor, payload); | ||
|
||
var verackMessage = connectionTestProbe.ExpectMsg<Tcp.Write>(); | ||
|
||
//Verack | ||
verackMessage.Data.Count.Should().Be(3); | ||
} | ||
} | ||
|
||
internal class TestProtocolFactory : IActorRefFactory | ||
{ | ||
private IActorRef protocolRef; | ||
|
||
public TestProtocolFactory(IActorRef protocolRef) | ||
{ | ||
this.protocolRef = protocolRef; | ||
} | ||
|
||
public IActorRef ActorOf(Props props, string name = null) | ||
{ | ||
return protocolRef; | ||
} | ||
|
||
public ActorSelection ActorSelection(ActorPath actorPath) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ActorSelection ActorSelection(string actorPath) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters