github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/pgwire/hba/fuzz.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // +build gofuzz
    12  
    13  package hba
    14  
    15  import (
    16  	"fmt"
    17  
    18  	"github.com/kr/pretty"
    19  )
    20  
    21  func FuzzParseAndNormalize(data []byte) int {
    22  	conf, err := ParseAndNormalize(string(data))
    23  	if err != nil {
    24  		return 0
    25  	}
    26  	s := conf.String()
    27  	conf2, err := ParseAndNormalize(s)
    28  	if err != nil {
    29  		panic(fmt.Errorf(`-- original:
    30  %s
    31  -- parsed:
    32  %# v
    33  -- new:
    34  %s
    35  -- error:
    36  %v`,
    37  			string(data),
    38  			pretty.Formatter(conf),
    39  			s,
    40  			err))
    41  	}
    42  	s2 := conf2.String()
    43  	if s != s2 {
    44  		panic(fmt.Errorf(`reparse mismatch:
    45  -- original:
    46  %s
    47  -- new:
    48  %# v
    49  -- printed:
    50  %s`,
    51  			s,
    52  			pretty.Formatter(conf2),
    53  			s2))
    54  	}
    55  	return 1
    56  }