-
Notifications
You must be signed in to change notification settings - Fork 9
Added Utils unit tests - envelope and crypto #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a1cc0fd
Added Utils unit tests - envelope and crypto
Effi-S 1e77309
Linter Fixes
Effi-S d7b9b57
Update utils/serialization/envelope_wrapper_test.go
Effi-S e900bab
Update utils/signature/sigtest/crypto_test.go
Effi-S 4f2ee86
Updated changes to reflect upstream repo
Effi-S 9f6b95b
Added Review suggestions from https://github.com/hyperledger/fabric-x…
Effi-S f10a450
combined and into
Effi-S efec497
combined TestSerializeSigningKey and TestParseSigningKey into TestSer…
Effi-S b560b7b
Minor: Removed Unwanted code in crypto_test.go
Effi-S 1654c32
Nit fix on envelope_wrapper_test.go
Effi-S b9e472d
Nit fix on envelope_wrapper_test.go
Effi-S 6dc43bd
[flaky] Fix flaky tests caused by port binding (#149)
liran-funaro ae8b726
Bump github.com/consensys/gnark-crypto from 0.14.0 to 0.18.1 in the g…
dependabot[bot] 74e126a
fix: remove unintended changes
Effi-S aa2509a
minor edits to envelope_wrapper_test.go - nested marshalling
Effi-S d843177
minor edits to crypto_test.go - nested empty key
Effi-S f921882
Added notNil check in crypto_test.go and embedded unused variable in …
Effi-S 8b4c747
Merged all tests in crypto_test.go
Effi-S df2f9a0
crypto_test.go cleanup
Effi-S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package serialization_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/hyperledger/fabric-protos-go-apiv2/common" | ||
| "github.com/hyperledger/fabric-x-common/protoutil" | ||
|
|
||
| "github.com/hyperledger/fabric-x-committer/utils/serialization" | ||
| "github.com/hyperledger/fabric-x-committer/utils/test" | ||
| ) | ||
|
|
||
| // TestUnwrapEnvelopeBadInput tests UnwrapEnvelope function with invalid inputs. | ||
| func TestUnwrapEnvelopeBadInput(t *testing.T) { | ||
| t.Parallel() | ||
| t.Run("Not an envelope", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, _, err := serialization.UnwrapEnvelope([]byte("invalid input")) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("OK Header with an invalid payload", func(t *testing.T) { | ||
| t.Parallel() | ||
| envelopeBytes := protoutil.MarshalOrPanic(&common.Envelope{ | ||
| Payload: []byte("not-a-payload"), | ||
| }) | ||
| _, _, err := serialization.UnwrapEnvelope(envelopeBytes) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("OK Payload with a nil Header", func(t *testing.T) { | ||
| t.Parallel() | ||
| envelopeBytes := protoutil.MarshalOrPanic(&common.Envelope{ | ||
| Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
| Header: nil, | ||
| Data: []byte("some data"), | ||
| }), | ||
| }) | ||
|
|
||
| _, _, err := serialization.UnwrapEnvelope(envelopeBytes) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("OK payload but invalid ChannelHeader", func(t *testing.T) { | ||
| t.Parallel() | ||
| envelopeBytes := protoutil.MarshalOrPanic(&common.Envelope{ | ||
| Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
| Header: &common.Header{ | ||
| ChannelHeader: []byte("not-a-channel-header"), | ||
| }, | ||
| Data: []byte("some data"), | ||
| }), | ||
| }) | ||
|
|
||
| _, _, err := serialization.UnwrapEnvelope(envelopeBytes) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| // TestUnwrapEnvelopeGoodInput Tests properly wrapped envelope is unwrapped correctly. | ||
| func TestUnwrapEnvelopeGoodInput(t *testing.T) { | ||
| t.Parallel() | ||
| expectedPayload := []byte("test payload") | ||
|
|
||
| expectedChannelHeader := &common.ChannelHeader{ | ||
| ChannelId: "test-channel", | ||
| } | ||
|
|
||
| // Wrap | ||
| wrappedEnvelope := protoutil.MarshalOrPanic(&common.Envelope{ | ||
| Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
| Header: &common.Header{ | ||
| ChannelHeader: protoutil.MarshalOrPanic(expectedChannelHeader), | ||
| }, | ||
| Data: expectedPayload, | ||
| }), | ||
| }) | ||
|
|
||
| // Unwrap | ||
| actualPayload, actualChannelHeader, err := serialization.UnwrapEnvelope(wrappedEnvelope) | ||
|
|
||
| // -Check 1- Check unwrap envelope has no error | ||
| require.NoError(t, err) | ||
|
|
||
| // -Check 2- Check we get the correct Payload & Header | ||
| require.Equal(t, expectedPayload, actualPayload) | ||
| test.RequireProtoEqual(t, expectedChannelHeader, actualChannelHeader) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| Copyright IBM Corp. All Rights Reserved. | ||
|
|
||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package sigtest | ||
|
|
||
| import ( | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestVerificationAndSigningKeys(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| curve elliptic.Curve | ||
| }{ | ||
| { | ||
| name: "P256", | ||
| curve: elliptic.P256(), | ||
| }, | ||
| { | ||
| name: "P384", | ||
| curve: elliptic.P384(), | ||
| }, | ||
| { | ||
| name: "P224", | ||
| curve: elliptic.P224(), | ||
| }, | ||
| { | ||
| name: "P521", | ||
| curve: elliptic.P521(), | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(fmt.Sprintf("Serialize %s VerificationKey", tt.name), | ||
| func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| privKey, err := ecdsa.GenerateKey(tt.curve, rand.Reader) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, privKey) | ||
|
|
||
| _, err = SerializeVerificationKey(&privKey.PublicKey) | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| t.Run(fmt.Sprintf("Serialize and Parse %s SigningKey", tt.name), | ||
liran-funaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func(t *testing.T) { | ||
| t.Parallel() | ||
| privateKey, err := ecdsa.GenerateKey(tt.curve, rand.Reader) | ||
| require.NoError(t, err) | ||
| key, err := SerializeSigningKey(privateKey) | ||
| require.NotNil(t, key) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = ParseSigningKey(key) | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
|
|
||
| t.Run("Signing Key Empty", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := SerializeSigningKey(&ecdsa.PrivateKey{}) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("Signing Key is nil", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := SerializeSigningKey(nil) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("Verification Key Empty", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := SerializeVerificationKey(&ecdsa.PublicKey{}) | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("Verification Key is nil", func(t *testing.T) { | ||
| t.Parallel() | ||
| _, err := SerializeVerificationKey(nil) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.