forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_data_test.go
145 lines (127 loc) · 3.93 KB
/
manage_data_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
package txnbuild
import (
"testing"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
)
func TestManageDataValidateName(t *testing.T) {
kp0 := newKeypair0()
sourceAccount := NewSimpleAccount(kp0.Address(), int64(3556091187167235))
manageData := ManageData{
Name: "This is a very long name for a field that only accepts 64 characters",
Value: []byte(""),
}
_, err := NewTransaction(
TransactionParams{
SourceAccount: &sourceAccount,
IncrementSequenceNum: false,
Operations: []Operation{&manageData},
BaseFee: MinBaseFee,
Preconditions: Preconditions{TimeBounds: NewInfiniteTimeout()},
},
)
if assert.Error(t, err) {
expected := "validation failed for *txnbuild.ManageData operation: Field: Name, Error: maximum length is 64 characters"
assert.Contains(t, err.Error(), expected)
}
}
func TestManageDataValidateValue(t *testing.T) {
kp0 := newKeypair0()
sourceAccount := NewSimpleAccount(kp0.Address(), int64(3556091187167235))
manageData := ManageData{
Name: "cars",
Value: []byte("toyota, ford, porsche, lamborghini, hyundai, volkswagen, gmc, kia"),
}
_, err := NewTransaction(
TransactionParams{
SourceAccount: &sourceAccount,
IncrementSequenceNum: false,
Operations: []Operation{&manageData},
BaseFee: MinBaseFee,
Preconditions: Preconditions{TimeBounds: NewInfiniteTimeout()},
},
)
if assert.Error(t, err) {
expected := "validation failed for *txnbuild.ManageData operation: Field: Value, Error: maximum length is 64 bytes"
assert.Contains(t, err.Error(), expected)
}
}
func TestManageDataRoundTrip(t *testing.T) {
kp0 := newKeypair0()
sourceAccount := NewSimpleAccount(kp0.Address(), int64(3556091187167235))
for _, testCase := range []struct {
name string
value []byte
}{
{
"nil data",
nil,
},
{
"empty data slice",
[]byte{},
},
{
"non-empty data slice",
[]byte{1, 2, 3},
},
} {
t.Run(testCase.name, func(t *testing.T) {
manageData := ManageData{
Name: "key",
Value: testCase.value,
}
tx, err := NewTransaction(
TransactionParams{
SourceAccount: &sourceAccount,
IncrementSequenceNum: false,
Operations: []Operation{&manageData},
BaseFee: MinBaseFee,
Preconditions: Preconditions{TimeBounds: NewInfiniteTimeout()},
},
)
assert.NoError(t, err)
envelope := tx.ToXDR()
assert.NoError(t, err)
assert.Len(t, envelope.Operations(), 1)
assert.Equal(t, xdr.String64(manageData.Name), envelope.Operations()[0].Body.ManageDataOp.DataName)
if testCase.value == nil {
assert.Nil(t, envelope.Operations()[0].Body.ManageDataOp.DataValue)
} else {
assert.Len(t, []byte(*envelope.Operations()[0].Body.ManageDataOp.DataValue), len(testCase.value))
if len(testCase.value) > 0 {
assert.Equal(t, testCase.value, []byte(*envelope.Operations()[0].Body.ManageDataOp.DataValue))
}
}
txe, err := tx.Base64()
if err != nil {
assert.NoError(t, err)
}
parsed, err := TransactionFromXDR(txe)
assert.NoError(t, err)
tx, _ = parsed.Transaction()
assert.Len(t, tx.Operations(), 1)
op := tx.Operations()[0].(*ManageData)
assert.Equal(t, manageData.Name, op.Name)
assert.Len(t, op.Value, len(manageData.Value))
if len(manageData.Value) > 0 {
assert.Equal(t, manageData.Value, op.Value)
}
})
}
}
func TestManageDataRoundtrip(t *testing.T) {
manageData := ManageData{
SourceAccount: "GB7BDSZU2Y27LYNLALKKALB52WS2IZWYBDGY6EQBLEED3TJOCVMZRH7H",
Name: "foo",
Value: []byte("bar"),
}
testOperationsMarshalingRoundtrip(t, []Operation{&manageData}, false)
// with muxed accounts
manageData = ManageData{
SourceAccount: "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK",
Name: "foo",
Value: []byte("bar"),
}
testOperationsMarshalingRoundtrip(t, []Operation{&manageData}, true)
}