summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/hashicorp/go-sockaddr/route_info_test.go
blob: 1716327e81c47ac9e277babe7dd447427dceeef2 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package sockaddr

import "testing"

func Test_parseBSDDefaultIfName(t *testing.T) {
	testCases := []struct {
		name     string
		routeOut string
		want     string
	}{
		{
			name: "macOS Sierra 10.12 - Common",
			routeOut: `   route to: default
destination: default
       mask: default
    gateway: 10.23.9.1
  interface: en0
      flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 
`,
			want: "en0",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseDefaultIfNameFromRoute(tc.routeOut)
			if err != nil {
				t.Fatalf("unable to parse default interface from route output: %v", err)
			}

			if got != tc.want {
				t.Errorf("got %s; want %s", got, tc.want)
			}
		})
	}
}

func Test_parseLinuxDefaultIfName(t *testing.T) {
	testCases := []struct {
		name     string
		routeOut string
		want     string
	}{
		{
			name: "Linux Ubuntu 14.04 - Common",
			routeOut: `default via 10.1.2.1 dev eth0 
10.1.2.0/24 dev eth0  proto kernel  scope link  src 10.1.2.5 
`,
			want: "eth0",
		},
		{
			name: "Chromebook - 8743.85.0 (Official Build) stable-channel gandof, Milestone 54",
			routeOut: `default via 192.168.1.1 dev wlan0  metric 1 
192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.174 
`,
			want: "wlan0",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseDefaultIfNameFromIPCmd(tc.routeOut)
			if err != nil {
				t.Fatalf("unable to parse default interface from route output: %v", err)
			}

			if got != tc.want {
				t.Errorf("got %+q; want %+q", got, tc.want)
			}
		})
	}
}

func Test_parseWindowsDefaultIfName(t *testing.T) {
	testCases := []struct {
		name        string
		routeOut    string
		ipconfigOut string
		want        string
	}{
		{
			name: "Windows 10 - Enterprise",
			routeOut: `===========================================================================
Interface List
 10...08 00 27 a2 e9 51 ......Intel(R) PRO/1000 MT Desktop Adapter
 13...08 00 27 35 02 ed ......Intel(R) PRO/1000 MT Desktop Adapter #2
  1...........................Software Loopback Interface 1
  5...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter
  8...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0         10.0.2.2        10.0.2.15     25
         10.0.2.0    255.255.255.0         On-link         10.0.2.15    281
        10.0.2.15  255.255.255.255         On-link         10.0.2.15    281
       10.0.2.255  255.255.255.255         On-link         10.0.2.15    281
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
     192.168.56.0    255.255.255.0         On-link    192.168.56.100    281
   192.168.56.100  255.255.255.255         On-link    192.168.56.100    281
   192.168.56.255  255.255.255.255         On-link    192.168.56.100    281
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link    192.168.56.100    281
        224.0.0.0        240.0.0.0         On-link         10.0.2.15    281
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link    192.168.56.100    281
  255.255.255.255  255.255.255.255         On-link         10.0.2.15    281
===========================================================================
Persistent Routes:
  None

IPv6 Route Table
===========================================================================
Active Routes:
 If Metric Network Destination      Gateway
  1    331 ::1/128                  On-link
 13    281 fe80::/64                On-link
 10    281 fe80::/64                On-link
 13    281 fe80::60cc:155f:77a4:ab99/128
                                    On-link
 10    281 fe80::cccc:710e:f5bb:3088/128
                                    On-link
  1    331 ff00::/8                 On-link
 13    281 ff00::/8                 On-link
 10    281 ff00::/8                 On-link
===========================================================================
Persistent Routes:
  None
`,
			ipconfigOut: `Windows IP Configuration


Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : host.example.org
   Link-local IPv6 Address . . . . . : fe80::cccc:710e:f5bb:3088%10
   IPv4 Address. . . . . . . . . . . : 10.0.2.15
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.2.2

Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . : 
   Link-local IPv6 Address . . . . . : fe80::60cc:155f:77a4:ab99%13
   IPv4 Address. . . . . . . . . . . : 192.168.56.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 

Tunnel adapter isatap.host.example.org:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 

Tunnel adapter Reusable ISATAP Interface {F3F2E4A5-8823-40E5-87EA-1F6881BACC95}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : host.example.org
`,
			want: "Ethernet",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseDefaultIfNameWindows(tc.routeOut, tc.ipconfigOut)
			if err != nil {
				t.Fatalf("unable to parse default interface from route output: %v", err)
			}

			if got != tc.want {
				t.Errorf("got %s; want %s", got, tc.want)
			}
		})
	}
}

func Test_VisitComands(t *testing.T) {
	ri, err := NewRouteInfo()
	if err != nil {
		t.Fatalf("bad: %v", err)
	}

	var count int
	ri.VisitCommands(func(name string, cmd []string) {
		count++
	})
	if count == 0 {
		t.Fatalf("Expected more than 0 items")
	}
}