Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zmq4: extend ZMTP greeting tests for various version cases #61

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ language: go
go:
- 1.13.x
- 1.12.x
- 1.11.x
- master
os:
- linux
Expand Down
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