2 // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
12 using NUnit.Framework;
15 using System.Security.Cryptography;
17 namespace MonoTests.System.Security.Cryptography {
23 public class ProtectedDataTest : Assertion {
25 private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
27 byte[] data = new byte [16];
28 byte[] encdata = ProtectedData.Protect (data, entropy, scope);
30 for (int i=0; i < 16; i++)
32 Assert ("Protect", (total != 0));
34 byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
36 for (int i=0; i < 16; i++)
38 Assert ("Unprotect", (total == 0));
42 public void ProtectCurrentUser ()
45 byte[] notMuchEntropy = new byte [16];
46 // we're testing the DataProtectionScope definition but
47 // not if it's really limited to the scope specified
48 ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
50 catch (PlatformNotSupportedException) {
51 Console.WriteLine ("Only supported under Windows 2000 and later");
56 public void ProtectLocalMachine ()
59 byte[] notMuchEntropy = new byte [16];
60 // we're testing the DataProtectionScope definition but
61 // not if it's really limited to the scope specified
62 ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
64 catch (PlatformNotSupportedException) {
65 Console.WriteLine ("Only supported under Windows 2000 and later");
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void ProtectNull ()
73 byte[] notMuchEntropy = new byte [16];
74 ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
78 public void ProtectNullEntropy ()
81 // we're testing the DataProtectionScope definition but
82 // not if it's really limited to the scope specified
83 ProtectUnprotect (null, DataProtectionScope.LocalMachine);
85 catch (PlatformNotSupportedException) {
86 Console.WriteLine ("Only supported under Windows 2000 and later");
91 [ExpectedException (typeof (CryptographicException))]
92 public void UnprotectNotProtectedData ()
94 byte[] baddata = new byte [16];
95 byte[] notMuchEntropy = new byte [16];
96 ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
100 [ExpectedException (typeof (ArgumentNullException))]
101 public void UnprotectNull ()
103 byte[] notMuchEntropy = new byte [16];
104 ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);