diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptions.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptions.cs index af8ab293e9..525a8182c3 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptions.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionOptions.cs @@ -17,12 +17,6 @@ public enum JsonProcessor Newtonsoft, #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - /// - /// System.Text.Json - /// - /// Available with .NET8.0 package only. - SystemTextJson, - /// /// Ut8JsonReader/Writer /// diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs index 8612eb3958..7bf05fb930 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/EncryptionProcessor.cs @@ -10,10 +10,6 @@ namespace Microsoft.Azure.Cosmos.Encryption.Custom using System.IO; using System.Linq; using System.Text; -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - using System.Text.Json; - using System.Text.Json.Nodes; -#endif using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation; @@ -33,7 +29,6 @@ internal static class EncryptionProcessor internal static readonly CosmosJsonDotNetSerializer BaseSerializer = new (JsonSerializerSettings); #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - private static readonly JsonWriterOptions JsonWriterOptions = new () { SkipValidation = true }; private static readonly StreamProcessor StreamProcessor = new (); #endif @@ -158,7 +153,6 @@ public static async Task EncryptAsync( { JsonProcessor.Newtonsoft => await DecryptAsync(input, encryptor, diagnosticsContext, cancellationToken), #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - JsonProcessor.SystemTextJson => await DecryptJsonNodeAsync(input, encryptor, diagnosticsContext, cancellationToken), JsonProcessor.Stream => await DecryptStreamAsync(input, encryptor, diagnosticsContext, cancellationToken), #endif _ => throw new InvalidOperationException("Unsupported Json Processor") @@ -229,43 +223,6 @@ public static async Task DecryptAsync( } #endif -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - public static async Task<(Stream, DecryptionContext)> DecryptJsonNodeAsync( - Stream input, - Encryptor encryptor, - CosmosDiagnosticsContext diagnosticsContext, - CancellationToken cancellationToken) - { - if (input == null) - { - return (input, null); - } - - Debug.Assert(input.CanSeek); - Debug.Assert(encryptor != null); - Debug.Assert(diagnosticsContext != null); - - JsonNode document = await JsonNode.ParseAsync(input, cancellationToken: cancellationToken); - - (JsonNode decryptedDocument, DecryptionContext context) = await DecryptAsync(document, encryptor, diagnosticsContext, cancellationToken); - if (context == null) - { - input.Position = 0; - return (input, null); - } - - await input.DisposeAsync(); - - MemoryStream ms = new (); - Utf8JsonWriter writer = new (ms, EncryptionProcessor.JsonWriterOptions); - - System.Text.Json.JsonSerializer.Serialize(writer, decryptedDocument); - - ms.Position = 0; - return (ms, context); - } -#endif - #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER public static async Task<(Stream, DecryptionContext)> DecryptStreamAsync( Stream input, @@ -327,53 +284,6 @@ public static async Task DecryptAsync( return (document, decryptionContext); } -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - public static async Task<(JsonNode, DecryptionContext)> DecryptAsync( - JsonNode document, - Encryptor encryptor, - CosmosDiagnosticsContext diagnosticsContext, - CancellationToken cancellationToken) - { - Debug.Assert(document != null); - - Debug.Assert(encryptor != null); - - if (!document.AsObject().TryGetPropertyValue(Constants.EncryptedInfo, out JsonNode encryptionPropertiesNode)) - { - return (document, null); - } - - EncryptionProperties encryptionProperties; - try - { - encryptionProperties = System.Text.Json.JsonSerializer.Deserialize(encryptionPropertiesNode); - } - catch (Exception) - { - return (document, null); - } - - DecryptionContext decryptionContext = await DecryptInternalAsync(encryptor, diagnosticsContext, document, encryptionProperties, cancellationToken); - - return (document, decryptionContext); - } - - private static async Task DecryptInternalAsync(Encryptor encryptor, CosmosDiagnosticsContext diagnosticsContext, JsonNode itemNode, EncryptionProperties encryptionProperties, CancellationToken cancellationToken) - { - DecryptionContext decryptionContext = encryptionProperties.EncryptionAlgorithm switch - { - CosmosEncryptionAlgorithm.MdeAeadAes256CbcHmac256Randomized => await MdeEncryptionProcessor.DecryptObjectAsync( - itemNode, - encryptor, - encryptionProperties, - diagnosticsContext, - cancellationToken), - _ => throw new NotSupportedException($"Encryption Algorithm : {encryptionProperties.EncryptionAlgorithm} is not supported."), - }; - return decryptionContext; - } -#endif - private static async Task DecryptInternalAsync(Encryptor encryptor, CosmosDiagnosticsContext diagnosticsContext, JObject itemJObj, JObject encryptionPropertiesJObj, CancellationToken cancellationToken) { EncryptionProperties encryptionProperties = encryptionPropertiesJObj.ToObject(); diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JsonNodeSqlSerializer.Preview.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JsonNodeSqlSerializer.Preview.cs deleted file mode 100644 index c96b76a731..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/JsonNodeSqlSerializer.Preview.cs +++ /dev/null @@ -1,117 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// ------------------------------------------------------------ - -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER -namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation -{ - using System; - using System.Diagnostics; - using System.Text.Json; - using System.Text.Json.Nodes; - using Microsoft.Data.Encryption.Cryptography.Serializers; - - internal class JsonNodeSqlSerializer - { - private static readonly SqlBitSerializer SqlBoolSerializer = new (); - private static readonly SqlFloatSerializer SqlDoubleSerializer = new (); - private static readonly SqlBigIntSerializer SqlLongSerializer = new (); - - // UTF-8 encoding. - private static readonly SqlVarCharSerializer SqlVarCharSerializer = new (size: -1, codePageCharacterEncoding: 65001); - -#pragma warning disable SA1101 // Prefix local calls with this - false positive on SerializeFixed - internal virtual (TypeMarker typeMarker, byte[] serializedBytes, int serializedBytesCount) Serialize(JsonNode propertyValue, ArrayPoolManager arrayPoolManager) - { - byte[] buffer; - int length; - - if (propertyValue == null) - { - return (TypeMarker.Null, null, -1); - } - - switch (propertyValue.GetValueKind()) - { - case JsonValueKind.Undefined: - Debug.Assert(false, "Undefined value cannot be in the JSON"); - return (default, null, -1); - case JsonValueKind.Null: - Debug.Assert(false, "Null type should have been handled by caller"); - return (TypeMarker.Null, null, -1); - case JsonValueKind.True: - (buffer, length) = SerializeFixed(SqlBoolSerializer, true); - return (TypeMarker.Boolean, buffer, length); - case JsonValueKind.False: - (buffer, length) = SerializeFixed(SqlBoolSerializer, false); - return (TypeMarker.Boolean, buffer, length); - case JsonValueKind.Number: - if (long.TryParse(propertyValue.ToJsonString(), out long longValue)) - { - (buffer, length) = SerializeFixed(SqlLongSerializer, longValue); - return (TypeMarker.Long, buffer, length); - } - else if (double.TryParse(propertyValue.ToJsonString(), out double doubleValue)) - { - (buffer, length) = SerializeFixed(SqlDoubleSerializer, doubleValue); - return (TypeMarker.Double, buffer, length); - } - else - { - throw new InvalidOperationException("Unsupported Number type"); - } - - case JsonValueKind.String: - (buffer, length) = SerializeString(propertyValue.GetValue()); - return (TypeMarker.String, buffer, length); - case JsonValueKind.Array: - (buffer, length) = SerializeString(propertyValue.ToJsonString()); - return (TypeMarker.Array, buffer, length); - case JsonValueKind.Object: - (buffer, length) = SerializeString(propertyValue.ToJsonString()); - return (TypeMarker.Object, buffer, length); - default: - throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.GetValueKind()}"); - } - - (byte[], int) SerializeFixed(IFixedSizeSerializer serializer, T value) - { - byte[] buffer = arrayPoolManager.Rent(serializer.GetSerializedMaxByteCount()); - int length = serializer.Serialize(value, buffer); - return (buffer, length); - } - - (byte[], int) SerializeString(string value) - { - byte[] buffer = arrayPoolManager.Rent(SqlVarCharSerializer.GetSerializedMaxByteCount(value.Length)); - int length = SqlVarCharSerializer.Serialize(value, buffer); - return (buffer, length); - } - } - - internal virtual JsonNode Deserialize( - TypeMarker typeMarker, - ReadOnlySpan serializedBytes) - { - switch (typeMarker) - { - case TypeMarker.Boolean: - return JsonValue.Create(SqlBoolSerializer.Deserialize(serializedBytes)); - case TypeMarker.Double: - return JsonValue.Create(SqlDoubleSerializer.Deserialize(serializedBytes)); - case TypeMarker.Long: - return JsonValue.Create(SqlLongSerializer.Deserialize(serializedBytes)); - case TypeMarker.String: - return JsonValue.Create(SqlVarCharSerializer.Deserialize(serializedBytes)); - case TypeMarker.Array: - return JsonNode.Parse(serializedBytes); - case TypeMarker.Object: - return JsonNode.Parse(serializedBytes); - default: - Debug.Fail($"Unexpected type marker {typeMarker}"); - return null; - } - } - } -} -#endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs index 2dc3eb7e10..389a709207 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeEncryptionProcessor.Preview.cs @@ -20,8 +20,6 @@ internal class MdeEncryptionProcessor internal MdeJObjectEncryptionProcessor JObjectEncryptionProcessor { get; set; } = new MdeJObjectEncryptionProcessor(); #if NET8_0_OR_GREATER - internal MdeJsonNodeEncryptionProcessor JsonNodeEncryptionProcessor { get; set; } = new MdeJsonNodeEncryptionProcessor(); - internal StreamProcessor StreamProcessor { get; set; } = new StreamProcessor(); #endif @@ -36,9 +34,6 @@ public async Task EncryptAsync( { case JsonProcessor.Newtonsoft: return await this.JObjectEncryptionProcessor.EncryptAsync(input, encryptor, encryptionOptions, token); - - case JsonProcessor.SystemTextJson: - return await this.JsonNodeEncryptionProcessor.EncryptAsync(input, encryptor, encryptionOptions, token); case JsonProcessor.Stream: MemoryStream ms = new (); await this.StreamProcessor.EncryptStreamAsync(input, ms, encryptor, encryptionOptions, token); @@ -65,18 +60,6 @@ internal async Task DecryptObjectAsync( { return await this.JObjectEncryptionProcessor.DecryptObjectAsync(document, encryptor, encryptionProperties, diagnosticsContext, cancellationToken); } - -#if NET8_0_OR_GREATER - internal async Task DecryptObjectAsync( - JsonNode document, - Encryptor encryptor, - EncryptionProperties encryptionProperties, - CosmosDiagnosticsContext diagnosticsContext, - CancellationToken cancellationToken) - { - return await this.JsonNodeEncryptionProcessor.DecryptObjectAsync(document, encryptor, encryptionProperties, diagnosticsContext, cancellationToken); - } -#endif } } #endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeJsonNodeEncryptionProcessor.Preview.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeJsonNodeEncryptionProcessor.Preview.cs deleted file mode 100644 index b7fcda6b43..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/MdeJsonNodeEncryptionProcessor.Preview.cs +++ /dev/null @@ -1,224 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// ------------------------------------------------------------ - -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - -namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Text.Json; - using System.Text.Json.Nodes; - using System.Threading; - using System.Threading.Tasks; - using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.SystemTextJson; - - internal class MdeJsonNodeEncryptionProcessor - { - private readonly JsonWriterOptions jsonWriterOptions = new () { SkipValidation = true }; - - internal JsonNodeSqlSerializer Serializer { get; set; } = new JsonNodeSqlSerializer(); - - internal MdeEncryptor Encryptor { get; set; } = new MdeEncryptor(); - - internal JsonSerializerOptions JsonSerializerOptions { get; set; } - - public MdeJsonNodeEncryptionProcessor() - { - this.JsonSerializerOptions = new JsonSerializerOptions(); - this.JsonSerializerOptions.Converters.Add(new JsonBytesConverter()); - } - - public async Task EncryptAsync( - Stream input, - Encryptor encryptor, - EncryptionOptions encryptionOptions, - CancellationToken token) - { - JsonNode itemJObj = JsonNode.Parse(input); - - Stream result = await this.EncryptAsync(itemJObj, encryptor, encryptionOptions, token); - - await input.DisposeAsync(); - return result; - } - - public async Task EncryptAsync( - JsonNode document, - Encryptor encryptor, - EncryptionOptions encryptionOptions, - CancellationToken token) - { - List pathsEncrypted = new (); - TypeMarker typeMarker; - - using ArrayPoolManager arrayPoolManager = new (); - - JsonObject itemObj = document.AsObject(); - - DataEncryptionKey encryptionKey = await encryptor.GetEncryptionKeyAsync(encryptionOptions.DataEncryptionKeyId, encryptionOptions.EncryptionAlgorithm, token); - - bool compressionEnabled = encryptionOptions.CompressionOptions.Algorithm != CompressionOptions.CompressionAlgorithm.None; - -#if NET8_0_OR_GREATER - BrotliCompressor compressor = encryptionOptions.CompressionOptions.Algorithm == CompressionOptions.CompressionAlgorithm.Brotli - ? new BrotliCompressor(encryptionOptions.CompressionOptions.CompressionLevel) : null; -#endif - Dictionary compressedPaths = new (); - - foreach (string pathToEncrypt in encryptionOptions.PathsToEncrypt) - { -#if NET8_0_OR_GREATER - string propertyName = pathToEncrypt[1..]; -#else - string propertyName = pathToEncrypt.Substring(1); -#endif - if (!itemObj.TryGetPropertyValue(propertyName, out JsonNode propertyValue)) - { - continue; - } - - if (propertyValue == null || propertyValue.GetValueKind() == JsonValueKind.Null) - { - continue; - } - - byte[] processedBytes = null; - (typeMarker, processedBytes, int processedBytesLength) = this.Serializer.Serialize(propertyValue, arrayPoolManager); - - if (processedBytes == null) - { - continue; - } - -#if NET8_0_OR_GREATER - if (compressor != null && (processedBytesLength >= encryptionOptions.CompressionOptions.MinimalCompressedLength)) - { - byte[] compressedBytes = arrayPoolManager.Rent(BrotliCompressor.GetMaxCompressedSize(processedBytesLength)); - processedBytesLength = compressor.Compress(compressedPaths, pathToEncrypt, processedBytes, processedBytesLength, compressedBytes); - processedBytes = compressedBytes; - } -#endif - (byte[] encryptedBytes, int encryptedBytesCount) = this.Encryptor.Encrypt(encryptionKey, typeMarker, processedBytes, processedBytesLength, arrayPoolManager); - - itemObj[propertyName] = JsonValue.Create(new Memory(encryptedBytes, 0, encryptedBytesCount)); - pathsEncrypted.Add(pathToEncrypt); - } - -#if NET8_0_OR_GREATER - compressor?.Dispose(); -#endif - EncryptionProperties encryptionProperties = new ( - encryptionFormatVersion: compressionEnabled ? 4 : 3, - encryptionOptions.EncryptionAlgorithm, - encryptionOptions.DataEncryptionKeyId, - encryptedData: null, - pathsEncrypted, - encryptionOptions.CompressionOptions.Algorithm, - compressedPaths); - - JsonNode propertiesNode = JsonSerializer.SerializeToNode(encryptionProperties); - - itemObj.Add(Constants.EncryptedInfo, propertiesNode); - - MemoryStream ms = new (); - Utf8JsonWriter writer = new (ms, this.jsonWriterOptions); - - JsonSerializer.Serialize(writer, document); - - ms.Position = 0; - return ms; - } - - internal async Task DecryptObjectAsync( - JsonNode document, - Encryptor encryptor, - EncryptionProperties encryptionProperties, - CosmosDiagnosticsContext diagnosticsContext, - CancellationToken cancellationToken) - { - _ = diagnosticsContext; - - if (encryptionProperties.EncryptionFormatVersion != EncryptionFormatVersion.Mde && encryptionProperties.EncryptionFormatVersion != EncryptionFormatVersion.MdeWithCompression) - { - throw new NotSupportedException($"Unknown encryption format version: {encryptionProperties.EncryptionFormatVersion}. Please upgrade your SDK to the latest version."); - } - - using ArrayPoolManager arrayPoolManager = new (); - - DataEncryptionKey encryptionKey = await encryptor.GetEncryptionKeyAsync(encryptionProperties.DataEncryptionKeyId, encryptionProperties.EncryptionAlgorithm, cancellationToken); - - List pathsDecrypted = new (encryptionProperties.EncryptedPaths.Count()); - - JsonObject itemObj = document.AsObject(); - -#if NET8_0_OR_GREATER - BrotliCompressor decompressor = null; - if (encryptionProperties.EncryptionFormatVersion == EncryptionFormatVersion.MdeWithCompression) - { - bool containsCompressed = encryptionProperties.CompressedEncryptedPaths?.Any() == true; - if (encryptionProperties.CompressionAlgorithm != CompressionOptions.CompressionAlgorithm.Brotli && containsCompressed) - { - throw new NotSupportedException($"Unknown compression algorithm {encryptionProperties.CompressionAlgorithm}"); - } - - if (containsCompressed) - { - decompressor = new (); - } - } -#endif - - foreach (string path in encryptionProperties.EncryptedPaths) - { - string propertyName = path[1..]; - - if (!itemObj.TryGetPropertyValue(propertyName, out JsonNode propertyValue)) - { - // malformed document, such record shouldn't be there at all - continue; - } - - // can we get to internal JsonNode buffers to avoid string allocation here? - string base64String = propertyValue.GetValue(); - byte[] cipherTextWithTypeMarker = arrayPoolManager.Rent((base64String.Length * sizeof(char) * 3 / 4) + 4); - if (!Convert.TryFromBase64Chars(base64String, cipherTextWithTypeMarker, out int cipherTextLength)) - { - continue; - } - - (byte[] bytes, int processedBytes) = this.Encryptor.Decrypt(encryptionKey, cipherTextWithTypeMarker, cipherTextLength, arrayPoolManager); - -#if NET8_0_OR_GREATER - if (decompressor != null) - { - if (encryptionProperties.CompressedEncryptedPaths?.TryGetValue(path, out int decompressedSize) == true) - { - byte[] buffer = arrayPoolManager.Rent(decompressedSize); - processedBytes = decompressor.Decompress(bytes, processedBytes, buffer); - - bytes = buffer; - } - } -#endif - document[propertyName] = this.Serializer.Deserialize( - (TypeMarker)cipherTextWithTypeMarker[0], - bytes.AsSpan(0, processedBytes)); - - pathsDecrypted.Add(path); - } - - DecryptionContext decryptionContext = EncryptionProcessor.CreateDecryptionContext( - pathsDecrypted, - encryptionProperties.DataEncryptionKeyId); - - itemObj.Remove(Constants.EncryptedInfo); - return decryptionContext; - } - } -} - -#endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytes.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytes.cs deleted file mode 100644 index a8bc77f75b..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytes.cs +++ /dev/null @@ -1,34 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// ------------------------------------------------------------ - -#if NET8_0_OR_GREATER -namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.SystemTextJson -{ - using System; - - internal class JsonBytes - { - internal byte[] Bytes { get; private set; } - - internal int Offset { get; private set; } - - internal int Length { get; private set; } - - public JsonBytes(byte[] bytes, int offset, int length) - { - ArgumentNullException.ThrowIfNull(bytes); - ArgumentOutOfRangeException.ThrowIfNegative(offset); - ArgumentOutOfRangeException.ThrowIfNegative(length); - if (bytes.Length < offset + length) - { - throw new ArgumentOutOfRangeException(null, "Offset + Length > bytes.Length"); - } - - this.Bytes = bytes; - this.Offset = offset; - this.Length = length; - } - } -} -#endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytesConverter.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytesConverter.cs deleted file mode 100644 index d9048375c0..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/src/Transformation/SystemTextJson/JsonBytesConverter.cs +++ /dev/null @@ -1,26 +0,0 @@ -// ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -// ------------------------------------------------------------ - -#if NET8_0_OR_GREATER - -namespace Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.SystemTextJson -{ - using System; - using System.Text.Json; - using System.Text.Json.Serialization; - - internal class JsonBytesConverter : JsonConverter - { - public override JsonBytes Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - throw new NotImplementedException(); - } - - public override void Write(Utf8JsonWriter writer, JsonBytes value, JsonSerializerOptions options) - { - writer.WriteBase64StringValue(value.Bytes.AsSpan(value.Offset, value.Length)); - } - } -} -#endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/EncryptionBenchmark.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/EncryptionBenchmark.cs index c851750c53..b436edd547 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/EncryptionBenchmark.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/EncryptionBenchmark.cs @@ -36,7 +36,7 @@ public partial class EncryptionBenchmark public CompressionOptions.CompressionAlgorithm CompressionAlgorithm { get; set; } #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [Params(JsonProcessor.Newtonsoft, JsonProcessor.SystemTextJson, JsonProcessor.Stream)] + [Params(JsonProcessor.Newtonsoft, JsonProcessor.Stream)] #else [Params(JsonProcessor.Newtonsoft)] #endif diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Readme.md b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Readme.md index 22cb620269..691dbf0a5d 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Readme.md +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Performance.Tests/Readme.md @@ -15,10 +15,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 1 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 1 | None | Newtonsoft | 26.31 μs | 0.224 μs | 0.322 μs | 26.23 μs | 0.1526 | 0.0305 | - | 41440 B | | DecryptToProvidedStream | 1 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **1** | **None** | **SystemTextJson** | **14.33 μs** | **0.137 μs** | **0.201 μs** | **14.32 μs** | **0.0916** | **0.0153** | **-** | **22904 B** | -| EncryptToProvidedStream | 1 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 1 | None | SystemTextJson | 14.54 μs | 0.124 μs | 0.186 μs | 14.52 μs | 0.0610 | 0.0305 | - | 21448 B | -| DecryptToProvidedStream | 1 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **1** | **None** | **Stream** | **12.85 μs** | **0.095 μs** | **0.143 μs** | **12.84 μs** | **0.0610** | **0.0153** | **-** | **17528 B** | | EncryptToProvidedStream | 1 | None | Stream | 13.00 μs | 0.096 μs | 0.141 μs | 12.98 μs | 0.0458 | 0.0153 | - | 11392 B | | Decrypt | 1 | None | Stream | 13.01 μs | 0.152 μs | 0.228 μs | 13.05 μs | 0.0458 | 0.0153 | - | 12672 B | @@ -27,10 +23,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 1 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 1 | Brotli | Newtonsoft | 33.49 μs | 0.910 μs | 1.335 μs | 33.99 μs | 0.1221 | - | - | 41064 B | | DecryptToProvidedStream | 1 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **1** | **Brotli** | **SystemTextJson** | **20.92 μs** | **0.136 μs** | **0.199 μs** | **20.95 μs** | **0.0610** | **-** | **-** | **21952 B** | -| EncryptToProvidedStream | 1 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 1 | Brotli | SystemTextJson | 20.53 μs | 0.136 μs | 0.200 μs | 20.52 μs | 0.0610 | 0.0305 | - | 20488 B | -| DecryptToProvidedStream | 1 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **1** | **Brotli** | **Stream** | **21.15 μs** | **1.037 μs** | **1.521 μs** | **20.52 μs** | **0.0610** | **0.0305** | **-** | **16584 B** | | EncryptToProvidedStream | 1 | Brotli | Stream | 20.57 μs | 0.213 μs | 0.292 μs | 20.57 μs | 0.0305 | - | - | 11672 B | | Decrypt | 1 | Brotli | Stream | 21.14 μs | 2.212 μs | 3.311 μs | 19.46 μs | 0.0305 | - | - | 13216 B | @@ -39,10 +31,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 10 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 10 | None | Newtonsoft | 112.98 μs | 15.294 μs | 21.934 μs | 100.38 μs | 0.6104 | 0.1221 | - | 157425 B | | DecryptToProvidedStream | 10 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **10** | **None** | **SystemTextJson** | **41.85 μs** | **0.868 μs** | **1.272 μs** | **41.40 μs** | **0.4272** | **0.0610** | **-** | **105345 B** | -| EncryptToProvidedStream | 10 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 10 | None | SystemTextJson | 41.79 μs | 0.501 μs | 0.718 μs | 41.64 μs | 0.3662 | 0.0610 | - | 96464 B | -| DecryptToProvidedStream | 10 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **10** | **None** | **Stream** | **39.63 μs** | **0.658 μs** | **0.923 μs** | **39.41 μs** | **0.3052** | **0.0610** | **-** | **82928 B** | | EncryptToProvidedStream | 10 | None | Stream | 36.59 μs | 0.272 μs | 0.399 μs | 36.57 μs | 0.1221 | - | - | 37048 B | | Decrypt | 10 | None | Stream | 28.64 μs | 0.378 μs | 0.517 μs | 28.59 μs | 0.1221 | 0.0305 | - | 29520 B | @@ -51,10 +39,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 10 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 10 | Brotli | Newtonsoft | 118.98 μs | 1.530 μs | 2.195 μs | 118.76 μs | 0.4883 | - | - | 144849 B | | DecryptToProvidedStream | 10 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **10** | **Brotli** | **SystemTextJson** | **71.40 μs** | **0.799 μs** | **1.145 μs** | **71.23 μs** | **0.2441** | **-** | **-** | **86217 B** | -| EncryptToProvidedStream | 10 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 10 | Brotli | SystemTextJson | 73.37 μs | 7.283 μs | 10.676 μs | 67.12 μs | 0.2441 | - | - | 82201 B | -| DecryptToProvidedStream | 10 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **10** | **Brotli** | **Stream** | **90.10 μs** | **3.136 μs** | **4.693 μs** | **88.92 μs** | **0.2441** | **-** | **-** | **63809 B** | | EncryptToProvidedStream | 10 | Brotli | Stream | 97.27 μs | 1.885 μs | 2.703 μs | 97.35 μs | 0.1221 | - | - | 32465 B | | Decrypt | 10 | Brotli | Stream | 58.48 μs | 0.956 μs | 1.372 μs | 58.59 μs | 0.1221 | 0.0610 | - | 30064 B | @@ -63,10 +47,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 100 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 100 | None | Newtonsoft | 1,177.48 μs | 25.746 μs | 38.535 μs | 1,172.04 μs | 17.5781 | 15.6250 | 15.6250 | 1260228 B | | DecryptToProvidedStream | 100 | None | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **100** | **None** | **SystemTextJson** | **824.48 μs** | **31.605 μs** | **47.305 μs** | **812.80 μs** | **25.3906** | **25.3906** | **25.3906** | **965259 B** | -| EncryptToProvidedStream | 100 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 100 | None | SystemTextJson | 814.40 μs | 50.865 μs | 76.132 μs | 811.34 μs | 21.4844 | 21.4844 | 21.4844 | 950333 B | -| DecryptToProvidedStream | 100 | None | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **100** | **None** | **Stream** | **636.72 μs** | **31.468 μs** | **47.099 μs** | **630.15 μs** | **16.6016** | **16.6016** | **16.6016** | **678066 B** | | EncryptToProvidedStream | 100 | None | Stream | 383.33 μs | 7.441 μs | 10.671 μs | 384.69 μs | 4.3945 | 4.3945 | 4.3945 | 230133 B | | Decrypt | 100 | None | Stream | 384.93 μs | 12.519 μs | 18.738 μs | 383.59 μs | 5.8594 | 5.8594 | 5.8594 | 230753 B | @@ -75,10 +55,6 @@ LaunchCount=2 WarmupCount=10 | EncryptToProvidedStream | 100 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | | Decrypt | 100 | Brotli | Newtonsoft | 1,175.01 μs | 41.917 μs | 61.441 μs | 1,156.01 μs | 11.7188 | 9.7656 | 9.7656 | 1124274 B | | DecryptToProvidedStream | 100 | Brotli | Newtonsoft | NA | NA | NA | NA | - | - | - | - | -| **Encrypt** | **100** | **Brotli** | **SystemTextJson** | **1,050.27 μs** | **31.128 μs** | **46.591 μs** | **1,052.70 μs** | **17.5781** | **17.5781** | **17.5781** | **766642 B** | -| EncryptToProvidedStream | 100 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | -| Decrypt | 100 | Brotli | SystemTextJson | 926.80 μs | 28.605 μs | 41.025 μs | 925.73 μs | 18.5547 | 18.5547 | 18.5547 | 801460 B | -| DecryptToProvidedStream | 100 | Brotli | SystemTextJson | NA | NA | NA | NA | - | - | - | - | | **Encrypt** | **100** | **Brotli** | **Stream** | **757.11 μs** | **19.549 μs** | **29.260 μs** | **754.55 μs** | **10.7422** | **10.7422** | **10.7422** | **479493 B** | | EncryptToProvidedStream | 100 | Brotli | Stream | 563.46 μs | 9.960 μs | 14.284 μs | 561.60 μs | 2.9297 | 2.9297 | 2.9297 | 180637 B | | Decrypt | 100 | Brotli | Stream | 542.34 μs | 14.514 μs | 21.724 μs | 542.04 μs | 6.8359 | 6.8359 | 6.8359 | 231162 B | diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/MdeEncryptionProcessorTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/MdeEncryptionProcessorTests.cs index 9e05872546..46fde1350c 100644 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/MdeEncryptionProcessorTests.cs +++ b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/MdeEncryptionProcessorTests.cs @@ -68,7 +68,6 @@ public static void ClassInitialize(TestContext testContext) [TestMethod] [DataRow(JsonProcessor.Newtonsoft)] #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [DataRow(JsonProcessor.SystemTextJson)] [DataRow(JsonProcessor.Stream)] #endif public async Task InvalidPathToEncrypt(JsonProcessor jsonProcessor) @@ -109,7 +108,6 @@ public async Task InvalidPathToEncrypt(JsonProcessor jsonProcessor) [TestMethod] [DataRow(JsonProcessor.Newtonsoft)] #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [DataRow(JsonProcessor.SystemTextJson)] [DataRow(JsonProcessor.Stream)] #endif public async Task DuplicatePathToEncrypt(JsonProcessor jsonProcessor) @@ -162,30 +160,6 @@ public async Task EncryptDecryptPropertyWithNullValue_VerifyByNewtonsoft(Encrypt decryptionContext); } -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [TestMethod] - [DynamicData(nameof(EncryptionOptionsCombinations))] - public async Task EncryptDecryptPropertyWithNullValue_VerifyBySystemText(EncryptionOptions encryptionOptions) - { - TestDoc testDoc = TestDoc.Create(); - testDoc.SensitiveStr = null; - - JsonNode encryptedDoc = await VerifyEncryptionSucceededSystemText(testDoc, encryptionOptions); - - (JsonNode decryptedDoc, DecryptionContext decryptionContext) = await EncryptionProcessor.DecryptAsync( - encryptedDoc, - mockEncryptor.Object, - new CosmosDiagnosticsContext(), - CancellationToken.None); - - VerifyDecryptionSucceeded( - decryptedDoc, - testDoc, - TestDoc.PathsToEncrypt.Count, - decryptionContext); - } -#endif - [TestMethod] [DynamicData(nameof(EncryptionOptionsCombinations))] public async Task ValidateEncryptDecryptDocument_VerifyByNewtonsoft(EncryptionOptions encryptionOptions) @@ -207,29 +181,6 @@ public async Task ValidateEncryptDecryptDocument_VerifyByNewtonsoft(EncryptionOp decryptionContext); } -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [TestMethod] - [DynamicData(nameof(EncryptionOptionsCombinations))] - public async Task ValidateEncryptDecryptDocument_VerifyBySystemText(EncryptionOptions encryptionOptions) - { - TestDoc testDoc = TestDoc.Create(); - - JsonNode encryptedDoc = await VerifyEncryptionSucceededSystemText(testDoc, encryptionOptions); - - (JsonNode decryptedDoc, DecryptionContext decryptionContext) = await EncryptionProcessor.DecryptAsync( - encryptedDoc, - mockEncryptor.Object, - new CosmosDiagnosticsContext(), - CancellationToken.None); - - VerifyDecryptionSucceeded( - decryptedDoc, - testDoc, - TestDoc.PathsToEncrypt.Count, - decryptionContext); - } -#endif - [TestMethod] [DynamicData(nameof(EncryptionOptionsCombinations))] public async Task ValidateDecryptByNewtonsoftStream_VerifyByNewtonsoft(EncryptionOptions encryptionOptions) @@ -319,7 +270,6 @@ public async Task ValidateDecryptBySystemTextStream_VerifyBySystemText(Encryptio [TestMethod] [DataRow(JsonProcessor.Newtonsoft)] #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - [DataRow(JsonProcessor.SystemTextJson)] [DataRow(JsonProcessor.Stream)] #endif public async Task DecryptStreamWithoutEncryptedProperty(JsonProcessor processor) @@ -389,53 +339,6 @@ private static async Task VerifyEncryptionSucceededNewtonsoft(TestDoc t return encryptedDoc; } -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - private static async Task VerifyEncryptionSucceededSystemText(TestDoc testDoc, EncryptionOptions encryptionOptions) - { - Stream encryptedStream = await EncryptionProcessor.EncryptAsync( - testDoc.ToStream(), - mockEncryptor.Object, - encryptionOptions, - new CosmosDiagnosticsContext(), - CancellationToken.None); - - JsonNode encryptedDoc = JsonNode.Parse(encryptedStream, documentOptions: new System.Text.Json.JsonDocumentOptions() { }); - - Assert.AreEqual(testDoc.Id, encryptedDoc["id"].GetValue()); - Assert.AreEqual(testDoc.PK, encryptedDoc[nameof(TestDoc.PK)].GetValue()); - Assert.AreEqual(testDoc.NonSensitive, encryptedDoc[nameof(TestDoc.NonSensitive)].GetValue()); - Assert.IsNotNull(encryptedDoc[nameof(TestDoc.SensitiveInt)].GetValue()); - Assert.AreNotEqual(testDoc.SensitiveInt, encryptedDoc[nameof(TestDoc.SensitiveInt)].GetValue()); // not equal since value is encrypted - - JsonNode eiJProp = encryptedDoc[Constants.EncryptedInfo]; - Assert.IsNotNull(eiJProp); - Assert.IsNotNull(eiJProp.AsObject()); - EncryptionProperties encryptionProperties = System.Text.Json.JsonSerializer.Deserialize(eiJProp); - - Assert.IsNotNull(encryptionProperties); - Assert.AreEqual(dekId, encryptionProperties.DataEncryptionKeyId); - - int expectedVersion = encryptionOptions.CompressionOptions.Algorithm != CompressionOptions.CompressionAlgorithm.None ? 4 : 3; - Assert.AreEqual(expectedVersion, encryptionProperties.EncryptionFormatVersion); - Assert.IsNull(encryptionProperties.EncryptedData); - Assert.IsNotNull(encryptionProperties.EncryptedPaths); - - if (testDoc.SensitiveStr == null) - { - AssertNullableValueKind(null, encryptedDoc, nameof(TestDoc.SensitiveStr)); // since null value is not encrypted - Assert.AreEqual(TestDoc.PathsToEncrypt.Count - 1, encryptionProperties.EncryptedPaths.Count()); - } - else - { - Assert.IsNotNull(encryptedDoc[nameof(TestDoc.SensitiveStr)].GetValue()); - Assert.AreNotEqual(testDoc.SensitiveStr, encryptedDoc[nameof(TestDoc.SensitiveStr)].GetValue()); // not equal since value is encrypted - Assert.AreEqual(TestDoc.PathsToEncrypt.Count, encryptionProperties.EncryptedPaths.Count()); - } - - return encryptedDoc; - } -#endif - private static void VerifyDecryptionSucceeded( JObject decryptedDoc, TestDoc expectedDoc, @@ -540,13 +443,10 @@ private static EncryptionOptions CreateEncryptionOptions(JsonProcessor processor public static IEnumerable EncryptionOptionsCombinations => new[] { new object[] { CreateEncryptionOptions(JsonProcessor.Newtonsoft, CompressionOptions.CompressionAlgorithm.None, CompressionLevel.NoCompression) }, #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - new object[] { CreateEncryptionOptions(JsonProcessor.SystemTextJson, CompressionOptions.CompressionAlgorithm.None, CompressionLevel.NoCompression) }, new object[] { CreateEncryptionOptions(JsonProcessor.Stream, CompressionOptions.CompressionAlgorithm.None, CompressionLevel.NoCompression) }, new object[] { CreateEncryptionOptions(JsonProcessor.Newtonsoft, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.Fastest) }, - new object[] { CreateEncryptionOptions(JsonProcessor.SystemTextJson, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.Fastest) }, new object[] { CreateEncryptionOptions(JsonProcessor.Stream, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.Fastest) }, new object[] { CreateEncryptionOptions(JsonProcessor.Newtonsoft, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.NoCompression) }, - new object[] { CreateEncryptionOptions(JsonProcessor.SystemTextJson, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.NoCompression) }, new object[] { CreateEncryptionOptions(JsonProcessor.Stream, CompressionOptions.CompressionAlgorithm.Brotli, CompressionLevel.NoCompression) }, #endif }; @@ -559,7 +459,6 @@ public static IEnumerable EncryptionOptionsStreamTestCombinations { yield return new object[] { encryptionOptions[0], JsonProcessor.Newtonsoft }; #if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - yield return new object[] { encryptionOptions[0], JsonProcessor.SystemTextJson }; yield return new object[] { encryptionOptions[0], JsonProcessor.Stream }; #endif } diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesConverterTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesConverterTests.cs deleted file mode 100644 index fbef47e872..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesConverterTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -#if NET8_0_OR_GREATER - -namespace Microsoft.Azure.Cosmos.Encryption.Tests.Transformation -{ - using System; - using System.IO; - using System.Text.Json; - using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.SystemTextJson; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - [TestClass] - public class JsonBytesConverterTests - { - [TestMethod] - public void Write_Results_IdenticalToNewtonsoft() - { - byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; - - JsonBytes jsonBytes = new (bytes, 5, 5); - - using MemoryStream ms = new (); - using Utf8JsonWriter writer = new (ms); - - JsonBytesConverter jsonConverter = new (); - jsonConverter.Write(writer, jsonBytes, JsonSerializerOptions.Default); - - writer.Flush(); - ms.Flush(); - ms.Position = 0; - StreamReader sr = new(ms); - string systemTextResult = sr.ReadToEnd(); - - byte[] newtonsoftBytes = bytes.AsSpan(5, 5).ToArray(); - string newtonsoftResult = Newtonsoft.Json.JsonConvert.SerializeObject(newtonsoftBytes); - - Assert.AreEqual(systemTextResult, newtonsoftResult); - } - } -} -#endif \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesTests.cs deleted file mode 100644 index a29f378ff2..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonBytesTests.cs +++ /dev/null @@ -1,33 +0,0 @@ -#if NET8_0_OR_GREATER - -namespace Microsoft.Azure.Cosmos.Encryption.Tests.Transformation -{ - using System; - using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.SystemTextJson; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - [TestClass] - public class JsonBytesTests - { - [TestMethod] - public void Ctor_ThrowsForInvalidInputs() - { - Assert.ThrowsException(() => new JsonBytes(null, 1, 1)); - Assert.ThrowsException(() => new JsonBytes(new byte[10], -1, 1)); - Assert.ThrowsException(() => new JsonBytes(new byte[10], 0, -1)); - Assert.ThrowsException(() => new JsonBytes(new byte[10], 8, 8)); - } - - [TestMethod] - public void Properties_AreSetCorrectly() - { - byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; - JsonBytes jsonBytes = new (bytes, 1, 5); - - Assert.AreEqual(1, jsonBytes.Offset); - Assert.AreEqual(5, jsonBytes.Length); - Assert.AreSame(bytes, jsonBytes.Bytes); - } - } -} -#endif diff --git a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonNodeSqlSerializerTests.cs b/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonNodeSqlSerializerTests.cs deleted file mode 100644 index e28bd409a1..0000000000 --- a/Microsoft.Azure.Cosmos.Encryption.Custom/tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation/JsonNodeSqlSerializerTests.cs +++ /dev/null @@ -1,171 +0,0 @@ -#if ENCRYPTION_CUSTOM_PREVIEW && NET8_0_OR_GREATER - -namespace Microsoft.Azure.Cosmos.Encryption.Tests.Transformation -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text.Json; - using System.Text.Json.Nodes; - using Microsoft.Azure.Cosmos.Encryption.Custom; - using Microsoft.Azure.Cosmos.Encryption.Custom.Transformation; - using Microsoft.VisualStudio.TestTools.UnitTesting; - using Newtonsoft.Json.Linq; - - [TestClass] - public class JsonNodeSqlSerializerTests - { - private static ArrayPoolManager _poolManager; - - [ClassInitialize] - public static void ClassInitialize(TestContext context) - { - _ = context; - _poolManager = new ArrayPoolManager(); - } - - [TestMethod] - [DynamicData(nameof(SerializationSamples))] - public void Serialize_SupportedValue(JsonNode testNode, byte expectedType, byte[] expectedBytes, int expectedLength) - { - JsonNodeSqlSerializer serializer = new(); - - (TypeMarker serializedType, byte[] serializedBytes, int serializedBytesCount) = serializer.Serialize(testNode, _poolManager); - - Assert.AreEqual((TypeMarker)expectedType, serializedType); - Assert.AreEqual(expectedLength, serializedBytesCount); - if (expectedLength == -1) - { - Assert.IsTrue(serializedBytes == null); - } - else - { - Assert.IsTrue(expectedBytes.SequenceEqual(serializedBytes.AsSpan(0, serializedBytesCount).ToArray())); - } - } - - [TestMethod] - [DynamicData(nameof(DeserializationSamples))] - public void Deserialize_SupportedValue(byte typeMarkerByte, byte[] serializedBytes, JsonNode expectedNode) - { - JsonNodeSqlSerializer serializer = new(); - TypeMarker typeMarker = (TypeMarker)typeMarkerByte; - JsonNode deserializedNode = serializer.Deserialize(typeMarker, serializedBytes); - - if ((expectedNode as JsonValue) != null) - { - AssertValueNodeEquality(expectedNode, deserializedNode); - return; - } - - if ((expectedNode as JsonArray) != null) - { - Assert.IsNotNull(deserializedNode as JsonArray); - - JsonArray expectedArray = expectedNode.AsArray(); - JsonArray deserializedArray = deserializedNode.AsArray(); - - Assert.AreEqual(expectedArray.Count, deserializedArray.Count); - - for (int i = 0; i < deserializedNode.AsArray().Count; i++) - { - AssertValueNodeEquality(expectedArray[i], deserializedArray[i]); - } - return; - } - - if ((expectedNode as JsonObject) != null) - { - Assert.IsNotNull(deserializedNode as JsonObject); - - JsonObject expectedObject = expectedNode.AsObject(); - JsonObject deserializedObject = deserializedNode.AsObject(); - - Assert.AreEqual(expectedObject.Count, deserializedObject.Count); - - foreach (KeyValuePair expected in expectedObject) - { - Assert.IsTrue(deserializedObject.ContainsKey(expected.Key)); - AssertValueNodeEquality(expected.Value, deserializedObject[expected.Key]); - } - return; - } - - Assert.Fail("Attempt to validate unsupported JsonNode type"); - } - - private static void AssertValueNodeEquality(JsonNode expectedNode, JsonNode actualNode) - { - JsonValue expectedValueNode = expectedNode.AsValue(); - JsonValue actualValueNode = actualNode.AsValue(); - - Assert.AreEqual(expectedValueNode.GetValueKind(), actualValueNode.GetValueKind()); - Assert.AreEqual(expectedValueNode.ToString(), actualValueNode.ToString()); - } - - public static IEnumerable DeserializationSamples - { - get - { - yield return new object[] { (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(true), JsonValue.Create(true) }; - yield return new object[] { (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(false), JsonValue.Create(false) }; - yield return new object[] { (byte)TypeMarker.Long, GetNewtonsoftValueEquivalent(192), JsonValue.Create(192) }; - yield return new object[] { (byte)TypeMarker.Double, GetNewtonsoftValueEquivalent(192.5), JsonValue.Create(192.5) }; - yield return new object[] { (byte)TypeMarker.String, GetNewtonsoftValueEquivalent(testString), JsonValue.Create(testString) }; - yield return new object[] { (byte)TypeMarker.Array, GetNewtonsoftValueEquivalent(testArray), JsonNode.Parse("[10,18,19]") }; - yield return new object[] { (byte)TypeMarker.Object, GetNewtonsoftValueEquivalent(testClass), JsonNode.Parse(testClass.ToJson()) }; - } - } - - public static IEnumerable SerializationSamples - { - get - { - List values = new() - { - new object[] {JsonValue.Create((string)null), (byte)TypeMarker.Null, null, -1 }, - new object[] {JsonValue.Create(true), (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(true), 8}, - new object[] {JsonValue.Create(false), (byte)TypeMarker.Boolean, GetNewtonsoftValueEquivalent(false), 8}, - new object[] {JsonValue.Create(192), (byte)TypeMarker.Long, GetNewtonsoftValueEquivalent(192), 8}, - new object[] {JsonValue.Create(192.5), (byte)TypeMarker.Double, GetNewtonsoftValueEquivalent(192.5), 8}, - new object[] {JsonValue.Create(testString), (byte)TypeMarker.String, GetNewtonsoftValueEquivalent(testString), 11}, - new object[] {JsonValue.Create(testArray), (byte)TypeMarker.Array, GetNewtonsoftValueEquivalent(testArray), 10}, - new object[] {JsonValue.Create(testClass), (byte)TypeMarker.Object, GetNewtonsoftValueEquivalent(testClass), 33} - }; - - return values; - } - } - - private static readonly string testString = "Hello world"; - private static readonly int[] testArray = new[] {10, 18, 19}; - private static readonly TestClass testClass = new() { SomeInt = 1, SomeString = "asdf" }; - - private class TestClass - { - public int SomeInt { get; set; } - public string SomeString { get; set; } - - public string ToJson() - { - return JsonSerializer.Serialize(this); - } - } - - private static byte[] GetNewtonsoftValueEquivalent(T value) - { - JObjectSqlSerializer serializer = new (); - JToken token = value switch - { - int[] => new JArray(value), - TestClass => JObject.FromObject(value), - _ => new JValue(value), - }; - (TypeMarker _, byte[] bytes, int lenght) = serializer.Serialize(token, _poolManager); - return bytes.AsSpan(0, lenght).ToArray(); - } - - } -} - -#endif \ No newline at end of file