IPEndPointTest.cs 4.2 KB

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