Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize wallet tests #1499

Merged
merged 14 commits into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 70 additions & 85 deletions tests/neo.UnitTests/Wallets/SQLite/UT_UserWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,46 @@ namespace Neo.UnitTests.Wallets.SQLite
[TestClass]
public class UT_UserWallet
{
private string path;
private UserWallet wallet;
private static string path;
private static UserWallet wallet;
private static WalletAccount account;
public static string GetRandomPath()
{
string threadName = Thread.CurrentThread.ManagedThreadId.ToString();
return Path.GetFullPath(string.Format("Wallet_{0}", new Random().Next(1, 1000000).ToString("X8")) + threadName);
}

[TestInitialize]
public void Setup()
[ClassInitialize]
public static void Setup(TestContext ctx)
{
path = GetRandomPath();
wallet = UserWallet.Create(path, "123456", new ScryptParameters(0, 0, 0));
byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(privateKey);
}
account = wallet.CreateAccount(privateKey);
}

[TestCleanup]
public void Cleanup()
[ClassCleanup]
public static void Cleanup()
{
TestUtils.DeleteFile(path);
}

[TestMethod]
public void TestGetName()
{
wallet.Name.Should().Be(Path.GetFileNameWithoutExtension(path));
}

[TestMethod]
public void TestGetVersion()
{
Action action = () => wallet.Version.ToString();
action.Should().NotThrow();
}

[TestMethod]
public void TestCreateAndOpenSecureString()
public void TestChangePassword()
{
string myPath = GetRandomPath();
var ss = new SecureString();
ss.AppendChar('a');
ss.AppendChar('b');
ss.AppendChar('c');

var w1 = UserWallet.Create(myPath, ss);
w1.Should().NotBeNull();

var w2 = UserWallet.Open(myPath, ss);
w2.Should().NotBeNull();

ss.AppendChar('d');
Action action = () => UserWallet.Open(myPath, ss);
action.Should().Throw<CryptographicException>();

TestUtils.DeleteFile(myPath);
wallet.ChangePassword("123455", "654321").Should().BeFalse();
wallet.ChangePassword("123456", "654321").Should().BeTrue();
wallet.ChangePassword("654321", "123456").Should().BeTrue();
}

[TestMethod]
public void TestOpen()
public void TestContains()
{
byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(privateKey);
}
var account = wallet.CreateAccount(privateKey);
var w1 = UserWallet.Open(path, "123456");
w1.Should().NotBeNull();

Action action = () => UserWallet.Open(path, "123");
action.Should().Throw<CryptographicException>();
wallet.Contains(account.ScriptHash).Should().BeTrue();
}

[TestMethod]
Expand All @@ -103,6 +73,8 @@ public void TestCreateAccountAndGetByPrivateKey()
var account1 = wallet.CreateAccount(privateKey);
var dbAccount1 = wallet.GetAccount(account1.ScriptHash);
account1.Should().Be(dbAccount1);
wallet.DeleteAccount(account.ScriptHash);
wallet.DeleteAccount(account1.ScriptHash);
}

[TestMethod]
Expand All @@ -111,6 +83,7 @@ public void TestCreateAccountByScriptHash()
var account = wallet.CreateAccount(UInt160.Parse("0xa6ee944042f3c7ea900481a95d65e4a887320cf0"));
var dbAccount = wallet.GetAccount(account.ScriptHash);
account.Should().Be(dbAccount);
wallet.DeleteAccount(account.ScriptHash);
}

[TestMethod]
Expand Down Expand Up @@ -145,65 +118,77 @@ public void TestCreateAccountBySmartContract()
var account2 = wallet.CreateAccount(contract2, key2);
var dbAccount2 = wallet.GetAccount(account2.ScriptHash);
account2.Should().Be(dbAccount2);
wallet.DeleteAccount(account.ScriptHash);
wallet.DeleteAccount(account2.ScriptHash);
}

[TestMethod]
public void TestDeleteAccount()
public void TestCreateAndOpenSecureString()
{
bool ret = wallet.DeleteAccount(UInt160.Parse("0xa6ee944042f3c7ea900481a95d65e4a887320cf0"));
ret.Should().BeFalse();
string myPath = GetRandomPath();
var ss = new SecureString();
ss.AppendChar('a');
ss.AppendChar('b');
ss.AppendChar('c');

byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
var w1 = UserWallet.Create(myPath, ss, new ScryptParameters(0, 0, 0));
w1.Should().NotBeNull();

var w2 = UserWallet.Open(myPath, ss);
w2.Should().NotBeNull();

ss.AppendChar('d');
Action action = () => UserWallet.Open(myPath, ss);
action.Should().Throw<CryptographicException>();

TestUtils.DeleteFile(myPath);
}

[TestMethod]
public void TestGetAccounts()
{
var ret = wallet.GetAccounts();
ret.Should().NotBeEmpty();
foreach (var dbAccount in ret)
{
rng.GetBytes(privateKey);
dbAccount.Should().Be(account);
}
var account = wallet.CreateAccount(privateKey);
bool ret2 = wallet.DeleteAccount(account.ScriptHash);
ret2.Should().BeTrue();
}

[TestMethod]
public void TestChangePassword()
public void TestGetName()
{
wallet.ChangePassword("123455", "654321").Should().BeFalse();
wallet.ChangePassword("123456", "654321").Should().BeTrue();
wallet.ChangePassword("654321", "123456").Should().BeTrue();
wallet.Name.Should().Be(Path.GetFileNameWithoutExtension(path));
}

[TestMethod]
public void TestContains()
public void TestGetVersion()
{
byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(privateKey);
}
var account = wallet.CreateAccount(privateKey);
wallet.Contains(account.ScriptHash).Should().BeTrue();
Action action = () => wallet.Version.ToString();
action.Should().NotThrow();
}

[TestMethod]
public void TestGetAccounts()
public void TestOpen()
{
var ret = wallet.GetAccounts();
ret.Should().BeNullOrEmpty();
var w1 = UserWallet.Open(path, "123456");
w1.Should().NotBeNull();

byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(privateKey);
}
var account = wallet.CreateAccount(privateKey);
ret = wallet.GetAccounts();
foreach (var dbAccount in ret)
{
dbAccount.Should().Be(account);
}
Action action = () => UserWallet.Open(path, "123");
action.Should().Throw<CryptographicException>();
}

[TestMethod]
public void TestToDeleteAccount()
{
bool ret = wallet.DeleteAccount(UInt160.Parse("0xa6ee944042f3c7ea900481a95d65e4a887320cf0"));
ret.Should().BeFalse();
bool ret2 = wallet.DeleteAccount(account.ScriptHash);
ret2.Should().BeTrue();
}

[TestMethod]
public void TestVerifyPassword()
public void TestToVerifyPassword()
{
wallet.VerifyPassword("123456").Should().BeTrue();
wallet.VerifyPassword("123").Should().BeFalse();
Expand Down