PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Driver.Core.Tests/Core/Misc/EndPointHelperTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 146 lines | 110 code | 22 blank | 14 comment | 2 complexity | 8820426e1203a8cd79624e7a7a5f08da MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using FluentAssertions;
  19. using MongoDB.Driver.Core.Misc;
  20. using NUnit.Framework;
  21. namespace MongoDB.Driver.Core.Misc
  22. {
  23. [TestFixture]
  24. public class EndPointHelperTests
  25. {
  26. [Test]
  27. [TestCase("localhost:27017", "localhost:27017", true)]
  28. [TestCase("localhost:27017", "localhost:27018", false)]
  29. [TestCase("localhost:27018", "localhost:27017", false)]
  30. [TestCase("127.0.0.1:27017", "localhost:27017", false)]
  31. [TestCase("localhost:27017", "127.0.0.1:27017", false)]
  32. [TestCase("127.0.0.1:27017", "127.0.0.1:27017", true)]
  33. [TestCase("127.0.0.1:27017", "127.0.0.1:27018", false)]
  34. [TestCase("127.0.0.1:27018", "127.0.0.1:27017", false)]
  35. [TestCase(null, "localhost:27017", false)]
  36. [TestCase("localhost:27017", null, false)]
  37. [TestCase(null, null, true)]
  38. public void Equals_should_return_true_when_endpoints_are_equal(string a, string b, bool expectedResult)
  39. {
  40. var endPoint1 = a == null ? null : EndPointHelper.Parse(a);
  41. var endPoint2 = b == null ? null : EndPointHelper.Parse(b);
  42. var result = EndPointHelper.Equals(endPoint1, endPoint2);
  43. result.Should().Be(expectedResult);
  44. }
  45. [Test]
  46. public void Parse_should_throw_an_ArgumentNullException_when_value_is_null()
  47. {
  48. Action act = () => EndPointHelper.Parse(null);
  49. act.ShouldThrow<ArgumentNullException>();
  50. }
  51. [Test]
  52. [TestCase("gob:2::212")]
  53. [TestCase("localhost:-1")]
  54. [TestCase("localhost:66000")]
  55. [TestCase(":21")]
  56. public void Parse_should_throw_an_ArgumentException_when_value_is_not_a_valid_end_point(string value)
  57. {
  58. Action act = () => EndPointHelper.Parse(value);
  59. act.ShouldThrow<ArgumentException>();
  60. }
  61. [Test]
  62. public void ToString_should_return_expected_result_when_value_is_a_DnsEndPoint()
  63. {
  64. var endPoint = new DnsEndPoint("localhost", 27017);
  65. var result = EndPointHelper.ToString(endPoint);
  66. result.Should().Be("localhost:27017");
  67. }
  68. [Test]
  69. public void ToString_should_return_expected_result_when_value_is_an_IPEndPoint()
  70. {
  71. var endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 27017);
  72. var result = EndPointHelper.ToString(endPoint);
  73. result.Should().Be("127.0.0.1:27017");
  74. }
  75. [Test]
  76. [TestCase("gob:2::212")]
  77. [TestCase("localhost:-1")]
  78. [TestCase("localhost:66000")]
  79. [TestCase(":21")]
  80. public void TryParse_should_return_false_when_the_end_point_is_invalid(string value)
  81. {
  82. EndPoint result;
  83. var success = EndPointHelper.TryParse(value, out result);
  84. success.Should().BeFalse();
  85. }
  86. [Test]
  87. [TestCase("localhost", "localhost", 27017)]
  88. [TestCase("localhost:28017", "localhost", 28017)]
  89. [TestCase("act.test.com", "act.test.com", 27017)]
  90. [TestCase("act.test.com:28017", "act.test.com", 28017)]
  91. [TestCase("123.test.com", "123.test.com", 27017)]
  92. [TestCase("123.test.com:28017", "123.test.com", 28017)]
  93. public void TryParse_should_parse_a_hostname(string value, string expectedHost, int expectedPort)
  94. {
  95. EndPoint result;
  96. var success = EndPointHelper.TryParse(value, out result);
  97. success.Should().BeTrue();
  98. result.Should().Be(new DnsEndPoint(expectedHost, expectedPort));
  99. result.AddressFamily.Should().Be(AddressFamily.Unspecified);
  100. }
  101. [Test]
  102. [TestCase("127.0.0.1", "127.0.0.1", 27017)]
  103. [TestCase("127.0.0.1:28017", "127.0.0.1", 28017)]
  104. public void TryParse_should_parse_an_ipv4_address(string value, string expectedAddress, int expectedPort)
  105. {
  106. EndPoint result;
  107. var success = EndPointHelper.TryParse(value, out result);
  108. success.Should().BeTrue();
  109. result.Should().Be(new IPEndPoint(IPAddress.Parse(expectedAddress), expectedPort));
  110. result.AddressFamily.Should().Be(AddressFamily.InterNetwork);
  111. }
  112. [Test]
  113. [TestCase("[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", "[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", 27017)]
  114. [TestCase("[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:28017", "[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", 28017)]
  115. public void TryParse_should_parse_an_ipv6_address(string value, string expectedAddress, int expectedPort)
  116. {
  117. EndPoint result;
  118. var success = EndPointHelper.TryParse(value, out result);
  119. success.Should().BeTrue();
  120. result.Should().Be(new IPEndPoint(IPAddress.Parse(expectedAddress), expectedPort));
  121. result.AddressFamily.Should().Be(AddressFamily.InterNetworkV6);
  122. }
  123. }
  124. }