PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 145 lines | 109 code | 22 blank | 14 comment | 2 complexity | e307ee4ff8c8364415279615801da68c MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-present 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 Xunit;
  21. namespace MongoDB.Driver.Core.Misc
  22. {
  23. public class EndPointHelperTests
  24. {
  25. [Theory]
  26. [InlineData("localhost:27017", "localhost:27017", true)]
  27. [InlineData("localhost:27017", "localhost:27018", false)]
  28. [InlineData("localhost:27018", "localhost:27017", false)]
  29. [InlineData("127.0.0.1:27017", "localhost:27017", false)]
  30. [InlineData("localhost:27017", "127.0.0.1:27017", false)]
  31. [InlineData("127.0.0.1:27017", "127.0.0.1:27017", true)]
  32. [InlineData("127.0.0.1:27017", "127.0.0.1:27018", false)]
  33. [InlineData("127.0.0.1:27018", "127.0.0.1:27017", false)]
  34. [InlineData(null, "localhost:27017", false)]
  35. [InlineData("localhost:27017", null, false)]
  36. [InlineData(null, null, true)]
  37. public void Equals_should_return_true_when_endpoints_are_equal(string a, string b, bool expectedResult)
  38. {
  39. var endPoint1 = a == null ? null : EndPointHelper.Parse(a);
  40. var endPoint2 = b == null ? null : EndPointHelper.Parse(b);
  41. var result = EndPointHelper.Equals(endPoint1, endPoint2);
  42. result.Should().Be(expectedResult);
  43. }
  44. [Fact]
  45. public void Parse_should_throw_an_ArgumentNullException_when_value_is_null()
  46. {
  47. Action act = () => EndPointHelper.Parse(null);
  48. act.ShouldThrow<ArgumentNullException>();
  49. }
  50. [Theory]
  51. [InlineData("gob:2::212")]
  52. [InlineData("localhost:-1")]
  53. [InlineData("localhost:66000")]
  54. [InlineData(":21")]
  55. public void Parse_should_throw_an_ArgumentException_when_value_is_not_a_valid_end_point(string value)
  56. {
  57. Action act = () => EndPointHelper.Parse(value);
  58. act.ShouldThrow<ArgumentException>();
  59. }
  60. [Fact]
  61. public void ToString_should_return_expected_result_when_value_is_a_DnsEndPoint()
  62. {
  63. var endPoint = new DnsEndPoint("localhost", 27017);
  64. var result = EndPointHelper.ToString(endPoint);
  65. result.Should().Be("localhost:27017");
  66. }
  67. [Fact]
  68. public void ToString_should_return_expected_result_when_value_is_an_IPEndPoint()
  69. {
  70. var endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 27017);
  71. var result = EndPointHelper.ToString(endPoint);
  72. result.Should().Be("127.0.0.1:27017");
  73. }
  74. [Theory]
  75. [InlineData("gob:2::212")]
  76. [InlineData("localhost:-1")]
  77. [InlineData("localhost:66000")]
  78. [InlineData(":21")]
  79. public void TryParse_should_return_false_when_the_end_point_is_invalid(string value)
  80. {
  81. EndPoint result;
  82. var success = EndPointHelper.TryParse(value, out result);
  83. success.Should().BeFalse();
  84. }
  85. [Theory]
  86. [InlineData("localhost", "localhost", 27017)]
  87. [InlineData("localhost:28017", "localhost", 28017)]
  88. [InlineData("act.test.com", "act.test.com", 27017)]
  89. [InlineData("act.test.com:28017", "act.test.com", 28017)]
  90. [InlineData("123.test.com", "123.test.com", 27017)]
  91. [InlineData("123.test.com:28017", "123.test.com", 28017)]
  92. public void TryParse_should_parse_a_hostname(string value, string expectedHost, int expectedPort)
  93. {
  94. EndPoint result;
  95. var success = EndPointHelper.TryParse(value, out result);
  96. success.Should().BeTrue();
  97. result.Should().Be(new DnsEndPoint(expectedHost, expectedPort));
  98. result.AddressFamily.Should().Be(AddressFamily.Unspecified);
  99. }
  100. [Theory]
  101. [InlineData("127.0.0.1", "127.0.0.1", 27017)]
  102. [InlineData("127.0.0.1:28017", "127.0.0.1", 28017)]
  103. public void TryParse_should_parse_an_ipv4_address(string value, string expectedAddress, int expectedPort)
  104. {
  105. EndPoint result;
  106. var success = EndPointHelper.TryParse(value, out result);
  107. success.Should().BeTrue();
  108. result.Should().Be(new IPEndPoint(IPAddress.Parse(expectedAddress), expectedPort));
  109. result.AddressFamily.Should().Be(AddressFamily.InterNetwork);
  110. }
  111. [Theory]
  112. [InlineData("[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", "[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", 27017)]
  113. [InlineData("[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]:28017", "[FE80:0000:0000:0000:0202:B3FF:FE1E:8329]", 28017)]
  114. public void TryParse_should_parse_an_ipv6_address(string value, string expectedAddress, int expectedPort)
  115. {
  116. EndPoint result;
  117. var success = EndPointHelper.TryParse(value, out result);
  118. success.Should().BeTrue();
  119. result.Should().Be(new IPEndPoint(IPAddress.Parse(expectedAddress), expectedPort));
  120. result.AddressFamily.Should().Be(AddressFamily.InterNetworkV6);
  121. }
  122. }
  123. }