Skip to content

Commit

Permalink
Fix handling ship error state handling
Browse files Browse the repository at this point in the history
The connection wasn’t closed and thus the device in a stalled process
  • Loading branch information
DerAndereAndi committed Apr 7, 2023
1 parent efc8640 commit 3f7399c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ship/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func (c *ShipConnection) CloseConnection(safe bool, reason string) {
}

_ = c.sendShipModel(model.MsgTypeEnd, closeMessage)
return

if c.smeState != smeError {
return
}
}

c.DataHandler.CloseDataConnection()
Expand Down
8 changes: 6 additions & 2 deletions ship/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,26 @@ func (c *ShipConnection) getState() shipMessageExchangeState {
// handle handshake state transitions
func (c *ShipConnection) handleState(timeout bool, message []byte) {
switch c.getState() {
case smeError:
logging.Log.Debug(c.RemoteSKI, "connection is in error state")
return

// cmiStateInit
case cmiStateInitStart:
// triggered without a message received
c.handshakeInit_cmiStateInitStart()

case cmiStateClientWait:
if timeout {
c.endHandshakeWithError(errors.New("ship handshake timeout"))
c.endHandshakeWithError(errors.New("ship client handshake timeout"))
return
}

c.handshakeInit_cmiStateClientWait(message)

case cmiStateServerWait:
if timeout {
c.endHandshakeWithError(errors.New("ship handshake timeout"))
c.endHandshakeWithError(errors.New("ship server handshake timeout"))
return
}
c.handshakeInit_cmiStateServerWait(message)
Expand Down
10 changes: 7 additions & 3 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 {
sentMessage []byte

mux sync.Mutex

handleConnectionClosedInvoked bool
}

func (s *dataHandlerTest) lastMessage() []byte {
Expand Down Expand Up @@ -44,9 +46,11 @@ func (s *dataHandlerTest) HandleClosedConnection(connection *ShipConnection) {}

var _ ShipServiceDataProvider = (*dataHandlerTest)(nil)

func (s *dataHandlerTest) IsRemoteServiceForSKIPaired(string) bool { return true }
func (s *dataHandlerTest) HandleConnectionClosed(*ShipConnection, bool) {}
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}
func (s *dataHandlerTest) IsRemoteServiceForSKIPaired(string) bool { return true }
func (s *dataHandlerTest) HandleConnectionClosed(*ShipConnection, bool) {
s.handleConnectionClosedInvoked = true
}
func (s *dataHandlerTest) ReportServiceShipID(string, string) {}

func initTest(role shipRole) (*ShipConnection, *dataHandlerTest) {
localDevice := spine.NewDeviceLocalImpl("TestBrandName", "TestDeviceModel", "TestSerialNumber", "TestDeviceCode",
Expand Down
14 changes: 14 additions & 0 deletions ship/hs_init_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func (s *InitClientSuite) Test_ClientWait() {
shutdownTest(sut)
}

func (s *InitClientSuite) Test_ClientWait_Timeout() {
sut, data := initTest(s.role)

sut.setState(cmiStateClientWait)

sut.handleState(true, nil)

assert.Equal(s.T(), smeError, sut.getState())
assert.NotNil(s.T(), data.lastMessage())
assert.Equal(s.T(), data.handleConnectionClosedInvoked, true)

shutdownTest(sut)
}

func (s *InitClientSuite) Test_ClientWait_InvalidMsgType() {
sut, data := initTest(s.role)

Expand Down

0 comments on commit 3f7399c

Please sign in to comment.