New run-jenkins tags to allow a checked build job and fix partial checked build suppo...
[mono.git] / mcs / class / System / Test / System.Net.NetworkInformation / IPInterfacePropertiesTest.cs
1 //
2 // IPInterfacePropertiesTest.cs - NUnit Test Cases for System.Net.NetworkInformation.IPInterfaceProperties
3 //
4 // Authors:
5 //   Ben Woods (woodsb02@gmail.com)
6 //
7
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
11 using System.IO;
12 using System.Net;
13 using System.Net.NetworkInformation;
14
15 namespace MonoTests.System.Net.NetworkInformation
16 {
17
18         [TestFixture]
19         public class IPInterfacePropertiesTest
20         {
21                 [Test]
22                 public void AtLeastOneUnicastAddress ()
23                 {
24                         int numUnicastAddresses = 0;
25                         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
26                         foreach (NetworkInterface adapter in adapters)
27                         {
28                                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
29                                 UnicastIPAddressInformationCollection unicastAddresses = adapterProperties.UnicastAddresses;
30                                 numUnicastAddresses += unicastAddresses.Count;
31                         }
32                         Assert.IsTrue (numUnicastAddresses > 0);
33                 }
34
35                 // Borrowed from IPInterfaceProperties.cs
36                 bool HasOnlyDefaultGateway (string iface)
37                 {
38                         int gwCount = 0;
39                         int defaultGwCount = 0;
40 #if MONODROID
41                         if (!File.Exists ("/proc/net/route"))
42                                 return false;
43                         try {
44                                 using (StreamReader reader = new StreamReader ("/proc/net/route")) {
45                                         string line;
46                                         reader.ReadLine (); // Ignore first line
47                                         while ((line = reader.ReadLine ()) != null) {
48                                                 line = line.Trim ();
49                                                 if (line.Length == 0)
50                                                         continue;
51
52                                                 string [] parts = line.Split ('\t');
53                                                 if (parts.Length < 3)
54                                                         continue;
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]))
60                                                                         continue;
61                                                         }
62                                                         IPAddress ip = new IPAddress (ipbytes);
63                                                         if (ip.Equals (IPAddress.Any))
64                                                                 defaultGwCount++;
65                                                         else
66                                                                 gwCount++;
67                                                 }
68                                         }
69                                 }
70                         } catch {
71                         }
72 #endif
73                         return gwCount == 0 && defaultGwCount > 0;
74                 }
75         
76                 [Test]
77                 public void AtLeastOneGatewayAddress ()
78                 {
79                         int numGatewayAddresses = 0;
80                         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
81                         
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)
84                         //
85                         //  Iface       Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT
86                         //  wlan0       0001A8C0        00000000        0001    0       0       0       00FFFFFF        0       0       0
87                         //
88                         // Gateway is set to any address and it is explicitly ignored by the route information parser
89                         //
90                         // For comparison, here's route contents from an Android 4.4.4 device:
91                         //
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
99                         //
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
102                         //
103                         foreach (NetworkInterface adapter in adapters)
104                         {
105                                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
106                                 GatewayIPAddressInformationCollection gatewayAddresses = adapterProperties.GatewayAddresses;
107                                 numGatewayAddresses += HasOnlyDefaultGateway (adapter.Name) ? 1 : gatewayAddresses.Count;
108                         }
109                         
110                         Assert.IsTrue (numGatewayAddresses > 0);
111                 }
112         
113                 [Test]
114                 public void DnsEnabled ()
115                 {
116                         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
117                         foreach (NetworkInterface adapter in adapters)
118                         {
119                                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
120                                 Assert.IsTrue (adapterProperties.IsDnsEnabled);
121                         }
122                 }
123         
124                 [Test]
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 ()
129                 {
130                         int numDnsAddresses = 0;
131                         NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces ();
132                         foreach (NetworkInterface adapter in adapters)
133                         {
134                                 IPInterfaceProperties adapterProperties = adapter.GetIPProperties ();
135                                 IPAddressCollection dnsAddresses = adapterProperties.DnsAddresses;
136                                 numDnsAddresses += dnsAddresses.Count;
137                         }
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
141 #if !MONOTOUCH
142                         Assert.IsTrue (numDnsAddresses > 0);
143 #endif
144                 }
145         
146         }
147 }