Link speed for windows network fingerprinting - tests

This commit is contained in:
Michal Wieczorek 2016-07-22 22:41:36 +02:00
parent 679fefc155
commit b6b3e24541
2 changed files with 38 additions and 3 deletions

View file

@ -19,16 +19,21 @@ func (f *NetworkFingerprint) linkSpeed(device string) int {
}
output := strings.TrimSpace(string(outBytes))
args := strings.Split(output, " ")
return f.parseLinkSpeed(output)
}
func (f *NetworkFingerprint) parseLinkSpeed(commandOutput string) int {
args := strings.Split(commandOutput, " ")
if len(args) != 2 {
f.logger.Printf("[WARN] fingerprint.network: Couldn't split LinkSpeed (%s)", output)
f.logger.Printf("[WARN] fingerprint.network: Couldn't split LinkSpeed (%s)", commandOutput)
return 0
}
unit := strings.Replace(args[1], "\r\n", "", -1)
value, err := strconv.Atoi(args[0])
if err != nil {
f.logger.Printf("[WARN] fingerprint.network: Unable to parse LinkSpeed value (%s)", output)
f.logger.Printf("[WARN] fingerprint.network: Unable to parse LinkSpeed value (%s)", commandOutput)
return 0
}

View file

@ -0,0 +1,30 @@
package fingerprint
import "testing"
func TestNetworkFingerPrint_linkspeed_parse(t *testing.T) {
f := &NetworkFingerprint{logger: testLogger(), interfaceDetector: &DefaultNetworkInterfaceDetector{}}
var outputTests = []struct {
in string
out int
}{
{"10 Mbps", 10},
{"2 bps", 0},
{"1 Gbps", 1000},
{"2Mbps", 0},
{"1000 Kbps", 1},
{"1 Kbps", 0},
{"0 Mbps", 0},
{"2 2 Mbps", 0},
{"a Mbps", 0},
{"1 Tbps", 0},
}
for _, ot := range outputTests {
out := f.parseLinkSpeed(ot.in)
if out != ot.out {
t.Errorf("parseLinkSpeed(%s) => %d, should be %d", ot.in, out, ot.out)
}
}
}