File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
token/core/common/encoding/pp Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Copyright IBM Corp. All Rights Reserved.
3+
4+ SPDX-License-Identifier: Apache-2.0
5+ */
6+
17package pp
28
39import (
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
916func 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
1324func Unmarshal (raw []byte ) (* pp.PublicParameters , error ) {
1425 pp := & pp.PublicParameters {}
1526 if err := json .Unmarshal (raw , pp ); err != nil {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments