File: main_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (158 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"sync"
	"testing"

	"golang.org/x/tools/internal/testenv"
)

func TestMain(m *testing.M) {
	if os.Getenv("GO_FILE2FUZZ_TEST_IS_FILE2FUZZ") != "" {
		main()
		os.Exit(0)
	}

	os.Exit(m.Run())
}

var f2f struct {
	once sync.Once
	path string
	err  error
}

func file2fuzz(t *testing.T, dir string, args []string, stdin string) (string, bool) {
	testenv.NeedsExec(t)

	f2f.once.Do(func() {
		f2f.path, f2f.err = os.Executable()
	})
	if f2f.err != nil {
		t.Fatal(f2f.err)
	}

	cmd := exec.Command(f2f.path, args...)
	cmd.Dir = dir
	cmd.Env = append(os.Environ(), "PWD="+dir, "GO_FILE2FUZZ_TEST_IS_FILE2FUZZ=1")
	if stdin != "" {
		cmd.Stdin = strings.NewReader(stdin)
	}
	out, err := cmd.CombinedOutput()
	if err != nil {
		return string(out), true
	}
	return string(out), false
}

func TestFile2Fuzz(t *testing.T) {
	type file struct {
		name    string
		dir     bool
		content string
	}
	tests := []struct {
		name           string
		args           []string
		stdin          string
		inputFiles     []file
		expectedStdout string
		expectedFiles  []file
		expectedError  string
	}{
		{
			name:           "stdin, stdout",
			stdin:          "hello",
			expectedStdout: "go test fuzz v1\n[]byte(\"hello\")",
		},
		{
			name:          "stdin, output file",
			stdin:         "hello",
			args:          []string{"-o", "output"},
			expectedFiles: []file{{name: "output", content: "go test fuzz v1\n[]byte(\"hello\")"}},
		},
		{
			name:          "stdin, output directory",
			stdin:         "hello",
			args:          []string{"-o", "output"},
			inputFiles:    []file{{name: "output", dir: true}},
			expectedFiles: []file{{name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"}},
		},
		{
			name:          "input file, output file",
			args:          []string{"-o", "output", "input"},
			inputFiles:    []file{{name: "input", content: "hello"}},
			expectedFiles: []file{{name: "output", content: "go test fuzz v1\n[]byte(\"hello\")"}},
		},
		{
			name:          "input file, output directory",
			args:          []string{"-o", "output", "input"},
			inputFiles:    []file{{name: "output", dir: true}, {name: "input", content: "hello"}},
			expectedFiles: []file{{name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"}},
		},
		{
			name:       "input files, output directory",
			args:       []string{"-o", "output", "input", "input-2"},
			inputFiles: []file{{name: "output", dir: true}, {name: "input", content: "hello"}, {name: "input-2", content: "hello :)"}},
			expectedFiles: []file{
				{name: "output/ffc7b87a0377262d4f77926bd235551d78e6037bbe970d81ec39ac1d95542f7b", content: "go test fuzz v1\n[]byte(\"hello\")"},
				{name: "output/28059db30ce420ff65b2c29b749804c69c601aeca21b3cbf0644244ff080d7a5", content: "go test fuzz v1\n[]byte(\"hello :)\")"},
			},
		},
		{
			name:          "input files, no output",
			args:          []string{"input", "input-2"},
			inputFiles:    []file{{name: "output", dir: true}, {name: "input", content: "hello"}, {name: "input-2", content: "hello :)"}},
			expectedError: "file2fuzz: -o required with multiple input files\n",
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			tmp, err := os.MkdirTemp(os.TempDir(), "file2fuzz")
			if err != nil {
				t.Fatalf("os.MkdirTemp failed: %s", err)
			}
			defer os.RemoveAll(tmp)
			for _, f := range tc.inputFiles {
				if f.dir {
					if err := os.Mkdir(filepath.Join(tmp, f.name), 0777); err != nil {
						t.Fatalf("failed to create test directory: %s", err)
					}
				} else {
					if err := os.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
						t.Fatalf("failed to create test input file: %s", err)
					}
				}
			}

			out, failed := file2fuzz(t, tmp, tc.args, tc.stdin)
			if failed && tc.expectedError == "" {
				t.Fatalf("file2fuzz failed unexpectedly: %s", out)
			} else if failed && out != tc.expectedError {
				t.Fatalf("file2fuzz returned unexpected error: got %q, want %q", out, tc.expectedError)
			}
			if !failed && out != tc.expectedStdout {
				t.Fatalf("file2fuzz unexpected stdout: got %q, want %q", out, tc.expectedStdout)
			}

			for _, f := range tc.expectedFiles {
				c, err := os.ReadFile(filepath.Join(tmp, f.name))
				if err != nil {
					t.Fatalf("failed to read expected output file %q: %s", f.name, err)
				}
				if string(c) != f.content {
					t.Fatalf("expected output file %q contains unexpected content: got %s, want %s", f.name, string(c), f.content)
				}
			}
		})
	}
}