Skip to content

Commit 1f146ee

Browse files
authored
fix: Random panic in handshake tests #530 (#543)
1 parent 3b70d53 commit 1f146ee

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

protocol/handshake/messages.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ const (
3030

3131
// Refusal reasons
3232
const (
33-
RefuseReasonVersionMismatch = 0
34-
RefuseReasonDecodeError = 1
35-
RefuseReasonRefused = 2
33+
RefuseReasonVersionMismatch uint64 = 0
34+
RefuseReasonDecodeError uint64 = 1
35+
RefuseReasonRefused uint64 = 2
3636
)
3737

3838
// NewMsgFromCbor parses a Handshake message from CBOR

protocol/handshake/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package handshake
1616

1717
import (
1818
"fmt"
19+
"sort"
1920

2021
"github.com/blinklabs-io/gouroboros/protocol"
2122
)
@@ -82,6 +83,12 @@ func (s *Server) handleProposeVersions(msg protocol.Message) error {
8283
for supportedVersion := range s.config.ProtocolVersionMap {
8384
supportedVersions = append(supportedVersions, supportedVersion)
8485
}
86+
87+
// sort asending - iterating over map is not deterministic
88+
sort.Slice(supportedVersions, func(i, j int) bool {
89+
return supportedVersions[i] < supportedVersions[j]
90+
})
91+
8592
msgRefuse := NewMsgRefuse(
8693
[]any{
8794
RefuseReasonVersionMismatch,

protocol/handshake/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ func TestServerHandshakeRefuseVersionMismatch(t *testing.T) {
117117
InputMessageType: handshake.MessageTypeRefuse,
118118
InputMessage: handshake.NewMsgRefuse(
119119
[]any{
120-
uint64(handshake.RefuseReasonVersionMismatch),
120+
handshake.RefuseReasonVersionMismatch,
121121
// Convert []uint16 to []any
122122
func(in []uint16) []any {
123123
var ret []any
124124
for _, item := range in {
125-
ret = append(ret, item)
125+
ret = append(ret, uint64(item))
126126
}
127127
return ret
128128
}(protocol.GetProtocolVersionsNtC()),

protocol/versions.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package protocol
1616

17+
import "sort"
18+
1719
// The NtC protocol versions have the 15th bit set in the handshake
1820
const ProtocolVersionNtCOffset = 0x8000
1921

@@ -258,6 +260,12 @@ func GetProtocolVersionsNtC() []uint16 {
258260
versions = append(versions, key)
259261
}
260262
}
263+
264+
// sort asending - iterating over map is not deterministic
265+
sort.Slice(versions, func(i, j int) bool {
266+
return versions[i] < versions[j]
267+
})
268+
261269
return versions
262270
}
263271

@@ -269,6 +277,12 @@ func GetProtocolVersionsNtN() []uint16 {
269277
versions = append(versions, key)
270278
}
271279
}
280+
281+
// sort asending - iterating over map is not deterministic
282+
sort.Slice(versions, func(i, j int) bool {
283+
return versions[i] < versions[j]
284+
})
285+
272286
return versions
273287
}
274288

0 commit comments

Comments
 (0)