From 45dc257aadf935972a8c6b31539c86f965f83474 Mon Sep 17 00:00:00 2001 From: eenblam Date: Mon, 9 Jul 2018 10:00:40 -0700 Subject: [PATCH 01/10] Add more tests to core/swag_test.go Trivial, but bumps coverage for core from 79.3% to 94.8%. (Local coverage reporting seems to differ from coveralls) --- core/swag_test.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/core/swag_test.go b/core/swag_test.go index e08221d3..3d1d99e2 100644 --- a/core/swag_test.go +++ b/core/swag_test.go @@ -1,6 +1,22 @@ package core -import "testing" +import ( + "os" + "testing" +) + +func TestIsDumbTerminal(t *testing.T) { + term := os.Getenv("TERM") + os.Setenv("TERM", "dumb") + if !isDumbTerminal() { + t.Fatal("Expected false when TERM==dumb") + } + os.Setenv("TERM", "") + if !isDumbTerminal() { + t.Fatal("Expected false when TERM empty") + } + os.Setenv("TERM", term) +} func TestW(t *testing.T) { exp := "<3\033[0m" @@ -57,3 +73,23 @@ func TestYellow(t *testing.T) { t.Fatalf("expected path '%s', got '%s'", exp, got) } } + +func TestInitSwag(t *testing.T) { + // Run after other tests to avoid breaking globals + // Test InitSwag unsets globals when set + BOLD = "\033[1m" + InitSwag(true) + if BOLD != "" { + t.Fatal("expected BOLD to be empty string") + } + term := os.Getenv("TERM") + os.Setenv("TERM", "dumb") + BOLD = "\033[1m" + InitSwag(false) + if BOLD != "" { + t.Fatal("expected BOLD to be empty string") + } + os.Setenv("TERM", term) + // Would be good to test BOLD *isn't* unset when we have a TTY + // but less trivial to stub os.File.Fd() without complicating architecture +} From 3c3ed300015c47e37d17b1c0ca1abf4d66784ba8 Mon Sep 17 00:00:00 2001 From: eenblam Date: Tue, 10 Jul 2018 08:47:13 -0700 Subject: [PATCH 02/10] Add tests for Session.ParseCommands() Small tests, but can be expanded to table of cases --- session/session_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 session/session_test.go diff --git a/session/session_test.go b/session/session_test.go new file mode 100644 index 00000000..29edcef3 --- /dev/null +++ b/session/session_test.go @@ -0,0 +1,38 @@ +package session + +import ( + "fmt" + "testing" +) + +func TestParseCommands(t *testing.T) { + //commands := ParseCommands("wifi.recon on; asdf; \"asdf;\" asdf") + t.Run("handles a semicolon as a delimiter", func(t *testing.T) { + first := "wifi.recon on" + second := "wifi.ap" + cmd := fmt.Sprintf("%s; %s", first, second) + commands := ParseCommands(cmd) + if l := len(commands); l != 2 { + t.Fatalf("Expected 2 commands, got %d", l) + } + if got := commands[0]; got != first { + t.Fatalf("expected %s got %s", first, got) + } + if got := commands[1]; got != second { + t.Fatalf("expected %s got %s", second, got) + } + }) + t.Run("handles semicolon inside quotes", func(t *testing.T) { + cmd := "set ticker.commands \"clear; net.show\"" + commands := ParseCommands(cmd) + if l := len(commands); l != 1 { + t.Fatalf("expected 1 command, got %d", l) + } + // Expect double-quotes stripped + expected := "set ticker.commands clear; net.show" + if got := commands[0]; got != expected { + fmt.Println(got) + t.Fatalf("expected %s got %s", cmd, got) + } + }) +} From 56d16557279bcfab2ec6356a69c53d933c9e09f1 Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 13:51:49 -0700 Subject: [PATCH 03/10] Add initial test for net_linux.go More tests needed based on iwlist output, but the side-effect-free part of GetSupportedFrequencies() has been been broken out into a function that can now be tested without calling core.Exec(). --- network/net_linux.go | 12 ++++++++---- network/net_linux_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 network/net_linux_test.go diff --git a/network/net_linux.go b/network/net_linux.go index 8cbc8282..decbc022 100644 --- a/network/net_linux.go +++ b/network/net_linux.go @@ -59,13 +59,12 @@ func SetInterfaceChannel(iface string, channel int) error { return nil } -func GetSupportedFrequencies(iface string) ([]int, error) { +func processSupportedFrequencies(output string, err error) ([]int, error) { freqs := make([]int, 0) - out, err := core.Exec("iwlist", []string{iface, "freq"}) if err != nil { return freqs, err - } else if out != "" { - scanner := bufio.NewScanner(strings.NewReader(out)) + } else if output != "" { + scanner := bufio.NewScanner(strings.NewReader(output)) for scanner.Scan() { line := scanner.Text() matches := WiFiFreqParser.FindStringSubmatch(line) @@ -78,3 +77,8 @@ func GetSupportedFrequencies(iface string) ([]int, error) { } return freqs, nil } + +func GetSupportedFrequencies(iface string) ([]int, error) { + out, err := core.Exec("iwlist", []string{iface, "freq"}) + return processSupportedFrequencies(out, err) +} diff --git a/network/net_linux_test.go b/network/net_linux_test.go new file mode 100644 index 00000000..55713b0e --- /dev/null +++ b/network/net_linux_test.go @@ -0,0 +1,40 @@ +package network + +import ( + "errors" + "reflect" + "testing" +) + +func TestProcessSupportedFrequencies(t *testing.T) { + // Actually test processSupportedFrequencies; IO is lifted out. + cases := []struct { + Name string + InputString string + InputError error + ExpectedFreqs []int + ExpectedError bool + }{ + { + "Returns empty with an error", + "Shouldn't matter", + errors.New("iwlist must have failed"), + []int{}, + true, + }, + } + for _, test := range cases { + t.Run(test.Name, func(t *testing.T) { + freqs, err := processSupportedFrequencies(test.InputString, test.InputError) + if err != nil && !test.ExpectedError { + t.Errorf("unexpected error: %s", err) + } + if err == nil && test.ExpectedError { + t.Error("expected error, but got none") + } + if !test.ExpectedError && !reflect.DeepEqual(freqs, test.ExpectedFreqs) { + t.Errorf("got %v, want %v", freqs, test.ExpectedFreqs) + } + }) + } +} From 8778a2e9d6bb3d058f857381550419dbd369c511 Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 14:07:47 -0700 Subject: [PATCH 04/10] Add iwlist freq test case --- network/net_linux_test.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/network/net_linux_test.go b/network/net_linux_test.go index 55713b0e..01fa3f4e 100644 --- a/network/net_linux_test.go +++ b/network/net_linux_test.go @@ -15,9 +15,28 @@ func TestProcessSupportedFrequencies(t *testing.T) { ExpectedFreqs []int ExpectedError bool }{ + { + "Returns appropriately formatted frequencies on valid input", + `wlan1 11 channels in total; available frequencies : + Channel 01 : 2.412 GHz + Channel 02 : 2.417 GHz + Channel 03 : 2.422 GHz + Channel 04 : 2.427 GHz + Channel 05 : 2.432 GHz + Channel 06 : 2.437 GHz + Channel 07 : 2.442 GHz + Channel 08 : 2.447 GHz + Channel 09 : 2.452 GHz + Channel 10 : 2.457 GHz + Channel 11 : 2.462 GHz + Current Frequency:2.437 GHz (Channel 6)`, + nil, + []int{2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462}, + false, + }, { "Returns empty with an error", - "Shouldn't matter", + "Doesn't matter", errors.New("iwlist must have failed"), []int{}, true, From 7cca7b9d9349db2961deedb27ab8d32abdb651af Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:01:44 -0700 Subject: [PATCH 05/10] Add tests for ParseTargets Still need tests with actual alias map --- network/net_test.go | 53 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/network/net_test.go b/network/net_test.go index 4d93a416..7aac451a 100644 --- a/network/net_test.go +++ b/network/net_test.go @@ -35,15 +35,52 @@ func TestNormalizeMac(t *testing.T) { // TODO: refactor to parse targets with an actual alias map func TestParseTargets(t *testing.T) { - ips, macs, err := ParseTargets("192.168.1.2, 192.168.1.3", &Aliases{}) - if err != nil { - t.Error("ips:", ips, "macs:", macs, "err:", err) + cases := []struct { + Name string + InputTargets string + InputAliases *Aliases + ExpectedIPCount int + ExpectedMACCount int + ExpectedError bool + }{ + // Not sure how to trigger sad path where macParser.FindAllString() + // finds a MAC but net.ParseMac() fails on the result. + { + "empty target string causes empty return", + "", + &Aliases{}, + 0, + 0, + false, + }, + { + "MACs are parsed", + "192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0", + &Aliases{}, + 2, + 2, + false, + }, } - if len(ips) != 2 { - t.Fatalf("expected '%d', got '%d'", 2, len(ips)) - } - if len(macs) != 0 { - t.Fatalf("expected '%d', got '%d'", 0, len(macs)) + for _, test := range cases { + t.Run(test.Name, func(t *testing.T) { + ips, macs, err := ParseTargets(test.InputTargets, test.InputAliases) + if err != nil && !test.ExpectedError { + t.Errorf("unexpected error: %s", err) + } + if err == nil && test.ExpectedError { + t.Error("Expected error, but got none") + } + if test.ExpectedError { + return + } + if len(ips) != test.ExpectedIPCount { + t.Errorf("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets) + } + if len(macs) != test.ExpectedMACCount { + t.Errorf("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets) + } + }) } } From 9f611beaa0e7c1866606c477120da87058f59191 Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:05:06 -0700 Subject: [PATCH 06/10] lint: unused value of out --- firewall/firewall_darwin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firewall/firewall_darwin.go b/firewall/firewall_darwin.go index d328a0fb..fd165b60 100644 --- a/firewall/firewall_darwin.go +++ b/firewall/firewall_darwin.go @@ -50,13 +50,13 @@ func (f PfFirewall) sysCtlRead(param string) (string, error) { func (f PfFirewall) sysCtlWrite(param string, value string) (string, error) { args := []string{"-w", fmt.Sprintf("%s=%s", param, value)} - out, err := core.ExecSilent("sysctl", args) + _, err := core.ExecSilent("sysctl", args) if err != nil { return "", err } // make sure we actually wrote the value - if out, err = f.sysCtlRead(param); err != nil { + if out, err := f.sysCtlRead(param); err != nil { return "", err } else if out != value { return "", fmt.Errorf("Expected value for '%s' is %s, found %s", param, value, out) From 851e39c11b2a4ac2b3c13607d17a56e2f8c96d04 Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:06:52 -0700 Subject: [PATCH 07/10] lint: remove comparisons to bool --- modules/http_proxy_base.go | 2 +- modules/net_sniff_mdns.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/http_proxy_base.go b/modules/http_proxy_base.go index 6677c365..433a0b94 100644 --- a/modules/http_proxy_base.go +++ b/modules/http_proxy_base.go @@ -124,7 +124,7 @@ func (p *HTTPProxy) Configure(address string, proxyPort int, httpPort int, scrip } if p.jsHook == "" && jsToInject != "" { - if strings.HasPrefix(jsToInject, "", jsToInject) } p.jsHook = fmt.Sprintf("%s", jsToInject) diff --git a/modules/net_sniff_mdns.go b/modules/net_sniff_mdns.go index fa13fa9d..bda764de 100644 --- a/modules/net_sniff_mdns.go +++ b/modules/net_sniff_mdns.go @@ -35,7 +35,7 @@ func mdnsParser(ip *layers.IPv4, pkt gopacket.Packet, udp *layers.UDP) bool { for _, answer := range answers { if answer.Type == layers.DNSTypeA || answer.Type == layers.DNSTypeAAAA { hostname := string(answer.Name) - if _, found := m[hostname]; found == false { + if _, found := m[hostname]; !found { m[hostname] = make([]string, 0) } m[hostname] = append(m[hostname], answer.IP.String()) From a859c80b64529af9e393db8855179e7dc9c174bb Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:18:01 -0700 Subject: [PATCH 08/10] lint: use raw string for regex.MustCompile --- network/net_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/net_darwin.go b/network/net_darwin.go index 31215728..ed2b64fb 100644 --- a/network/net_darwin.go +++ b/network/net_darwin.go @@ -10,7 +10,7 @@ import ( const airPortPath = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport" -var IPv4RouteParser = regexp.MustCompile("^([a-z]+)+\\s+(\\d+\\.+\\d+.\\d.+\\d)+\\s+([a-zA-z]+)+\\s+(\\d+)+\\s+(\\d+)+\\s+([a-zA-Z]+\\d+)$") +var IPv4RouteParser = regexp.MustCompile(`^([a-z]+)+\s+(\d+\.+\d+.\d.+\d)+\s+([a-zA-z]+)+\s+(\d+)+\s+(\d+)+\s+([a-zA-Z]+\d+)$`) var IPv4RouteTokens = 7 var IPv4RouteCmd = "netstat" var IPv4RouteCmdOpts = []string{"-n", "-r"} From 06ba0903e7b60b647967ac033438ba721a4a463a Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:41:35 -0700 Subject: [PATCH 09/10] lint: replace loop append(x, y...) --- modules/net_recon_show.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/net_recon_show.go b/modules/net_recon_show.go index f3f59ddc..c0840dc5 100644 --- a/modules/net_recon_show.go +++ b/modules/net_recon_show.go @@ -146,9 +146,7 @@ func (d *Discovery) Show(by string) error { rows := make([][]string, 0) for i, t := range targets { - for _, r := range d.getRow(t, hasMeta) { - rows = append(rows, r) - } + rows = append(rows, d.getRow(t, hasMeta)...) if i == pad { rows = append(rows, padCols) } From c917c6ddf5d84ae2e095e6ef5c3ff10defdd7a0f Mon Sep 17 00:00:00 2001 From: eenblam Date: Fri, 31 Aug 2018 15:42:15 -0700 Subject: [PATCH 10/10] lint: apply annoying gofmt This keeps getting applied every time make test is run. --- modules/http_proxy_js_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/http_proxy_js_request.go b/modules/http_proxy_js_request.go index d1ec1b47..cc59b534 100644 --- a/modules/http_proxy_js_request.go +++ b/modules/http_proxy_js_request.go @@ -6,8 +6,8 @@ import ( "io/ioutil" "net/http" "net/url" - "strings" "regexp" + "strings" ) type JSRequest struct {