|
| 1 | +/* Copyright 2021-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 | + |
| 16 | +using System; |
| 17 | +using FluentAssertions; |
| 18 | +using MongoDB.Driver.Encryption; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests |
| 22 | +{ |
| 23 | + public class EncryptOptionsTests |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void Constructor_should_fail_when_algorithm_is_null() |
| 27 | + { |
| 28 | + var exception = Record.Exception(() => new EncryptOptions(algorithm: null)); |
| 29 | + exception.Should().BeOfType<ArgumentNullException>(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Constructor_should_fail_when_keyId_and_alternateKeyName_are_both_empty() |
| 34 | + { |
| 35 | + var exception = Record.Exception(() => new EncryptOptions(algorithm: "test", alternateKeyName: null, keyId: null)); |
| 36 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 37 | + e.Message.Should().Be("Key Id and AlternateKeyName may not both be null."); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Constructor_should_fail_when_keyId_and_alternateKeyName_are_both_specified() |
| 42 | + { |
| 43 | + var exception = Record.Exception(() => new EncryptOptions(algorithm: "test", alternateKeyName: "alternateKeyName", keyId: Guid.NewGuid())); |
| 44 | + var e = exception.Should().BeOfType<ArgumentException>().Subject; |
| 45 | + e.Message.Should().Be("Key Id and AlternateKeyName may not both be set."); |
| 46 | + } |
| 47 | + |
| 48 | + [Theory] |
| 49 | + [InlineData(EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic, "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")] |
| 50 | + [InlineData(EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random, "AEAD_AES_256_CBC_HMAC_SHA_512-Random")] |
| 51 | + // these values are required to be supported due a CSHARP-3527 bug of how we worked with input algorithm values. So, |
| 52 | + // since we cannot remove them because of BC, we need to keep supporting them even after solving the underlying bug |
| 53 | + [InlineData("AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")] |
| 54 | + [InlineData("AEAD_AES_256_CBC_HMAC_SHA_512_Random", "AEAD_AES_256_CBC_HMAC_SHA_512-Random")] |
| 55 | + // the below values match to the spec wording |
| 56 | + [InlineData("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic", "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")] |
| 57 | + [InlineData("AEAD_AES_256_CBC_HMAC_SHA_512-Random", "AEAD_AES_256_CBC_HMAC_SHA_512-Random")] |
| 58 | + // just a random string value |
| 59 | + [InlineData("TEST_random", "TEST_random")] |
| 60 | + // just a random value in enum form |
| 61 | + [InlineData((EncryptionAlgorithm)99, "99")] |
| 62 | + public void Constructor_should_support_different_algorithm_representations(object algorithm, string expectedAlgorithmRepresentation) |
| 63 | + { |
| 64 | + var alternateKeyName = "test"; |
| 65 | + |
| 66 | + EncryptOptions subject; |
| 67 | + if (algorithm is EncryptionAlgorithm enumedAlgorithm) |
| 68 | + { |
| 69 | + subject = new EncryptOptions(enumedAlgorithm, alternateKeyName: alternateKeyName); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + subject = new EncryptOptions(algorithm.ToString(), alternateKeyName: alternateKeyName); |
| 74 | + } |
| 75 | + |
| 76 | + subject.Algorithm.Should().Be(expectedAlgorithmRepresentation); |
| 77 | + subject.AlternateKeyName.Should().Be("test"); |
| 78 | + subject.KeyId.Should().NotHaveValue(); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void With_should_set_correct_values() |
| 83 | + { |
| 84 | + var originalAlgorithm = "originalAlgorithm"; |
| 85 | + var newAlgorithm = "newAlgorithm"; |
| 86 | + var originalKeyId = Guid.Empty; |
| 87 | + var newKeyId = Guid.NewGuid(); |
| 88 | + var originalAlternateKeyName = "test"; |
| 89 | + var newAlternateKeyName = "new"; |
| 90 | + |
| 91 | + var subject = CreateConfiguredSubject(withKeyId: true); |
| 92 | + AssertValues(subject, originalAlgorithm, originalKeyId, null); |
| 93 | + |
| 94 | + subject = subject.With(algorithm: newAlgorithm); |
| 95 | + AssertValues(subject, newAlgorithm, originalKeyId, null); |
| 96 | + |
| 97 | + subject = subject.With(keyId: newKeyId); |
| 98 | + AssertValues(subject, newAlgorithm, newKeyId, null); |
| 99 | + |
| 100 | + subject = CreateConfiguredSubject(withKeyId: false); |
| 101 | + AssertValues(subject, originalAlgorithm, null, originalAlternateKeyName); |
| 102 | + |
| 103 | + subject = subject.With(alternateKeyName: newAlternateKeyName); |
| 104 | + AssertValues(subject, originalAlgorithm, null, newAlternateKeyName); |
| 105 | + |
| 106 | + static void AssertValues(EncryptOptions subject, string algorithm, Guid? keyId, string alternateKeyName) |
| 107 | + { |
| 108 | + subject.Algorithm.Should().Be(algorithm); |
| 109 | + subject.KeyId.Should().Be(keyId); |
| 110 | + subject.AlternateKeyName.Should().Be(alternateKeyName); |
| 111 | + } |
| 112 | + |
| 113 | + EncryptOptions CreateConfiguredSubject(bool withKeyId) |
| 114 | + { |
| 115 | + if (withKeyId) |
| 116 | + { |
| 117 | + return new EncryptOptions(algorithm: originalAlgorithm, keyId: originalKeyId); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + return new EncryptOptions(algorithm: originalAlgorithm, alternateKeyName: originalAlternateKeyName); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments