summaryrefslogtreecommitdiff
path: root/adapters/netscan/netscan_test.go
blob: b50ef0c93bd07cbe9f70e09cea1064ec9d41455d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package netscan

import (
	"reflect"
	"testing"
)

func TestHostIPs(t *testing.T) {
	ips, err := hostIPs("192.168.1.0/30")
	if err != nil {
		t.Fatal(err)
	}
	if want := []string{"192.168.1.1", "192.168.1.2"}; !reflect.DeepEqual(ips, want) {
		t.Errorf("/30 = %v, want %v", ips, want)
	}

	ips, err = hostIPs("10.0.0.0/24")
	if err != nil {
		t.Fatal(err)
	}
	if len(ips) != 254 || ips[0] != "10.0.0.1" || ips[253] != "10.0.0.254" {
		t.Errorf("/24 = %d hosts (%q..%q), want 254 (.1...254)", len(ips), ips[0], ips[len(ips)-1])
	}
}

func TestHostIPsRejects(t *testing.T) {
	if _, err := hostIPs("10.0.0.0/8"); err == nil {
		t.Error("oversized subnet should error")
	}
	if _, err := hostIPs("not-a-cidr"); err == nil {
		t.Error("bad cidr should error")
	}
}