Skip to content

Commit

Permalink
Merge branch 'master' into 3.0/SVM-3
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Jul 3, 2019
2 parents e80bb72 + 950b5ff commit 6361bf3
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 8 deletions.
38 changes: 38 additions & 0 deletions neo.UnitTests/UT_AssetDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Persistence;
using Neo.SmartContract.Native;

namespace Neo.UnitTests
{
[TestClass]
public class UT_AssetDescription
{
private Store Store;

[TestInitialize]
public void TestSetup()
{
TestBlockchain.InitializeMockNeoSystem();
Store = TestBlockchain.GetStore();
}

[TestMethod]
public void Check_GAS()
{
var descriptor = new Wallets.AssetDescriptor(NativeContract.GAS.Hash);
descriptor.AssetId.Should().Be(NativeContract.GAS.Hash);
descriptor.AssetName.Should().Be("GAS");
descriptor.Decimals.Should().Be(8);
}

[TestMethod]
public void Check_NEO()
{
var descriptor = new Wallets.AssetDescriptor(NativeContract.NEO.Hash);
descriptor.AssetId.Should().Be(NativeContract.NEO.Hash);
descriptor.AssetName.Should().Be("NEO");
descriptor.Decimals.Should().Be(0);
}
}
}
31 changes: 24 additions & 7 deletions neo.UnitTests/UT_Culture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -36,7 +37,8 @@ where t.GetCustomAttribute<TestClassAttribute>() != null

var cultures = new string[] { "en-US", "zh-CN", "de-DE", "ko-KR", "ja-JP" };
var originalUICulture = CultureInfo.CurrentCulture;
var emtpyObjArray = new object[] { };
var emptyObjArray = new object[] { };
var testContext = new object[] { new UnitTestContext() };

// run all the tests, varying the culture each time.
try
Expand All @@ -47,26 +49,26 @@ where t.GetCustomAttribute<TestClassAttribute>() != null

foreach (var c in testClasses)
{
var instance = c.Constructor.Invoke(emtpyObjArray);
var instance = c.Constructor.Invoke(emptyObjArray);
if (c.ClassInit != null)
{
c.ClassInit.Invoke(instance, emtpyObjArray);
c.ClassInit.Invoke(instance, testContext);
}
foreach (var m in c.TestMethods)
{
if (c.TestInit != null)
{
c.TestInit.Invoke(instance, emtpyObjArray);
c.TestInit.Invoke(instance, emptyObjArray);
}
m.Invoke(instance, emtpyObjArray);
m.Invoke(instance, emptyObjArray);
if (c.TestCleanup != null)
{
c.TestCleanup.Invoke(instance, emtpyObjArray);
c.TestCleanup.Invoke(instance, emptyObjArray);
}
}
if (c.ClassCleanup != null)
{
c.ClassCleanup.Invoke(instance, emtpyObjArray);
c.ClassCleanup.Invoke(instance, emptyObjArray);
}
}
}
Expand All @@ -79,6 +81,21 @@ where t.GetCustomAttribute<TestClassAttribute>() != null
}
}

public class UnitTestContext : TestContext
{
public override IDictionary<string, object> Properties => throw new NotImplementedException();

public override void WriteLine(string message)
{
Console.WriteLine(message);
}

public override void WriteLine(string format, params object[] args)
{
Console.WriteLine(format, args);
}
}

public class NotReRunnableAttribute : Attribute
{

Expand Down
50 changes: 50 additions & 0 deletions neo.UnitTests/UT_ProtocolHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Akka.TestKit.Xunit2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P;
using Neo.Network.P2P.Capabilities;
using Neo.Network.P2P.Payloads;

namespace Neo.UnitTests
{
[TestClass]
public class UT_ProtocolHandler : TestKit
{
private NeoSystem testBlockchain;

[TestCleanup]
public void Cleanup()
{
Shutdown();
}

[TestInitialize]
public void TestSetup()
{
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void ProtocolHandler_Test_SendVersion_TellParent()
{
var senderProbe = CreateTestProbe();
var parent = CreateTestProbe();
var protocolActor = ActorOfAsTestActorRef(() => 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>();
}
}
}
85 changes: 85 additions & 0 deletions neo.UnitTests/UT_RemoteNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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;

namespace Neo.UnitTests
{
[TestClass]
public class UT_RemoteNode : TestKit
{
private static NeoSystem testBlockchain;

public UT_RemoteNode()
: base($"remote-node-mailbox {{ mailbox-type: \"{typeof(RemoteNodeMailbox).AssemblyQualifiedName}\" }}" +
$"protocol-handler-mailbox {{ mailbox-type: \"{typeof(ProtocolHandlerMailbox).AssemblyQualifiedName}\" }}")
{
}

[ClassInitialize]
public static void TestSetup(TestContext ctx)
{
testBlockchain = TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void RemoteNode_Test_Abort_DifferentMagic()
{
var connectionTestProbe = CreateTestProbe();
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null));

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)
}
};

var testProbe = CreateTestProbe();
testProbe.Send(remoteNodeActor, payload);

connectionTestProbe.ExpectMsg<Tcp.Abort>();
}

[TestMethod]
public void RemoteNode_Test_Accept_IfSameMagic()
{
var connectionTestProbe = CreateTestProbe();
var remoteNodeActor = ActorOfAsTestActorRef(() => new RemoteNode(testBlockchain, connectionTestProbe, null, null));

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)
}
};

var testProbe = CreateTestProbe();
testProbe.Send(remoteNodeActor, payload);

var verackMessage = connectionTestProbe.ExpectMsg<Tcp.Write>();

//Verack
verackMessage.Data.Count.Should().Be(3);
}
}
}
2 changes: 1 addition & 1 deletion neo/Wallets/AssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AssetDescriptor(UInt160 asset_id)
sb.EmitAppCall(asset_id, "name");
script = sb.ToArray();
}
ApplicationEngine engine = ApplicationEngine.Run(script);
ApplicationEngine engine = ApplicationEngine.Run(script, extraGAS: 3_000_000);
if (engine.State.HasFlag(VMState.FAULT)) throw new ArgumentException();
this.AssetId = asset_id;
this.AssetName = engine.ResultStack.Pop().GetString();
Expand Down

0 comments on commit 6361bf3

Please sign in to comment.