public void ConvertFromUnknownRASTUNNELENDPOINTTest() { NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT(); value.type = NativeMethods.RASTUNNELENDPOINTTYPE.Unknown; IPAddressConverter target = new IPAddressConverter(); IPAddress result = (IPAddress)target.ConvertFrom(value); Assert.IsNull(result); }
/// <summary> /// Converts the given object to the type of this converter. /// </summary> /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">A <see cref="System.Globalization.CultureInfo"/>. If null is passed, the current culture is presumed.</param> /// <param name="value">The value to convert.</param> /// <param name="destinationType">The destination type.</param> /// <returns>The converted object.</returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { IPAddress addr = (IPAddress)value; if (destinationType == typeof(NativeMethods.RASIPADDR) && (addr == null || (addr != null && addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork))) { NativeMethods.RASIPADDR ipv4 = new NativeMethods.RASIPADDR(); if (value == null) { ipv4.addr = IPAddress.Any.GetAddressBytes(); } else { ipv4.addr = addr.GetAddressBytes(); } return ipv4; } #if (WIN2K8 || WIN7 || WIN8) else if (destinationType == typeof(NativeMethods.RASIPV6ADDR) && (addr == null || (addr != null && addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))) { NativeMethods.RASIPV6ADDR ipv6 = new NativeMethods.RASIPV6ADDR(); if (addr == null) { ipv6.addr = IPAddress.IPv6Any.GetAddressBytes(); } else { ipv6.addr = addr.GetAddressBytes(); } return ipv6; } #endif #if (WIN7 || WIN8) else if (destinationType == typeof(NativeMethods.RASTUNNELENDPOINT)) { NativeMethods.RASTUNNELENDPOINT endpoint = new NativeMethods.RASTUNNELENDPOINT(); if (addr != null) { byte[] bytes = new byte[16]; byte[] actual = addr.GetAddressBytes(); // Transfer the bytes to the Array.Copy(actual, bytes, actual.Length); switch (addr.AddressFamily) { case AddressFamily.InterNetwork: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4; break; case AddressFamily.InterNetworkV6: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv6; break; default: endpoint.type = NativeMethods.RASTUNNELENDPOINTTYPE.Unknown; break; } endpoint.addr = bytes; } return endpoint; } #endif return base.ConvertTo(context, culture, value, destinationType); }
public void ConvertFromIPv6RASTUNNELENDPOINTTest() { byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes(); NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT(); value.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv6; value.addr = expected; IPAddressConverter target = new IPAddressConverter(); IPAddress result = (IPAddress)target.ConvertFrom(value); byte[] actual = result.GetAddressBytes(); Assert.AreEqual<System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetworkV6, result.AddressFamily); CollectionAssert.AreEqual(expected, actual); }