1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Net.Sockets;
6 using System.Net.Test.Common;
7 
8 using Xunit;
9 using Xunit.Abstractions;
10 
11 namespace System.Net.NetworkInformation.Tests
12 {
13     [PlatformSpecific(TestPlatforms.OSX)]
14     public class IPInterfacePropertiesTest_OSX
15     {
16         private readonly ITestOutputHelper _log;
17 
IPInterfacePropertiesTest_OSX()18         public IPInterfacePropertiesTest_OSX()
19         {
20             _log = TestLogging.GetInstance();
21         }
22 
23         [Fact]
IPInfoTest_AccessAllProperties_NoErrors()24         public void IPInfoTest_AccessAllProperties_NoErrors()
25         {
26             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
27             {
28                 _log.WriteLine("Nic: " + nic.Name);
29                 _log.WriteLine("- Supports IPv4: " + nic.Supports(NetworkInterfaceComponent.IPv4));
30                 _log.WriteLine("- Supports IPv6: " + nic.Supports(NetworkInterfaceComponent.IPv6));
31 
32                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
33 
34                 Assert.NotNull(ipProperties);
35 
36                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.AnycastAddresses);
37 
38                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.DhcpServerAddresses);
39 
40                 try
41                 {
42                     Assert.NotNull(ipProperties.DnsAddresses);
43                     _log.WriteLine("- Dns Addresses: " + ipProperties.DnsAddresses.Count);
44                     foreach (IPAddress dns in ipProperties.DnsAddresses)
45                     {
46                         _log.WriteLine("-- " + dns.ToString());
47                     }
48                     Assert.NotNull(ipProperties.DnsSuffix);
49                     _log.WriteLine("- Dns Suffix: " + ipProperties.DnsSuffix);
50                 }
51                 // If /etc/resolv.conf does not exist, DNS values cannot be retrieved.
52                 catch (PlatformNotSupportedException) { }
53 
54                 Assert.NotNull(ipProperties.GatewayAddresses);
55                 _log.WriteLine("- Gateway Addresses: " + ipProperties.GatewayAddresses.Count);
56                 foreach (GatewayIPAddressInformation gateway in ipProperties.GatewayAddresses)
57                 {
58                     _log.WriteLine("-- " + gateway.Address.ToString());
59                 }
60 
61                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.IsDnsEnabled);
62 
63                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.IsDynamicDnsEnabled);
64 
65                 Assert.NotNull(ipProperties.MulticastAddresses);
66                 _log.WriteLine("- Multicast Addresses: " + ipProperties.MulticastAddresses.Count);
67                 foreach (IPAddressInformation multi in ipProperties.MulticastAddresses)
68                 {
69                     _log.WriteLine("-- " + multi.Address.ToString());
70                     Assert.Throws<PlatformNotSupportedException>(() => multi.IsDnsEligible);
71                     Assert.Throws<PlatformNotSupportedException>(() => multi.IsTransient);
72                 }
73 
74                 Assert.NotNull(ipProperties.UnicastAddresses);
75                 _log.WriteLine("- Unicast Addresses: " + ipProperties.UnicastAddresses.Count);
76                 foreach (UnicastIPAddressInformation uni in ipProperties.UnicastAddresses)
77                 {
78                     _log.WriteLine("-- " + uni.Address.ToString());
79                     Assert.Throws<PlatformNotSupportedException>(() => uni.AddressPreferredLifetime);
80                     Assert.Throws<PlatformNotSupportedException>(() => uni.AddressValidLifetime);
81                     Assert.Throws<PlatformNotSupportedException>(() => uni.DhcpLeaseLifetime);
82                     Assert.Throws<PlatformNotSupportedException>(() => uni.DuplicateAddressDetectionState);
83 
84                     Assert.NotNull(uni.IPv4Mask);
85                     _log.WriteLine("--- IPv4 Mask: " + uni.IPv4Mask);
86                     Assert.Throws<PlatformNotSupportedException>(() => uni.IsDnsEligible);
87                     Assert.Throws<PlatformNotSupportedException>(() => uni.IsTransient);
88                     Assert.Throws<PlatformNotSupportedException>(() => uni.PrefixOrigin);
89                     Assert.Throws<PlatformNotSupportedException>(() => uni.SuffixOrigin);
90 
91                     // Prefix Length
92                     Assert.Throws<PlatformNotSupportedException>(() => uni.PrefixLength);
93                 }
94 
95                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.WinsServersAddresses);
96             }
97         }
98 
99         [Fact]
IPInfoTest_AccessAllIPv4Properties_NoErrors()100         public void IPInfoTest_AccessAllIPv4Properties_NoErrors()
101         {
102             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
103             {
104                 _log.WriteLine("Nic: " + nic.Name);
105 
106                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
107 
108                 _log.WriteLine("IPv4 Properties:");
109 
110                 IPv4InterfaceProperties ipv4Properties = ipProperties.GetIPv4Properties();
111 
112                 _log.WriteLine("Index: " + ipv4Properties.Index);
113                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsAutomaticPrivateAddressingActive);
114                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsAutomaticPrivateAddressingEnabled);
115                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsDhcpEnabled);
116                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsForwardingEnabled);
117                 _log.WriteLine("Mtu: " + ipv4Properties.Mtu);
118                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.UsesWins);
119             }
120         }
121 
122         [Fact]
IPInfoTest_AccessAllIPv6Properties_NoErrors()123         public void IPInfoTest_AccessAllIPv6Properties_NoErrors()
124         {
125             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
126             {
127                 _log.WriteLine("Nic: " + nic.Name);
128 
129                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
130 
131                 _log.WriteLine("IPv6 Properties:");
132 
133                 IPv6InterfaceProperties ipv6Properties = ipProperties.GetIPv6Properties();
134 
135                 if (ipv6Properties == null)
136                 {
137                     _log.WriteLine("IPv6Properties is null");
138                     continue;
139                 }
140 
141                 _log.WriteLine("Index: " + ipv6Properties.Index);
142                 _log.WriteLine("Mtu: " + ipv6Properties.Mtu);
143                 Assert.Throws<PlatformNotSupportedException>(() => ipv6Properties.GetScopeId(ScopeLevel.Link));
144             }
145         }
146 
147         [Fact]
148         [Trait("IPv6", "true")]
IPv6ScopeId_AccessAllValues_Success()149         public void IPv6ScopeId_AccessAllValues_Success()
150         {
151             Assert.True(Capability.IPv6Support());
152 
153             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
154             {
155                 _log.WriteLine("Nic: " + nic.Name);
156 
157                 if (!nic.Supports(NetworkInterfaceComponent.IPv6))
158                 {
159                     continue;
160                 }
161 
162                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
163 
164                 IPv6InterfaceProperties ipv6Properties = ipProperties.GetIPv6Properties();
165 
166                 Array values = Enum.GetValues(typeof(ScopeLevel));
167                 foreach (ScopeLevel level in values)
168                 {
169                     Assert.Throws<PlatformNotSupportedException>(() => ipv6Properties.GetScopeId(level));
170                 }
171             }
172         }
173     }
174 }
175