Repository navigation
Expand file tree
/
Copy pathmain_cli_plugins_test.go
More file actions
131 lines (124 loc) · 4.1 KB
/
Copy pathmain_cli_plugins_test.go
File metadata and controls
131 lines (124 loc) · 4.1 KB
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
//go:build !web && !plugin_selective
package main
import (
"bytes"
"flag"
"io"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/shadow1ng/fscan/common"
"github.com/shadow1ng/fscan/common/config"
"github.com/shadow1ng/fscan/plugins"
)
func TestCLIPluginDefaultPorts(t *testing.T) {
portsFile := filepath.Join(t.TempDir(), "ports.txt")
if err := os.WriteFile(portsFile, []byte("9139\n8445\n"), 0600); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
args []string
want string
}{
{name: "netbios", args: []string{"-m", "netbios"}, want: "137,139"},
{name: "multiple plugins", args: []string{"-m", "ssh, netbios,ssh,"}, want: "22,137,139,2200,2222,22222"},
{name: "nonstandard port", args: []string{"-m", "ssh", "-p", "2222"}, want: "2222"},
{name: "explicit common ports", args: []string{"-m", "netbios", "-p", config.MainPorts}, want: config.MainPorts},
{name: "ports file", args: []string{"-m", "netbios", "-pf", portsFile}, want: "9139,8445"},
{name: "all", args: []string{"-m", "all"}, want: config.MainPorts},
{name: "web plugin", args: []string{"-m", "webtitle"}, want: config.MainPorts},
{name: "web and service", args: []string{"-m", "ssh,webtitle"}, want: config.MainPorts},
{name: "unknown plugin", args: []string{"-m", "ssh,unknown-plugin"}, want: config.MainPorts},
{name: "alive only", args: []string{"-m", "netbios", "-ao"}, want: config.MainPorts},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setCLIArgs(t, append([]string{"-h", "127.0.0.1", "-silent"}, tt.args...)...)
info := &common.HostInfo{}
if err := common.Flag(info); err != nil {
t.Fatal(err)
}
setPluginDefaultPorts(common.GetFlagVars())
cfg, _, err := common.BuildConfig(common.GetFlagVars(), info)
if err != nil {
t.Fatal(err)
}
if cfg.Target.Ports != tt.want {
t.Errorf("ports = %q, want %q", cfg.Target.Ports, tt.want)
}
})
}
}
func TestPrintPluginList(t *testing.T) {
var output bytes.Buffer
if err := printPluginList(&output); err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(output.String()), "\n")
names := plugins.All()
slices.Sort(names)
if len(lines) != len(names)+1 {
t.Fatalf("got %d rows for %d plugins", len(lines)-1, len(names))
}
for i, name := range names {
fields := strings.Fields(lines[i+1])
if len(fields) != 5 {
t.Fatalf("invalid plugin row: %q", lines[i+1])
}
if fields[0] != name || fields[len(fields)-1] != name {
t.Errorf("plugin row = %q, want %s", lines[i+1], name)
}
if name == "netbios" && strings.Join(fields, " ") != "netbios service 137,139 -m netbios" {
t.Errorf("netbios metadata = %q", lines[i+1])
}
if plugins.HasType(name, plugins.PluginTypeLocal) && fields[len(fields)-2] != "-local" {
t.Errorf("local plugin usage = %q", lines[i+1])
}
}
}
func TestCLIListPluginsExitsWithoutTargetOrOutput(t *testing.T) {
dir := t.TempDir()
outputFile := filepath.Join(dir, "result.txt")
setCLIArgs(t, "-list-plugins", "-silent", "-o", outputFile)
output, err := os.CreateTemp(dir, "stdout-")
if err != nil {
t.Fatal(err)
}
defer output.Close()
previousStdout := os.Stdout
os.Stdout = output
t.Cleanup(func() { os.Stdout = previousStdout })
if code := run(); code != 0 {
t.Fatalf("exit code = %d", code)
}
if _, err := os.Stat(outputFile); !os.IsNotExist(err) {
t.Fatalf("listing initialized scan output: %v", err)
}
if _, err := output.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(output)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "netbios") || !strings.Contains(string(data), "137,139") {
t.Fatalf("plugin list missing from output: %s", data)
}
}
func setCLIArgs(t *testing.T, args ...string) {
t.Helper()
previousArgs := os.Args
previousFlagSet := flag.CommandLine
previousFlags := *common.GetFlagVars()
t.Cleanup(func() {
os.Args = previousArgs
flag.CommandLine = previousFlagSet
*common.GetFlagVars() = previousFlags
})
os.Args = append([]string{"fscan-test"}, args...)
flag.CommandLine = flag.NewFlagSet("fscan-test", flag.ContinueOnError)
*common.GetFlagVars() = common.FlagVars{}
}