cosmos-sdk/crypto/ledger/encode_test.go
Julien Robert 1fe59eb22a
ci: improve error checking (errcheck linter) (#11195)
## Description

Implements part of #7258

Check some of currently unchecked errors.

- [x] baseapp
- [x] client
- [x] codec
- [x] crypto
- [x] server
- [x] simapp
- [ ] snapshots
- [ ] store
- [x] testutil
- [ ] types
- [ ] modules

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-04-13 06:46:51 +00:00

64 lines
1.7 KiB
Go

package ledger
import (
"os"
"testing"
"github.com/stretchr/testify/require"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)
type byter interface {
Bytes() []byte
}
func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
// Marshal to JSON bytes.
js, err := cdc.MarshalJSON(src)
require.Nil(t, err, "%+v", err)
if isNil {
require.Equal(t, string(js), `null`)
} else {
require.Contains(t, string(js), `"type":`)
require.Contains(t, string(js), `"value":`)
}
// Unmarshal.
err = cdc.UnmarshalJSON(js, dst)
require.Nil(t, err, "%+v", err)
}
// nolint: govet
func ExamplePrintRegisteredTypes() {
_ = cdc.PrintTypes(os.Stdout)
// | Type | Name | Prefix | Length | Notes |
// | ---- | ---- | ------ | ----- | ------ |
// | PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable | |
// | PubKey | tendermint/PubKeyEd25519 | 0x1624DE64 | variable | |
// | PubKey | tendermint/PubKeySr25519 | 0x0DFB1005 | variable | |
// | PubKey | tendermint/PubKeySecp256k1 | 0xEB5AE987 | variable | |
// | PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
// | PrivKey | tendermint/PrivKeyEd25519 | 0xA3288910 | variable | |
// | PrivKey | tendermint/PrivKeySr25519 | 0x2F82D78B | variable | |
// | PrivKey | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | variable | |
}
func TestNilEncodings(t *testing.T) {
// Check nil Signature.
var a, b []byte
checkAminoJSON(t, &a, &b, true)
require.EqualValues(t, a, b)
// Check nil PubKey.
var c, d cryptotypes.PubKey
checkAminoJSON(t, &c, &d, true)
require.EqualValues(t, c, d)
// Check nil PrivKey.
var e, f cryptotypes.PrivKey
checkAminoJSON(t, &e, &f, true)
require.EqualValues(t, e, f)
}