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
deps: godep golint gomegacheck
deps: godep golint gofmt gomegacheck
@dep ensure
build: resources
@ -54,3 +54,6 @@ golint:
gomegacheck:
@go get honnef.co/go/tools/cmd/megacheck
gofmt:
gofmt -s -w $(PACKAGES)

View file

@ -4,51 +4,12 @@ import (
"bytes"
"io"
"os"
"os/user"
"path/filepath"
"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 {
for _, n := range a {
if n == v {
@ -132,30 +93,6 @@ func sameStrings(a []string, b []string) bool {
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) {
var units = []struct {
exec string
@ -182,7 +119,7 @@ func TestCoreExec(t *testing.T) {
io.Copy(&buf, r)
os.Stdout = oldStdout
gotStdout := Trim(buf.String())
gotStdout := str.Trim(buf.String())
if gotOut != u.out {
t.Fatalf("expected output '%s', got '%s'", u.out, gotOut)
} else if u.err == "" && gotErr != nil {
@ -208,7 +145,7 @@ func TestCoreExec(t *testing.T) {
io.Copy(&buf, r)
os.Stdout = oldStdout
gotStdout := Trim(buf.String())
gotStdout := str.Trim(buf.String())
if gotOut != u.out {
t.Fatalf("expected output '%s', got '%s'", u.out, gotOut)
} else if u.err == "" && gotErr != nil {
@ -236,38 +173,9 @@ func TestCoreExists(t *testing.T) {
}
for _, u := range units {
got := Exists(u.what)
got := fs.Exists(u.what)
if got != u.exists {
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
}
// search for interesting headers and cookies
for name, _ := range req.Headers {
for name := range req.Headers {
headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") {
return true
@ -48,7 +48,7 @@ func (s *EventsStream) shouldDumpHttpResponse(res HTTPResponse) bool {
return true
}
// search for interesting headers
for name, _ := range res.Headers {
for name := range res.Headers {
headerName := strings.ToLower(name)
if strings.Contains(headerName, "auth") || strings.Contains(headerName, "token") || strings.Contains(headerName, "cookie") {
return true

View file

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

View file

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

View file

@ -2,11 +2,11 @@ package network
import (
"fmt"
"strings"
"math/big"
"strings"
)
var manuf = map[string]string {
var manuf = map[string]string{
"24.24664": "Copper Mountain Communications, Inc.",
"24.2789": "ScottCare Corporation",
"12.346853203968": "Energy ICT",
@ -35139,7 +35139,6 @@ var manuf = map[string]string {
"12.123917679357952": "Key Chemical & Equipment Company",
}
func ManufLookup(mac string) string {
macHex := strings.Replace(mac, ":", "", -1)
macInt := new(big.Int)

View file

@ -84,7 +84,7 @@ func NewMDNSProbe(from net.IP, from_hw net.HardwareAddr) (error, []byte) {
RD: true,
OpCode: layers.DNSOpCodeQuery,
Questions: []layers.DNSQuestion{
layers.DNSQuestion{
{
Name: []byte("_services._dns-sd._udp.local"),
Type: layers.DNSTypePTR,
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
} else if p.Type == BOOL {
case BOOL:
lvalue := strings.ToLower(value)
if lvalue == "true" {
return nil, true
@ -80,7 +81,7 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
} else {
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
}
} else if p.Type == INT {
case INT:
i, err := strconv.Atoi(value)
return err, i
} else if p.Type == FLOAT {
@ -98,17 +99,21 @@ const ParamRandomMAC = "<random mac>"
func (p ModuleParam) Get(s *Session) (error, interface{}) {
_, v := s.Env.Get(p.Name)
if v == ParamIfaceName {
switch v {
case ParamIfaceName:
v = s.Interface.Name()
} else if v == ParamIfaceAddress {
case ParamIfaceAddress:
v = s.Interface.IpAddress
} else if v == ParamSubnet {
case ParamSubnet:
v = s.Interface.CIDR()
} else if v == ParamRandomMAC {
case ParamRandomMAC:
hw := make([]byte, 6)
rand.Read(hw)
v = net.HardwareAddr(hw).String()
default:
return fmt.Errorf("Uknown value for v"), nil
}
return p.Validate(v)
}

View file

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