|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package token_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + math "github.com/IBM/mathlib" |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections" |
| 14 | + v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1" |
| 15 | + "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup" |
| 16 | + token2 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/token" |
| 17 | + "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/token/mock" |
| 18 | + "github.com/hyperledger-labs/fabric-token-sdk/token/services/logging" |
| 19 | + "github.com/hyperledger-labs/fabric-token-sdk/token/token" |
| 20 | + "github.com/pkg/errors" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestNewTokensService(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + init func() (logging.Logger, *setup.PublicParams, token2.IdentityDeserializer, error) |
| 28 | + check func(pp *setup.PublicParams, ts *token2.TokensService) error |
| 29 | + wantErr bool |
| 30 | + expectedError string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "publicParams cannot be nil", |
| 34 | + init: func() (logging.Logger, *setup.PublicParams, token2.IdentityDeserializer, error) { |
| 35 | + return nil, nil, nil, nil |
| 36 | + }, |
| 37 | + wantErr: true, |
| 38 | + expectedError: "publicParams cannot be nil", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "identityDeserializer cannot be nil", |
| 42 | + init: func() (logging.Logger, *setup.PublicParams, token2.IdentityDeserializer, error) { |
| 43 | + pp, err := setup.Setup(32, nil, math.FP256BN_AMCL) |
| 44 | + if err != nil { |
| 45 | + return nil, nil, nil, err |
| 46 | + } |
| 47 | + return nil, pp, nil, nil |
| 48 | + }, |
| 49 | + wantErr: true, |
| 50 | + expectedError: "identityDeserializer cannot be nil", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "success", |
| 54 | + init: func() (logging.Logger, *setup.PublicParams, token2.IdentityDeserializer, error) { |
| 55 | + pp, err := setup.Setup(32, []byte("issuer public key"), math.FP256BN_AMCL) |
| 56 | + if err != nil { |
| 57 | + return nil, nil, nil, err |
| 58 | + } |
| 59 | + return nil, pp, &mock.IdentityDeserializer{}, nil |
| 60 | + }, |
| 61 | + check: func(pp *setup.PublicParams, ts *token2.TokensService) error { |
| 62 | + // check pp |
| 63 | + if ts.PublicParameters != pp { |
| 64 | + return errors.Errorf("public parameters not equal") |
| 65 | + } |
| 66 | + // check OutputTokenFormat |
| 67 | + outputTokenFormat, err := token2.ComputeTokenFormat(ts.PublicParameters, 32) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if ts.OutputTokenFormat != outputTokenFormat { |
| 72 | + return errors.Errorf("invalid token format [%s]", ts.OutputTokenFormat) |
| 73 | + } |
| 74 | + |
| 75 | + if len(ts.SupportedTokenFormats()) != 4 { |
| 76 | + return errors.Errorf("invalid number of supported token formats [%d]", len(ts.SupportedTokenFormats())) |
| 77 | + } |
| 78 | + dlog16, err1 := token2.ComputeTokenFormat(pp, 16) |
| 79 | + dlog32, err2 := token2.ComputeTokenFormat(pp, 32) |
| 80 | + ft16, err3 := v1.ComputeTokenFormat(16) |
| 81 | + ft32, err4 := v1.ComputeTokenFormat(32) |
| 82 | + if err1 != nil || err2 != nil || err3 != nil || err4 != nil { |
| 83 | + return errors.Errorf("failed computing token format") |
| 84 | + } |
| 85 | + stf := collections.NewSet[token.Format](ts.SupportedTokenFormats()...) |
| 86 | + if !stf.Contains(dlog16) { |
| 87 | + return errors.Errorf("stf does not contain dlog16") |
| 88 | + } |
| 89 | + if !stf.Contains(dlog32) { |
| 90 | + return errors.Errorf("stf does not contain dlog32") |
| 91 | + } |
| 92 | + if !stf.Contains(ft16) { |
| 93 | + return errors.Errorf("stf does not contain ft16") |
| 94 | + } |
| 95 | + if !stf.Contains(ft32) { |
| 96 | + return errors.Errorf("stf does not contain ft32") |
| 97 | + } |
| 98 | + return nil |
| 99 | + }, |
| 100 | + wantErr: false, |
| 101 | + }, |
| 102 | + } |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + logger, pp, deserializer, err := tt.init() |
| 106 | + assert.NoError(t, err) |
| 107 | + ts, err := token2.NewTokensService(logger, pp, deserializer) |
| 108 | + if tt.wantErr { |
| 109 | + assert.Error(t, err) |
| 110 | + assert.EqualError(t, err, tt.expectedError) |
| 111 | + assert.Nil(t, ts) |
| 112 | + } else { |
| 113 | + assert.NoError(t, err) |
| 114 | + assert.NotNil(t, ts) |
| 115 | + assert.NoError(t, tt.check(pp, ts)) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments