vitess.io/vitess@v0.16.2/go/test/fuzzing/vt_schema_fuzzer.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 fuzzing
    18  
    19  import (
    20  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    21  
    22  	"vitess.io/vitess/go/vt/schema"
    23  	"vitess.io/vitess/go/vt/sqlparser"
    24  )
    25  
    26  // FuzzOnlineDDLFromCommentedStatement implements a fuzzer
    27  // that targets schema.OnlineDDLFromCommentedStatement
    28  func FuzzOnlineDDLFromCommentedStatement(data []byte) int {
    29  	stmt, err := sqlparser.Parse(string(data))
    30  	if err != nil {
    31  		return 0
    32  	}
    33  	onlineDDL, err := schema.OnlineDDLFromCommentedStatement(stmt)
    34  	if err != nil {
    35  		return 0
    36  	}
    37  	_, _ = onlineDDL.GetAction()
    38  	_, _, _ = onlineDDL.GetActionStr()
    39  	_ = onlineDDL.GetGCUUID()
    40  	return 1
    41  }
    42  
    43  // FuzzNewOnlineDDLs implements a fuzzer that
    44  // targets schema.NewOnlineDDLs
    45  func FuzzNewOnlineDDLs(data []byte) int {
    46  	f := fuzz.NewConsumer(data)
    47  
    48  	keyspace, err := f.GetString()
    49  	if err != nil {
    50  		return 0
    51  	}
    52  
    53  	ddlstmtString, err := f.GetString()
    54  	if err != nil {
    55  		return 0
    56  	}
    57  	ddlStmt, _, err := schema.ParseOnlineDDLStatement(ddlstmtString)
    58  	if err != nil {
    59  		return 0
    60  	}
    61  
    62  	sql, err := f.GetString()
    63  	if err != nil {
    64  		return 0
    65  	}
    66  
    67  	ddlStrategySetting := &schema.DDLStrategySetting{}
    68  	err = f.GenerateStruct(ddlStrategySetting)
    69  	if err != nil {
    70  		return 0
    71  	}
    72  
    73  	requestContext, err := f.GetString()
    74  	if err != nil {
    75  		return 0
    76  	}
    77  
    78  	onlineDDLs, err := schema.NewOnlineDDLs(keyspace, sql, ddlStmt, ddlStrategySetting, requestContext)
    79  	if err != nil {
    80  		return 0
    81  	}
    82  	for _, onlineDDL := range onlineDDLs {
    83  		_, _ = onlineDDL.GetAction()
    84  		_, _, _ = onlineDDL.GetActionStr()
    85  		_ = onlineDDL.GetGCUUID()
    86  	}
    87  	return 1
    88  }