IPEndPointTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // IPEndPointTest.cs - NUnit Test Cases for System.Net.IPEndPoint
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Net;
  10. using System.Runtime.InteropServices;
  11. namespace MonoTests.System.Net
  12. {
  13. public class IPEndPointTest : TestCase
  14. {
  15. private const int MyPort = 42;
  16. private const int MyMaxPort = 65535;
  17. private const int MyMinPort = 0;
  18. private const string MyIPAddressString = "192.168.1.1";
  19. private IPAddress ipAddress;
  20. private long ip;
  21. private IPEndPoint endPoint1;
  22. private IPEndPoint endPoint2;
  23. public IPEndPointTest () :
  24. base ("[MonoTests.System.Net.IPEndPointTest]") {}
  25. public IPEndPointTest (string name) : base (name) {}
  26. protected override void SetUp ()
  27. {
  28. ipAddress = IPAddress.Parse (MyIPAddressString);
  29. ip = ipAddress.Address;
  30. endPoint1 = new IPEndPoint(ipAddress, MyPort);
  31. endPoint2 = new IPEndPoint(ip, MyPort);
  32. }
  33. protected override void TearDown () {}
  34. public static ITest Suite
  35. {
  36. get {
  37. return new TestSuite(typeof(IPEndPointTest));
  38. }
  39. }
  40. public void TestPublicFields ()
  41. {
  42. AssertEquals ("MinPort", IPEndPoint.MinPort, MyMinPort);
  43. AssertEquals ("MaxPort", IPEndPoint.MaxPort, MyMaxPort);
  44. }
  45. public void TestConstructors ()
  46. {
  47. try {
  48. new IPEndPoint(null, 0);
  49. Fail("Should raise an ArgumentNullException");
  50. } catch (ArgumentNullException) {
  51. }
  52. try {
  53. new IPEndPoint(ipAddress, MyMinPort - 1);
  54. Fail("Should raise an ArgumentOutOfRangeException #1");
  55. } catch (ArgumentOutOfRangeException) {
  56. }
  57. try {
  58. new IPEndPoint(ipAddress, MyMaxPort + 1);
  59. Fail("Should raise an ArgumentOutOfRangeException #2");
  60. } catch (ArgumentOutOfRangeException) {
  61. }
  62. try {
  63. new IPEndPoint(ip, MyMinPort -1);
  64. Fail("Should raise an ArgumentOutOfRangeException #3");
  65. } catch (ArgumentOutOfRangeException) {
  66. }
  67. try {
  68. new IPEndPoint(ip, MyMaxPort + 1);
  69. Fail("Should raise an ArgumentOutOfRangeException #4");
  70. } catch (ArgumentOutOfRangeException) {
  71. }
  72. }
  73. public void TestPortProperty ()
  74. {
  75. try {
  76. endPoint1.Port = MyMinPort - 1;
  77. Fail("Should raise an ArgumentOutOfRangeException #1");
  78. } catch (ArgumentOutOfRangeException) {
  79. }
  80. try {
  81. endPoint1.Port = MyMaxPort + 1;
  82. Fail("Should raise an ArgumentOutOfRangeException #2");
  83. } catch (ArgumentOutOfRangeException) {
  84. }
  85. }
  86. public void TestCreateAndSerialize()
  87. {
  88. SocketAddress addr = endPoint1.Serialize();
  89. EndPoint endPoint3 = endPoint2.Create(addr);
  90. Assert("#1", endPoint1.Equals(endPoint3));
  91. IPAddress ipAddress = IPAddress.Parse ("255.255.255.255");
  92. IPEndPoint endPoint4 = new IPEndPoint(ipAddress, MyMaxPort);
  93. addr = endPoint4.Serialize();
  94. EndPoint endPoint5 = endPoint2.Create(addr);
  95. Assert("#2", endPoint4.Equals(endPoint5));
  96. AssertEquals("#3", endPoint5.ToString(), "255.255.255.255:" + MyMaxPort);
  97. }
  98. public void TestEquals ()
  99. {
  100. Assert("Equals", endPoint1.Equals(endPoint2));
  101. Assert("Not Equals", !endPoint1.Equals(new IPEndPoint(ip, MyPort + 1)));
  102. }
  103. public void TestGetHashCode ()
  104. {
  105. AssertEquals(endPoint1.GetHashCode(), endPoint2.GetHashCode());
  106. }
  107. public void TestToString ()
  108. {
  109. AssertEquals("ToString #1", endPoint1.ToString(), MyIPAddressString + ":" + MyPort);
  110. AssertEquals("ToString #2", endPoint2.ToString(), MyIPAddressString + ":" + MyPort);
  111. }
  112. }
  113. }