mirror of
https://github.com/bettercap/bettercap
synced 2025-07-12 08:07:00 -07:00
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().
This commit is contained in:
parent
3c3ed30001
commit
56d1655727
2 changed files with 48 additions and 4 deletions
|
@ -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)
|
||||
}
|
||||
|
|
40
network/net_linux_test.go
Normal file
40
network/net_linux_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue