Reconciled shared test components into servertest

This commit is contained in:
Samuel Hawksby-Robinson 2023-03-21 12:22:46 +00:00
parent b16631bbc3
commit 374898d7c2
9 changed files with 37 additions and 96 deletions

View File

@ -22,6 +22,8 @@ func makeRandomSerialNumber() (*big.Int, error) {
return rand.Int(rand.Reader, serialNumberLimit) return rand.Int(rand.Reader, serialNumberLimit)
} }
// TODO duped in pairing
func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int { func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int {
h := sha256.New() h := sha256.New()
h.Write(append(pk.D.Bytes(), append(pk.Y.Bytes(), pk.X.Bytes()...)...)) h.Write(append(pk.D.Bytes(), append(pk.Y.Bytes(), pk.X.Bytes()...)...))
@ -29,6 +31,8 @@ func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int {
return new(big.Int).SetBytes(h.Sum(nil)) return new(big.Int).SetBytes(h.Sum(nil))
} }
// TODO duped in pairing
func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Certificate { func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Certificate {
c := &x509.Certificate{ c := &x509.Certificate{
SerialNumber: sn, SerialNumber: sn,
@ -51,6 +55,8 @@ func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Ce
return c return c
} }
// TODO duped in pairing
func GenerateX509PEMs(cert *x509.Certificate, key *ecdsa.PrivateKey) (certPem, keyPem []byte, err error) { func GenerateX509PEMs(cert *x509.Certificate, key *ecdsa.PrivateKey) (certPem, keyPem []byte, err error) {
derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key) derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
if err != nil { if err != nil {

View File

@ -6,6 +6,8 @@ import (
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/server/servertest"
) )
func TestCerts(t *testing.T) { func TestCerts(t *testing.T) {
@ -14,8 +16,8 @@ func TestCerts(t *testing.T) {
type CertsSuite struct { type CertsSuite struct {
suite.Suite suite.Suite
TestKeyComponents servertest.TestKeyComponents
TestCertComponents servertest.TestCertComponents
} }
func (s *CertsSuite) SetupSuite() { func (s *CertsSuite) SetupSuite() {
@ -28,7 +30,7 @@ func (s *CertsSuite) Test_makeSerialNumberFromKey() {
} }
func (s *CertsSuite) TestToECDSA() { func (s *CertsSuite) TestToECDSA() {
k := ToECDSA(base58.Decode(DB58)) k := ToECDSA(base58.Decode(servertest.DB58))
s.Require().NotNil(k.PublicKey.X) s.Require().NotNil(k.PublicKey.X)
s.Require().NotNil(k.PublicKey.Y) s.Require().NotNil(k.PublicKey.Y)
@ -37,7 +39,7 @@ func (s *CertsSuite) TestToECDSA() {
s.Require().Zero(k.D.Cmp(s.D)) s.Require().Zero(k.D.Cmp(s.D))
b58 := base58.Encode(s.D.Bytes()) b58 := base58.Encode(s.D.Bytes())
s.Require().Equal(DB58, b58) s.Require().Equal(servertest.DB58, b58)
} }
func (s *CertsSuite) TestGenerateX509Cert() { func (s *CertsSuite) TestGenerateX509Cert() {

View File

@ -21,6 +21,8 @@ import (
// TODO Reconcile duplicate function here and in server/certs.go // TODO Reconcile duplicate function here and in server/certs.go
// https://github.com/status-im/status-go/issues/3300 // https://github.com/status-im/status-go/issues/3300
// TODO duped, but only used here
func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int { func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int {
h := sha256.New() h := sha256.New()
h.Write(append(pk.D.Bytes(), append(pk.Y.Bytes(), pk.X.Bytes()...)...)) h.Write(append(pk.D.Bytes(), append(pk.Y.Bytes(), pk.X.Bytes()...)...))
@ -28,6 +30,8 @@ func makeSerialNumberFromKey(pk *ecdsa.PrivateKey) *big.Int {
return new(big.Int).SetBytes(h.Sum(nil)) return new(big.Int).SetBytes(h.Sum(nil))
} }
// todo duped
func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Certificate { func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Certificate {
c := &x509.Certificate{ c := &x509.Certificate{
SerialNumber: sn, SerialNumber: sn,
@ -50,6 +54,8 @@ func GenerateX509Cert(sn *big.Int, from, to time.Time, hostname string) *x509.Ce
return c return c
} }
// todo duped
func GenerateX509PEMs(cert *x509.Certificate, key *ecdsa.PrivateKey) (certPem, keyPem []byte, err error) { func GenerateX509PEMs(cert *x509.Certificate, key *ecdsa.PrivateKey) (certPem, keyPem []byte, err error) {
derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key) derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
if err != nil { if err != nil {

View File

@ -5,85 +5,15 @@ import (
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/tls" "crypto/tls"
"encoding/asn1"
"math/big"
"net" "net"
"testing" "testing"
"time" "time"
"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/server" "github.com/status-im/status-go/server"
"github.com/stretchr/testify/require"
) )
const (
X = "7744735542292224619198421067303535767629647588258222392379329927711683109548"
Y = "6855516769916529066379811647277920115118980625614889267697023742462401590771"
D = "38564357061962143106230288374146033267100509055924181407058066820384455255240"
AES = "BbnZ7Gc66t54a9kEFCf7FW8SGQuYypwHVeNkRYeNoqV6"
DB58 = "6jpbvo2ucrtrnpXXF4DQYuysh697isH9ppd2aT8uSRDh"
SN = "91849736469742262272885892667727604096707836853856473239722372976236128900962"
CertTime = "eQUriVtGtkWhPJFeLZjF"
)
type TestKeyComponents struct {
X *big.Int
Y *big.Int
D *big.Int
AES []byte
DBytes []byte
PK *ecdsa.PrivateKey
}
func (tk *TestKeyComponents) SetupKeyComponents(t *testing.T) {
var ok bool
tk.X, ok = new(big.Int).SetString(X, 10)
require.True(t, ok)
tk.Y, ok = new(big.Int).SetString(Y, 10)
require.True(t, ok)
tk.D, ok = new(big.Int).SetString(D, 10)
require.True(t, ok)
tk.AES = base58.Decode(AES)
require.Len(t, tk.AES, 32)
tk.DBytes = base58.Decode(DB58)
require.Exactly(t, tk.D.Bytes(), tk.DBytes)
tk.PK = &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: elliptic.P256(),
X: tk.X,
Y: tk.Y,
},
D: tk.D,
}
}
type TestCertComponents struct {
NotBefore, NotAfter time.Time
SN *big.Int
}
func (tcc *TestCertComponents) SetupCertComponents(t *testing.T) {
var ok bool
tcc.SN, ok = new(big.Int).SetString(SN, 10)
require.True(t, ok)
_, err := asn1.Unmarshal(base58.Decode(CertTime), &tcc.NotBefore)
require.NoError(t, err)
tcc.NotAfter = tcc.NotBefore.Add(time.Hour)
}
type TestPairingServerComponents struct { type TestPairingServerComponents struct {
EphemeralPK *ecdsa.PrivateKey EphemeralPK *ecdsa.PrivateKey
EphemeralAES []byte EphemeralAES []byte
@ -126,14 +56,6 @@ func (tpsc *TestPairingServerComponents) SetupPairingServerComponents(t *testing
require.NoError(t, err) require.NoError(t, err)
} }
type TestLoggerComponents struct {
Logger *zap.Logger
}
func (tlc *TestLoggerComponents) SetupLoggerComponents() {
tlc.Logger = logutils.ZapLogger()
}
type MockPayloadReceiver struct { type MockPayloadReceiver struct {
encryptor *PayloadEncryptor encryptor *PayloadEncryptor
} }

View File

@ -5,7 +5,8 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
internalServer "github.com/status-im/status-go/server" "github.com/status-im/status-go/server"
"github.com/status-im/status-go/server/servertest"
) )
var ( var (
@ -18,9 +19,9 @@ func TestConnectionParamsSuite(t *testing.T) {
type ConnectionParamsSuite struct { type ConnectionParamsSuite struct {
suite.Suite suite.Suite
TestKeyComponents servertest.TestKeyComponents
TestCertComponents servertest.TestCertComponents
TestLoggerComponents servertest.TestLoggerComponents
server *BaseServer server *BaseServer
} }
@ -30,10 +31,10 @@ func (s *ConnectionParamsSuite) SetupSuite() {
s.SetupCertComponents(s.T()) s.SetupCertComponents(s.T())
s.SetupLoggerComponents() s.SetupLoggerComponents()
cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, internalServer.DefaultIP.String()) cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, server.DefaultIP.String())
s.Require().NoError(err) s.Require().NoError(err)
bs := internalServer.NewServer(&cert, internalServer.DefaultIP.String(), nil, s.Logger) bs := server.NewServer(&cert, server.DefaultIP.String(), nil, s.Logger)
err = bs.SetPort(1337) err = bs.SetPort(1337)
s.Require().NoError(err) s.Require().NoError(err)
@ -64,7 +65,7 @@ func (s *ConnectionParamsSuite) TestConnectionParams_Generate() {
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Equal("https://127.0.0.1:1337", u.String()) s.Require().Equal("https://127.0.0.1:1337", u.String())
s.Require().Equal(internalServer.DefaultIP.String(), u.Hostname()) s.Require().Equal(server.DefaultIP.String(), u.Hostname())
s.Require().Equal("1337", u.Port()) s.Require().Equal("1337", u.Port())
s.Require().True(cp.publicKey.Equal(&s.PK.PublicKey)) s.Require().True(cp.publicKey.Equal(&s.PK.PublicKey))

View File

@ -16,6 +16,7 @@ import (
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/server/servertest"
"github.com/status-im/status-go/t/utils" "github.com/status-im/status-go/t/utils"
) )
@ -40,7 +41,7 @@ func TestPayloadMarshallerSuite(t *testing.T) {
type PayloadMarshallerSuite struct { type PayloadMarshallerSuite struct {
suite.Suite suite.Suite
TestLoggerComponents servertest.TestLoggerComponents
teardown func() teardown func()

View File

@ -13,12 +13,13 @@ import (
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/server/servertest"
) )
type QROpsTestSuite struct { type QROpsTestSuite struct {
suite.Suite suite.Suite
TestKeyComponents servertest.TestKeyComponents
TestLoggerComponents servertest.TestLoggerComponents
server *MediaServer server *MediaServer
serverNoPort *MediaServer serverNoPort *MediaServer

View File

@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/server/servertest"
) )
const ( const (
@ -36,8 +37,8 @@ func TestServerURLSuite(t *testing.T) {
type ServerURLSuite struct { type ServerURLSuite struct {
suite.Suite suite.Suite
TestKeyComponents servertest.TestKeyComponents
TestLoggerComponents servertest.TestLoggerComponents
server *MediaServer server *MediaServer
serverForQR *MediaServer serverForQR *MediaServer

View File

@ -1,4 +1,5 @@
package server // Package servertest provides utilities for server testing.
package servertest
import ( import (
"crypto/ecdsa" "crypto/ecdsa"