git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/toml/fuzz_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package toml
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  )
    10  
    11  func FuzzDecode(f *testing.F) {
    12  	buf := make([]byte, 0, 2048)
    13  
    14  	f.Add(`
    15  # This is an example TOML document which shows most of its features.
    16  
    17  # Simple key/value with a string.
    18  title = "TOML example \U0001F60A"
    19  
    20  desc = """
    21  An example TOML document. \
    22  """
    23  
    24  # Array with integers and floats in the various allowed formats.
    25  integers = [42, 0x42, 0o42, 0b0110]
    26  floats   = [1.42, 1e-02]
    27  
    28  # Array with supported datetime formats.
    29  times = [
    30  	2021-11-09T15:16:17+01:00,  # datetime with timezone.
    31  	2021-11-09T15:16:17Z,       # UTC datetime.
    32  	2021-11-09T15:16:17,        # local datetime.
    33  	2021-11-09,                 # local date.
    34  	15:16:17,                   # local time.
    35  ]
    36  
    37  # Durations.
    38  duration = ["4m49s", "8m03s", "1231h15m55s"]
    39  
    40  # Table with inline tables.
    41  distros = [
    42  	{name = "Arch Linux", packages = "pacman"},
    43  	{name = "Void Linux", packages = "xbps"},
    44  	{name = "Debian",     packages = "apt"},
    45  ]
    46  
    47  # Create new table; note the "servers" table is created implicitly.
    48  [servers.alpha]
    49  	# You can indent as you please, tabs or spaces.
    50  	ip        = '10.0.0.1'
    51  	hostname  = 'server1'
    52  	enabled   = false
    53  [servers.beta]
    54  	ip        = '10.0.0.2'
    55  	hostname  = 'server2'
    56  	enabled   = true
    57  
    58  # Start a new table array; note that the "characters" table is created implicitly.
    59  [[characters.star-trek]]
    60  	name = "James Kirk"
    61  	rank = "Captain\u0012 \t"
    62  [[characters.star-trek]]
    63  	name = "Spock"
    64  	rank = "Science officer"
    65  
    66  [undecoded] # To show the MetaData.Undecoded() feature.
    67  	key = "This table intentionally left undecoded"
    68  `)
    69  	f.Fuzz(func(t *testing.T, file string) {
    70  		var m map[string]interface{}
    71  		_, err := Decode(file, &m)
    72  		if err != nil {
    73  			t.Skip()
    74  		}
    75  
    76  		NewEncoder(bytes.NewBuffer(buf)).Encode(m)
    77  
    78  		// TODO: should check if the output is equal to the input, too, but some
    79  		// information is lost when encoding.
    80  	})
    81  }