Debian Codegraph @

cmd/file2fuzz/main_test.go

raw symbols SCIP
1 // Copyright 2021 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 package mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/
6
7 import (
8 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
9 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
10 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
11 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
12 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
13 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
14
15 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
16 )
17
18 func TestMainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/TestMain().(m *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Mscip-go gomod github.com/golang/go/src go1.25.0 testing/M#) {
19 if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GO_FILE2FUZZ_TEST_IS_FILE2FUZZ") != "" {
20 mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/main().()
21 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(0)
22 }
23
24 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(m.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/M#Run().())
25 }
26
27 var f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f. struct {
28 oncescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:once. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.0 sync/Once#
29 pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:path. string
30 errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:err. error
31 }
32
33 func file2fuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/file2fuzz().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, dir string, args []string, stdin string) (string, bool) {
34 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t)
35
36 f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..oncescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:once..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() {
37 f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:path., f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:err. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Executablescip-go gomod github.com/golang/go/src go1.25.0 os/Executable().()
38 })
39 if f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:err. != nil {
40 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:err.)
41 }
42
43 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(f2fscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f..pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/f2f:path., args...)
44 cmd.Dirscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Dir. = dir
45 cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().(), "PWD="+dir, "GO_FILE2FUZZ_TEST_IS_FILE2FUZZ=1")
46 if stdin != "" {
47 cmd.Stdinscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdin. = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReader().(stdin)
48 }
49 out, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().()
50 if err != nil {
51 return string(out), true
52 }
53 return string(out), false
54 }
55
56 func TestFile2Fuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/TestFile2Fuzz().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
57 type file struct {
58 name string
59 dir bool
60 content string
61 }
62 tests := []struct {
63 name string
64 args []string
65 stdin string
66 inputFiles []file
67 expectedStdout string
68 expectedFiles []file
69 expectedError string
70 }{
71 {
72 name: "stdin, stdout",
73 stdin: "hello",
74 expectedStdout: "go test fuzz v1\n[]byte(\"hello\")",
75 },
76 {
77 name: "stdin, output file",
78 stdin: "hello",
79 args: []string{"-o", "output"},
80 expectedFiles: []file{{name: "output", content: "go test fuzz v1\n[]byte(\"hello\")"}},
81 },
82 {
83 name: "stdin, output directory",
84 stdin: "hello",
85 args: []string{"-o", "output"},
86 inputFiles: []file{{name: "output", dir: true}},
87 expectedFiles: []file{{name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"}},
88 },
89 {
90 name: "input file, output file",
91 args: []string{"-o", "output", "input"},
92 inputFiles: []file{{name: "input", content: "hello"}},
93 expectedFiles: []file{{name: "output", content: "go test fuzz v1\n[]byte(\"hello\")"}},
94 },
95 {
96 name: "input file, output directory",
97 args: []string{"-o", "output", "input"},
98 inputFiles: []file{{name: "output", dir: true}, {name: "input", content: "hello"}},
99 expectedFiles: []file{{name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"}},
100 },
101 {
102 name: "input files, output directory",
103 args: []string{"-o", "output", "input", "input-2"},
104 inputFiles: []file{{name: "output", dir: true}, {name: "input", content: "hello"}, {name: "input-2", content: "hello :)"}},
105 expectedFiles: []file{
106 {name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"},
107 {name: "output/28059db30ce420ff65b2c29b749804c69c601aeca21b3cbf0644244ff080d7a5", content: "go test fuzz v1\n[]byte(\"hello :)\")"},
108 },
109 },
110 {
111 name: "input files, no output",
112 args: []string{"input", "input-2"},
113 inputFiles: []file{{name: "output", dir: true}, {name: "input", content: "hello"}, {name: "input-2", content: "hello :)"}},
114 expectedError: "file2fuzz: -o required with multiple input files\n",
115 },
116 }
117
118 for _, tc := range tests {
119 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
120 tmp, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirTemp().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.25.0 os/TempDir().(), "file2fuzz")
121 if err != nil {
122 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("os.MkdirTemp failed: %s", err)
123 }
124 defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.0 os/RemoveAll().(tmp)
125 for _, f := range tc.inputFiles {
126 if f.dir {
127 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(tmp, f.name), 0777); err != nil {
128 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to create test directory: %s", err)
129 }
130 } else {
131 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(tmp, f.name), []byte(f.content), 0666); err != nil {
132 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to create test input file: %s", err)
133 }
134 }
135 }
136
137 out, failed := file2fuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/file2fuzz`/file2fuzz().(t, tmp, tc.args, tc.stdin)
138 if failed && tc.expectedError == "" {
139 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("file2fuzz failed unexpectedly: %s", out)
140 } else if failed && out != tc.expectedError {
141 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("file2fuzz returned unexpected error: got %q, want %q", out, tc.expectedError)
142 }
143 if !failed && out != tc.expectedStdout {
144 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("file2fuzz unexpected stdout: got %q, want %q", out, tc.expectedStdout)
145 }
146
147 for _, f := range tc.expectedFiles {
148 c, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(tmp, f.name))
149 if err != nil {
150 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to read expected output file %q: %s", f.name, err)
151 }
152 if string(c) != f.content {
153 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected output file %q contains unexpected content: got %s, want %s", f.name, string(c), f.content)
154 }
155 }
156 })
157 }
158 }