Skip to content

Commit a84997b

Browse files
committed
pp marshalling with testing
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 25e4c42 commit a84997b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

token/core/common/encoding/pp/pp.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
17
package pp
28

39
import (
10+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
411
"github.com/hyperledger-labs/fabric-token-sdk/token/core/common/encoding/json"
512
"github.com/hyperledger-labs/fabric-token-sdk/token/driver/protos-go/pp"
613
)
714

815
// Marshal marshals the passed public parameters
916
func Marshal(pp *pp.PublicParameters) ([]byte, error) {
17+
if pp == nil {
18+
return nil, errors.New("nil public parameters")
19+
}
1020
return json.Marshal(pp)
1121
}
1222

23+
// Unmarshal unmarshals the passed slice into an instance of pp.PublicParameters
1324
func Unmarshal(raw []byte) (*pp.PublicParameters, error) {
1425
pp := &pp.PublicParameters{}
1526
if err := json.Unmarshal(raw, pp); err != nil {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package pp
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver/protos-go/pp"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestSerialization(t *testing.T) {
17+
// Marshal
18+
19+
// check failures
20+
_, err := Marshal(nil)
21+
assert.Error(t, err)
22+
assert.EqualError(t, err, "nil public parameters")
23+
24+
// success
25+
pp := &pp.PublicParameters{
26+
Identifier: "pineapple",
27+
Raw: []byte{1, 2, 3},
28+
}
29+
res, err := Marshal(pp)
30+
assert.NoError(t, err)
31+
32+
// Unmarshall
33+
34+
// success
35+
pp2, err := Unmarshal(res)
36+
assert.NoError(t, err)
37+
assert.Equal(t, pp, pp2)
38+
39+
// failure
40+
_, err = Unmarshal([]byte{})
41+
assert.Error(t, err)
42+
43+
_, err = Unmarshal(nil)
44+
assert.Error(t, err)
45+
46+
_, err = Unmarshal([]byte{1, 2, 3})
47+
assert.Error(t, err)
48+
}

0 commit comments

Comments
 (0)