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.Linux)]
14     public class IPInterfacePropertiesTest_Linux
15     {
16         private readonly ITestOutputHelper _log;
17 
IPInterfacePropertiesTest_Linux()18         public IPInterfacePropertiesTest_Linux()
19         {
20             _log = TestLogging.GetInstance();
21         }
22 
23         // dotnet: /d/git/corefx/src/Native/Unix/Common/pal_safecrt.h:47: errno_t memcpy_s(void *, size_t, const void *, size_t): Assertion `sizeInBytes >= count' failed.
24         [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/dotnet/corefx/issues/15513
IPInfoTest_AccessAllProperties_NoErrors()25         public void IPInfoTest_AccessAllProperties_NoErrors()
26         {
27             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
28             {
29                 _log.WriteLine("Nic: " + nic.Name);
30                 _log.WriteLine("- Supports IPv4: " + nic.Supports(NetworkInterfaceComponent.IPv4));
31                 _log.WriteLine("- Supports IPv6: " + nic.Supports(NetworkInterfaceComponent.IPv6));
32 
33                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
34 
35                 Assert.NotNull(ipProperties);
36 
37                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.AnycastAddresses);
38 
39                 Assert.NotNull(ipProperties.DhcpServerAddresses);
40                 _log.WriteLine("- Dhcp Server Addresses: " + ipProperties.DhcpServerAddresses.Count);
41                 foreach (IPAddress dhcp in ipProperties.DhcpServerAddresses)
42                 {
43                     _log.WriteLine("-- " + dhcp.ToString());
44                 }
45 
46                 Assert.NotNull(ipProperties.DnsAddresses);
47                 _log.WriteLine("- Dns Addresses: " + ipProperties.DnsAddresses.Count);
48                 foreach (IPAddress dns in ipProperties.DnsAddresses)
49                 {
50                     _log.WriteLine("-- " + dns.ToString());
51                 }
52 
53                 Assert.NotNull(ipProperties.DnsSuffix);
54                 _log.WriteLine("- Dns Suffix: " + ipProperties.DnsSuffix);
55 
56                 Assert.NotNull(ipProperties.GatewayAddresses);
57                 _log.WriteLine("- Gateway Addresses: " + ipProperties.GatewayAddresses.Count);
58                 foreach (GatewayIPAddressInformation gateway in ipProperties.GatewayAddresses)
59                 {
60                     _log.WriteLine("-- " + gateway.Address.ToString());
61                 }
62 
63                 _log.WriteLine("- Dns Enabled: " + ipProperties.IsDnsEnabled);
64 
65                 Assert.Throws<PlatformNotSupportedException>(() => ipProperties.IsDynamicDnsEnabled);
66 
67                 Assert.NotNull(ipProperties.MulticastAddresses);
68                 _log.WriteLine("- Multicast Addresses: " + ipProperties.MulticastAddresses.Count);
69                 foreach (IPAddressInformation multi in ipProperties.MulticastAddresses)
70                 {
71                     _log.WriteLine("-- " + multi.Address.ToString());
72                     Assert.Throws<PlatformNotSupportedException>(() => multi.IsDnsEligible);
73                     Assert.Throws<PlatformNotSupportedException>(() => multi.IsTransient);
74                 }
75 
76                 Assert.NotNull(ipProperties.UnicastAddresses);
77                 _log.WriteLine("- Unicast Addresses: " + ipProperties.UnicastAddresses.Count);
78                 foreach (UnicastIPAddressInformation uni in ipProperties.UnicastAddresses)
79                 {
80                     _log.WriteLine("-- " + uni.Address.ToString());
81                     Assert.Throws<PlatformNotSupportedException>(() => uni.AddressPreferredLifetime);
82                     Assert.Throws<PlatformNotSupportedException>(() => uni.AddressValidLifetime);
83                     Assert.Throws<PlatformNotSupportedException>(() => uni.DhcpLeaseLifetime);
84                     Assert.Throws<PlatformNotSupportedException>(() => uni.DuplicateAddressDetectionState);
85 
86                     Assert.NotNull(uni.IPv4Mask);
87                     _log.WriteLine("--- IPv4 Mask: " + uni.IPv4Mask);
88                     Assert.Throws<PlatformNotSupportedException>(() => uni.IsDnsEligible);
89                     Assert.Throws<PlatformNotSupportedException>(() => uni.IsTransient);
90                     Assert.Throws<PlatformNotSupportedException>(() => uni.PrefixOrigin);
91                     Assert.Throws<PlatformNotSupportedException>(() => uni.SuffixOrigin);
92 
93                     // Prefix Length
94                     Assert.Throws<PlatformNotSupportedException>(() => uni.PrefixLength);
95                 }
96 
97                 Assert.NotNull(ipProperties.WinsServersAddresses);
98                 _log.WriteLine("- Wins Addresses: " + ipProperties.WinsServersAddresses.Count);
99                 foreach (IPAddress wins in ipProperties.WinsServersAddresses)
100                 {
101                     _log.WriteLine("-- " + wins.ToString());
102                 }
103             }
104         }
105 
106         // dotnet: /d/git/corefx/src/Native/Unix/Common/pal_safecrt.h:47: errno_t memcpy_s(void *, size_t, const void *, size_t): Assertion `sizeInBytes >= count' failed.
107         [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/dotnet/corefx/issues/15513
IPInfoTest_AccessAllIPv4Properties_NoErrors()108         public void IPInfoTest_AccessAllIPv4Properties_NoErrors()
109         {
110             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
111             {
112                 _log.WriteLine("Nic: " + nic.Name);
113 
114                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
115 
116                 _log.WriteLine("IPv4 Properties:");
117 
118                 IPv4InterfaceProperties ipv4Properties = ipProperties.GetIPv4Properties();
119 
120                 _log.WriteLine("Index: " + ipv4Properties.Index);
121                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsAutomaticPrivateAddressingActive);
122                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsAutomaticPrivateAddressingEnabled);
123                 Assert.Throws<PlatformNotSupportedException>(() => ipv4Properties.IsDhcpEnabled);
124                 _log.WriteLine("IsForwardingEnabled: " + ipv4Properties.IsForwardingEnabled);
125                 _log.WriteLine("Mtu: " + ipv4Properties.Mtu);
126                 _log.WriteLine("UsesWins: " + ipv4Properties.UsesWins);
127             }
128         }
129 
130         // dotnet: /d/git/corefx/src/Native/Unix/Common/pal_safecrt.h:47: errno_t memcpy_s(void *, size_t, const void *, size_t): Assertion `sizeInBytes >= count' failed.
131         [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/dotnet/corefx/issues/15513
IPInfoTest_AccessAllIPv6Properties_NoErrors()132         public void IPInfoTest_AccessAllIPv6Properties_NoErrors()
133         {
134             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
135             {
136                 _log.WriteLine("Nic: " + nic.Name);
137 
138                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
139 
140                 _log.WriteLine("IPv6 Properties:");
141 
142                 IPv6InterfaceProperties ipv6Properties = ipProperties.GetIPv6Properties();
143 
144                 if (ipv6Properties == null)
145                 {
146                     _log.WriteLine("IPv6Properties is null");
147                     continue;
148                 }
149 
150                 _log.WriteLine("Index: " + ipv6Properties.Index);
151                 _log.WriteLine("Mtu: " + ipv6Properties.Mtu);
152                 Assert.Throws<PlatformNotSupportedException>(() => ipv6Properties.GetScopeId(ScopeLevel.Link));
153             }
154         }
155 
156         // dotnet: /d/git/corefx/src/Native/Unix/Common/pal_safecrt.h:47: errno_t memcpy_s(void *, size_t, const void *, size_t): Assertion `sizeInBytes >= count' failed.
157         [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // https://github.com/dotnet/corefx/issues/15513
158         [Trait("IPv6", "true")]
IPv6ScopeId_AccessAllValues_Success()159         public void IPv6ScopeId_AccessAllValues_Success()
160         {
161             Assert.True(Capability.IPv6Support());
162 
163             foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
164             {
165                 _log.WriteLine("Nic: " + nic.Name);
166 
167                 if (!nic.Supports(NetworkInterfaceComponent.IPv6))
168                 {
169                     continue;
170                 }
171 
172                 IPInterfaceProperties ipProperties = nic.GetIPProperties();
173 
174                 IPv6InterfaceProperties ipv6Properties = ipProperties.GetIPv6Properties();
175 
176                 Array values = Enum.GetValues(typeof(ScopeLevel));
177                 foreach (ScopeLevel level in values)
178                 {
179                     Assert.Throws<PlatformNotSupportedException>(() => ipv6Properties.GetScopeId(level));
180                 }
181             }
182         }
183     }
184 }
185