|
| 1 | +package conv |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/lazyledger/lazyledger-core/crypto/secp256k1" |
| 10 | + "github.com/lazyledger/lazyledger-core/p2p" |
| 11 | + pb "github.com/libp2p/go-libp2p-core/crypto/pb" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGetNodeKey(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + valid := p2p.GenNodeKey() |
| 18 | + invalid := p2p.NodeKey{ |
| 19 | + PrivKey: secp256k1.GenPrivKey(), |
| 20 | + } |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + name string |
| 24 | + input *p2p.NodeKey |
| 25 | + expectedType pb.KeyType |
| 26 | + err error |
| 27 | + }{ |
| 28 | + {"nil", nil, pb.KeyType(-1), ErrNilKey}, |
| 29 | + {"empty", &p2p.NodeKey{}, pb.KeyType(-1), ErrNilKey}, |
| 30 | + {"invalid", &invalid, pb.KeyType(-1), ErrUnsupportedKeyType}, |
| 31 | + {"valid", &valid, pb.KeyType_Ed25519, nil}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, c := range cases { |
| 35 | + t.Run(c.name, func(t *testing.T) { |
| 36 | + actual, err := GetNodeKey(c.input) |
| 37 | + if c.err != nil { |
| 38 | + assert.Nil(t, actual) |
| 39 | + assert.Error(t, err) |
| 40 | + assert.ErrorIs(t, c.err, err) |
| 41 | + } else { |
| 42 | + require.NotNil(t, actual) |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.Equal(t, c.expectedType, actual.Type()) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
0 commit comments