2 // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using NUnit.Framework;
15 using System.Security.Cryptography;
17 namespace MonoTests.System.Security.Cryptography {
20 public class ProtectedDataTest {
22 private byte[] notMuchEntropy = new byte[16];
24 private bool IsEmpty (byte[] array)
27 for (int i = 0; i < array.Length; i++)
32 private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
35 byte[] data = new byte [16];
36 byte[] encdata = ProtectedData.Protect (data, entropy, scope);
37 Assert.IsFalse (IsEmpty (encdata), "Protect");
39 byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
40 Assert.IsTrue (IsEmpty (decdata), "Unprotect");
42 catch (CryptographicException ce) {
43 if (ce.InnerException is UnauthorizedAccessException)
44 Assert.Ignore ("The machine key store hasn't yet been created (as root).");
46 catch (PlatformNotSupportedException) {
47 Assert.Ignore ("Only supported under Windows 2000 and later");
52 public void ProtectCurrentUser ()
54 // we're testing the DataProtectionScope definition but
55 // not if it's really limited to the scope specified
56 ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
60 public void ProtectLocalMachine ()
62 // we're testing the DataProtectionScope definition but
63 // not if it's really limited to the scope specified
64 ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
68 public void DataProtectionScope_All ()
70 byte[] data = new byte[16];
72 foreach (DataProtectionScope dps in Enum.GetValues (typeof (DataProtectionScope))) {
73 byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, dps);
74 Assert.IsFalse (IsEmpty (encdata), "Protect");
75 Assert.IsTrue (IsEmpty (data), "Protect(original unmodified)");
76 byte[] decdata = ProtectedData.Unprotect (encdata, notMuchEntropy, dps);
77 Assert.IsTrue (IsEmpty (decdata), "Unprotect");
80 catch (CryptographicException ce) {
81 if (ce.InnerException is UnauthorizedAccessException)
82 Assert.Ignore ("The machine key store hasn't yet been created (as root).");
84 catch (PlatformNotSupportedException) {
85 Assert.Ignore ("Only supported under Windows 2000 and later");
90 [ExpectedException (typeof (ArgumentException))]
91 [Category ("NotDotNet")]
92 public void Protect_InvalidDataProtectionScope ()
95 byte[] data = new byte[16];
96 ProtectedData.Protect (data, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
97 // MS doesn't throw an ArgumentException but returning from
98 // this method will throw an UnhandledException in NUnit
100 catch (PlatformNotSupportedException) {
101 Assert.Ignore ("Only supported under Windows 2000 and later");
106 [ExpectedException (typeof (ArgumentNullException))]
107 public void ProtectNull ()
109 ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
113 public void ProtectNullEntropy ()
115 // we're testing the DataProtectionScope definition but
116 // not if it's really limited to the scope specified
117 ProtectUnprotect (null, DataProtectionScope.CurrentUser);
121 [ExpectedException (typeof (CryptographicException))]
122 public void UnprotectNotProtectedData ()
125 byte[] baddata = new byte [16];
126 ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
128 catch (PlatformNotSupportedException) {
129 Assert.Ignore ("Only supported under Windows 2000 and later");
134 [ExpectedException (typeof (ArgumentException))]
135 [Category ("NotDotNet")]
136 public void Unprotect_InvalidDataProtectionScope ()
139 byte[] data = new byte[16];
140 byte[] encdata = ProtectedData.Protect (data, notMuchEntropy, DataProtectionScope.CurrentUser);
141 ProtectedData.Unprotect (encdata, notMuchEntropy, (DataProtectionScope) Int32.MinValue);
142 // MS doesn't throw an ArgumentException but returning from
143 // this method will throw an UnhandledException in NUnit
145 catch (PlatformNotSupportedException) {
146 Assert.Ignore ("Only supported under Windows 2000 and later");
151 [ExpectedException (typeof (ArgumentNullException))]
152 public void UnprotectNull ()
154 ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);