Skip to content

Commit

Permalink
test(keystore): add TestDeprecatedEllipticMarshal and update TestService
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa committed Nov 3, 2024
1 parent 59382fd commit 6786026
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
60 changes: 58 additions & 2 deletions pkg/keystore/file/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,72 @@
package file_test

import (
"bytes"
"crypto/elliptic"
"testing"

"github.com/btcsuite/btcd/btcec/v2"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/keystore/file"
"github.com/ethersphere/bee/v2/pkg/keystore/test"
)

func TestService(t *testing.T) {
t.Parallel()

dir := t.TempDir()
t.Run("EDGSecp256_K1", func(t *testing.T) {
test.Service(t, file.New(t.TempDir()), crypto.EDGSecp256_K1)
})

test.Service(t, file.New(dir))
t.Run("EDGSecp256_R1", func(t *testing.T) {
test.Service(t, file.New(t.TempDir()), crypto.EDGSecp256_R1)
})
}

func TestDeprecatedEllipticMarshal(t *testing.T) {
t.Parallel()

t.Run("EDGSecp256_K1", func(t *testing.T) {
pk, err := crypto.EDGSecp256_K1.Generate()
if err != nil {
t.Fatal(err)
}

pubBytes := ethcrypto.S256().Marshal(pk.X, pk.Y)
if len(pubBytes) != 65 {
t.Fatalf("public key bytes length mismatch")
}

// nolint:staticcheck
pubBytesDeprecated := elliptic.Marshal(btcec.S256(), pk.X, pk.Y)

if !bytes.Equal(pubBytes, pubBytesDeprecated) {
t.Fatalf("public key bytes mismatch")
}
})

t.Run("EDGSecp256_R1", func(t *testing.T) {
pk, err := crypto.EDGSecp256_R1.Generate()
if err != nil {
t.Fatal(err)
}

pkECDH, err := pk.ECDH()
if err != nil {
t.Fatalf("ecdh failed: %v", err)
}

pubBytes := pkECDH.PublicKey().Bytes()
if len(pubBytes) != 65 {
t.Fatalf("public key bytes length mismatch")
}

// nolint:staticcheck
pubBytesDeprecated := elliptic.Marshal(elliptic.P256(), pk.X, pk.Y)

if !bytes.Equal(pubBytes, pubBytesDeprecated) {
t.Fatalf("public key bytes mismatch")
}
})
}
9 changes: 8 additions & 1 deletion pkg/keystore/mem/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ package mem_test
import (
"testing"

"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/keystore/mem"
"github.com/ethersphere/bee/v2/pkg/keystore/test"
)

func TestService(t *testing.T) {
t.Parallel()

test.Service(t, mem.New())
t.Run("EDGSecp256_K1", func(t *testing.T) {
test.Service(t, mem.New(), crypto.EDGSecp256_K1)
})

t.Run("EDGSecp256_R1", func(t *testing.T) {
test.Service(t, mem.New(), crypto.EDGSecp256_R1)
})
}
4 changes: 1 addition & 3 deletions pkg/keystore/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"errors"
"testing"

"github.com/ethersphere/bee/v2/pkg/crypto"
"github.com/ethersphere/bee/v2/pkg/keystore"
)

// Service is a utility testing function that can be used to test
// implementations of the keystore.Service interface.
func Service(t *testing.T, s keystore.Service) {
func Service(t *testing.T, s keystore.Service, edg keystore.EDG) {
t.Helper()

exists, err := s.Exists("swarm")
Expand All @@ -27,7 +26,6 @@ func Service(t *testing.T, s keystore.Service) {
t.Fatal("should not exist")
}

edg := crypto.EDGSecp256_K1
// create a new swarm key
k1, created, err := s.Key("swarm", "pass123456", edg)
if err != nil {
Expand Down

0 comments on commit 6786026

Please sign in to comment.