initial commit [141.14.140.180,mike]
This commit is contained in:
commit
e34ba75b7b
33 files changed
+6267
No files matched your search
@@ -0,0 +1,262 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/dns/dnsmessage"
|
||||
)
|
||||
|
||||
func TestParseRange(t *testing.T) {
|
||||
tests := []struct {
|
||||
spec string
|
||||
n int
|
||||
first, end string
|
||||
}{
|
||||
{"192.168.1.0/24", 254, "192.168.1.1", "192.168.1.254"},
|
||||
{"192.168.1.77/30", 2, "192.168.1.77", "192.168.1.78"},
|
||||
{"10.0.0.5", 1, "10.0.0.5", "10.0.0.5"},
|
||||
{"10.0.0.10-20", 11, "10.0.0.10", "10.0.0.20"},
|
||||
{"10.0.0.250-10.0.1.3", 10, "10.0.0.250", "10.0.1.3"},
|
||||
{"10.0.0.3, 10.0.0.1-3", 3, "10.0.0.1", "10.0.0.3"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, err := ParseRange(tt.spec)
|
||||
if err != nil {
|
||||
t.Errorf("ParseRange(%q): %v", tt.spec, err)
|
||||
continue
|
||||
}
|
||||
if len(got) != tt.n || got[0].String() != tt.first || got[len(got)-1].String() != tt.end {
|
||||
t.Errorf("ParseRange(%q) = %d addrs %v…%v, want %d %s…%s", tt.spec, len(got), got[0], got[len(got)-1], tt.n, tt.first, tt.end)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"", "foo", "10.0.0.20-10", "10.0.0.0/8", "10.0.0.1-300", "::1"} {
|
||||
if _, err := ParseRange(bad); err == nil {
|
||||
t.Errorf("ParseRange(%q) succeeded, want error", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworkHelpers(t *testing.T) {
|
||||
n := &Network{Prefix: netip.MustParsePrefix("192.168.178.23/24"), LocalIP: netip.MustParseAddr("192.168.178.23")}
|
||||
if got := n.Broadcast().String(); got != "192.168.178.255" {
|
||||
t.Errorf("Broadcast = %s", got)
|
||||
}
|
||||
if !n.OnLink(netip.MustParseAddr("192.168.178.1")) || n.OnLink(netip.MustParseAddr("192.168.179.1")) {
|
||||
t.Error("OnLink wrong")
|
||||
}
|
||||
big := &Network{Prefix: netip.MustParsePrefix("10.20.30.40/16"), LocalIP: netip.MustParseAddr("10.20.30.40")}
|
||||
if got := big.DefaultRange().String(); got != "10.20.30.0/24" {
|
||||
t.Errorf("DefaultRange of /16 = %s, want the local /24", got)
|
||||
}
|
||||
mid := &Network{Prefix: netip.MustParsePrefix("172.16.40.180/20"), LocalIP: netip.MustParseAddr("172.16.40.180")}
|
||||
if got := mid.DefaultRange().String(); got != "172.16.32.0/20" {
|
||||
t.Errorf("DefaultRange of /20 = %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLooseMAC(t *testing.T) {
|
||||
mac, ok := parseLooseMAC("0:1a:2:b3:4:5")
|
||||
if !ok || mac.String() != "00:1a:02:b3:04:05" {
|
||||
t.Errorf("got %v %v", mac, ok)
|
||||
}
|
||||
if _, ok := parseLooseMAC("ff-ff-ff-ff-ff-ff"); ok {
|
||||
t.Error("broadcast accepted")
|
||||
}
|
||||
if _, ok := parseLooseMAC("01:00:5e:00:00:fb"); ok {
|
||||
t.Error("multicast accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseARPOutput(t *testing.T) {
|
||||
out := `? (10.0.1.1) at 3c:22:fb:12:34:56 on en1 ifscope [ethernet]
|
||||
? (10.0.1.2) at (incomplete) on en1 ifscope [ethernet]
|
||||
? (10.0.1.205) at e:4f:6b:00:00:21 on en1 ifscope [ethernet]
|
||||
? (10.0.1.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
|
||||
? (224.0.0.251) at 1:0:5e:0:0:fb on en1 ifscope permanent [ethernet]
|
||||
192.168.0.1 00-11-22-33-44-55 dynamic
|
||||
`
|
||||
table := parseARPOutput(out)
|
||||
want := map[string]string{
|
||||
"10.0.1.1": "3c:22:fb:12:34:56",
|
||||
"10.0.1.205": "0e:4f:6b:00:00:21",
|
||||
"192.168.0.1": "00:11:22:33:44:55",
|
||||
}
|
||||
if len(table) != len(want) {
|
||||
t.Fatalf("got %v", table)
|
||||
}
|
||||
for ip, mac := range want {
|
||||
if got := table[netip.MustParseAddr(ip)].String(); got != mac {
|
||||
t.Errorf("%s = %s, want %s", ip, got, mac)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePorts(t *testing.T) {
|
||||
got, err := ParsePorts("443, 22,8000-8002,22")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []int{22, 443, 8000, 8001, 8002}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
if all, _ := ParsePorts("all"); len(all) != 65535 {
|
||||
t.Errorf("all = %d ports", len(all))
|
||||
}
|
||||
for _, bad := range []string{"0", "70000", "20-10", "abc"} {
|
||||
if _, err := ParsePorts(bad); err == nil {
|
||||
t.Errorf("ParsePorts(%q) succeeded", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverseName(t *testing.T) {
|
||||
ip := netip.MustParseAddr("192.168.1.42")
|
||||
name := reverseName(ip)
|
||||
if name != "42.1.168.192.in-addr.arpa." {
|
||||
t.Fatalf("reverseName = %s", name)
|
||||
}
|
||||
if back, ok := parseReverseName(name); !ok || back != ip {
|
||||
t.Errorf("parseReverseName = %v %v", back, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanHostname(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"Mikes-MacBook-Pro.local.": "Mikes-MacBook-Pro",
|
||||
"fritz.box.": "fritz.box",
|
||||
"nas.fritz.box": "nas",
|
||||
"host.example.org.": "host.example.org",
|
||||
"printer.lan": "printer",
|
||||
} {
|
||||
if got := cleanHostname(in); got != want {
|
||||
t.Errorf("cleanHostname(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBonjourNames(t *testing.T) {
|
||||
inst := "Living Room TV._airplay._tcp.local."
|
||||
if got := serviceTypeOf(inst); got != "_airplay._tcp.local." {
|
||||
t.Errorf("serviceTypeOf = %q", got)
|
||||
}
|
||||
if got := instanceLabel(inst, "_airplay._tcp.local."); got != "Living Room TV" {
|
||||
t.Errorf("instanceLabel = %q", got)
|
||||
}
|
||||
if got := (Service{Type: "_googlecast._tcp"}).Short(); got != "googlecast" {
|
||||
t.Errorf("Short = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMDNSRoundTrip(t *testing.T) {
|
||||
// A responder's answer to our reverse query must yield the host name.
|
||||
name := dnsmessage.MustNewName(reverseName(netip.MustParseAddr("10.0.0.7")))
|
||||
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{Response: true, Authoritative: true})
|
||||
b.StartAnswers()
|
||||
b.PTRResource(dnsmessage.ResourceHeader{Name: name, Type: dnsmessage.TypePTR, Class: dnsmessage.ClassINET, TTL: 120},
|
||||
dnsmessage.PTRResource{PTR: dnsmessage.MustNewName("Apple-TV.local.")})
|
||||
msg, err := b.Finish()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rrs := parseRecords(msg)
|
||||
if len(rrs) != 1 {
|
||||
t.Fatalf("got %d records", len(rrs))
|
||||
}
|
||||
ptr := rrs[0].Body.(*dnsmessage.PTRResource)
|
||||
if got := cleanHostname(ptr.PTR.String()); got != "Apple-TV" {
|
||||
t.Errorf("name = %q", got)
|
||||
}
|
||||
|
||||
q, err := question("_services._dns-sd._udp.local.", dnsmessage.TypePTR)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := buildQuery(0, q); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNBStat(t *testing.T) {
|
||||
req := nbstatRequest(0x1234)
|
||||
if len(req) != 50 || req[12] != 0x20 || string(req[13:15]) != "CK" {
|
||||
t.Fatalf("bad request % x", req)
|
||||
}
|
||||
|
||||
// Response: header, the queried name, NBSTAT RR with a group name first.
|
||||
resp := []byte{0x12, 0x34, 0x84, 0x00, 0, 0, 0, 1, 0, 0, 0, 0}
|
||||
resp = append(resp, req[12:46]...)
|
||||
resp = append(resp, 0x00, 0x21, 0x00, 0x01, 0, 0, 0, 0)
|
||||
entry := func(name string, suffix byte, flags uint16) []byte {
|
||||
e := bytes.Repeat([]byte(" "), 15)
|
||||
copy(e, name)
|
||||
return append(e, suffix, byte(flags>>8), byte(flags))
|
||||
}
|
||||
rdata := []byte{2}
|
||||
rdata = append(rdata, entry("WORKGROUP", 0x00, 0x8400)...)
|
||||
rdata = append(rdata, entry("DESKTOP-ABC", 0x00, 0x0400)...)
|
||||
rdata = append(rdata, make([]byte, 6)...)
|
||||
resp = append(resp, byte(len(rdata)>>8), byte(len(rdata)))
|
||||
resp = append(resp, rdata...)
|
||||
|
||||
name, ok := parseNBStat(resp)
|
||||
if !ok || name != "DESKTOP-ABC" {
|
||||
t.Errorf("parseNBStat = %q %v", name, ok)
|
||||
}
|
||||
if _, ok := parseNBStat(resp[:20]); ok {
|
||||
t.Error("truncated response accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMagicPacket(t *testing.T) {
|
||||
mac, _ := net.ParseMAC("aa:bb:cc:dd:ee:ff")
|
||||
p, err := MagicPacket(mac)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(p) != 102 || !bytes.Equal(p[:6], bytes.Repeat([]byte{0xff}, 6)) || !bytes.Equal(p[96:], mac) {
|
||||
t.Errorf("bad packet % x", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVendorLabel(t *testing.T) {
|
||||
private, _ := net.ParseMAC("02:5a:7c:11:22:33")
|
||||
apple, _ := net.ParseMAC("d0:11:e5:12:34:56")
|
||||
tests := []struct {
|
||||
h Host
|
||||
maker, want string
|
||||
}{
|
||||
{Host{MAC: private, Vendor: "Private address", Model: "MacBookAir10,1"}, "Apple", "Apple MacBookAir10,1"},
|
||||
{Host{MAC: private, Vendor: "Private address"}, "Private address", "Private address"},
|
||||
{Host{MAC: private, Vendor: "Private address", Model: "VMware7,1"}, "Private address", "VMware7,1"},
|
||||
{Host{MAC: apple, Vendor: "Apple", Model: "Mac16,10"}, "Apple", "Apple Mac16,10"},
|
||||
{Host{MAC: apple, Vendor: "Apple"}, "Apple", "Apple"},
|
||||
{Host{Model: "iPhone15,3"}, "Apple", "Apple iPhone15,3"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := tt.h.Maker(); got != tt.maker {
|
||||
t.Errorf("Maker(%v) = %q, want %q", tt.h.Model, got, tt.maker)
|
||||
}
|
||||
if got := tt.h.VendorLabel(); got != tt.want {
|
||||
t.Errorf("VendorLabel(%v) = %q, want %q", tt.h.Model, got, tt.want)
|
||||
}
|
||||
}
|
||||
if txtValue([]string{"model=Mac16,10", "osxvers=26"}, "model") != "Mac16,10" {
|
||||
t.Error("txtValue")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceModelsLoopback(t *testing.T) {
|
||||
// The local mDNS responder answers for this machine, if there is one.
|
||||
got := deviceModels(t.Context(), map[netip.Addr][]string{netip.MustParseAddr("127.0.0.1"): {localHostname()}}, 500*time.Millisecond)
|
||||
t.Logf("loopback model: %q", got[netip.MustParseAddr("127.0.0.1")])
|
||||
}
|
||||
Reference in new issue
Block a user