From b171491a7fcc0eab73dc8f37472a6b956d5f848d Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 21 Apr 2022 14:29:21 +0100 Subject: [PATCH] return an ErrNotSupported when lazy negotiation fails --- lazyClient.go | 4 ++++ multistream_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lazyClient.go b/lazyClient.go index 202fd64..76d79ff 100644 --- a/lazyClient.go +++ b/lazyClient.go @@ -77,6 +77,10 @@ func (l *lazyClientConn) doReadHandshake() { return } + if tok == "na" { + l.rerr = ErrNotSupported + return + } if tok != proto { l.rerr = fmt.Errorf("protocol mismatch in lazy handshake ( %s != %s )", tok, proto) return diff --git a/multistream_test.go b/multistream_test.go index 5668fa7..8b3c4ec 100644 --- a/multistream_test.go +++ b/multistream_test.go @@ -149,6 +149,26 @@ func TestProtocolNegotiationLazy(t *testing.T) { verifyPipe(t, ac, b) } +func TestProtocolNegotiationUnsupported(t *testing.T) { + a, b := newPipe(t) + mux := NewMultistreamMuxer() + + done := make(chan struct{}) + go func() { + defer close(done) + mux.Negotiate(a) + }() + + c := NewMSSelect(b, "/foo") + c.Write([]byte("foo protocol data")) + _, err := c.Read([]byte{0}) + if err != ErrNotSupported { + t.Fatalf("expected protocol /foo to be unsupported, got: %v", err) + } + c.Close() + <-done +} + func TestNegLazyStressRead(t *testing.T) { const count = 75