Skip to content

Add fuzz tests #6728

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
95 changes: 95 additions & 0 deletions server/parser_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2012-2020 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"sync"
"testing"
)

func dummyFuzzClient(kind int) *client {
var r *route
var gw *gateway
var lf *leaf

switch kind {
case ROUTER:
r = &route{}
case GATEWAY:
gw = &gateway{outbound: false, connected: true, insim: make(map[string]*insie), outsim: &sync.Map{}}
case LEAF:
lf = &leaf{}
}

return &client{
srv: New(&defaultServerOptions),
kind: kind,
msubs: -1,
in: readCache{
results: make(map[string]*SublistResult),
pacache: make(map[string]*perAccountCache),
},
mpay: MAX_PAYLOAD_SIZE,
mcl: MAX_CONTROL_LINE_SIZE,
route: r,
gw: gw,
leaf: lf,
}
}

// FuzzParser performs fuzz testing on the NATS protocol parser implementation.
// It tests the parser's ability to handle various NATS protocol messages, including
// partial (chunked) message delivery scenarios that may occur in real-world usage.
func FuzzParser(f *testing.F) {
msgs := []string{
"PING\r\n",
"PONG\r\n",
"PUB foo 33333\r\n",
"HPUB foo INBOX.22 0 5\r\nHELLO\r",
"HMSG $foo foo 10 8\r\nXXXhello\r",
"MSG $foo foo 5\r\nhello\r",
"SUB foo 1\r\nSUB foo 2\r\n",
"UNSUB 1 5\r\n",
"RMSG $G foo.bar | baz 11\r\nhello world\r",
"CONNECT {\"verbose\":false,\"pedantic\":true,\"tls_required\":false}\r\n",
}

clientKinds := []int{
CLIENT,
ROUTER,
GATEWAY,
LEAF,
}

for _, ck := range clientKinds {
for _, crp := range msgs {
f.Add(ck, crp)
}
}

f.Fuzz(func(t *testing.T, kind int, orig string) {
c := dummyFuzzClient(kind)

data := []byte(orig)
half := len(data) / 2

if err := c.parse(data[:half]); err != nil {
return
}

if err := c.parse(data[half:]); err != nil {
return
}
})
}
14 changes: 13 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,7 @@ func (s *Server) AcceptLoop(clr chan struct{}) {
// Setup state that can enable shutdown
s.mu.Lock()
hp := net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port))
l, e := natsListen("tcp", hp)
l, e := s.getServerListener(hp)
s.listenerErr = e
if e != nil {
s.mu.Unlock()
Expand Down Expand Up @@ -2761,6 +2761,18 @@ func (s *Server) AcceptLoop(clr chan struct{}) {
clr = nil
}

// getServerListener returns a network listener for the given host-port address.
// If the Server already has an active listener (s.listener), it returns that listener
// along with any previous error (s.listenerErr). Otherwise, it creates and returns
// a new TCP listener on the specified address using natsListen.
func (s *Server) getServerListener(hp string) (net.Listener, error) {
if s.listener != nil {
return s.listener, s.listenerErr
}

return natsListen("tcp", hp)
}

// InProcessConn returns an in-process connection to the server,
// avoiding the need to use a TCP listener for local connectivity
// within the same process. This can be used regardless of the
Expand Down
Loading