vitess.io/vitess@v0.16.2/go/test/fuzzing/ast_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/sqlparser" 23 ) 24 25 // FuzzEqualsSQLNode implements the fuzzer 26 func FuzzEqualsSQLNode(data []byte) int { 27 if len(data) < 10 { 28 return 0 29 } 30 f := fuzz.NewConsumer(data) 31 query1, err := f.GetSQLString() 32 if err != nil { 33 return 0 34 } 35 query2, err := f.GetSQLString() 36 if err != nil { 37 return 0 38 } 39 inA, err := sqlparser.Parse(query1) 40 if err != nil { 41 return 0 42 } 43 inB, err := sqlparser.Parse(query2) 44 if err != nil { 45 return 0 46 } 47 48 // There are 3 targets in this fuzzer: 49 // 1) sqlparser.EqualsSQLNode 50 // 2) sqlparser.CloneSQLNode 51 // 3) sqlparser.VisitSQLNode 52 53 // Target 1: 54 identical := sqlparser.EqualsSQLNode(inA, inA) 55 if !identical { 56 panic("Should be identical") 57 } 58 identical = sqlparser.EqualsSQLNode(inB, inB) 59 if !identical { 60 panic("Should be identical") 61 } 62 63 // Target 2: 64 newSQLNode := sqlparser.CloneSQLNode(inA) 65 if !sqlparser.EqualsSQLNode(inA, newSQLNode) { 66 panic("These two nodes should be identical") 67 } 68 69 // Target 3: 70 _ = sqlparser.VisitSQLNode(inA, func(node sqlparser.SQLNode) (bool, error) { return false, nil }) 71 return 1 72 }