2022-06-10 15:32:15 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPairingServerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(PairingServerSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type PairingServerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
TestPairingServerComponents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PairingServerSuite) SetupSuite() {
|
|
|
|
s.SetupPairingServerComponents(s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PairingServerSuite) TestPairingServer_StartPairing() {
|
2022-07-01 15:37:53 +00:00
|
|
|
// Replace PairingServer.PayloadManager with a MockEncryptOnlyPayloadManager
|
|
|
|
pm, err := NewMockEncryptOnlyPayloadManager(s.EphemeralPK)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.PS.PayloadManager = pm
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
modes := []Mode{
|
|
|
|
Receiving,
|
|
|
|
Sending,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range modes {
|
|
|
|
s.PS.mode = m
|
|
|
|
|
|
|
|
if m == Sending {
|
2022-07-05 05:40:43 +00:00
|
|
|
err := s.PS.Mount()
|
2022-06-10 23:03:16 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.PS.StartPairing()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Give time for the sever to be ready, hacky I know, I'll iron this out
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
cp, err := s.PS.MakeConnectionParams()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
qr, err := cp.ToString()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Client reads QR code and parses the connection string
|
|
|
|
ccp := new(ConnectionParams)
|
|
|
|
err = ccp.FromString(qr)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-29 15:21:22 +00:00
|
|
|
c, err := NewPairingClient(ccp, nil)
|
2022-06-10 15:32:15 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-07-01 15:37:53 +00:00
|
|
|
// Replace PairingClient.PayloadManager with a MockEncryptOnlyPayloadManager
|
|
|
|
c.PayloadManager, err = NewMockEncryptOnlyPayloadManager(s.EphemeralPK)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-10 15:32:15 +00:00
|
|
|
if m == Receiving {
|
2022-07-05 05:40:43 +00:00
|
|
|
err := c.Mount()
|
2022-06-10 23:03:16 +00:00
|
|
|
s.Require().NoError(err)
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.PairAccount()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-10 23:03:16 +00:00
|
|
|
switch m {
|
|
|
|
case Receiving:
|
2022-07-05 05:40:43 +00:00
|
|
|
s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain, s.PS.Received())
|
2022-07-01 15:37:53 +00:00
|
|
|
s.Require().Equal(s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted)
|
2022-07-05 05:40:43 +00:00
|
|
|
s.Require().Nil(s.PS.ToSend())
|
|
|
|
s.Require().Nil(c.Received())
|
2022-06-10 23:03:16 +00:00
|
|
|
case Sending:
|
2022-07-05 05:40:43 +00:00
|
|
|
s.Require().Equal(c.Received(), s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain)
|
2022-07-01 15:37:53 +00:00
|
|
|
s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted)
|
2022-07-05 05:40:43 +00:00
|
|
|
s.Require().Nil(c.ToSend())
|
|
|
|
s.Require().Nil(s.PS.Received())
|
2022-06-10 23:03:16 +00:00
|
|
|
}
|
2022-06-10 15:32:15 +00:00
|
|
|
|
2022-06-29 15:21:22 +00:00
|
|
|
// Reset the server's PayloadEncryptionManager
|
2022-07-05 05:40:43 +00:00
|
|
|
s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).ResetPayload()
|
2022-06-10 15:32:15 +00:00
|
|
|
}
|
|
|
|
}
|