MCPcopy
hub / github.com/tailscale/tailscale / TestSelectBestService

Function TestSelectBestService

net/portmapper/select_test.go:21–198  ·  view source on GitHub ↗

NOTE: this is in a distinct file because the various string constants are pretty verbose.

(t *testing.T)

Source from the content-addressed store, hash-verified

19// pretty verbose.
20
21func TestSelectBestService(t *testing.T) {
22 mustParseURL := func(ss string) *url.URL {
23 u, err := url.Parse(ss)
24 if err != nil {
25 t.Fatalf("error parsing URL %q: %v", ss, err)
26 }
27 return u
28 }
29
30 // Run a fake IGD server to respond to UPnP requests.
31 igd, err := NewTestIGD(t, TestIGDOptions{UPnP: true})
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer igd.Close()
36
37 testCases := []struct {
38 name string
39 rootDesc string
40 control map[string]map[string]any
41 want string // controlURL field
42 }{
43 {
44 name: "single_device",
45 rootDesc: testRootDesc,
46 control: map[string]map[string]any{
47 // Service that's up and should be selected.
48 "/ctl/IPConn": {
49 "GetExternalIPAddress": testGetExternalIPAddressResponse,
50 "GetStatusInfo": testGetStatusInfoResponse,
51 },
52 },
53 want: "/ctl/IPConn",
54 },
55 {
56 name: "first_device_disconnected",
57 rootDesc: testSelectRootDesc,
58 control: map[string]map[string]any{
59 // Service that's down; it's important that this is the
60 // one that's down since it's ordered first in the XML
61 // and we want to verify that our code properly queries
62 // and then skips it.
63 "/upnp/control/yomkmsnooi/wanipconn-1": {
64 "GetStatusInfo": testGetStatusInfoResponseDisconnected,
65 // NOTE: nothing else should be called
66 // if GetStatusInfo returns a
67 // disconnected result
68 },
69 // Service that's up and should be selected.
70 "/upnp/control/xstnsgeuyh/wanipconn-7": {
71 "GetExternalIPAddress": testGetExternalIPAddressResponse,
72 "GetStatusInfo": testGetStatusInfoResponse,
73 },
74 },
75 want: "/upnp/control/xstnsgeuyh/wanipconn-7",
76 },
77 {
78 name: "prefer_public_external_IP",

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
SetUPnPHandlerMethod · 0.95
NewTestIGDFunction · 0.85
mustParseRootDevFunction · 0.85
selectBestServiceFunction · 0.85
WithValueMethod · 0.80
upnpHTTPClientLockedMethod · 0.80
newTestClientFunction · 0.70
FatalfMethod · 0.65
FatalMethod · 0.65
RunMethod · 0.65
LogfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…