1package sockaddr 2 3import "testing" 4 5func Test_parseBSDDefaultIfName(t *testing.T) { 6 testCases := []struct { 7 name string 8 routeOut string 9 want string 10 }{ 11 { 12 name: "macOS Sierra 10.12 - Common", 13 routeOut: ` route to: default 14destination: default 15 mask: default 16 gateway: 10.23.9.1 17 interface: en0 18 flags: <UP,GATEWAY,DONE,STATIC,PRCLONING> 19 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire 20 0 0 0 0 0 0 1500 0 21`, 22 want: "en0", 23 }, 24 } 25 26 for _, tc := range testCases { 27 t.Run(tc.name, func(t *testing.T) { 28 got, err := parseDefaultIfNameFromRoute(tc.routeOut) 29 if err != nil { 30 t.Fatalf("unable to parse default interface from route output: %v", err) 31 } 32 33 if got != tc.want { 34 t.Errorf("got %s; want %s", got, tc.want) 35 } 36 }) 37 } 38} 39 40func Test_parseLinuxDefaultIfName(t *testing.T) { 41 testCases := []struct { 42 name string 43 routeOut string 44 want string 45 }{ 46 { 47 name: "Linux Ubuntu 14.04 - Common", 48 routeOut: `default via 10.1.2.1 dev eth0 4910.1.2.0/24 dev eth0 proto kernel scope link src 10.1.2.5 50`, 51 want: "eth0", 52 }, 53 { 54 name: "Chromebook - 8743.85.0 (Official Build) stable-channel gandof, Milestone 54", 55 routeOut: `default via 192.168.1.1 dev wlan0 metric 1 56192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.174 57`, 58 want: "wlan0", 59 }, 60 } 61 62 for _, tc := range testCases { 63 t.Run(tc.name, func(t *testing.T) { 64 got, err := parseDefaultIfNameFromIPCmd(tc.routeOut) 65 if err != nil { 66 t.Fatalf("unable to parse default interface from route output: %v", err) 67 } 68 69 if got != tc.want { 70 t.Errorf("got %+q; want %+q", got, tc.want) 71 } 72 }) 73 } 74} 75 76func Test_parseWindowsDefaultIfName(t *testing.T) { 77 testCases := []struct { 78 name string 79 routeOut string 80 ipconfigOut string 81 want string 82 }{ 83 { 84 name: "Windows 10 - Enterprise", 85 routeOut: `=========================================================================== 86Interface List 87 10...08 00 27 a2 e9 51 ......Intel(R) PRO/1000 MT Desktop Adapter 88 13...08 00 27 35 02 ed ......Intel(R) PRO/1000 MT Desktop Adapter #2 89 1...........................Software Loopback Interface 1 90 5...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter 91 8...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3 92=========================================================================== 93 94IPv4 Route Table 95=========================================================================== 96Active Routes: 97Network Destination Netmask Gateway Interface Metric 98 0.0.0.0 0.0.0.0 10.0.2.2 10.0.2.15 25 99 10.0.2.0 255.255.255.0 On-link 10.0.2.15 281 100 10.0.2.15 255.255.255.255 On-link 10.0.2.15 281 101 10.0.2.255 255.255.255.255 On-link 10.0.2.15 281 102 127.0.0.0 255.0.0.0 On-link 127.0.0.1 331 103 127.0.0.1 255.255.255.255 On-link 127.0.0.1 331 104 127.255.255.255 255.255.255.255 On-link 127.0.0.1 331 105 192.168.56.0 255.255.255.0 On-link 192.168.56.100 281 106 192.168.56.100 255.255.255.255 On-link 192.168.56.100 281 107 192.168.56.255 255.255.255.255 On-link 192.168.56.100 281 108 224.0.0.0 240.0.0.0 On-link 127.0.0.1 331 109 224.0.0.0 240.0.0.0 On-link 192.168.56.100 281 110 224.0.0.0 240.0.0.0 On-link 10.0.2.15 281 111 255.255.255.255 255.255.255.255 On-link 127.0.0.1 331 112 255.255.255.255 255.255.255.255 On-link 192.168.56.100 281 113 255.255.255.255 255.255.255.255 On-link 10.0.2.15 281 114=========================================================================== 115Persistent Routes: 116 None 117 118IPv6 Route Table 119=========================================================================== 120Active Routes: 121 If Metric Network Destination Gateway 122 1 331 ::1/128 On-link 123 13 281 fe80::/64 On-link 124 10 281 fe80::/64 On-link 125 13 281 fe80::60cc:155f:77a4:ab99/128 126 On-link 127 10 281 fe80::cccc:710e:f5bb:3088/128 128 On-link 129 1 331 ff00::/8 On-link 130 13 281 ff00::/8 On-link 131 10 281 ff00::/8 On-link 132=========================================================================== 133Persistent Routes: 134 None 135`, 136 ipconfigOut: `Windows IP Configuration 137 138 139Ethernet adapter Ethernet: 140 141 Connection-specific DNS Suffix . : host.example.org 142 Link-local IPv6 Address . . . . . : fe80::cccc:710e:f5bb:3088%10 143 IPv4 Address. . . . . . . . . . . : 10.0.2.15 144 Subnet Mask . . . . . . . . . . . : 255.255.255.0 145 Default Gateway . . . . . . . . . : 10.0.2.2 146 147Ethernet adapter Ethernet 2: 148 149 Connection-specific DNS Suffix . : 150 Link-local IPv6 Address . . . . . : fe80::60cc:155f:77a4:ab99%13 151 IPv4 Address. . . . . . . . . . . : 192.168.56.100 152 Subnet Mask . . . . . . . . . . . : 255.255.255.0 153 Default Gateway . . . . . . . . . : 154 155Tunnel adapter isatap.host.example.org: 156 157 Media State . . . . . . . . . . . : Media disconnected 158 Connection-specific DNS Suffix . : 159 160Tunnel adapter Reusable ISATAP Interface {F3F2E4A5-8823-40E5-87EA-1F6881BACC95}: 161 162 Media State . . . . . . . . . . . : Media disconnected 163 Connection-specific DNS Suffix . : host.example.org 164`, 165 want: "Ethernet", 166 }, 167 } 168 169 for _, tc := range testCases { 170 t.Run(tc.name, func(t *testing.T) { 171 got, err := parseDefaultIfNameWindows(tc.routeOut, tc.ipconfigOut) 172 if err != nil { 173 t.Fatalf("unable to parse default interface from route output: %v", err) 174 } 175 176 if got != tc.want { 177 t.Errorf("got %s; want %s", got, tc.want) 178 } 179 }) 180 } 181} 182 183func Test_VisitComands(t *testing.T) { 184 ri, err := NewRouteInfo() 185 if err != nil { 186 t.Fatalf("bad: %v", err) 187 } 188 189 var count int 190 ri.VisitCommands(func(name string, cmd []string) { 191 count++ 192 }) 193 if count == 0 { 194 t.Fatalf("Expected more than 0 items") 195 } 196} 197