[#180] node: Refactor panics in unit test

* Replace panics in unit tests by require.NoError and t.Fatalf

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2023-03-28 17:16:03 +03:00
parent 91717d4b98
commit 221203beeb
30 changed files with 76 additions and 79 deletions

View file

@ -6,7 +6,9 @@ import (
"crypto/rand"
"crypto/x509"
"encoding/hex"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
// Keys is a list of test private keys in hex format.
@ -114,29 +116,22 @@ var Keys = []string{
}
// DecodeKey creates a test private key.
func DecodeKey(i int) *ecdsa.PrivateKey {
func DecodeKey(t testing.TB, i int) *ecdsa.PrivateKey {
if i < 0 {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic("could not generate uniq key")
}
require.NoError(t, err, "could not generate uniq key")
return key
}
if current, size := i, len(Keys); current >= size {
panic("add more test keys, used " + strconv.Itoa(current) + " from " + strconv.Itoa(size))
t.Fatalf("add more test keys, used %d from %d", current, size)
}
buf, err := hex.DecodeString(Keys[i])
if err != nil {
panic("could not hex.Decode: " + err.Error())
}
require.NoError(t, err, "could not to decode hex string")
key, err := x509.ParseECPrivateKey(buf)
if err != nil {
panic("could x509.ParseECPrivateKey: " + err.Error())
}
require.NoError(t, err, "could not to parse ec private key")
return key
}