github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/fuzz.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // +build gofuzz
    12  
    13  package pgwire
    14  
    15  import (
    16  	"context"
    17  	"io"
    18  	"io/ioutil"
    19  	"net"
    20  	"time"
    21  
    22  	"github.com/cockroachdb/cockroach/pkg/base"
    23  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    24  	"github.com/cockroachdb/cockroach/pkg/sql"
    25  	"github.com/cockroachdb/cockroach/pkg/util/log"
    26  	"github.com/cockroachdb/cockroach/pkg/util/mon"
    27  )
    28  
    29  func FuzzServeConn(data []byte) int {
    30  	s := MakeServer(
    31  		log.AmbientContext{},
    32  		&base.Config{},
    33  		&cluster.Settings{},
    34  		sql.MemoryMetrics{},
    35  		&mon.BytesMonitor{},
    36  		time.Minute,
    37  		&sql.ExecutorConfig{
    38  			Settings: &cluster.Settings{},
    39  		},
    40  	)
    41  
    42  	// Fake a connection using a pipe.
    43  	srv, client := net.Pipe()
    44  	go func() {
    45  		// Write the fuzz data to the connection and close.
    46  		_, _ = client.Write(data)
    47  		_ = client.Close()
    48  	}()
    49  	go func() {
    50  		// Discard all data sent from the server.
    51  		_, _ = io.Copy(ioutil.Discard, client)
    52  	}()
    53  	err := s.ServeConn(context.Background(), srv)
    54  	if err != nil {
    55  		return 0
    56  	}
    57  	return 1
    58  }