...

Source file src/cmd/go/internal/work/shell_test.go

Documentation: cmd/go/internal/work

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build unix
     6  
     7  package work
     8  
     9  import (
    10  	"bytes"
    11  	"internal/testenv"
    12  	"strings"
    13  	"testing"
    14  	"unicode"
    15  )
    16  
    17  func FuzzSplitPkgConfigOutput(f *testing.F) {
    18  	testenv.MustHaveExecPath(f, "/bin/sh")
    19  
    20  	f.Add([]byte(`$FOO`))
    21  	f.Add([]byte(`\$FOO`))
    22  	f.Add([]byte(`${FOO}`))
    23  	f.Add([]byte(`\${FOO}`))
    24  	f.Add([]byte(`$(/bin/false)`))
    25  	f.Add([]byte(`\$(/bin/false)`))
    26  	f.Add([]byte(`$((0))`))
    27  	f.Add([]byte(`\$((0))`))
    28  	f.Add([]byte(`unescaped space`))
    29  	f.Add([]byte(`escaped\ space`))
    30  	f.Add([]byte(`"unterminated quote`))
    31  	f.Add([]byte(`'unterminated quote`))
    32  	f.Add([]byte(`unterminated escape\`))
    33  	f.Add([]byte(`"quote with unterminated escape\`))
    34  	f.Add([]byte(`'quoted "double quotes"'`))
    35  	f.Add([]byte(`"quoted 'single quotes'"`))
    36  	f.Add([]byte(`"\$0"`))
    37  	f.Add([]byte(`"\$\0"`))
    38  	f.Add([]byte(`"\$"`))
    39  	f.Add([]byte(`"\$ "`))
    40  
    41  	// Example positive inputs from TestSplitPkgConfigOutput.
    42  	// Some bare newlines have been removed so that the inputs
    43  	// are valid in the shell script we use for comparison.
    44  	f.Add([]byte(`-r:foo -L/usr/white\ space/lib -lfoo\ bar -lbar\ baz`))
    45  	f.Add([]byte(`-lextra\ fun\ arg\\`))
    46  	f.Add([]byte("\textra     whitespace\r"))
    47  	f.Add([]byte("     \r      "))
    48  	f.Add([]byte(`"-r:foo" "-L/usr/white space/lib" "-lfoo bar" "-lbar baz"`))
    49  	f.Add([]byte(`"-lextra fun arg\\"`))
    50  	f.Add([]byte(`"     \r\n\      "`))
    51  	f.Add([]byte(`""`))
    52  	f.Add([]byte(``))
    53  	f.Add([]byte(`"\\"`))
    54  	f.Add([]byte(`"\x"`))
    55  	f.Add([]byte(`"\\x"`))
    56  	f.Add([]byte(`'\\'`))
    57  	f.Add([]byte(`'\x'`))
    58  	f.Add([]byte(`"\\x"`))
    59  	f.Add([]byte("\\\n"))
    60  	f.Add([]byte(`-fPIC -I/test/include/foo -DQUOTED='"/test/share/doc"'`))
    61  	f.Add([]byte(`-fPIC -I/test/include/foo -DQUOTED="/test/share/doc"`))
    62  	f.Add([]byte(`-fPIC -I/test/include/foo -DQUOTED=\"/test/share/doc\"`))
    63  	f.Add([]byte(`-fPIC -I/test/include/foo -DQUOTED='/test/share/doc'`))
    64  	f.Add([]byte(`-DQUOTED='/te\st/share/d\oc'`))
    65  	f.Add([]byte(`-Dhello=10 -Dworld=+32 -DDEFINED_FROM_PKG_CONFIG=hello\ world`))
    66  	f.Add([]byte(`"broken\"" \\\a "a"`))
    67  
    68  	// Example negative inputs from TestSplitPkgConfigOutput.
    69  	f.Add([]byte(`"     \r\n      `))
    70  	f.Add([]byte(`"-r:foo" "-L/usr/white space/lib "-lfoo bar" "-lbar baz"`))
    71  	f.Add([]byte(`"-lextra fun arg\\`))
    72  	f.Add([]byte(`broken flag\`))
    73  	f.Add([]byte(`extra broken flag \`))
    74  	f.Add([]byte(`\`))
    75  	f.Add([]byte(`"broken\"" "extra" \`))
    76  
    77  	f.Fuzz(func(t *testing.T, b []byte) {
    78  		t.Parallel()
    79  
    80  		if bytes.ContainsAny(b, "*?[#~%\x00{}!") {
    81  			t.Skipf("skipping %#q: contains a sometimes-quoted character", b)
    82  		}
    83  		// splitPkgConfigOutput itself rejects inputs that contain unquoted
    84  		// shell operator characters. (Quoted shell characters are fine.)
    85  
    86  		for _, c := range b {
    87  			if c > unicode.MaxASCII {
    88  				t.Skipf("skipping %#q: contains a non-ASCII character %q", b, c)
    89  			}
    90  			if !unicode.IsGraphic(rune(c)) && !unicode.IsSpace(rune(c)) {
    91  				t.Skipf("skipping %#q: contains non-graphic character %q", b, c)
    92  			}
    93  		}
    94  
    95  		args, err := splitPkgConfigOutput(b)
    96  		if err != nil {
    97  			// We haven't checked that the shell would actually reject this input too,
    98  			// but if splitPkgConfigOutput rejected it it's probably too dangerous to
    99  			// run in the script.
   100  			t.Logf("%#q: %v", b, err)
   101  			return
   102  		}
   103  		t.Logf("splitPkgConfigOutput(%#q) = %#q", b, args)
   104  		if len(args) == 0 {
   105  			t.Skipf("skipping %#q: contains no arguments", b)
   106  		}
   107  
   108  		var buf strings.Builder
   109  		for _, arg := range args {
   110  			buf.WriteString(arg)
   111  			buf.WriteString("\n")
   112  		}
   113  		wantOut := buf.String()
   114  
   115  		if strings.Count(wantOut, "\n") != len(args)+bytes.Count(b, []byte("\n")) {
   116  			// One of the newlines in b was treated as a delimiter and not part of an
   117  			// argument. Our bash test script would interpret that as a syntax error.
   118  			t.Skipf("skipping %#q: contains a bare newline", b)
   119  		}
   120  
   121  		// We use the printf shell command to echo the arguments because, per
   122  		// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37_16:
   123  		// “It is not possible to use echo portably across all POSIX systems unless
   124  		// both -n (as the first argument) and escape sequences are omitted.”
   125  		cmd := testenv.Command(t, "/bin/sh", "-c", "printf '%s\n' "+string(b))
   126  		cmd.Env = append(cmd.Environ(), "LC_ALL=POSIX", "POSIXLY_CORRECT=1")
   127  		cmd.Stderr = new(strings.Builder)
   128  		out, err := cmd.Output()
   129  		if err != nil {
   130  			t.Fatalf("%#q: %v\n%s", cmd.Args, err, cmd.Stderr)
   131  		}
   132  
   133  		if string(out) != wantOut {
   134  			t.Logf("%#q:\n%#q", cmd.Args, out)
   135  			t.Logf("want:\n%#q", wantOut)
   136  			t.Errorf("parsed args do not match")
   137  		}
   138  	})
   139  }
   140  

View as plain text