func TestReportLocalNetworks(t *testing.T) { r := report.MakeReport().Merge(report.Report{ Host: report.Topology{ Nodes: report.Nodes{ "nonets": report.MakeNode("nonets"), "foo": report.MakeNode("foo").WithSets(report.EmptySets. Add(host.LocalNetworks, report.MakeStringSet( "10.0.0.1/8", "192.168.1.1/24", "10.0.0.1/8", "badnet/33")), ), }, }, Overlay: report.Topology{ Nodes: report.Nodes{ "router": report.MakeNode("router").WithSets(report.EmptySets. Add(host.LocalNetworks, report.MakeStringSet("10.32.0.1/12")), ), }, }, }) want := report.Networks([]*net.IPNet{ mustParseCIDR("10.0.0.1/8"), mustParseCIDR("192.168.1.1/24"), mustParseCIDR("10.32.0.1/12"), }) have := render.LocalNetworks(r) if !reflect.DeepEqual(want, have) { t.Errorf("%s", test.Diff(want, have)) } }
func TestMerge(t *testing.T) { var ( hostID = "xyz" src = newMockSource([]byte{}, nil) on = time.Millisecond off = time.Millisecond rpt = report.MakeReport() p = sniff.Packet{ SrcIP: "1.0.0.0", SrcPort: "1000", DstIP: "2.0.0.0", DstPort: "2000", Network: 512, Transport: 256, } _, ipnet, _ = net.ParseCIDR(p.SrcIP + "/24") // ;) localNets = report.Networks([]*net.IPNet{ipnet}) ) sniff.New(hostID, localNets, src, on, off).Merge(p, &rpt) var ( srcEndpointNodeID = report.MakeEndpointNodeID(hostID, p.SrcIP, p.SrcPort) dstEndpointNodeID = report.MakeEndpointNodeID(hostID, p.DstIP, p.DstPort) ) if want, have := (report.Topology{ Nodes: report.Nodes{ srcEndpointNodeID: report.MakeNode(srcEndpointNodeID).WithEdge(dstEndpointNodeID, report.EdgeMetadata{ EgressPacketCount: newu64(1), EgressByteCount: newu64(256), }), dstEndpointNodeID: report.MakeNode(dstEndpointNodeID), }, }), rpt.Endpoint; !reflect.DeepEqual(want, have) { t.Errorf("%s", test.Diff(want, have)) } var ( srcAddressNodeID = report.MakeAddressNodeID(hostID, p.SrcIP) dstAddressNodeID = report.MakeAddressNodeID(hostID, p.DstIP) ) if want, have := (report.Topology{ Nodes: report.Nodes{ srcAddressNodeID: report.MakeNode(srcAddressNodeID).WithEdge(dstAddressNodeID, report.EdgeMetadata{ EgressPacketCount: newu64(1), EgressByteCount: newu64(512), }), dstAddressNodeID: report.MakeNode(dstAddressNodeID), }, }), rpt.Address; !reflect.DeepEqual(want, have) { t.Errorf("%s", test.Diff(want, have)) } }
func TestContains(t *testing.T) { networks := report.Networks([]*net.IPNet{ mustParseCIDR("10.0.0.1/8"), mustParseCIDR("192.168.1.1/24"), }) if networks.Contains(net.ParseIP("52.52.52.52")) { t.Errorf("52.52.52.52 not in %v", networks) } if !networks.Contains(net.ParseIP("10.0.0.1")) { t.Errorf("10.0.0.1 in %v", networks) } }
func testMap(t *testing.T, f render.MapFunc, input testcase) { _, ipNet, err := net.ParseCIDR("1.2.3.0/16") if err != nil { t.Fatalf(err.Error()) } localNetworks := report.Networks([]*net.IPNet{ipNet}) if have := f(input.n, localNetworks); input.ok != (len(have) > 0) { name := input.name if name == "" { name = fmt.Sprintf("%v", input.n) } t.Errorf("%s: want %v, have %v", name, input.ok, have) } }
func TestAddLocal(t *testing.T) { oldInterfaceByNameStub := report.InterfaceByNameStub defer func() { report.InterfaceByNameStub = oldInterfaceByNameStub }() report.InterfaceByNameStub = func(name string) (report.Interface, error) { return mockInterface{[]net.Addr{mockAddr("52.53.54.55/16")}}, nil } err := report.AddLocalBridge("foo") if err != nil { t.Errorf("%v", err) } want := report.Networks([]*net.IPNet{mustParseCIDR("52.53.54.55/16")}) have := report.LocalNetworks if !reflect.DeepEqual(want, have) { t.Errorf("%s", test.Diff(want, have)) } }