Skip to content
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

chore: set beelocal branch to update-k3s-1.30.3 and go 1.23 upgrade #4878

Merged
merged 10 commits into from
Nov 5, 2024
Prev Previous commit
Next Next commit
test(keystore): add TestDeprecatedEllipticMarshal and update TestService
  • Loading branch information
gacevicljubisa committed Nov 3, 2024
commit 67860265e1b5486039ef153a76567d480be69de3
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