-
Notifications
You must be signed in to change notification settings - Fork 2
/
usage_test.go
64 lines (59 loc) · 1.78 KB
/
usage_test.go
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
package main
import (
"strings"
"testing"
"github.com/markdingo/autoreverse/log"
"github.com/markdingo/autoreverse/mock"
)
func TestUsage(t *testing.T) {
out := &mock.IOWriter{}
log.SetOut(out)
cfg := newConfig()
ar := newAutoReverse(cfg, nil)
testCases := []struct {
options string
expect string
result parseResult
}{
{"", "", parseContinue},
{"-h", "SYNOPSIS", parseStop},
{"--help", "SYNOPSIS", parseStop},
{"-v", "Program:", parseStop},
{"--version", "Program:", parseStop},
{"--manpage", ".Sh NAME", parseStop},
{"goop", "goop", parseFailed},
{"-X", "unknown shorthand flag", parseFailed},
{"--forward o.example.net --forward t.example.net", "Duplicate option", parseFailed},
{"--listen 127.0.0.1 --listen ::1", "", parseContinue}, // This duplicate is ok
{"--forward a.v --reverse 127.0.0.1/24" +
" --listen ::1 --listen 127.0.0.1" +
" --PTR-deduce http://url --PTR-deduce axfr://url" +
" --passthru a-server --synthesize=true --CHAOS=true" +
" --NSID myname --TTL 45m" +
" --user u --group g --chroot /root" +
" --log-major --log-minor --log-debug=true" +
" --log-queries=false --report 4h", "", parseContinue}, // Every legit option
}
for ix, tc := range testCases {
var opts []string
if len(tc.options) > 0 {
opts = strings.Split(tc.options, " ")
}
args := []string{programName}
args = append(args, opts...)
out.Reset()
res := ar.parseOptions(args)
if res != tc.result {
t.Error(ix, "Results mismatch. Want", tc.result, "got", res)
}
got := out.String()
if len(tc.expect) == 0 && len(got) != 0 {
t.Error(ix, "Did not expect any output, but got", len(got), got)
}
if len(tc.expect) > 0 {
if !strings.Contains(got, tc.expect) {
t.Error(ix, "Output does not contain", tc.expect, "got", got)
}
}
}
}