-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_test.go
96 lines (89 loc) · 3.08 KB
/
account_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
package libra_test
import (
"bytes"
"encoding/hex"
"testing"
"github.com/go-test/deep"
libra "github.com/philippgille/libra-sdk-go"
)
const (
// In the Libra CLI:
//
//Decoded: AccountResource {
// authentication_key: 0x8cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969,
// balance: 62500000,
// received_events_count: 1,
// sent_events_count: 4,
// sequence_number: 4,
// }
testAcc1StateString = "010000002100000001217da6c6b3e19f1825cfb2676daecce3bf3de03cf26647c78df00b371b25cc9744000000200000008cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969a0acb90300000000010000000000000004000000000000000400000000000000"
// The account resource part of the above account state blob
testAcc1ResString = "200000008cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969a0acb90300000000010000000000000004000000000000000400000000000000"
// The auth key part of the above account resource blob
testAcc1AuthKey = "8cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969"
)
// TestAccountStateDecoding tests if libra.FromAccountStateBlob(...) works correctly.
// It uses libra.FromAccountResourceBlob(...), so that function must be tested independently.
func TestAccountStateDecoding(t *testing.T) {
testAccState, err := hex.DecodeString(testAcc1StateString)
if err != nil {
t.Fatal(err)
}
accState, err := libra.FromAccountStateBlob(testAccState)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(accState.Blob, testAccState) {
t.Fatal("accState.Blob != expected account state blob")
}
accRes := accState.AccountResource
if diff := deep.Equal(accRes, libra.AccountResource{}); diff == nil {
t.Fatal("accState.AccountResource has a nil value, but should be an object with values")
}
// Construct expected account resource
expectedAccResBlob, err := hex.DecodeString(testAcc1ResString)
if err != nil {
t.Fatal(err)
}
expectedAccRes, err := libra.FromAccountResourceBlob(expectedAccResBlob)
if err != nil {
t.Fatal(err)
}
if diff := deep.Equal(accRes, expectedAccRes); diff != nil {
t.Fatal(diff)
}
}
// TestAccountResourceDecoding tests if libra.FromAccountResourceBlob(...) works correctly.
func TestAccountResourceDecoding(t *testing.T) {
// The string is a substring of the account state tested elsewhere
testAccRes, err := hex.DecodeString(testAcc1ResString)
if err != nil {
t.Fatal(err)
}
accRes, err := libra.FromAccountResourceBlob(testAccRes)
if err != nil {
t.Fatal(err)
}
if diff := deep.Equal(accRes, libra.AccountResource{}); diff == nil {
t.Fatal("accRes is a nil value, but should be an object with values")
}
expectedAuthKey, err := hex.DecodeString(testAcc1AuthKey)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(accRes.AuthKey, expectedAuthKey) {
t.Fatal("accRes.AuthKey != expected auth key")
}
if accRes.Balance != uint64(62500000) {
t.Fatal("accRes.Balance != 62500000")
}
if accRes.ReceivedEvents != uint64(1) {
t.Fatal("accRes.ReceivedEvents != 1")
}
if accRes.SentEvents != uint64(4) {
t.Fatal("accRes.SentEvents != 4")
}
if accRes.SequenceNo != uint64(4) {
t.Fatal("accRes.SequenceNo != 4")
}
}