コード例 #1
0
 public void PassingNullToTheStringConstructorCausesGetValueToThrow()
 {
     var protectedValue = new DPAPIProtectedValue((string)null);
     Assert.That(() => protectedValue.GetValue(),
         Throws.InstanceOf<DataProtectionException>()
             .With.InnerException.InstanceOf<ArgumentNullException>());
 }
コード例 #2
0
 public void PassingNonBase64EncodedStringToTheStringConstructorCausesGetValueToThrow()
 {
     var protectedValue = new DPAPIProtectedValue("wtf");
     Assert.That(() => protectedValue.GetValue(),
         Throws.InstanceOf<DataProtectionException>()
             .With.InnerException.InstanceOf<FormatException>());
 }
コード例 #3
0
        public void TheEncryptedDataByteArrayPassedToTheByteArrayConstructorIsReturnedByTheEncryptedDataProperty()
        {
            var encryptedData = new byte[] { 1, 2, 3 };

            var value = new DPAPIProtectedValue(encryptedData);

            Assert.That(value.EncryptedData, Is.EqualTo(encryptedData));
        }
コード例 #4
0
        public void TheEncryptedDataBase64EncodedStringPassedToTheStringConstructorIsDecodedAndReturnedByTheEncryptedDataProperty()
        {
            var encryptedDataBytes = new byte[] { 1, 2, 3 };
            var encryptedData = Convert.ToBase64String(encryptedDataBytes);

            var value = new DPAPIProtectedValue(encryptedData);

            Assert.That(value.EncryptedData, Is.EqualTo(encryptedDataBytes));
        }
コード例 #5
0
        public void TheOptionalEntropyBase64EncodedStringPassedToTheStringConstructorIsDecodedAndReturnedByTheOptionalEntropyProperty()
        {
            var encryptedDataBytes = new byte[] { 1, 2, 3 };
            var optionalEntropyBytes = new byte[] { 4, 5, 6 };
            var encryptedData = Convert.ToBase64String(encryptedDataBytes);
            var optionalEntropy = Convert.ToBase64String(optionalEntropyBytes);

            var value = new DPAPIProtectedValue(encryptedData, optionalEntropy);

            Assert.That(value.OptionalEntropy, Is.EqualTo(optionalEntropyBytes));
        }
コード例 #6
0
        public void TheOptionalEntropyByteArrayPassedToTheByteArrayConstructorIsReturnedByTheOptionalEntropyProperty()
        {
            var encryptedData = new byte[] { 1, 2, 3 };
            var optionalEntropy = new byte[] { 4, 5, 6 };

            var value = new DPAPIProtectedValue(encryptedData, optionalEntropy);

            Assert.That(value.OptionalEntropy, Is.EqualTo(optionalEntropy));
        }
コード例 #7
0
        public void PassingBadEncryptedDataToTheByteArrayConstructorCausesGetValueToThrow()
        {
            var userData = Encoding.UTF8.GetBytes("Hello, world!");
            var optionalEntropy = new byte[] { 4, 5, 6 };
            var scope = DataProtectionScope.LocalMachine;

            var encryptedData = ProtectedData.Protect(userData, optionalEntropy, scope);
            encryptedData[0] ^= 0xFF;

            var protectedValue = new DPAPIProtectedValue(Convert.ToBase64String(encryptedData), optionalEntropy == null ? null : Convert.ToBase64String(optionalEntropy));

            Assert.That(() => protectedValue.GetValue(),
                Throws.TypeOf<DataProtectionException>()
                    .With.InnerException.TypeOf<CryptographicException>());
        }
コード例 #8
0
        public void PassingInvalidBase64EncodedOptionalEntropyToTheStringConstructorCausesGetValueToThrow()
        {
            var userData = Encoding.UTF8.GetBytes("Hello, world!");
            var validOptionalEntropy = new byte[] { 4, 5, 6 };
            var scope = DataProtectionScope.LocalMachine;
            var encryptedData = Convert.ToBase64String(
                ProtectedData.Protect(userData, validOptionalEntropy, scope));

            var invalidOptionalEntropy = "omg!wtf!bbq!";

            var protectedValue = new DPAPIProtectedValue(encryptedData, invalidOptionalEntropy);

            Assert.That(() => protectedValue.GetValue(),
                Throws.TypeOf<DataProtectionException>()
                    .With.InnerException.TypeOf<FormatException>());
        }
コード例 #9
0
        public void CanUnprotectDPAPIProtectedValueWhenNoScopeIsProvided()
        {
            var userData = Encoding.UTF8.GetBytes("Hello, world!");
            var optionalEntropy = new byte[] { 4, 5, 6 };

            var encryptedData = ProtectedData.Protect(userData, optionalEntropy, DataProtectionScope.CurrentUser);

            Assume.That(encryptedData, Is.Not.EqualTo(userData));

            var protectedValue = new DPAPIProtectedValue(encryptedData, optionalEntropy);

            var unprotectedValue = protectedValue.GetValue();

            Assert.That(Encoding.UTF8.GetString(unprotectedValue), Is.EqualTo("Hello, world!"));
        }
コード例 #10
0
        public void CanUnprotectDPAPIProtectedValueWhenNoOptionalEntropyIsProvided()
        {
            var userData = Encoding.UTF8.GetBytes("Hello, world!");
            var scope = DataProtectionScope.LocalMachine;

            var encryptedData = ProtectedData.Protect(userData, null, scope);

            Assume.That(encryptedData, Is.Not.EqualTo(userData));

            var protectedValue = new DPAPIProtectedValue(encryptedData, scope:scope);

            var unprotectedValue = protectedValue.GetValue();

            Assert.That(Encoding.UTF8.GetString(unprotectedValue), Is.EqualTo("Hello, world!"));
        }
コード例 #11
0
        public void TheScopePassedToTheStringConstructorIsReturnedByTheScopeProperty()
        {
            var encryptedDataBytes = new byte[] { 1, 2, 3 };
            var encryptedData = Convert.ToBase64String(encryptedDataBytes);

            var value = new DPAPIProtectedValue(encryptedData, scope:DataProtectionScope.LocalMachine);

            Assert.That(value.Scope, Is.EqualTo(DataProtectionScope.LocalMachine));
        }