PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/MongoDB.Driver.Tests/ClusterKeyTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 381 lines | 344 code | 23 blank | 14 comment | 8 complexity | e77b756ab746bdfd404547a71c267993 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-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.Collections.Generic;
  17. using System.Linq;
  18. using System.Security.Authentication;
  19. using FluentAssertions;
  20. using MongoDB.Bson;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using MongoDB.Driver.Core.Compression;
  23. using MongoDB.Driver.Core.Configuration;
  24. using Xunit;
  25. namespace MongoDB.Driver.Tests
  26. {
  27. public class ClusterKeyTests
  28. {
  29. private const string Key1 = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk";
  30. private const string Key2 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  31. [Fact]
  32. public void Equals_should_return_true_if_all_fields_are_equal()
  33. {
  34. var subject1 = CreateSubject();
  35. var subject2 = CreateSubject();
  36. subject1.Should().NotBeSameAs(subject2);
  37. subject1.Equals(subject2).Should().BeTrue();
  38. subject1.GetHashCode().Should().Be(subject2.GetHashCode());
  39. }
  40. [Theory]
  41. [InlineData("AllowInsecureTls", true)]
  42. [InlineData("ApplicationName", true)]
  43. [InlineData("ClusterConfigurator", true)]
  44. [InlineData("Compressors", true)]
  45. [InlineData("ConnectionMode", true)]
  46. [InlineData("ConnectTimeout", true)]
  47. [InlineData("Credentials", false)]
  48. [InlineData("HeartbeatInterval", true)]
  49. [InlineData("HeartbeatTimeout", true)]
  50. [InlineData("IPv6", true)]
  51. [InlineData("KmsProviders", true)]
  52. [InlineData("MaxConnectionIdleTime", true)]
  53. [InlineData("MaxConnectionLifeTime", true)]
  54. [InlineData("MaxConnectionPoolSize", true)]
  55. [InlineData("MinConnectionPoolSize", true)]
  56. [InlineData("ReceiveBufferSize", true)]
  57. [InlineData("ReplicaSetName", true)]
  58. [InlineData("LocalThreshold", true)]
  59. [InlineData("SchemaMap", true)]
  60. [InlineData("Scheme", true)]
  61. [InlineData("SdamLogFileName", true)]
  62. [InlineData("SendBufferSize", true)]
  63. [InlineData("Servers", false)]
  64. [InlineData("ServerSelectionTimeout", true)]
  65. [InlineData("SocketTimeout", true)]
  66. [InlineData("SslSettings", true)]
  67. [InlineData("UseTls", true)]
  68. [InlineData("WaitQueueSize", true)]
  69. [InlineData("WaitQueueTimeout", true)]
  70. public void Equals_should_return_false_if_any_field_is_not_equal(string notEqualFieldName, bool expectEqualHashCode)
  71. {
  72. var subject1 = CreateSubject();
  73. var subject2 = CreateSubject(notEqualFieldName);
  74. subject1.Should().NotBeSameAs(subject2);
  75. subject1.Equals(subject2).Should().BeFalse();
  76. subject1.GetHashCode().Equals(subject2.GetHashCode()).Should().Be(expectEqualHashCode);
  77. }
  78. [Theory]
  79. [InlineData(true, true)]
  80. [InlineData(true, false)]
  81. public void Equals_should_return_true_if_kms_providers_have_different_records_count(
  82. bool skipTheLastMainRecord,
  83. bool skipTheLastNestedRecord)
  84. {
  85. var kmsProvider1 = GetKmsProviders();
  86. var kmsProvider2 = GetKmsProviders(skipTheLastMainRecord: skipTheLastMainRecord, skipTheLastNestedRecord: skipTheLastNestedRecord);
  87. var subject1 = CreateSubjectWith(kmsProvidersValue: kmsProvider1);
  88. var subject2 = CreateSubjectWith(kmsProvidersValue: kmsProvider2);
  89. subject1.Should().NotBe(subject2);
  90. }
  91. [Theory]
  92. [InlineData(true, true)]
  93. [InlineData(true, false)]
  94. [InlineData(false, false)]
  95. public void Equals_should_return_true_if_kms_providers_have_the_same_items_but_with_different_order(
  96. bool withReverseInMainKeys,
  97. bool withReverseInNestedKeys)
  98. {
  99. var kmsProviders1 = GetKmsProviders();
  100. var kmsProviders2 = GetKmsProviders(withReverseInMainKeys: withReverseInMainKeys, withReverseInNestedKeys: withReverseInNestedKeys);
  101. var subject1 = CreateSubjectWith(kmsProvidersValue: kmsProviders1);
  102. var subject2 = CreateSubjectWith(kmsProvidersValue: kmsProviders2);
  103. subject1.Should().Be(subject2);
  104. }
  105. [Theory]
  106. [ParameterAttributeData]
  107. public void Equals_should_return_true_if_schema_maps_have_the_same_items_but_with_different_order(
  108. [Values(false, true)] bool withReverse)
  109. {
  110. var schemaMap1 = GetSchemaMaps();
  111. var schemaMap2 = GetSchemaMaps(withReverse: withReverse);
  112. var subject1 = CreateSubjectWith(schemaMapValue: schemaMap1);
  113. var subject2 = CreateSubjectWith(schemaMapValue: schemaMap2);
  114. subject1.Should().Be(subject2);
  115. }
  116. private ClusterKey CreateSubject(string notEqualFieldName = null)
  117. {
  118. var allowInsecureTls = true;
  119. var applicationName = "app1";
  120. var clusterConfigurator = new Action<ClusterBuilder>(b => { });
  121. var compressors = new CompressorConfiguration[0];
  122. var connectionMode = ConnectionMode.Direct;
  123. var connectTimeout = TimeSpan.FromSeconds(1);
  124. #pragma warning disable 618
  125. var credentials = new List<MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
  126. #pragma warning restore 618
  127. var heartbeatInterval = TimeSpan.FromSeconds(7);
  128. var heartbeatTimeout = TimeSpan.FromSeconds(8);
  129. var ipv6 = false;
  130. var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>();
  131. var localThreshold = TimeSpan.FromMilliseconds(20);
  132. var maxConnectionIdleTime = TimeSpan.FromSeconds(2);
  133. var maxConnectionLifeTime = TimeSpan.FromSeconds(3);
  134. var maxConnectionPoolSize = 50;
  135. var minConnectionPoolSize = 5;
  136. var receiveBufferSize = 1;
  137. var replicaSetName = "abc";
  138. var schemaMap = new Dictionary<string, BsonDocument>();
  139. var scheme = ConnectionStringScheme.MongoDB;
  140. var sdamLogFileName = "stdout";
  141. var sendBufferSize = 1;
  142. var servers = new[] { new MongoServerAddress("localhost") };
  143. var serverSelectionTimeout = TimeSpan.FromSeconds(6);
  144. var socketTimeout = TimeSpan.FromSeconds(4);
  145. var sslSettings = new SslSettings
  146. {
  147. CheckCertificateRevocation = true,
  148. EnabledSslProtocols = SslProtocols.Tls
  149. };
  150. var useTls = false;
  151. var waitQueueSize = 20;
  152. var waitQueueTimeout = TimeSpan.FromSeconds(5);
  153. if (notEqualFieldName != null)
  154. {
  155. switch (notEqualFieldName)
  156. {
  157. case "AllowInsecureTls": allowInsecureTls = !allowInsecureTls; break;
  158. case "ApplicationName": applicationName = "app2"; break;
  159. case "ClusterConfigurator": clusterConfigurator = new Action<ClusterBuilder>(b => { }); break;
  160. case "Compressors": compressors = new[] { new CompressorConfiguration(CompressorType.Zlib) }; break;
  161. case "ConnectionMode": connectionMode = ConnectionMode.ReplicaSet; break;
  162. case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break;
  163. #pragma warning disable 618
  164. case "Credentials": credentials = new List<MongoCredential> { MongoCredential.CreateMongoCRCredential("different", "different", "different") }; break;
  165. #pragma warning restore 618
  166. case "HeartbeatInterval": heartbeatInterval = TimeSpan.FromSeconds(99); break;
  167. case "HeartbeatTimeout": heartbeatTimeout = TimeSpan.FromSeconds(99); break;
  168. case "IPv6": ipv6 = !ipv6; break;
  169. case "KmsProviders": kmsProviders.Add("local", new Dictionary<string, object>() { { "key", "test" } }); break;
  170. case "LocalThreshold": localThreshold = TimeSpan.FromMilliseconds(99); break;
  171. case "MaxConnectionIdleTime": maxConnectionIdleTime = TimeSpan.FromSeconds(99); break;
  172. case "MaxConnectionLifeTime": maxConnectionLifeTime = TimeSpan.FromSeconds(99); break;
  173. case "MaxConnectionPoolSize": maxConnectionPoolSize = 99; break;
  174. case "MinConnectionPoolSize": minConnectionPoolSize = 99; break;
  175. case "ReceiveBufferSize": receiveBufferSize = 2; break;
  176. case "ReplicaSetName": replicaSetName = "different"; break;
  177. case "SchemaMap": schemaMap.Add("db.coll", new BsonDocument()); break;
  178. case "Scheme": scheme = ConnectionStringScheme.MongoDBPlusSrv; break;
  179. case "SdamLogFileName": sdamLogFileName = "different"; break;
  180. case "SendBufferSize": sendBufferSize = 2; break;
  181. case "Servers": servers = new[] { new MongoServerAddress("different") }; break;
  182. case "ServerSelectionTimeout": serverSelectionTimeout = TimeSpan.FromSeconds(98); break;
  183. case "SocketTimeout": socketTimeout = TimeSpan.FromSeconds(99); break;
  184. case "SslSettings": sslSettings.CheckCertificateRevocation = !sslSettings.CheckCertificateRevocation; break;
  185. case "UseTls": useTls = !useTls; break;
  186. case "WaitQueueSize": waitQueueSize = 99; break;
  187. case "WaitQueueTimeout": waitQueueTimeout = TimeSpan.FromSeconds(99); break;
  188. default: throw new ArgumentException($"Invalid field name: \"{notEqualFieldName}\".", nameof(notEqualFieldName));
  189. }
  190. }
  191. return new ClusterKey(
  192. allowInsecureTls,
  193. applicationName,
  194. clusterConfigurator,
  195. compressors,
  196. connectionMode,
  197. connectTimeout,
  198. credentials,
  199. heartbeatInterval,
  200. heartbeatTimeout,
  201. ipv6,
  202. kmsProviders,
  203. localThreshold,
  204. maxConnectionIdleTime,
  205. maxConnectionLifeTime,
  206. maxConnectionPoolSize,
  207. minConnectionPoolSize,
  208. receiveBufferSize,
  209. replicaSetName,
  210. schemaMap,
  211. scheme,
  212. sdamLogFileName,
  213. sendBufferSize,
  214. servers,
  215. serverSelectionTimeout,
  216. socketTimeout,
  217. sslSettings,
  218. useTls,
  219. waitQueueSize,
  220. waitQueueTimeout);
  221. }
  222. internal ClusterKey CreateSubjectWith(
  223. Dictionary<string, IReadOnlyDictionary<string, object>> kmsProvidersValue = null,
  224. Dictionary<string, BsonDocument> schemaMapValue = null)
  225. {
  226. var allowInsecureTls = true;
  227. var applicationName = "app1";
  228. var clusterConfigurator = new Action<ClusterBuilder>(b => { });
  229. var compressors = new CompressorConfiguration[0];
  230. var connectionMode = ConnectionMode.Direct;
  231. var connectTimeout = TimeSpan.FromSeconds(1);
  232. #pragma warning disable 618
  233. var credentials = new List<MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
  234. #pragma warning restore 618
  235. var heartbeatInterval = TimeSpan.FromSeconds(7);
  236. var heartbeatTimeout = TimeSpan.FromSeconds(8);
  237. var ipv6 = false;
  238. var kmsProviders = kmsProvidersValue ?? new Dictionary<string, IReadOnlyDictionary<string, object>>();
  239. var localThreshold = TimeSpan.FromMilliseconds(20);
  240. var maxConnectionIdleTime = TimeSpan.FromSeconds(2);
  241. var maxConnectionLifeTime = TimeSpan.FromSeconds(3);
  242. var maxConnectionPoolSize = 50;
  243. var minConnectionPoolSize = 5;
  244. var receiveBufferSize = 1;
  245. var replicaSetName = "abc";
  246. var schemaMap = schemaMapValue ?? new Dictionary<string, BsonDocument>();
  247. var scheme = ConnectionStringScheme.MongoDB;
  248. var sdamLogFileName = "stdout";
  249. var sendBufferSize = 1;
  250. var servers = new[] { new MongoServerAddress("localhost") };
  251. var serverSelectionTimeout = TimeSpan.FromSeconds(6);
  252. var socketTimeout = TimeSpan.FromSeconds(4);
  253. var sslSettings = new SslSettings
  254. {
  255. CheckCertificateRevocation = true,
  256. EnabledSslProtocols = SslProtocols.Tls
  257. };
  258. var useTls = false;
  259. var waitQueueSize = 20;
  260. var waitQueueTimeout = TimeSpan.FromSeconds(5);
  261. return new ClusterKey(
  262. allowInsecureTls,
  263. applicationName,
  264. clusterConfigurator,
  265. compressors,
  266. connectionMode,
  267. connectTimeout,
  268. credentials,
  269. heartbeatInterval,
  270. heartbeatTimeout,
  271. ipv6,
  272. kmsProviders,
  273. localThreshold,
  274. maxConnectionIdleTime,
  275. maxConnectionLifeTime,
  276. maxConnectionPoolSize,
  277. minConnectionPoolSize,
  278. receiveBufferSize,
  279. replicaSetName,
  280. schemaMap,
  281. scheme,
  282. sdamLogFileName,
  283. sendBufferSize,
  284. servers,
  285. serverSelectionTimeout,
  286. socketTimeout,
  287. sslSettings,
  288. useTls,
  289. waitQueueSize,
  290. waitQueueTimeout);
  291. }
  292. private Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviders(
  293. bool withReverseInMainKeys = false,
  294. bool withReverseInNestedKeys = false,
  295. bool skipTheLastMainRecord = false,
  296. bool skipTheLastNestedRecord = false)
  297. {
  298. var options1 = new Dictionary<string, object>
  299. {
  300. { "key1", new BsonBinaryData(Convert.FromBase64String(Key1)).Bytes },
  301. };
  302. if (!skipTheLastNestedRecord)
  303. {
  304. options1.Add("key2", new BsonBinaryData(Convert.FromBase64String(Key2)).Bytes);
  305. }
  306. var options2 = new Dictionary<string, object>
  307. {
  308. { "key1", "value1" },
  309. { "key2", "value2" },
  310. { "key3", "value3" }
  311. };
  312. Dictionary<string, IReadOnlyDictionary<string, object>> kmsProviders = null;
  313. if (withReverseInNestedKeys)
  314. {
  315. kmsProviders =
  316. new Dictionary<string, IReadOnlyDictionary<string, object>>()
  317. {
  318. { "options1", options1.Reverse().ToDictionary(k => k.Key, v => v.Value) },
  319. };
  320. if (!skipTheLastMainRecord)
  321. {
  322. kmsProviders.Add("options2", options2.Reverse().ToDictionary(k => k.Key, v => v.Value));
  323. }
  324. }
  325. else
  326. {
  327. kmsProviders =
  328. new Dictionary<string, IReadOnlyDictionary<string, object>>()
  329. {
  330. { "options1", options1 },
  331. };
  332. if (!skipTheLastMainRecord)
  333. {
  334. kmsProviders.Add("options2", options2);
  335. }
  336. }
  337. return withReverseInMainKeys
  338. ? kmsProviders.Reverse().ToDictionary(k => k.Key, v => v.Value)
  339. : kmsProviders;
  340. }
  341. private Dictionary<string, BsonDocument> GetSchemaMaps(
  342. bool withReverse = false,
  343. bool skipLastRecord = false)
  344. {
  345. var options = new Dictionary<string, BsonDocument>
  346. {
  347. { "key1", new BsonDocument("a", 1) }
  348. };
  349. if (!skipLastRecord)
  350. {
  351. options.Add("key2", new BsonDocument("b", 2));
  352. }
  353. return withReverse
  354. ? options.Reverse().ToDictionary(k => k.Key, v => v.Value)
  355. : options;
  356. }
  357. }
  358. }