• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

enbility / ship-go / 16122996795

07 Jul 2025 04:51PM UTC coverage: 89.044% (+0.5%) from 88.567%
16122996795

push

github

DerAndereAndi
🧑‍💻 chore: clean up test duplication and add comprehensive unit tests

- Remove hub_connections_sorting_test.go which was duplicating logic instead of calling actual functions
- Simplify hub_test.go to use actual hub methods instead of accessing internal fields
- Add comprehensive unit tests for sortIPAddresses function with 100% coverage
- Add targeted tests for connection management edge cases
- Fix test isolation by using proper hub method calls

22 of 22 new or added lines in 2 files covered. (100.0%)

7 existing lines in 2 files now uncovered.

2926 of 3286 relevant lines covered (89.04%)

8085.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.93
/ship/hs_prot.go
1
package ship
2

3
import (
4
        "encoding/json"
5
        "errors"
6

7
        "github.com/enbility/ship-go/logging"
8
        "github.com/enbility/ship-go/model"
9
)
10

11
// Handshake Prot covers the states smeProt...
12

13
func (c *ShipConnection) handshakeProtocol_Init() {
6✔
14
        switch c.role {
6✔
15
        case ShipRoleServer:
3✔
16
                c.setState(model.SmeProtHStateServerInit, nil)
3✔
17
                c.setHandshakeTimer(timeoutTimerTypeWaitForReady, cmiTimeout)
3✔
18
                c.setState(model.SmeProtHStateServerListenProposal, nil)
3✔
19
        case ShipRoleClient:
3✔
20
                c.setState(model.SmeProtHStateClientInit, nil)
3✔
21
                c.handshakeProtocol_smeProtHStateClientInit()
3✔
22
        }
23
}
24

25
// provide a ship.MessageProtocolHandshake struct
26
func (c *ShipConnection) protocolHandshake() model.MessageProtocolHandshake {
5✔
27
        protocolHandshake := model.MessageProtocolHandshake{
5✔
28
                MessageProtocolHandshake: model.MessageProtocolHandshakeType{
5✔
29
                        Version: model.Version{Major: 1, Minor: 0},
5✔
30
                        Formats: model.MessageProtocolFormatsType{
5✔
31
                                Format: []model.MessageProtocolFormatType{model.MessageProtocolFormatTypeUTF8},
5✔
32
                        },
5✔
33
                },
5✔
34
        }
5✔
35

5✔
36
        return protocolHandshake
5✔
37
}
5✔
38

39
func (c *ShipConnection) handshakeProtocol_smeProtHStateServerListenProposal(message []byte) {
2✔
40
        _, data := c.parseMessage(message, true)
2✔
41

2✔
42
        messageProtocolHandshake := model.MessageProtocolHandshake{}
2✔
43
        if err := json.Unmarshal([]byte(data), &messageProtocolHandshake); err != nil {
2✔
44
                c.endHandshakeWithError(err)
×
45
                return
×
46
        }
×
47

48
        if messageProtocolHandshake.MessageProtocolHandshake.HandshakeType != model.ProtocolHandshakeTypeTypeAnnounceMax {
3✔
49
                c.endHandshakeWithError(errors.New("Invalid protocol handshake request"))
1✔
50
                return
1✔
51
        }
1✔
52

53
        c.stopTimerSafe()
1✔
54

1✔
55
        protocolHandshake := c.protocolHandshake()
1✔
56
        protocolHandshake.MessageProtocolHandshake.HandshakeType = model.ProtocolHandshakeTypeTypeSelect
1✔
57

1✔
58
        if err := c.sendShipModel(model.MsgTypeControl, protocolHandshake); err != nil {
1✔
59
                c.endHandshakeWithError(err)
×
60
        }
×
61

62
        c.setHandshakeTimer(timeoutTimerTypeWaitForReady, cmiTimeout)
1✔
63

1✔
64
        c.setState(model.SmeProtHStateServerListenConfirm, nil)
1✔
65
}
66

67
func (c *ShipConnection) handshakeProtocol_smeProtHStateServerListenConfirm(message []byte) {
2✔
68
        _, data := c.parseMessage(message, true)
2✔
69

2✔
70
        var messageProtocolHandshake model.MessageProtocolHandshake
2✔
71
        if err := json.Unmarshal([]byte(data), &messageProtocolHandshake); err != nil {
2✔
72
                logging.Log().Debug(err)
×
73
                c.abortProtocolHandshake(model.MessageProtocolHandshakeErrorErrorTypeUnexpectedMessage)
×
74
                return
×
75
        }
×
76

77
        if messageProtocolHandshake.MessageProtocolHandshake.HandshakeType != model.ProtocolHandshakeTypeTypeSelect {
3✔
78
                logging.Log().Debug("invalid protocol handshake response")
1✔
79
                c.abortProtocolHandshake(model.MessageProtocolHandshakeErrorErrorTypeSelectionMismatch)
1✔
80
                return
1✔
81
        }
1✔
82

83
        c.stopTimerSafe()
1✔
84

1✔
85
        c.setAndHandleState(model.SmeProtHStateServerOk)
1✔
86
}
87

