cuelang.org/go@v0.10.1/cue/parser/fuzz_test.go (about)

     1  // Copyright 2019 CUE Authors
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package parser_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"cuelang.org/go/cue/parser"
    21  )
    22  
    23  func FuzzParseFile(f *testing.F) {
    24  	// Add a wide sample of different kinds of supported syntax.
    25  	f.Add([]byte(`
    26  package p
    27  
    28  import "foo"
    29  import b "bar"
    30  import . "baz"
    31  `))
    32  	f.Add([]byte(`
    33  // some comment
    34  // group // here
    35  `))
    36  	f.Add([]byte(`"some string"`))
    37  	f.Add([]byte(`[1, 2.3, 4M, 5Gi]`))
    38  	f.Add([]byte(`if foo { if bar if baz { x } }`))
    39  	f.Add([]byte(`[x for x in [a, b, c]]`))
    40  	f.Add([]byte(`foo: "bar": (baz): "\(x)": y`))
    41  	f.Add([]byte(`{x: _, y: _|_}`))
    42  	f.Add([]byte(`3 & int32`))
    43  	f.Add([]byte(`string | *"foo"`))
    44  	f.Add([]byte(`let x = y`))
    45  	f.Add([]byte(`[1+1, 2-2, 3*3, 4/4]`))
    46  	f.Add([]byte(`[1>1, 2>=2, 3==3, 4!=4]`))
    47  	f.Add([]byte(`[=~"^a"]: bool`))
    48  	f.Add([]byte(`[X=string]: Y={}`))
    49  	f.Add([]byte(`[len(x), close(y), and([]), or([])]`))
    50  	f.Add([]byte(`[null, bool, float, bytes, int16, uint128]`))
    51  	f.Add([]byte(`[ [...string], {x: string, ...}]]`))
    52  	f.Add([]byte(`{regular: x, required!: x, optional?: x}`))
    53  	f.Add([]byte(`{_hidden: x, #Definition: x, αβ: x}`))
    54  	f.Add([]byte(`["\u65e5本\U00008a9e", '\xff\u00FF']`))
    55  	f.Add([]byte(`["\(expr)", #"\#(expr) \(notexpr)"#]`))
    56  	f.Add([]byte(`{@jsonschema(id="foo"), field: string @go(Field)}`))
    57  	f.Fuzz(func(t *testing.T, b []byte) {
    58  		_, err := parser.ParseFile("fuzz.cue", b)
    59  		if err != nil {
    60  			t.Skip()
    61  		}
    62  	})
    63  }