/Branches/1.3/Source/DotRas/DotRas.UnitTests/IPAddressConverterTest.cs

# · C# · 339 lines · 233 code · 87 blank · 19 comment · 3 complexity · 2270aa12d1693d09bb22e28550631ff3 MD5 · raw file

  1. //--------------------------------------------------------------------------
  2. // <copyright file="IPAddressConverterTest.cs" company="Jeff Winn">
  3. // Copyright (c) Jeff Winn. All rights reserved.
  4. //
  5. // The use and distribution terms for this software is covered by the
  6. // GNU Library General Public License (LGPL) v2.1 which can be found
  7. // in the License.rtf at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. // </copyright>
  13. //--------------------------------------------------------------------------
  14. namespace DotRas.UnitTests
  15. {
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Net;
  20. using System.Text;
  21. using DotRas.Internal;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. /// <summary>
  24. /// This is a test class for <see cref="DotRas.Internal.IPAddressConverter"/> and is intended to contain all associated unit tests.
  25. /// </summary>
  26. [TestClass]
  27. public class IPAddressConverterTest
  28. {
  29. #region Constructors
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="IPAddressConverterTest"/> class.
  32. /// </summary>
  33. public IPAddressConverterTest()
  34. {
  35. }
  36. #endregion
  37. [TestMethod]
  38. public void CanConvertFromUnsupportedType()
  39. {
  40. bool expected = false;
  41. IPAddressConverter target = new IPAddressConverter();
  42. bool actual = target.CanConvertFrom(typeof(bool));
  43. Assert.AreEqual(expected, actual);
  44. }
  45. [TestMethod]
  46. public void CanConvertFromRASIPADDRTest()
  47. {
  48. bool expected = true;
  49. IPAddressConverter target = new IPAddressConverter();
  50. bool actual = target.CanConvertFrom(typeof(NativeMethods.RASIPADDR));
  51. Assert.AreEqual(expected, actual);
  52. }
  53. [TestMethod]
  54. public void CanConvertFromStringTest()
  55. {
  56. bool expected = true;
  57. IPAddressConverter target = new IPAddressConverter();
  58. bool actual = target.CanConvertFrom(typeof(string));
  59. Assert.AreEqual(expected, actual);
  60. }
  61. [TestMethod]
  62. public void CanConvertToUnsupportedTypeTest()
  63. {
  64. bool expected = false;
  65. IPAddressConverter target = new IPAddressConverter();
  66. bool actual = target.CanConvertTo(typeof(bool));
  67. Assert.AreEqual(expected, actual);
  68. }
  69. [TestMethod]
  70. public void CanConvertToRASIPADDRTest()
  71. {
  72. bool expected = true;
  73. IPAddressConverter target = new IPAddressConverter();
  74. bool actual = target.CanConvertTo(typeof(NativeMethods.RASIPADDR));
  75. Assert.AreEqual(expected, actual);
  76. }
  77. [TestMethod]
  78. public void ConvertFromRASIPADDRTest()
  79. {
  80. byte[] expected = IPAddress.Loopback.GetAddressBytes();
  81. NativeMethods.RASIPADDR value = new NativeMethods.RASIPADDR();
  82. value.addr = expected;
  83. IPAddressConverter target = new IPAddressConverter();
  84. IPAddress result = (IPAddress)target.ConvertFrom(value);
  85. byte[] actual = result.GetAddressBytes();
  86. Assert.AreEqual<System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetwork, result.AddressFamily);
  87. CollectionAssert.AreEqual(expected, actual);
  88. }
  89. [TestMethod]
  90. public void ConvertFromStringTest()
  91. {
  92. string expected = IPAddress.Loopback.ToString();
  93. IPAddressConverter target = new IPAddressConverter();
  94. IPAddress result = (IPAddress)target.ConvertFrom(expected);
  95. Assert.IsNotNull(result);
  96. string actual = result.ToString();
  97. Assert.AreEqual(expected, actual, true);
  98. }
  99. [TestMethod]
  100. [ExpectedException(typeof(NotSupportedException))]
  101. public void ConvertFromUnsupportedTypeTest()
  102. {
  103. IPAddressConverter target = new IPAddressConverter();
  104. target.ConvertFrom(true);
  105. }
  106. [TestMethod]
  107. public void ConvertToRASIPADDRTest()
  108. {
  109. byte[] expected = IPAddress.Loopback.GetAddressBytes();
  110. IPAddressConverter target = new IPAddressConverter();
  111. NativeMethods.RASIPADDR result = (NativeMethods.RASIPADDR)target.ConvertTo(IPAddress.Loopback, typeof(NativeMethods.RASIPADDR));
  112. byte[] actual = result.addr;
  113. CollectionAssert.AreEqual(expected, actual);
  114. }
  115. [TestMethod]
  116. [ExpectedException(typeof(NotSupportedException))]
  117. public void ConvertToRASIPADDRFromIPv6Test()
  118. {
  119. IPAddressConverter target = new IPAddressConverter();
  120. target.ConvertTo(IPAddress.IPv6Loopback, typeof(NativeMethods.RASIPADDR));
  121. }
  122. [TestMethod]
  123. public void ConvertToRASIPADDRFromNullTest()
  124. {
  125. byte[] expected = IPAddress.Any.GetAddressBytes();
  126. IPAddressConverter target = new IPAddressConverter();
  127. NativeMethods.RASIPADDR result = (NativeMethods.RASIPADDR)target.ConvertTo(null, typeof(NativeMethods.RASIPADDR));
  128. byte[] actual = result.addr;
  129. CollectionAssert.AreEqual(expected, actual);
  130. }
  131. [TestMethod]
  132. [ExpectedException(typeof(NotSupportedException))]
  133. public void ConvertToUnsupportedTypeTest()
  134. {
  135. IPAddressConverter target = new IPAddressConverter();
  136. target.ConvertTo(null, typeof(bool));
  137. }
  138. #if (WIN2K8 || WIN7 || WIN8)
  139. [TestMethod]
  140. public void CanConvertFromRASIPV6ADDRTest()
  141. {
  142. bool expected = true;
  143. IPAddressConverter target = new IPAddressConverter();
  144. bool actual = target.CanConvertFrom(typeof(NativeMethods.RASIPV6ADDR));
  145. Assert.AreEqual(expected, actual);
  146. }
  147. [TestMethod]
  148. public void CanConvertToRASIPV6ADDRTest()
  149. {
  150. bool expected = true;
  151. IPAddressConverter target = new IPAddressConverter();
  152. bool actual = target.CanConvertTo(typeof(NativeMethods.RASIPV6ADDR));
  153. Assert.AreEqual(expected, actual);
  154. }
  155. [TestMethod]
  156. public void ConvertFromRASIPV6ADDRTest()
  157. {
  158. byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();
  159. NativeMethods.RASIPV6ADDR value = new NativeMethods.RASIPV6ADDR();
  160. value.addr = expected;
  161. IPAddressConverter target = new IPAddressConverter();
  162. IPAddress result = (IPAddress)target.ConvertFrom(value);
  163. byte[] actual = result.GetAddressBytes();
  164. Assert.AreEqual<System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetworkV6, result.AddressFamily);
  165. CollectionAssert.AreEqual(expected, actual);
  166. }
  167. [TestMethod]
  168. public void ConvertToRASIPV6ADDRTest()
  169. {
  170. byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();
  171. IPAddressConverter target = new IPAddressConverter();
  172. NativeMethods.RASIPV6ADDR result = (NativeMethods.RASIPV6ADDR)target.ConvertTo(IPAddress.IPv6Loopback, typeof(NativeMethods.RASIPV6ADDR));
  173. byte[] actual = result.addr;
  174. CollectionAssert.AreEqual(expected, actual);
  175. }
  176. [TestMethod]
  177. public void ConvertToRASIPV6ADDRFromNullTest()
  178. {
  179. byte[] expected = IPAddress.IPv6Any.GetAddressBytes();
  180. IPAddressConverter target = new IPAddressConverter();
  181. NativeMethods.RASIPV6ADDR result = (NativeMethods.RASIPV6ADDR)target.ConvertTo(null, typeof(NativeMethods.RASIPV6ADDR));
  182. byte[] actual = result.addr;
  183. CollectionAssert.AreEqual(expected, actual);
  184. }
  185. #endif
  186. #if (WIN7 || WIN8)
  187. [TestMethod]
  188. public void CanConvertFromRASTUNNELENDPOINTTest()
  189. {
  190. bool expected = true;
  191. IPAddressConverter target = new IPAddressConverter();
  192. bool actual = target.CanConvertFrom(typeof(NativeMethods.RASTUNNELENDPOINT));
  193. Assert.AreEqual(expected, actual);
  194. }
  195. [TestMethod]
  196. public void ConvertFromIPv4RASTUNNELENDPOINTTest()
  197. {
  198. byte[] expected = IPAddress.Loopback.GetAddressBytes();
  199. NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT();
  200. value.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4;
  201. value.addr = expected;
  202. IPAddressConverter target = new IPAddressConverter();
  203. IPAddress result = (IPAddress)target.ConvertFrom(value);
  204. byte[] actual = result.GetAddressBytes();
  205. Assert.AreEqual<System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetwork, result.AddressFamily);
  206. CollectionAssert.AreEqual(expected, actual);
  207. }
  208. [TestMethod]
  209. public void ConvertFromIPv6RASTUNNELENDPOINTTest()
  210. {
  211. byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();
  212. NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT();
  213. value.type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv6;
  214. value.addr = expected;
  215. IPAddressConverter target = new IPAddressConverter();
  216. IPAddress result = (IPAddress)target.ConvertFrom(value);
  217. byte[] actual = result.GetAddressBytes();
  218. Assert.AreEqual<System.Net.Sockets.AddressFamily>(System.Net.Sockets.AddressFamily.InterNetworkV6, result.AddressFamily);
  219. CollectionAssert.AreEqual(expected, actual);
  220. }
  221. [TestMethod]
  222. public void ConvertFromUnknownRASTUNNELENDPOINTTest()
  223. {
  224. NativeMethods.RASTUNNELENDPOINT value = new NativeMethods.RASTUNNELENDPOINT();
  225. value.type = NativeMethods.RASTUNNELENDPOINTTYPE.Unknown;
  226. IPAddressConverter target = new IPAddressConverter();
  227. IPAddress result = (IPAddress)target.ConvertFrom(value);
  228. Assert.IsNull(result);
  229. }
  230. [TestMethod]
  231. public void ConvertToIPv4RASTUNNELENDPOINTTest()
  232. {
  233. byte[] expected = IPAddress.Loopback.GetAddressBytes();
  234. IPAddressConverter target = new IPAddressConverter();
  235. NativeMethods.RASTUNNELENDPOINT result = (NativeMethods.RASTUNNELENDPOINT)target.ConvertTo(IPAddress.Loopback, typeof(NativeMethods.RASTUNNELENDPOINT));
  236. byte[] actual = result.addr;
  237. Assert.AreEqual<NativeMethods.RASTUNNELENDPOINTTYPE>(NativeMethods.RASTUNNELENDPOINTTYPE.IPv4, result.type);
  238. CollectionAssert.IsSubsetOf(expected, actual);
  239. }
  240. [TestMethod]
  241. public void ConvertToIPv6RASTUNNELENDPOINTTest()
  242. {
  243. byte[] expected = IPAddress.IPv6Loopback.GetAddressBytes();
  244. IPAddressConverter target = new IPAddressConverter();
  245. NativeMethods.RASTUNNELENDPOINT result = (NativeMethods.RASTUNNELENDPOINT)target.ConvertTo(IPAddress.IPv6Loopback, typeof(NativeMethods.RASTUNNELENDPOINT));
  246. byte[] actual = result.addr;
  247. Assert.AreEqual<NativeMethods.RASTUNNELENDPOINTTYPE>(NativeMethods.RASTUNNELENDPOINTTYPE.IPv6, result.type);
  248. CollectionAssert.AreEqual(expected, actual);
  249. }
  250. #endif
  251. }
  252. }