2004-05-06 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / ProtectedDataTest.cs
1 //
2 // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 #if NET_2_0
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Security.Cryptography;
16
17 namespace MonoTests.System.Security.Cryptography {
18
19         // References:
20         // a.   
21
22         [TestFixture]
23         public class ProtectedDataTest : Assertion {
24
25                 private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope) 
26                 {
27                         byte[] data = new byte [16];
28                         byte[] encdata = ProtectedData.Protect (data, entropy, scope);
29                         int total = 0;
30                         for (int i=0; i < 16; i++)
31                                 total += encdata [i];
32                         Assert ("Protect", (total != 0));
33
34                         byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
35                         total = 0;
36                         for (int i=0; i < 16; i++)
37                                 total += decdata [i];
38                         Assert ("Unprotect", (total == 0));
39                 }
40
41                 [Test]
42                 public void ProtectCurrentUser () 
43                 {
44                         try {
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);
49                         }
50                         catch (PlatformNotSupportedException) {
51                                 Console.WriteLine ("Only supported under Windows 2000 and later");
52                         }
53                 }
54
55                 [Test]
56                 public void ProtectLocalMachine () 
57                 {
58                         try {
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);
63                         }
64                         catch (PlatformNotSupportedException) {
65                                 Console.WriteLine ("Only supported under Windows 2000 and later");
66                         }
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void ProtectNull () 
72                 {
73                         byte[] notMuchEntropy = new byte [16];
74                         ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
75                 }
76
77                 [Test]
78                 public void ProtectNullEntropy () 
79                 {
80                         try {
81                                 // we're testing the DataProtectionScope definition but
82                                 // not if it's really limited to the scope specified
83                                 ProtectUnprotect (null, DataProtectionScope.LocalMachine);
84                         }
85                         catch (PlatformNotSupportedException) {
86                                 Console.WriteLine ("Only supported under Windows 2000 and later");
87                         }
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (CryptographicException))]
92                 public void UnprotectNotProtectedData () 
93                 {
94                         byte[] baddata = new byte [16];
95                         byte[] notMuchEntropy = new byte [16];
96                         ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentNullException))]
101                 public void UnprotectNull () 
102                 {
103                         byte[] notMuchEntropy = new byte [16];
104                         ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
105                 }
106         }
107 }
108
109 #endif