-
Notifications
You must be signed in to change notification settings - Fork 501
/
asset_test.go
104 lines (87 loc) · 2.79 KB
/
asset_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
package txnbuild
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stellar/go/xdr"
)
func TestNativeAssetToXDR(t *testing.T) {
asset := NativeAsset{}
received, err := asset.ToXDR()
assert.Nil(t, err)
expected := xdr.Asset{Type: xdr.AssetTypeAssetTypeNative}
assert.Equal(t, expected, received, "Empty asset converts to native XDR")
}
func TestAlphaNum4AssetToXDR(t *testing.T) {
asset := CreditAsset{
Code: "USD",
Issuer: newKeypair0().Address(),
}
var xdrAssetCode [4]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress(asset.Issuer))
received, err := asset.ToXDR()
assert.Nil(t, err)
expected := xdr.Asset{Type: xdr.AssetTypeAssetTypeCreditAlphanum4,
AlphaNum4: &xdr.AlphaNum4{
AssetCode: xdrAssetCode,
Issuer: xdrIssuer,
}}
assert.Equal(t, expected, received, "4 digit codes ok")
}
func TestAlphaNum12AssetToXDR(t *testing.T) {
asset := CreditAsset{
Code: "MEGAUSD",
Issuer: newKeypair0().Address(),
}
var xdrAssetCode [12]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress(asset.Issuer))
received, err := asset.ToXDR()
assert.Nil(t, err)
expected := xdr.Asset{Type: xdr.AssetTypeAssetTypeCreditAlphanum12,
AlphaNum12: &xdr.AlphaNum12{
AssetCode: xdrAssetCode,
Issuer: xdrIssuer,
}}
assert.Equal(t, expected, received, "12 digit codes ok")
}
func TestCodeTooShort(t *testing.T) {
asset := CreditAsset{
Code: "",
Issuer: newKeypair0().Address(),
}
var xdrAssetCode [12]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress(asset.Issuer))
_, err := asset.ToXDR()
expectedErrMsg := "asset code length must be between 1 and 12 characters: Asset code length is invalid"
require.EqualError(t, err, expectedErrMsg, "Minimum code length should be enforced")
}
func TestCodeTooLong(t *testing.T) {
asset := CreditAsset{
Code: "THIRTEENCHARS",
Issuer: newKeypair0().Address(),
}
var xdrAssetCode [12]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress(asset.Issuer))
_, err := asset.ToXDR()
expectedErrMsg := "asset code length must be between 1 and 12 characters: Asset code length is invalid"
require.EqualError(t, err, expectedErrMsg, "Maximum code length should be enforced")
}
func TestBadIssuer(t *testing.T) {
asset := CreditAsset{
Code: "USD",
Issuer: "DOESNTLOOKLIKEANADDRESS",
}
var xdrAssetCode [4]byte
copy(xdrAssetCode[:], asset.Code)
var xdrIssuer xdr.AccountId
expectedErrMsg := "non-canonical strkey; unused bits should be set to 0"
require.EqualError(t, xdrIssuer.SetAddress(asset.Issuer), expectedErrMsg, "Issuer address should be validated")
}