From 7312a02e957ddd8e0a09089ef9ca87cb4489e57b Mon Sep 17 00:00:00 2001 From: Matias Quaranta Date: Thu, 18 Jun 2020 12:30:23 -0700 Subject: [PATCH 1/2] Adding checks --- Microsoft.Azure.Cosmos/src/CosmosClient.cs | 8 ++++---- Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/CosmosClient.cs b/Microsoft.Azure.Cosmos/src/CosmosClient.cs index 2c87f5a78d..f83c66f126 100644 --- a/Microsoft.Azure.Cosmos/src/CosmosClient.cs +++ b/Microsoft.Azure.Cosmos/src/CosmosClient.cs @@ -194,12 +194,12 @@ public CosmosClient( string authKeyOrResourceToken, CosmosClientOptions clientOptions = null) { - if (accountEndpoint == null) + if (string.IsNullOrEmpty(accountEndpoint)) { throw new ArgumentNullException(nameof(accountEndpoint)); } - if (authKeyOrResourceToken == null) + if (string.IsNullOrEmpty(authKeyOrResourceToken)) { throw new ArgumentNullException(nameof(authKeyOrResourceToken)); } @@ -222,12 +222,12 @@ internal CosmosClient( CosmosClientOptions cosmosClientOptions, DocumentClient documentClient) { - if (accountEndpoint == null) + if (string.IsNullOrEmpty(accountEndpoint)) { throw new ArgumentNullException(nameof(accountEndpoint)); } - if (authKeyOrResourceToken == null) + if (string.IsNullOrEmpty(authKeyOrResourceToken)) { throw new ArgumentNullException(nameof(authKeyOrResourceToken)); } diff --git a/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs b/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs index 517d0a002f..90bf23739d 100644 --- a/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs +++ b/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs @@ -54,12 +54,12 @@ public CosmosClientBuilder( string accountEndpoint, string authKeyOrResourceToken) { - if (accountEndpoint == null) + if (string.IsNullOrEmpty(accountEndpoint)) { throw new ArgumentNullException(nameof(CosmosClientBuilder.accountEndpoint)); } - if (authKeyOrResourceToken == null) + if (string.IsNullOrEmpty(authKeyOrResourceToken)) { throw new ArgumentNullException(nameof(authKeyOrResourceToken)); } From 72fb339a44edd28b85f86feb478f998f711ea2c3 Mon Sep 17 00:00:00 2001 From: Matias Quaranta Date: Thu, 18 Jun 2020 12:30:28 -0700 Subject: [PATCH 2/2] tests --- .../CosmosClientTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientTests.cs index c1b5568370..10626f6565 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosClientTests.cs @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos.Tests using System; using System.Collections.Generic; using System.Threading.Tasks; + using Microsoft.Azure.Cosmos.Fluent; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -52,5 +53,41 @@ public async Task TestDispose() catch (ObjectDisposedException) { } } } + + [DataTestMethod] + [DataRow(null, "425Mcv8CXQqzRNCgFNjIhT424GK99CKJvASowTnq15Vt8LeahXTcN5wt3342vQ==")] + [DataRow(AccountEndpoint, null)] + [DataRow("", "425Mcv8CXQqzRNCgFNjIhT424GK99CKJvASowTnq15Vt8LeahXTcN5wt3342vQ==")] + [DataRow(AccountEndpoint, "")] + [ExpectedException(typeof(ArgumentNullException))] + public void InvalidEndpointAndKey(string endpoint, string key) + { + new CosmosClient(endpoint, key); + } + + [DataTestMethod] + [DataRow(null, "425Mcv8CXQqzRNCgFNjIhT424GK99CKJvASowTnq15Vt8LeahXTcN5wt3342vQ==")] + [DataRow(AccountEndpoint, null)] + [DataRow("", "425Mcv8CXQqzRNCgFNjIhT424GK99CKJvASowTnq15Vt8LeahXTcN5wt3342vQ==")] + [DataRow(AccountEndpoint, "")] + [ExpectedException(typeof(ArgumentNullException))] + public void Builder_InvalidEndpointAndKey(string endpoint, string key) + { + new CosmosClientBuilder(endpoint, key); + } + + [TestMethod] + public void InvalidConnectionString() + { + Assert.ThrowsException(() => new CosmosClient("")); + Assert.ThrowsException(() => new CosmosClient(null)); + } + + [TestMethod] + public void Builder_InvalidConnectionString() + { + Assert.ThrowsException(() => new CosmosClientBuilder("")); + Assert.ThrowsException(() => new CosmosClientBuilder(null)); + } } }