Skip to content

Commit

Permalink
CosmosClient: Add check for empty keys (#1639)
Browse files Browse the repository at this point in the history
* Adding checks

* tests
  • Loading branch information
ealsur authored Jun 18, 2020
1 parent c5a5b88 commit 5ce8426
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<ArgumentException>(() => new CosmosClient(""));
Assert.ThrowsException<ArgumentNullException>(() => new CosmosClient(null));
}

[TestMethod]
public void Builder_InvalidConnectionString()
{
Assert.ThrowsException<ArgumentException>(() => new CosmosClientBuilder(""));
Assert.ThrowsException<ArgumentNullException>(() => new CosmosClientBuilder(null));
}
}
}

0 comments on commit 5ce8426

Please sign in to comment.