Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libraries/Microsoft.Bot.Builder.Azure/CosmosDBKeyEscape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Microsoft.Bot.Builder.Azure
{
public static class CosmosDBKeyEscape
public static class CosmosDbKeyEscape
{
// The list of illegal characters for CosmosDB Keys comes from this list on
// The list of illegal characters for Cosmos DB Keys comes from this list on
// the CosmostDB docs: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.resource.id?view=azure-dotnet#remarks
// Note: We are also escapting the "*" character, as that what we're using
// as our escape character.
Expand All @@ -22,7 +22,7 @@ public static class CosmosDBKeyEscape
new Dictionary<char, string>(_illegalKeys.ToDictionary(c => c, c => '*' + ((int)c).ToString("x2")));

/// <summary>
/// Converts the key into a DocumentID that can be used safely with CosmosDB.
/// Converts the key into a DocumentID that can be used safely with Cosmos DB.
/// The following characters are restricted and cannot be used in the Id property: '/', '\', '?', '#'
/// More information at https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.resource.id?view=azure-dotnet#remarks.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions libraries/Microsoft.Bot.Builder.Azure/CosmosDbStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public CosmosDbStorage(IDocumentClient documentClient, CosmosDbCustomClientOptio
}

[Obsolete("Replaced by CosmosDBKeyEscape.EscapeKey.")]
public static string SanitizeKey(string key) => CosmosDBKeyEscape.EscapeKey(key);
public static string SanitizeKey(string key) => CosmosDbKeyEscape.EscapeKey(key);

/// <summary>
/// Deletes storage items from storage.
Expand All @@ -140,7 +140,7 @@ public async Task DeleteAsync(string[] keys, CancellationToken cancellationToken
// Parallelize deletion
var tasks = keys.Select(key =>
_client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri(_databaseId, _collectionId, CosmosDBKeyEscape.EscapeKey(key)),
UriFactory.CreateDocumentUri(_databaseId, _collectionId, CosmosDbKeyEscape.EscapeKey(key)),
cancellationToken: cancellationToken));

// await to deletion tasks to complete
Expand Down Expand Up @@ -172,7 +172,7 @@ public async Task<IDictionary<string, object>> ReadAsync(string[] keys, Cancella
var storeItems = new Dictionary<string, object>(keys.Length);

var parameterSequence = string.Join(",", Enumerable.Range(0, keys.Length).Select(i => $"@id{i}"));
var parameterValues = keys.Select((key, ix) => new SqlParameter($"@id{ix}", CosmosDBKeyEscape.EscapeKey(key)));
var parameterValues = keys.Select((key, ix) => new SqlParameter($"@id{ix}", CosmosDbKeyEscape.EscapeKey(key)));
var querySpec = new SqlQuerySpec
{
QueryText = $"SELECT c.id, c.realId, c.document, c._etag FROM c WHERE c.id in ({parameterSequence})",
Expand Down Expand Up @@ -227,7 +227,7 @@ public async Task WriteAsync(IDictionary<string, object> changes, CancellationTo

var documentChange = new DocumentStoreItem
{
Id = CosmosDBKeyEscape.EscapeKey(change.Key),
Id = CosmosDbKeyEscape.EscapeKey(change.Key),
ReadlId = change.Key,
Document = json,
};
Expand Down
24 changes: 12 additions & 12 deletions tests/Microsoft.Bot.Builder.Azure.Tests/CosmosDBKeyEscapeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,48 @@ public class CosmosDBKeyEscapeTests
public void Sanitize_Key_Should_Fail_With_Null_Key()
{
// Null key should throw
Assert.ThrowsException<ArgumentNullException>(() => CosmosDBKeyEscape.EscapeKey(null));
Assert.ThrowsException<ArgumentNullException>(() => CosmosDbKeyEscape.EscapeKey(null));

// Empty string should throw
Assert.ThrowsException<ArgumentNullException>(() => CosmosDBKeyEscape.EscapeKey(string.Empty));
Assert.ThrowsException<ArgumentNullException>(() => CosmosDbKeyEscape.EscapeKey(string.Empty));

// Whitespace key should throw
Assert.ThrowsException<ArgumentNullException>(() => CosmosDBKeyEscape.EscapeKey(" "));
Assert.ThrowsException<ArgumentNullException>(() => CosmosDbKeyEscape.EscapeKey(" "));
}

[TestMethod]
public void Sanitize_Key_Should_Not_Change_A_Valid_Key()
{
var validKey = "Abc12345";
var sanitizedKey = CosmosDBKeyEscape.EscapeKey(validKey);
var sanitizedKey = CosmosDbKeyEscape.EscapeKey(validKey);
Assert.AreEqual(validKey, sanitizedKey);
}

[TestMethod]
public void Sanitize_Key_Should_Escape_Illegal_Character()
{
// Ascii code of "?" is "3f".
var sanitizedKey = CosmosDBKeyEscape.EscapeKey("?test?");
var sanitizedKey = CosmosDbKeyEscape.EscapeKey("?test?");
Assert.AreEqual(sanitizedKey, "*3ftest*3f");

// Ascii code of "/" is "2f".
var sanitizedKey2 = CosmosDBKeyEscape.EscapeKey("/test/");
var sanitizedKey2 = CosmosDbKeyEscape.EscapeKey("/test/");
Assert.AreEqual(sanitizedKey2, "*2ftest*2f");

// Ascii code of "\" is "5c".
var sanitizedKey3 = CosmosDBKeyEscape.EscapeKey("\\test\\");
var sanitizedKey3 = CosmosDbKeyEscape.EscapeKey("\\test\\");
Assert.AreEqual(sanitizedKey3, "*5ctest*5c");

// Ascii code of "#" is "23".
var sanitizedKey4 = CosmosDBKeyEscape.EscapeKey("#test#");
var sanitizedKey4 = CosmosDbKeyEscape.EscapeKey("#test#");
Assert.AreEqual(sanitizedKey4, "*23test*23");

// Ascii code of "*" is "2a".
var sanitizedKey5 = CosmosDBKeyEscape.EscapeKey("*test*");
var sanitizedKey5 = CosmosDbKeyEscape.EscapeKey("*test*");
Assert.AreEqual(sanitizedKey5, "*2atest*2a");

// Check a compound key
var compoundSanitizedKey = CosmosDBKeyEscape.EscapeKey("?#/");
var compoundSanitizedKey = CosmosDbKeyEscape.EscapeKey("?#/");
Assert.AreEqual(compoundSanitizedKey, "*3f*23*2f");
}

Expand All @@ -71,8 +71,8 @@ public void Collisions_Should_Not_Happen()
// we makes sure to escape the *.

// Ascii code of "*" is "2a".
var escaped1 = CosmosDBKeyEscape.EscapeKey(validKey);
var escaped2 = CosmosDBKeyEscape.EscapeKey(validKey2);
var escaped1 = CosmosDbKeyEscape.EscapeKey(validKey);
var escaped2 = CosmosDbKeyEscape.EscapeKey(validKey2);

Assert.AreNotEqual(escaped1, escaped2);
}
Expand Down