88
func (c *ShipConnection) handshakeProtocol_smeProtHStateClientInit() {
3✔
89
        c.setState(model.SmeProtHStateClientInit, nil)
3✔
90

3✔
91
        protocolHandshake := c.protocolHandshake()
3✔
92
        protocolHandshake.MessageProtocolHandshake.HandshakeType = model.ProtocolHandshakeTypeTypeAnnounceMax
3✔
93

3✔
94
        if err := c.sendShipModel(model.MsgTypeControl, protocolHandshake); err != nil {
3✔
95
                c.endHandshakeWithError(err)
×
96
                return
×
97
        }
×
98

99
        c.setState(model.SmeProtHStateClientListenChoice, nil)
3✔
100
}
101

102
func (c *ShipConnection) handshakeProtocol_smeProtHStateClientListenChoice(message []byte) {
3✔
103
        _, data := c.parseMessage(message, true)
3✔
104

3✔
105
        messageProtocolHandshake := model.MessageProtocolHandshake{}
3✔
106
        if err := json.Unmarshal([]byte(data), &messageProtocolHandshake); err != nil {
3✔
UNCOV
107
                logging.Log().Debug(err)
×
UNCOV
108
                c.abortProtocolHandshake(model.MessageProtocolHandshakeErrorErrorTypeUnexpectedMessage)
×
UNCOV
109
                return
×
UNCOV
110
        }
×
111

112
        msgHandshake := messageProtocolHandshake.MessageProtocolHandshake
3✔
113

3✔
114
        abort := false
3✔
115
        if msgHandshake.HandshakeType != model.ProtocolHandshakeTypeTypeSelect {
5✔
116
                logging.Log().Debug("invalid protocol handshake response")
2✔
117
                abort = true
2✔
118
        }
2✔
119

120
        if msgHandshake.Version.Major != 1 {
5✔
121
                logging.Log().Debug("unsupported protocol major version")
2✔
122
                abort = true
2✔
123
        }
2✔
124

125
        if msgHandshake.Version.Minor != 0 {
5✔
126
                logging.Log().Debug("unsupported protocol minor version")
2✔
127
                abort = true
2✔
128
        }
2✔
129

130
        if len(msgHandshake.Formats.Format) == 0 {
4✔
131
                logging.Log().Debug("format is missing")
1✔
132
                abort = true
1✔
133
        }
1✔
134

135
        if len(msgHandshake.Formats.Format) != 1 {
4✔
136
                logging.Log().Debug("unsupported format response")
1✔
137
                abort = true
1✔
138
        }
1✔
139

140
        if msgHandshake.Formats.Format != nil && msgHandshake.Formats.Format[0] != model.MessageProtocolFormatTypeUTF8 {
4✔
141
                logging.Log().Debug("unsupported format")
1✔
142
                abort = true
1✔
143
        }
1✔
144

145
        if abort {
5✔
146
                c.abortProtocolHandshake(model.MessageProtocolHandshakeErrorErrorTypeSelectionMismatch)
2✔
147
                return
2✔
148
        }
2✔
149

150
        c.stopTimerSafe()
1✔
151

1✔
152
        protocolHandshake := c.protocolHandshake()
1✔
153
        protocolHandshake.MessageProtocolHandshake.HandshakeType = model.ProtocolHandshakeTypeTypeSelect
1✔
154

1✔
155
        if err := c.sendShipModel(model.MsgTypeControl, protocolHandshake); err != nil {
1✔
156
                c.endHandshakeWithError(err)
×
157
                return
×
158
        }
×
159

160
        c.setAndHandleState(model.SmeProtHStateClientOk)
1✔
161
}
162

163
func (c *ShipConnection) abortProtocolHandshake(err model.MessageProtocolHandshakeErrorErrorType) {
4✔
164
        c.stopTimerSafe()
4✔
165

4✔
166
        msg := model.MessageProtocolHandshakeError{
4✔
167
                Error: err,
4✔
168
        }
4✔
169

4✔
170
        _ = c.sendShipModel(model.MsgTypeControl, msg)
4✔
171

4✔
172
        c.setState(model.SmeStateError, errors.New("handshake error"))
4✔
173

4✔
174
        c.CloseConnection(false, 0, "")
4✔
175
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc