forked from btcpay-monero/monero-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.cs
More file actions
130 lines (103 loc) · 4.39 KB
/
Copy pathTestUtils.cs
File metadata and controls
130 lines (103 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Numerics;
using Monero.Common;
using Monero.Daemon;
using Monero.Wallet;
using Monero.Wallet.Common;
using Xunit;
namespace Monero.IntegrationTests.Utils;
internal abstract class TestUtils
{
private static MoneroDaemonRpc? daemonRpc;
private static MoneroWalletRpc? walletRpc;
public static readonly bool TestsInContainer = GetDefaultEnv("TESTS_INCONTAINER", "false") == "true";
public static readonly Uri DaemonRpcUri = new(GetDefaultEnv("XMR_DAEMON_URI", "http://127.0.0.1:28081"));
public const string DaemonRpcUsername = "";
public const string DaemonRpcPassword = "";
public static readonly Uri PrimaryWalletRpcUri = new(GetDefaultEnv("XMR_WALLET_1_URI", "http://127.0.0.1:18082"));
private static readonly Uri SecondaryWalletRpcUri = new(GetDefaultEnv("XMR_WALLET_2_URI", "http://127.0.0.1:18083"));
public const string WalletRpcUsername = "";
public const string WalletRpcPassword = "";
// test wallet config
private const string WalletName = "test_wallet_1";
public const string WalletPassword = "supersecretpassword123";
public const string Seed = "origin hickory pavements tudor sizes hornet tether segments sack technical elbow unsafe legion nitrogen adapt yearbook idols fuzzy pitched goes tusks elbow erase fossil erase";
public const string Address = "9xSyMy1r9h3BVjMrF3CTqQCQy36yCfkpn7uVfMyTUbez3hhumqBUqGUNNALjcd7f1HJBRdeH82bCC3veFHW7z3xm28gug4d";
public const uint FirstReceiveHeight = 171; // NOTE: this value must be the height of the wallet's first tx for tests
public const int SyncPeriodInMs = 5000; // period between wallet syncs in milliseconds
public static MoneroDaemonRpc GetDaemonRpc()
{
if (daemonRpc != null)
{
return daemonRpc;
}
MoneroRpcConnection rpc = new(DaemonRpcUri, DaemonRpcUsername, DaemonRpcPassword);
daemonRpc = new MoneroDaemonRpc(rpc);
return daemonRpc;
}
public static Task<MoneroWalletRpc> GetCreateWallet()
{
MoneroRpcConnection connection = new(SecondaryWalletRpcUri, WalletRpcUsername, WalletRpcPassword);
return Task.FromResult(new MoneroWalletRpc(connection));
}
public static async Task<MoneroWalletRpc> GetWalletRpc()
{
if (walletRpc == null)
{
MoneroRpcConnection rpc = new(PrimaryWalletRpcUri, WalletRpcUsername, WalletRpcPassword);
walletRpc = new MoneroWalletRpc(rpc);
}
// attempt to open a test wallet
try
{
await walletRpc.OpenWallet(WalletName, WalletPassword);
}
catch (JsonRpcApiException e)
{
// -1 returned when the wallet does not exist or fails to open e.g. it's already open by another application
if (e.Error.Code == -1)
{
// create wallet
await walletRpc.CreateWallet(new MoneroWalletConfig().SetPath(WalletName).SetPassword(WalletPassword)
.SetSeed(Seed).SetRestoreHeight(FirstReceiveHeight));
}
else
{
throw;
}
}
// ensure we're testing the right wallet
Assert.Equal(Seed, await walletRpc.GetSeed());
Assert.Equal(Address, await walletRpc.GetPrimaryAddress());
// sync and save wallet
await walletRpc.Sync(null, null);
await walletRpc.Save();
await walletRpc.StartSyncing(SyncPeriodInMs);
// return cached wallet rpc
return walletRpc;
}
public static void TestUnsignedBigInteger(BigInteger? num, bool? nonZero = null)
{
if (num == null)
{
throw new ArgumentNullException(nameof(num), "BigInteger cannot be null");
}
int comparison = BigInteger.Compare((BigInteger)num, BigInteger.Zero);
if (comparison < 0)
{
throw new MoneroError("BigInteger must be >= 0 (unsigned)");
}
if (nonZero == true && comparison <= 0)
{
throw new MoneroError("BigInteger must be > 0");
}
if (nonZero == false && comparison != 0)
{
throw new MoneroError("BigInteger must be == 0");
}
}
private static string GetDefaultEnv(string key, string defaultValue)
{
string? currentValue = Environment.GetEnvironmentVariable(key);
return string.IsNullOrEmpty(currentValue) ? defaultValue : currentValue;
}
}