Merge pull request #373 from Edznux/refactoring

Refactoring and fix tests
This commit is contained in:
evilsocket 2018-10-29 15:24:31 +01:00 committed by GitHub
commit 24037bce47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 35181 additions and 35268 deletions

View file

@ -3,7 +3,7 @@ PACKAGES=core firewall log modules network packets session tls
all: deps build all: deps build
deps: godep golint gomegacheck deps: godep golint gofmt gomegacheck
@dep ensure @dep ensure
build: resources build: resources
@ -54,3 +54,6 @@ golint:
gomegacheck: gomegacheck:
@go get honnef.co/go/tools/cmd/megacheck @go get honnef.co/go/tools/cmd/megacheck
gofmt:
gofmt -s -w $(PACKAGES)

View file

@ -4,51 +4,12 @@ import (
"bytes" "bytes"
"io" "io"
"os" "os"
"os/user"
"path/filepath"
"testing" "testing"
"github.com/evilsocket/islazy/fs"
"github.com/evilsocket/islazy/str"
) )
func TestCoreTrim(t *testing.T) {
var units = []struct {
from string
to string
}{
{"\t ", ""},
{" ", ""},
{" hello world", "hello world"},
{"hello world ", "hello world"},
{" hello world\t", "hello world"},
}
for _, u := range units {
got := Trim(u.from)
if got != u.to {
t.Fatalf("expected '%s', got '%s'", u.to, got)
}
}
}
func TestCoreTrimRight(t *testing.T) {
var units = []struct {
from string
to string
}{
{"\t ", ""},
{" ", ""},
{" hello world", " hello world"},
{"hello world ", "hello world"},
{" hello world\t", " hello world"},
}
for _, u := range units {
got := TrimRight(u.from)
if got != u.to {
t.Fatalf("expected '%s', got '%s'", u.to, got)
}
}
}
func hasInt(a []int, v int) bool { func hasInt(a []int, v int) bool {
for _, n := range a { for _, n := range a {
if n == v { if n == v {
@ -132,30 +93,6 @@ func sameStrings(a []string, b []string) bool {
return true return true
} }
func TestCoreSepSplitAndCommaSplit(t *testing.T) {
var units = []struct {
from string
to []string
}{
{"foo", []string{"foo"}},
{"foo#bar", []string{"foo#bar"}},
{"foo,bar", []string{"foo", "bar"}},
{"foo,bar,", []string{"foo", "bar"}},
{"foo,bar,", []string{"foo", "bar"}},
{"foo,,bar,", []string{"foo", "bar"}},
{"foo,,bar,,,,", []string{"foo", "bar"}},
{"foo,bar@wut,.ok", []string{"foo", "bar@wut", ".ok"}},
}
for _, u := range units {
if got := SepSplit(u.from, ","); !sameStrings(got, u.to) {
t.Fatalf("expected '%v', got '%v'", u.to, got)
} else if got = CommaSplit(u.from); !sameStrings(got, u.to) {
t.Fatalf("expected '%v', got '%v'", u.to, got)
}
}
}
func TestCoreExec(t *testing.T) { func TestCoreExec(t *testing.T) {
var units = []struct { var units = []struct {
exec string exec string
@ -182,7 +119,7 @@ func TestCoreExec(t *testing.T) {
io.Copy(&buf, r) io.Copy(&buf, r)
os.Stdout = oldStdout os.Stdout = oldStdout
gotStdout := Trim(buf.String()) gotStdout := str.Trim(buf.String())
if gotOut != u.out { if gotOut != u.out {
t.Fatalf("expected output '%s', got '%s'", u.out, gotOut) t.Fatalf("expected output '%s', got '%s'", u.out, gotOut)
} else if u.err == "" && gotErr != nil { } else if u.err == "" && gotErr != nil {
@ -208,7 +145,7 @@ func TestCoreExec(t *testing.T) {
io.Copy(&buf, r) io.Copy(&buf, r)
os.Stdout = oldStdout os.Stdout = oldStdout
gotStdout := Trim(buf.String()) gotStdout := str.Trim(buf.String())
if gotOut != u.out { if gotOut != u.out {
t.Fatalf("expected output '%s', got '%s'", u.out, gotOut) t.Fatalf("expected output '%s', got '%s'", u.out, gotOut)
} else if u.err == "" && gotErr != nil { } else if u.err == "" && gotErr != nil {
@ -236,38 +173,9 @@ func TestCoreExists(t *testing.T) {
} }
for _, u := range units { for _, u := range units {
got := Exists(u.what) got := fs.Exists(u.what)
if got != u.exists { if got != u.exists {
t.Fatalf("expected '%v', got '%v'", u.exists, got) t.Fatalf("expected '%v', got '%v'", u.exists, got)
} }
} }
} }
func TestCoreExpandPath(t *testing.T) {
base, _ := filepath.Abs(".")
usr, _ := user.Current()
var units = []struct {
from string
to string
err string
}{
{"", "", ""},
{"/lulz", "/lulz", ""},
{".", base, ""},
{"~", usr.HomeDir, ""},
}
for _, u := range units {
gotPath, gotErr := ExpandPath(u.from)
if gotPath != u.to {
t.Fatalf("expected path '%s', got '%s'", u.to, gotPath)
} else if u.err == "" && gotErr != nil {
t.Fatalf("expected no error, got '%v'", gotErr)
} else if u.err != "" && gotErr == nil {
t.Fatalf("expected error '%s', got none", u.err)
} else if u.err != "" && gotErr != nil && gotErr.Error() != u.err {
t.Fatalf("expected error '%s', got '%s'", u.err, gotErr.Error())
}
}
}

View file

@ -28,7 +28,7 @@ func (s *EventsStream) shouldDumpHttpRequest(req HTTPRequest) bool {
return true return true
} }
// search for interesting headers and cookies // search for interesting headers and cookies
for name, _ := range req.Headers { for name := range req.Headers {
headerName := strings.ToLower(name) headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") { if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") {
return true return true
@ -48,7 +48,7 @@ func (s *EventsStream) shouldDumpHttpResponse(res HTTPResponse) bool {
return true return true
} }
// search for interesting headers // search for interesting headers
for name, _ := range res.Headers { for name := range res.Headers {
headerName := strings.ToLower(name) headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") || strings.Contains(headerName, "cookie") { if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") || strings.Contains(headerName, "cookie") {
return true return true

View file

@ -119,14 +119,14 @@ func (d *Discovery) Show(by string, expr string) (err error) {
} else { } else {
targets = d.Session.Lan.List() targets = d.Session.Lan.List()
} }
switch by {
if by == "seen" { case "seen":
sort.Sort(BySeenSorter(targets)) sort.Sort(BySeenSorter(targets))
} else if by == "sent" { case "sent":
sort.Sort(BySentSorter(targets)) sort.Sort(BySentSorter(targets))
} else if by == "rcvd" { case "rcvd":
sort.Sort(ByRcvdSorter(targets)) sort.Sort(ByRcvdSorter(targets))
} else { default:
sort.Sort(ByAddressSorter(targets)) sort.Sort(ByAddressSorter(targets))
} }

View file

@ -123,14 +123,14 @@ func (w *WiFiModule) Show(by string) error {
} else { } else {
stations = w.Session.WiFi.Stations() stations = w.Session.WiFi.Stations()
} }
switch by {
if by == "seen" { case "seen":
sort.Sort(ByWiFiSeenSorter(stations)) sort.Sort(ByWiFiSeenSorter(stations))
} else if by == "essid" { case "essid":
sort.Sort(ByEssidSorter(stations)) sort.Sort(ByEssidSorter(stations))
} else if by == "channel" { case "channel":
sort.Sort(ByChannelSorter(stations)) sort.Sort(ByChannelSorter(stations))
} else { default:
sort.Sort(ByRSSISorter(stations)) sort.Sort(ByRSSISorter(stations))
} }

File diff suppressed because it is too large Load diff

View file

@ -84,7 +84,7 @@ func NewMDNSProbe(from net.IP, from_hw net.HardwareAddr) (error, []byte) {
RD: true, RD: true,
OpCode: layers.DNSOpCodeQuery, OpCode: layers.DNSOpCodeQuery,
Questions: []layers.DNSQuestion{ Questions: []layers.DNSQuestion{
layers.DNSQuestion{ {
Name: []byte("_services._dns-sd._udp.local"), Name: []byte("_services._dns-sd._udp.local"),
Type: layers.DNSTypePTR, Type: layers.DNSTypePTR,
Class: layers.DNSClassIN, Class: layers.DNSClassIN,

View file

@ -69,9 +69,10 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
} }
} }
if p.Type == STRING { switch p.Type {
case STRING:
return nil, value return nil, value
} else if p.Type == BOOL { case BOOL:
lvalue := strings.ToLower(value) lvalue := strings.ToLower(value)
if lvalue == "true" { if lvalue == "true" {
return nil, true return nil, true
@ -80,7 +81,7 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
} else { } else {
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
} }
} else if p.Type == INT { case INT:
i, err := strconv.Atoi(value) i, err := strconv.Atoi(value)
return err, i return err, i
} else if p.Type == FLOAT { } else if p.Type == FLOAT {
@ -98,17 +99,21 @@ const ParamRandomMAC = "<random mac>"
func (p ModuleParam) Get(s *Session) (error, interface{}) { func (p ModuleParam) Get(s *Session) (error, interface{}) {
_, v := s.Env.Get(p.Name) _, v := s.Env.Get(p.Name)
if v == ParamIfaceName { switch v {
case ParamIfaceName:
v = s.Interface.Name() v = s.Interface.Name()
} else if v == ParamIfaceAddress { case ParamIfaceAddress:
v = s.Interface.IpAddress v = s.Interface.IpAddress
} else if v == ParamSubnet { case ParamSubnet:
v = s.Interface.CIDR() v = s.Interface.CIDR()
} else if v == ParamRandomMAC { case ParamRandomMAC:
hw := make([]byte, 6) hw := make([]byte, 6)
rand.Read(hw) rand.Read(hw)
v = net.HardwareAddr(hw).String() v = net.HardwareAddr(hw).String()
default:
return fmt.Errorf("Uknown value for v"), nil
} }
return p.Validate(v) return p.Validate(v)
} }

View file

@ -324,9 +324,7 @@ func parseCapletCommand(line string) (is bool, caplet *caplets.Caplet, argv []st
// check for any arguments // check for any arguments
if argc > 1 { if argc > 1 {
file = str.Trim(parts[0]) file = str.Trim(parts[0])
if argc >= 2 { argv = parts[1:]
argv = parts[1:]
}
} }
if err, cap := caplets.Load(file); err == nil { if err, cap := caplets.Load(file); err == nil {