|
| 1 | +package xdr |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "github.com/stellar/go/gxdr" |
| 9 | + "github.com/stellar/go/randxdr" |
| 10 | + "github.com/stellar/go/strkey" |
| 11 | +) |
| 12 | + |
| 13 | +func TestClaimableBalanceIdStrKey(t *testing.T) { |
| 14 | + gen := randxdr.NewGenerator() |
| 15 | + // generate 10,000 random claimable balance ids and ensure that the strkey |
| 16 | + // encoding / decoding round trips successfully |
| 17 | + for i := 0; i < 10000; i++ { |
| 18 | + id := &ClaimableBalanceId{} |
| 19 | + shape := &gxdr.ClaimableBalanceID{} |
| 20 | + gen.Next( |
| 21 | + shape, |
| 22 | + []randxdr.Preset{}, |
| 23 | + ) |
| 24 | + assert.NoError(t, gxdr.Convert(shape, id)) |
| 25 | + |
| 26 | + encoded, err := id.EncodeToStrkey() |
| 27 | + assert.NoError(t, err) |
| 28 | + |
| 29 | + var decoded ClaimableBalanceId |
| 30 | + assert.NoError(t, decoded.DecodeFromStrkey(encoded)) |
| 31 | + |
| 32 | + serializedBytes, err := id.MarshalBinary() |
| 33 | + assert.NoError(t, err) |
| 34 | + serializedDecoded, err := decoded.MarshalBinary() |
| 35 | + assert.NoError(t, err) |
| 36 | + assert.Equal(t, serializedBytes, serializedDecoded) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestClaimableBalanceIdDecodeErrors(t *testing.T) { |
| 41 | + var decoded ClaimableBalanceId |
| 42 | + payload := []byte{ |
| 43 | + // first byte represents ClaimableBalanceIDType (in this case it's V0) |
| 44 | + 0x00, |
| 45 | + // the remaining 32 bytes are the contents of the claimable balance hash |
| 46 | + 0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99, |
| 47 | + 0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7, 0x05, 0x51, |
| 48 | + 0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2, |
| 49 | + 0xfb, 0x0d, 0x7a, 0x03, 0xfc, 0x7f, 0xe8, 0x9a, |
| 50 | + } |
| 51 | + address := "BAAD6DBUX6J22DMZOHIEZTEQ64CVCHEDRKWZONFEUL5Q26QD7R76RGR4TU" |
| 52 | + assert.Equal(t, address, strkey.MustEncode(strkey.VersionByteClaimableBalance, payload)) |
| 53 | + |
| 54 | + payload[0] = 1 |
| 55 | + invalidIdTypeAddress := strkey.MustEncode(strkey.VersionByteClaimableBalance, payload) |
| 56 | + assert.EqualError(t, decoded.DecodeFromStrkey(invalidIdTypeAddress), "invalid claimable balance id type: 1") |
| 57 | + |
| 58 | + payload[0] = 0 |
| 59 | + payload = append(payload, 0) |
| 60 | + invalidLengthAddress := strkey.MustEncode(strkey.VersionByteClaimableBalance, payload) |
| 61 | + assert.EqualError(t, decoded.DecodeFromStrkey(invalidLengthAddress), "invalid payload length, expected 33 but got 34") |
| 62 | +} |
0 commit comments