vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/fuzz.go (about)

     1  //go:build gofuzz
     2  // +build gofuzz
     3  
     4  /*
     5  Copyright 2021 The Vitess Authors.
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tabletserver
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"testing"
    23  
    24  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    25  
    26  	"vitess.io/vitess/go/mysql/fakesqldb"
    27  	"vitess.io/vitess/go/sqltypes"
    28  	"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
    29  	"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
    30  )
    31  
    32  var initter sync.Once
    33  
    34  func initForFuzzing() {
    35  	testing.Init()
    36  }
    37  
    38  // FuzzGetPlan implements a fuzzer that tests GetPlan.
    39  func FuzzGetPlan(data []byte) int {
    40  	initter.Do(initForFuzzing)
    41  	t := &testing.T{}
    42  	f := fuzz.NewConsumer(data)
    43  	query1, err := f.GetSQLString()
    44  	if err != nil {
    45  		return 0
    46  	}
    47  	query2, err := f.GetString()
    48  	if err != nil {
    49  		return 0
    50  	}
    51  	db := fakesqldb.New(t)
    52  	defer db.Close()
    53  
    54  	// Add a query
    55  	db.AddQuery(query1, &sqltypes.Result{})
    56  
    57  	// Set up the environment
    58  	config := tabletenv.NewDefaultConfig()
    59  	config.DB = newDBConfigs(db)
    60  	env := tabletenv.NewEnv(config, "TabletServerTest")
    61  	se := schema.NewEngine(env)
    62  	qe := NewQueryEngine(env, se)
    63  	defer qe.Close()
    64  	qe.Open()
    65  
    66  	logStats := tabletenv.NewLogStats(context.Background(), "GetPlanStats")
    67  	qe.SetQueryPlanCacheCap(1024)
    68  
    69  	// Call target
    70  	_, _ = qe.GetPlan(context.Background(), logStats, query2, true)
    71  	return 1
    72  }