vitess.io/vitess@v0.16.2/go/test/fuzzing/tabletserver_schema_fuzzer.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package fuzzing
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  	"testing"
    20  
    21  	"vitess.io/vitess/go/mysql/fakesqldb"
    22  	"vitess.io/vitess/go/sqltypes"
    23  	"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
    24  	"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
    25  	"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
    26  
    27  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    28  )
    29  
    30  var initter sync.Once
    31  
    32  func FuzzLoadTable(data []byte) int {
    33  	initter.Do(initTesting)
    34  	f := fuzz.NewConsumer(data)
    35  	tableName, err := f.GetString()
    36  	if err != nil {
    37  		return 0
    38  	}
    39  	comment, err := f.GetString()
    40  	if err != nil {
    41  		return 0
    42  	}
    43  	query, err := f.GetSQLString()
    44  	if err != nil {
    45  		return 0
    46  	}
    47  
    48  	t := &testing.T{}
    49  
    50  	db := fakesqldb.New(t)
    51  	defer db.Close()
    52  	db.AddQuery(query, &sqltypes.Result{})
    53  
    54  	_, _ = newTestLoadTable(tableName, comment, db)
    55  	return 1
    56  }
    57  
    58  func newTestLoadTable(tableName, comment string, db *fakesqldb.DB) (*schema.Table, error) {
    59  	ctx := context.Background()
    60  	appParams := db.ConnParams()
    61  	dbaParams := db.ConnParams()
    62  	connPool := connpool.NewPool(tabletenv.NewEnv(nil, "SchemaTest"), "", tabletenv.ConnPoolConfig{
    63  		Size:               2,
    64  		IdleTimeoutSeconds: 10,
    65  	})
    66  	connPool.Open(appParams, dbaParams, appParams)
    67  	conn, err := connPool.Get(ctx, nil)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	defer conn.Recycle()
    72  
    73  	return schema.LoadTable(conn, "fakesqldb", tableName, comment)
    74  }