|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package token |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestParseFabtokenToken(t *testing.T) { |
| 17 | + nilGetTokFunc := func() (*actions.Output, []byte, error) { |
| 18 | + return nil, nil, nil |
| 19 | + } |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + tok func() (*actions.Output, []byte, error) |
| 23 | + precision uint64 |
| 24 | + maxPrecision uint64 |
| 25 | + wantErr bool |
| 26 | + expectedError string |
| 27 | + expectedQuantity uint64 |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "precision is langer than maxPrecision", |
| 31 | + tok: nilGetTokFunc, |
| 32 | + precision: 10, |
| 33 | + maxPrecision: 5, |
| 34 | + wantErr: true, |
| 35 | + expectedError: "unsupported precision [10], max [5]", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "invalid tok", |
| 39 | + tok: nilGetTokFunc, |
| 40 | + precision: 5, |
| 41 | + maxPrecision: 10, |
| 42 | + wantErr: true, |
| 43 | + expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: syntax error: sequence truncated", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "invalid tok 2", |
| 47 | + tok: func() (*actions.Output, []byte, error) { |
| 48 | + return nil, []byte{}, nil |
| 49 | + }, |
| 50 | + precision: 5, |
| 51 | + maxPrecision: 10, |
| 52 | + wantErr: true, |
| 53 | + expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: syntax error: sequence truncated", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "invalid tok 3", |
| 57 | + tok: func() (*actions.Output, []byte, error) { |
| 58 | + return nil, []byte{0, 1, 2}, nil |
| 59 | + }, |
| 60 | + precision: 5, |
| 61 | + maxPrecision: 10, |
| 62 | + wantErr: true, |
| 63 | + expectedError: "failed to unmarshal fabtoken: failed deserializing token: failed unmarshalling token: failed to unmarshal to TypedToken: asn1: structure error: tags don't match (16 vs {class:0 tag:0 length:1 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} TypedToken @2", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "invalid quantity", |
| 67 | + tok: func() (*actions.Output, []byte, error) { |
| 68 | + output := &actions.Output{ |
| 69 | + Owner: nil, |
| 70 | + Type: "", |
| 71 | + Quantity: "", |
| 72 | + } |
| 73 | + raw, err := output.Serialize() |
| 74 | + return output, raw, err |
| 75 | + }, |
| 76 | + precision: 5, |
| 77 | + maxPrecision: 10, |
| 78 | + wantErr: true, |
| 79 | + expectedError: "failed to create quantity: invalid input [,5]", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "success", |
| 83 | + tok: func() (*actions.Output, []byte, error) { |
| 84 | + output := &actions.Output{ |
| 85 | + Owner: nil, |
| 86 | + Type: "", |
| 87 | + Quantity: "10", |
| 88 | + } |
| 89 | + raw, err := output.Serialize() |
| 90 | + return output, raw, err |
| 91 | + }, |
| 92 | + precision: 5, |
| 93 | + maxPrecision: 10, |
| 94 | + wantErr: false, |
| 95 | + expectedQuantity: 10, |
| 96 | + }, |
| 97 | + } |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + tok, tokBytes, err := tt.tok() |
| 101 | + assert.NoError(t, err) |
| 102 | + output, quantity, err := ParseFabtokenToken(tokBytes, tt.precision, tt.maxPrecision) |
| 103 | + if tt.wantErr { |
| 104 | + assert.Error(t, err) |
| 105 | + assert.EqualError(t, err, tt.expectedError) |
| 106 | + } else { |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.Equal(t, tok, output) |
| 109 | + assert.Equal(t, tt.expectedQuantity, quantity) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments