Skip to content

Commit

Permalink
zmq4: extend ZMTP greeting tests for various version cases
Browse files Browse the repository at this point in the history
Updates go-zeromq#56.
  • Loading branch information
sbinet committed Jan 20, 2020
1 parent a444b04 commit 6eb16eb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
22 changes: 19 additions & 3 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ func (g *greeting) read(r io.Reader) error {
return xerrors.Errorf("invalid ZMTP signature footer: %w", errGreeting)
}

// FIXME(sbinet): handle version negotiations as per
// https://rfc.zeromq.org/spec:23/ZMTP/#version-negotiation
if g.Version != defaultVersion {
if !g.validate(defaultVersion) {
return xerrors.Errorf(
"invalid ZMTP version (got=%v, want=%v): %w",
g.Version, defaultVersion, errGreeting,
Expand Down Expand Up @@ -148,6 +146,24 @@ func (g *greeting) marshal() []byte {
return buf[:]
}

func (g *greeting) validate(ref [2]uint8) bool {
switch {
case g.Version == ref:
return true
case g.Version[0] > ref[0] ||
g.Version[0] == ref[0] && g.Version[1] > ref[1]:
// accept higher protocol values
return true
case g.Version[0] < ref[0] ||
g.Version[0] == ref[0] && g.Version[1] < ref[1]:
// FIXME(sbinet): handle version negotiations as per
// https://rfc.zeromq.org/spec:23/ZMTP/#version-negotiation
return false
default:
return false
}
}

const (
sysSockType = "Socket-Type"
sysSockID = "Identity"
Expand Down
38 changes: 35 additions & 3 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,43 @@ func TestGreeting(t *testing.T) {
want: xerrors.Errorf("invalid ZMTP signature footer: %w", errGreeting),
},
{
name: "invalid-version", // FIXME(sbinet): adapt for when/if we support multiple ZMTP versions
name: "higher-major-version",
data: func() []byte {
w := new(bytes.Buffer)
g := greeting{
Version: [2]uint8{1, 1},
Version: [2]uint8{defaultVersion[0] + 1, defaultVersion[1]},
}
g.Sig.Header = sigHeader
g.Sig.Footer = sigFooter
err := g.write(w)
if err != nil {
t.Fatalf("could not marshal greeting: %+v", err)
}
return w.Bytes()
}(),
},
{
name: "higher-minor-version",
data: func() []byte {
w := new(bytes.Buffer)
g := greeting{
Version: [2]uint8{defaultVersion[0], defaultVersion[1] + 1},
}
g.Sig.Header = sigHeader
g.Sig.Footer = sigFooter
err := g.write(w)
if err != nil {
t.Fatalf("could not marshal greeting: %+v", err)
}
return w.Bytes()
}(),
},
{
name: "smaller-major-version", // FIXME(sbinet): adapt for when/if we support multiple ZMTP versions
data: func() []byte {
w := new(bytes.Buffer)
g := greeting{
Version: [2]uint8{defaultVersion[0] - 1, defaultVersion[1]},
}
g.Sig.Header = sigHeader
g.Sig.Footer = sigFooter
Expand All @@ -94,7 +126,7 @@ func TestGreeting(t *testing.T) {
return w.Bytes()
}(),
want: xerrors.Errorf("invalid ZMTP version (got=%v, want=%v): %w",
[2]uint{1, 1},
[2]uint8{defaultVersion[0] - 1, defaultVersion[1]},
defaultVersion,
errGreeting,
),
Expand Down

0 comments on commit 6eb16eb

Please sign in to comment.