Skip to content

Commit

Permalink
prune tests aligning to FAB-15864 cleanup
Browse files Browse the repository at this point in the history
prune depending on fabric core
  • Loading branch information
davidkhala committed Dec 20, 2023
1 parent 2edc445 commit 7486e2d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 385 deletions.
26 changes: 0 additions & 26 deletions blockutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ func TestGetConsenterMetadataFromBlock(t *testing.T) {
orderer: nil,
pass: true,
},
{
name: "orderer only",
value: []byte("hello"),
signatures: nil,
orderer: protoutil.MarshalOrPanic(&common.Metadata{Value: []byte("hello")}),
pass: true,
},
{
name: "both signatures and orderer",
value: []byte("hello"),
Expand All @@ -163,7 +156,6 @@ func TestGetConsenterMetadataFromBlock(t *testing.T) {
for _, test := range cases {
block := protoutil.NewBlock(0, nil)
block.Metadata.Metadata[common.BlockMetadataIndex_SIGNATURES] = test.signatures
block.Metadata.Metadata[common.BlockMetadataIndex_ORDERER] = test.orderer
result, err := protoutil.GetConsenterMetadataFromBlock(block)

if test.pass {
Expand Down Expand Up @@ -240,24 +232,6 @@ func TestGetLastConfigIndexFromBlock(t *testing.T) {
require.Contains(t, err.Error(), "failed to unmarshal orderer block metadata")
})

t.Run("malformed metadata", func(t *testing.T) {
block.Metadata.Metadata[common.BlockMetadataIndex_LAST_CONFIG] = []byte("bad metadata")
_, err := protoutil.GetLastConfigIndexFromBlock(block)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to retrieve metadata: error unmarshalling metadata at index [LAST_CONFIG]")
})

t.Run("malformed last config", func(t *testing.T) {
block.Metadata.Metadata[common.BlockMetadataIndex_LAST_CONFIG] = protoutil.MarshalOrPanic(&common.Metadata{
Value: []byte("bad last config"),
})
_, err := protoutil.GetLastConfigIndexFromBlock(block)
require.Error(t, err, "Expected error with malformed last config metadata")
require.Contains(t, err.Error(), "error unmarshalling LastConfig")
require.Panics(t, func() {
_ = protoutil.GetLastConfigIndexFromBlockOrPanic(block)
}, "Expected panic with malformed last config metadata")
})
}

func TestBlockSignatureVerifierEmptyMetadata(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions common/crypto/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package crypto

import (
"crypto/rand"

"github.com/pkg/errors"
)

const (
// NonceSize is the default NonceSize
NonceSize = 24
)

// GetRandomBytes returns len random looking bytes
func GetRandomBytes(len int) ([]byte, error) {
key := make([]byte, len)

// TODO: rand could fill less bytes then len
_, err := rand.Read(key)
if err != nil {
return nil, errors.Wrap(err, "error getting random bytes")
}

return key, nil
}

// GetRandomNonce returns a random byte array of length NonceSize
func GetRandomNonce() ([]byte, error) {
return GetRandomBytes(NonceSize)
}
4 changes: 2 additions & 2 deletions commonutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"errors"
"testing"

"github.com/davidkhala/fabric-protoutil/common/crypto"
"github.com/davidkhala/fabric-protoutil/fakes"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
"github.com/hyperledger/fabric/common/crypto"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestNonceRandomness(t *testing.T) {
Expand Down
30 changes: 3 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module github.com/davidkhala/fabric-protoutil
go 1.20

require (
github.com/golang/protobuf v1.5.3
github.com/hyperledger/fabric v2.1.1+incompatible
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
Expand All @@ -13,37 +11,15 @@ require (

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hyperledger/fabric-amcl v0.0.0-20230602173724-9e02669dceb2 // indirect
github.com/hyperledger/fabric-protos-go v0.3.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 7486e2d

Please sign in to comment.