Skip to content

Commit

Permalink
Improving HelloSuite Pending Listen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Jan 3, 2024
1 parent e205f75 commit 2497413
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
14 changes: 1 addition & 13 deletions ship/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,7 @@ func (c *ShipConnection) handleState(timeout bool, message []byte) {
c.handshakeHello_PendingInit()

case SmeHelloStatePendingListen:
if timeout {
// The device needs to be in a state for the user to allow trusting the device
// e.g. either the web UI or by other means
if !c.serviceDataProvider.AllowWaitingForTrust(c.remoteShipID) {
c.handshakeHello_PendingTimeout()
return
}

c.handshakeHello_PendingProlongationRequest()
return
}

c.handshakeHello_PendingListen(message)
c.handshakeHello_PendingListen(timeout, message)

case SmeHelloStateOk:
c.handshakeProtocol_Init()
Expand Down
14 changes: 13 additions & 1 deletion ship/hs_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,19 @@ func (c *ShipConnection) handshakeHello_PendingInit() {
}

// SME_HELLO_PENDING_LISTEN
func (c *ShipConnection) handshakeHello_PendingListen(message []byte) {
func (c *ShipConnection) handshakeHello_PendingListen(timeout bool, message []byte) {
if timeout {
// The device needs to be in a state for the user to allow trusting the device
// e.g. either the web UI or by other means
if !c.serviceDataProvider.AllowWaitingForTrust(c.remoteShipID) {
c.handshakeHello_PendingTimeout()
} else {
c.handshakeHello_PendingProlongationRequest()
}

return
}

var helloReturnMsg model.ConnectionHello
if err := c.processShipJsonMessage(message, &helloReturnMsg); err != nil {
c.setState(SmeHelloStateAbort, nil)
Expand Down
19 changes: 18 additions & 1 deletion ship/hs_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (s *HelloSuite) Test_PendingListen_Timeout() {
time.Sleep(tHelloInit + time.Second)
} else {
// speed up the test by running the method directly
sut.handshakeHello_PendingTimeout()
sut.handshakeHello_PendingListen(true, nil)
}

assert.Equal(s.T(), SmeHelloStateAbortDone, sut.getState())
Expand All @@ -180,6 +180,23 @@ func (s *HelloSuite) Test_PendingListen_Timeout() {
shutdownTest(sut)
}

func (s *HelloSuite) Test_PendingListen_Timeout_Prolongation() {
sut, data := initTest(s.role)

data.allowWaitingForTrust = true

sut.setState(SmeHelloStatePendingInit, nil) // inits the timer
sut.setState(SmeHelloStatePendingListen, nil)

// speed up the test by running the method directly, the timer is already checked
sut.handshakeHello_PendingListen(true, nil)

assert.Equal(s.T(), SmeHelloStatePendingListen, sut.getState())
assert.NotNil(s.T(), data.lastMessage())

shutdownTest(sut)
}

func (s *HelloSuite) Test_PendingListen_ReadyAbort() {
sut, data := initTest(s.role)

Expand Down
8 changes: 6 additions & 2 deletions ship/hs_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type dataHandlerTest struct {

mux sync.Mutex

allowWaitingForTrust bool

handleConnectionClosedInvoked bool
}

Expand Down Expand Up @@ -45,8 +47,10 @@ func (s *dataHandlerTest) IsRemoteServiceForSKIPaired(string) bool { return true
func (s *dataHandlerTest) HandleConnectionClosed(*ShipConnection, bool) {
s.handleConnectionClosedInvoked = true
}
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}
func (s *dataHandlerTest) AllowWaitingForTrust(string) bool { return false }
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}
func (s *dataHandlerTest) AllowWaitingForTrust(string) bool {
return s.allowWaitingForTrust
}
func (s *dataHandlerTest) HandleShipHandshakeStateUpdate(string, ShipState) {}

func initTest(role shipRole) (*ShipConnection, *dataHandlerTest) {
Expand Down

0 comments on commit 2497413

Please sign in to comment.