Fixed PairingPayloadMarshaller logger issue

This commit is contained in:
Samuel Hawksby-Robinson 2022-10-21 16:19:46 +01:00
parent 71170e7099
commit 95eba8c123
6 changed files with 28 additions and 17 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap"
"github.com/status-im/status-go/logutils" "github.com/status-im/status-go/logutils"
) )
@ -117,6 +118,14 @@ 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 MockEncryptOnlyPayloadManager struct { type MockEncryptOnlyPayloadManager struct {
*PayloadEncryptionManager *PayloadEncryptionManager
} }

View File

@ -4,8 +4,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/logutils"
) )
var ( var (
@ -20,6 +18,7 @@ type ConnectionParamsSuite struct {
suite.Suite suite.Suite
TestKeyComponents TestKeyComponents
TestCertComponents TestCertComponents
TestLoggerComponents
server *PairingServer server *PairingServer
} }
@ -27,11 +26,12 @@ type ConnectionParamsSuite struct {
func (s *ConnectionParamsSuite) SetupSuite() { func (s *ConnectionParamsSuite) SetupSuite() {
s.SetupKeyComponents(s.T()) s.SetupKeyComponents(s.T())
s.SetupCertComponents(s.T()) s.SetupCertComponents(s.T())
s.SetupLoggerComponents()
cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, defaultIP.String()) cert, _, err := GenerateCertFromKey(s.PK, s.NotBefore, defaultIP.String())
s.Require().NoError(err) s.Require().NoError(err)
bs := NewServer(&cert, defaultIP.String(), nil, logutils.ZapLogger()) bs := NewServer(&cert, defaultIP.String(), nil, s.Logger)
err = bs.SetPort(1337) err = bs.SetPort(1337)
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -68,7 +68,7 @@ func NewPairingPayloadManager(aesKey []byte, config *PairingPayloadManagerConfig
logger: l, logger: l,
pp: p, pp: p,
PayloadEncryptionManager: pem, PayloadEncryptionManager: pem,
ppm: NewPairingPayloadMarshaller(p), ppm: NewPairingPayloadMarshaller(p, l),
ppr: NewPairingPayloadRepository(p, config), ppr: NewPairingPayloadRepository(p, config),
}, nil }, nil
} }
@ -229,8 +229,8 @@ type PairingPayloadMarshaller struct {
*PairingPayload *PairingPayload
} }
func NewPairingPayloadMarshaller(p *PairingPayload) *PairingPayloadMarshaller { func NewPairingPayloadMarshaller(p *PairingPayload, logger *zap.Logger) *PairingPayloadMarshaller {
return &PairingPayloadMarshaller{PairingPayload: p} return &PairingPayloadMarshaller{logger: logger, PairingPayload: p}
} }
func (ppm *PairingPayloadMarshaller) MarshalToProtobuf() ([]byte, error) { func (ppm *PairingPayloadMarshaller) MarshalToProtobuf() ([]byte, error) {

View File

@ -39,6 +39,7 @@ func TestPayloadMarshallerSuite(t *testing.T) {
type PayloadMarshallerSuite struct { type PayloadMarshallerSuite struct {
suite.Suite suite.Suite
TestLoggerComponents
teardown func() teardown func()
@ -115,6 +116,8 @@ func getFiles(t *testing.T, keyStorePath string) map[string][]byte {
} }
func (pms *PayloadMarshallerSuite) SetupTest() { func (pms *PayloadMarshallerSuite) SetupTest() {
pms.SetupLoggerComponents()
db1, db1td := setupTestDB(pms.T()) db1, db1td := setupTestDB(pms.T())
db2, db2td := setupTestDB(pms.T()) db2, db2td := setupTestDB(pms.T())
keystore1, keystore2, kstd := makeKeystores(pms.T()) keystore1, keystore2, kstd := makeKeystores(pms.T())
@ -194,7 +197,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_MarshalToProtobuf() {
pms.Require().NoError(err) pms.Require().NoError(err)
// Make and Load PairingPayloadMarshaller 1 // Make and Load PairingPayloadMarshaller 1
ppm := NewPairingPayloadMarshaller(pp) ppm := NewPairingPayloadMarshaller(pp, pms.Logger)
// TEST PairingPayloadMarshaller 1 MarshalToProtobuf() // TEST PairingPayloadMarshaller 1 MarshalToProtobuf()
pb, err := ppm.MarshalToProtobuf() pb, err := ppm.MarshalToProtobuf()
@ -223,7 +226,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_UnmarshalProtobuf() {
pms.Require().NoError(err) pms.Require().NoError(err)
// Make and Load PairingPayloadMarshaller 1 // Make and Load PairingPayloadMarshaller 1
ppm := NewPairingPayloadMarshaller(pp) ppm := NewPairingPayloadMarshaller(pp, pms.Logger)
pb, err := ppm.MarshalToProtobuf() pb, err := ppm.MarshalToProtobuf()
pms.Require().NoError(err) pms.Require().NoError(err)
@ -232,7 +235,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_UnmarshalProtobuf() {
pp2 := new(PairingPayload) pp2 := new(PairingPayload)
// Make PairingPayloadMarshaller 2 // Make PairingPayloadMarshaller 2
ppm2 := NewPairingPayloadMarshaller(pp2) ppm2 := NewPairingPayloadMarshaller(pp2, pms.Logger)
// TEST PairingPayloadMarshaller 2 is empty // TEST PairingPayloadMarshaller 2 is empty
pms.Require().Nil(ppm2.keys) pms.Require().Nil(ppm2.keys)
@ -276,7 +279,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() {
pms.Require().NoError(err) pms.Require().NoError(err)
// Make and Load PairingPayloadMarshaller 1 // Make and Load PairingPayloadMarshaller 1
ppm := NewPairingPayloadMarshaller(pp) ppm := NewPairingPayloadMarshaller(pp, pms.Logger)
pb, err := ppm.MarshalToProtobuf() pb, err := ppm.MarshalToProtobuf()
pms.Require().NoError(err) pms.Require().NoError(err)
@ -285,7 +288,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() {
pp2 := new(PairingPayload) pp2 := new(PairingPayload)
// Make PairingPayloadMarshaller 2 // Make PairingPayloadMarshaller 2
ppm2 := NewPairingPayloadMarshaller(pp2) ppm2 := NewPairingPayloadMarshaller(pp2, pms.Logger)
err = ppm2.UnmarshalProtobuf(pb) err = ppm2.UnmarshalProtobuf(pb)
pms.Require().NoError(err) pms.Require().NoError(err)

View File

@ -31,7 +31,7 @@ func (s *PairingServerSuite) TestMultiBackgroundForeground() {
s.PS.ToBackground() s.PS.ToBackground()
s.PS.ToForeground() s.PS.ToForeground()
s.PS.ToForeground() s.PS.ToForeground()
s.Require().Regexp(regexp.MustCompile("(https://192\\.168\\.0\\.\\d+:\\d+)"), s.PS.MakeBaseURL().String()) s.Require().Regexp(regexp.MustCompile("(https://\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,5})"), s.PS.MakeBaseURL().String())
} }
func (s *PairingServerSuite) TestPairingServer_StartPairing() { func (s *PairingServerSuite) TestPairingServer_StartPairing() {

View File

@ -5,8 +5,6 @@ import (
"time" "time"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/logutils"
) )
const ( const (
@ -20,6 +18,7 @@ func TestServerURLSuite(t *testing.T) {
type ServerURLSuite struct { type ServerURLSuite struct {
suite.Suite suite.Suite
TestKeyComponents TestKeyComponents
TestLoggerComponents
server *MediaServer server *MediaServer
serverNoPort *MediaServer serverNoPort *MediaServer
@ -28,18 +27,18 @@ type ServerURLSuite struct {
func (s *ServerURLSuite) SetupTest() { func (s *ServerURLSuite) SetupTest() {
s.SetupKeyComponents(s.T()) s.SetupKeyComponents(s.T())
logger := logutils.ZapLogger() s.SetupLoggerComponents()
s.server = &MediaServer{Server: Server{ s.server = &MediaServer{Server: Server{
hostname: defaultIP.String(), hostname: defaultIP.String(),
portManger: newPortManager(logger, nil), portManger: newPortManager(s.Logger, nil),
}} }}
err := s.server.SetPort(1337) err := s.server.SetPort(1337)
s.Require().NoError(err) s.Require().NoError(err)
s.serverNoPort = &MediaServer{Server: Server{ s.serverNoPort = &MediaServer{Server: Server{
hostname: defaultIP.String(), hostname: defaultIP.String(),
portManger: newPortManager(logger, nil), portManger: newPortManager(s.Logger, nil),
}} }}
go func() { go func() {
time.Sleep(waitTime) time.Sleep(waitTime)