/
common_test.go
154 lines (115 loc) · 4.09 KB
/
common_test.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package eostest_test
import (
"context"
"os"
"reflect"
"strconv"
"testing"
"time"
eostest "github.com/digital-scarcity/eos-go-test"
"github.com/sebastianmontero/eos-go"
"github.com/sebastianmontero/eos-go/ecc"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)
const testingEndpoint = "http://localhost:8888"
const defaultKey = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
var chainResponsePause time.Duration
func setupTestCase(t *testing.T) func(t *testing.T) {
t.Log("Bootstrapping testing environment ...")
cmd, err := eostest.RestartNodeos(true)
assert.NilError(t, err)
chainResponsePause = time.Second
t.Log("nodeos PID: ", cmd.Process.Pid)
return func(t *testing.T) {
// any cleanup code would go here
}
}
func TestNewRandomPrivateKey(t *testing.T) {
key, err := ecc.NewRandomPrivateKey()
require.NoError(t, err)
assert.Assert(t, key.String() != "")
}
func TestK1PrivateToPublic(t *testing.T) {
wif := "5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss"
privKey, err := ecc.NewPrivateKey(wif)
require.NoError(t, err)
pubKey := privKey.PublicKey()
pubKeyString := pubKey.String()
assert.Equal(t, "EOS859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM", pubKeyString)
}
func TestCreateAccountWithKey(t *testing.T) {
teardownTestCase := setupTestCase(t)
defer teardownTestCase(t)
ctx := context.Background()
api := eos.New(testingEndpoint)
keyBag := &eos.KeyBag{}
api.SetSigner(keyBag)
err := keyBag.ImportPrivateKey(ctx, defaultKey)
assert.NilError(t, err)
randomAccountName := eostest.RandAccountName()
account, err := eostest.CreateAccountFromString(ctx, api, randomAccountName, "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3")
assert.NilError(t, err)
t.Log("Creating account: ", randomAccountName)
assert.Equal(t, string(account), randomAccountName)
}
func TestCreateAccountWithRandomKey(t *testing.T) {
teardownTestCase := setupTestCase(t)
defer teardownTestCase(t)
ctx := context.Background()
api := eos.New(testingEndpoint)
keyBag := &eos.KeyBag{}
err := keyBag.ImportPrivateKey(ctx, defaultKey)
assert.NilError(t, err)
api.SetSigner(keyBag)
randomAccountName := eostest.RandAccountName()
key, account, err := eostest.CreateAccountWithRandomKey(ctx, api, randomAccountName)
assert.NilError(t, err)
t.Log("New random key: ", key.String())
t.Log("Created account: ", randomAccountName, " with random key")
assert.Equal(t, string(account), randomAccountName)
}
func TestCreateAccountWithRandomNameAndKey(t *testing.T) {
teardownTestCase := setupTestCase(t)
defer teardownTestCase(t)
ctx := context.Background()
api := eos.New(testingEndpoint)
keyBag := &eos.KeyBag{}
err := keyBag.ImportPrivateKey(ctx, defaultKey)
assert.NilError(t, err)
api.SetSigner(keyBag)
key, account, err := eostest.CreateAccountWithRandomNameAndKey(ctx, api)
assert.NilError(t, err)
t.Log("New random key: ", key.String())
t.Log("Created account: ", string(account))
}
func TestCreateManyRandomAccounts(t *testing.T) {
teardownTestCase := setupTestCase(t)
defer teardownTestCase(t)
ctx := context.Background()
api := eos.New(testingEndpoint)
keyBag := &eos.KeyBag{}
err := keyBag.ImportPrivateKey(ctx, defaultKey)
assert.NilError(t, err)
api.SetSigner(keyBag)
accountCount := 100
accounts, err := eostest.CreateRandoms(ctx, api, accountCount)
assert.NilError(t, err)
for i, account := range accounts {
t.Log("Created account: " + strconv.Itoa(i) + " of " + strconv.Itoa(accountCount) + ": " + string(account))
}
}
func TestDefaultProgressBarInteractiveModeFalse(t *testing.T) {
prevInteactive := os.Getenv("INTERACTIVE_MODE")
os.Setenv("INTERACTIVE_MODE", "false")
bar := eostest.DefaultProgressBar("foo", 10)
os.Setenv("INTERACTIVE_MODE", prevInteactive)
assert.Equal(t, reflect.TypeOf(bar).String(), "*eostest.FakeProgressBar")
}
func TestDefaultProgressBarInteractiveMode(t *testing.T) {
prevInteactive := os.Getenv("INTERACTIVE_MODE")
os.Setenv("INTERACTIVE_MODE", "")
bar := eostest.DefaultProgressBar("foo", 10)
os.Setenv("INTERACTIVE_MODE", prevInteactive)
assert.Assert(t, reflect.TypeOf(bar).String() != "*eostest.FakeProgressBar")
}