2 // IPInterfacePropertiesTest.cs - NUnit Test Cases for System.Net.NetworkInformation.IPInterfaceProperties
5 // Ben Woods (woodsb02@gmail.com)
10 using System.Globalization;
13 using System.Net.NetworkInformation;
15 namespace MonoTests.System.Net.NetworkInformation
19 public class IPInterfacePropertiesTest
22 public void AtLeastOneUnicastAddress ()
24 int numUnicastAddresses = 0;
25 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
26 foreach (NetworkInterface adapter in adapters)
28 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
29 UnicastIPAddressInformationCollection unicastAddresses = adapterProperties.UnicastAddresses;
30 numUnicastAddresses += unicastAddresses.Count;
32 Assert.IsTrue (numUnicastAddresses > 0);
35 // Borrowed from IPInterfaceProperties.cs
36 bool HasOnlyDefaultGateway (string iface)
39 int defaultGwCount = 0;
41 if (!File.Exists ("/proc/net/route"))
44 using (StreamReader reader = new StreamReader ("/proc/net/route")) {
46 reader.ReadLine (); // Ignore first line
47 while ((line = reader.ReadLine ()) != null) {
52 string [] parts = line.Split ('\t');
55 string gw_address = parts [2].Trim ();
56 byte [] ipbytes = new byte [4];
57 if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
58 for (int i = 0; i < 4; i++) {
59 if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
62 IPAddress ip = new IPAddress (ipbytes);
63 if (ip.Equals (IPAddress.Any))
73 return gwCount == 0 && defaultGwCount > 0;
77 public void AtLeastOneGatewayAddress ()
79 int numGatewayAddresses = 0;
80 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
82 // On Android (possibly on other systems too) it is possible that no gateway address is available and its lack is NOT an error
83 // Here is a sample of /proc/net/route from Nexus 9 running Android 5.1.1 (IPInterfaceProperties parses that file on Linux)
85 // Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
86 // wlan0 0001A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
88 // Gateway is set to any address and it is explicitly ignored by the route information parser
90 // For comparison, here's route contents from an Android 4.4.4 device:
92 // Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
93 // wlan0 00000000 0101A8C0 0003 0 0 0 00000000 0 0 0
94 // wlan0 00000000 0101A8C0 0003 0 0 203 00000000 0 0 0
95 // wlan0 0001A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
96 // wlan0 0001A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
97 // wlan0 0001A8C0 00000000 0001 0 0 203 00FFFFFF 0 0 0
98 // wlan0 0101A8C0 00000000 0005 0 0 0 FFFFFFFF 0 0 0
100 // Obviously, this test fails on the first device and succeeds on the second. For this reason the test is modified to succeed
101 // in case of devices like the first one since it's not a real failure but a shortcoming of the .NET API
103 foreach (NetworkInterface adapter in adapters)
105 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
106 GatewayIPAddressInformationCollection gatewayAddresses = adapterProperties.GatewayAddresses;
107 numGatewayAddresses += HasOnlyDefaultGateway (adapter.Name) ? 1 : gatewayAddresses.Count;
110 Assert.IsTrue (numGatewayAddresses > 0);
114 public void DnsEnabled ()
116 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
117 foreach (NetworkInterface adapter in adapters)
119 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
120 Assert.IsTrue (adapterProperties.IsDnsEnabled);
125 // The code works as expected when part of a regular app. It fails when executed from within an NUnit test
126 // Might be a problem with the test suite. To investigate.
127 [Category("AndroidNotWorking")]
128 public void AtLeastOneDnsAddress ()
130 int numDnsAddresses = 0;
131 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
132 foreach (NetworkInterface adapter in adapters)
134 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
135 IPAddressCollection dnsAddresses = adapterProperties.DnsAddresses;
136 numDnsAddresses += dnsAddresses.Count;
138 Console.WriteLine ("numDnsAddresses == {0}", numDnsAddresses);
139 // reading /etc/resolve.conf does not work on iOS devices (but works on simulator)
140 // ref: https://bugzilla.xamarin.com/show_bug.cgi?id=27707
142 Assert.IsTrue (numDnsAddresses > 0);