Skip to content

Commit 0d21381

Browse files
committed
ParseFabtokenToken unit-test
Signed-off-by: Angelo De Caro <[email protected]>
1 parent 881d1a5 commit 0d21381

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

Diff for: token/core/zkatdlog/nogh/v1/token/fabtoken.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ package token
88

99
import (
1010
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
11-
"github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
11+
fabtoken "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/actions"
1212
"github.com/hyperledger-labs/fabric-token-sdk/token/token"
1313
)
1414

15-
func ParseFabtokenToken(tok []byte, precision uint64, maxPrecision uint64) (*actions.Output, uint64, error) {
16-
if precision < maxPrecision {
15+
// ParseFabtokenToken unmarshals tok as a fabtoken.Output using precision to parse the quantity.
16+
// If precision is larger than maxPrecision, it returns an error
17+
func ParseFabtokenToken(tok []byte, precision uint64, maxPrecision uint64) (*fabtoken.Output, uint64, error) {
18+
if precision > maxPrecision {
1719
return nil, 0, errors.Errorf("unsupported precision [%d], max [%d]", precision, maxPrecision)
1820
}
1921

20-
output := &actions.Output{}
22+
output := &fabtoken.Output{}
2123
err := output.Deserialize(tok)
2224
if err != nil {
2325
return nil, 0, errors.Wrap(err, "failed to unmarshal fabtoken")

Diff for: token/core/zkatdlog/nogh/v1/token/fabtoken_test.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)