vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/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 vreplication
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/sqltypes"
    26  	"vitess.io/vitess/go/sync2"
    27  	"vitess.io/vitess/go/vt/binlog/binlogplayer"
    28  	"vitess.io/vitess/go/vt/mysqlctl/fakemysqldaemon"
    29  	"vitess.io/vitess/go/vt/topo/memorytopo"
    30  
    31  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    32  )
    33  
    34  var initter sync.Once
    35  
    36  func initTesting() {
    37  	testing.Init()
    38  }
    39  
    40  func createQueries(f *fuzz.ConsumeFuzzer) ([]string, error) {
    41  	query1, err := f.GetSQLString()
    42  	if err != nil {
    43  		return []string{}, err
    44  	}
    45  	query2, err := f.GetSQLString()
    46  	if err != nil {
    47  		return []string{}, err
    48  	}
    49  	query3, err := f.GetSQLString()
    50  	if err != nil {
    51  		return []string{}, err
    52  	}
    53  	query4, err := f.GetSQLString()
    54  	if err != nil {
    55  		return []string{}, err
    56  	}
    57  	query5, err := f.GetSQLString()
    58  	if err != nil {
    59  		return []string{}, err
    60  	}
    61  	return []string{query1, query2, query3, query4, query5}, nil
    62  }
    63  
    64  func makeExpectations(dbClient *binlogplayer.MockDBClient, f *fuzz.ConsumeFuzzer) error {
    65  	noOfExpects, err := f.GetInt()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if noOfExpects%15 == 0 {
    70  		return fmt.Errorf("len is 0")
    71  	}
    72  	for i := 0; i < noOfExpects%15; i++ {
    73  		query, err := f.GetString()
    74  		if err != nil {
    75  			return err
    76  		}
    77  		dbClient.ExpectRequest(query, &sqltypes.Result{}, nil)
    78  	}
    79  	return nil
    80  }
    81  
    82  func FuzzEngine(data []byte) int {
    83  	initter.Do(initTesting)
    84  	t := &testing.T{}
    85  	f := fuzz.NewConsumer(data)
    86  	queries, err := createQueries(f)
    87  	if err != nil {
    88  		return 0
    89  	}
    90  	topoServer := memorytopo.NewServer("cell1")
    91  	_ = topoServer
    92  	defer func() { globalStats = &vrStats{} }()
    93  
    94  	resetBinlogClient()
    95  	dbClient := binlogplayer.NewMockDBClient(t)
    96  	dbClientFactory := func() binlogplayer.DBClient { return dbClient }
    97  	mysqld := &fakemysqldaemon.FakeMysqlDaemon{MysqlPort: sync2.NewAtomicInt32(3306)}
    98  
    99  	vre := NewTestEngine(topoServer, "cell1", mysqld, dbClientFactory, dbClientFactory, dbClient.DBName(), nil)
   100  
   101  	// Fuzzer fails if this expectation is not made first:
   102  	dbClient.ExpectRequest("select * from _vt.vreplication where db_name='db'", &sqltypes.Result{}, nil)
   103  	err = makeExpectations(dbClient, f)
   104  	if err != nil {
   105  		return 0
   106  	}
   107  	vre.Open(context.Background())
   108  	defer vre.Close()
   109  	if !vre.IsOpen() {
   110  		return 0
   111  	}
   112  	_, _ = vre.Exec(queries[0])
   113  	_, _ = vre.Exec(queries[1])
   114  	_, _ = vre.Exec(queries[2])
   115  	_, _ = vre.Exec(queries[3])
   116  	_, _ = vre.Exec(queries[4])
   117  
   118  	return 1
   119  }