[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography / ProtectedDataTest.cs
1 //
2 // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Security.Cryptography;
16
17 namespace MonoTests.System.Security.Cryptography {
18
19         [TestFixture]
20         public class ProtectedDataTest {
21
22                 private byte[] notMuchEntropy = new byte[16];
23
24                 private bool IsEmpty (byte[] array)
25                 {
26                         int total = 0;
27                         for (int i = 0; i < array.Length; i++)
28                                 total += array[i];
29                         return (total == 0);
30                 }
31
32                 private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope) 
33                 {
34                         try {
35                                 byte[] data = new byte [16];
36                                 byte[] encdata = ProtectedData.Protect (data, entropy, scope);
37                                 Assert.IsFalse (IsEmpty (encdata), "Protect");
38
39                                 byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
40                                 Assert.IsTrue (IsEmpty (decdata), "Unprotect");
41                         }
42                         catch (CryptographicException ce) {
43                                 if (ce.InnerException is UnauthorizedAccessException)
44                                         Assert.Ignore ("The machine key store hasn't yet been created (as root).");
45                         }
46                         catch (PlatformNotSupportedException) {
47                                 Assert.Ignore ("Only supported under Windows 2000 and later");
48                         }
49                 }
50
51                 [Test]
52                 public void ProtectCurrentUser () 
53                 {
54                         // we're testing the DataProtectionScope definition but
55                         // not if it's really limited to the scope specified
56                         ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
57                 }
58
59                 [Test]
60                 public void ProtectLocalMachine () 
61                 {
62                         // we're testing the DataProtectionScope definition but
63                         // not if it's really limited to the scope specified
64                         ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
65                 }
66
67                 [Test]
68                 public void DataProtectionScope_All ()
69                 {
70                         byte[] data = new byte[16];
71                         try {
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");
78                                 }
79                         }
80                         catch (CryptographicException ce) {
81                                 if (ce.InnerException is UnauthorizedAccessException)
82                                         Assert.Ignore ("The machine key store hasn't yet been created (as root).");
83                         }
84                         catch (PlatformNotSupportedException) {
85                                 Assert.Ignore ("Only supported under Windows 2000 and later");
86                         }
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 [Category ("NotDotNet")]
92                 public void Protect_InvalidDataProtectionScope ()
93                 {
94                         try {
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
99                         }
100                         catch (PlatformNotSupportedException) {
101                                 Assert.Ignore ("Only supported under Windows 2000 and later");
102                         }
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentNullException))]
107                 public void ProtectNull () 
108                 {
109                         ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
110                 }
111
112                 [Test]
113                 public void ProtectNullEntropy () 
114                 {
115                         // we're testing the DataProtectionScope definition but
116                         // not if it's really limited to the scope specified
117                         ProtectUnprotect (null, DataProtectionScope.CurrentUser);
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (CryptographicException))]
122                 public void UnprotectNotProtectedData () 
123                 {
124                         try {
125                                 byte[] baddata = new byte [16];
126                                 ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
127                         }
128                         catch (PlatformNotSupportedException) {
129                                 Assert.Ignore ("Only supported under Windows 2000 and later");
130                         }
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (ArgumentException))]
135                 [Category ("NotDotNet")]
136                 public void Unprotect_InvalidDataProtectionScope ()
137                 {
138                         try {
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
144                         }
145                         catch (PlatformNotSupportedException) {
146                                 Assert.Ignore ("Only supported under Windows 2000 and later");
147                         }
148                 }
149
150                 [Test]
151                 [ExpectedException (typeof (ArgumentNullException))]
152                 public void UnprotectNull () 
153                 {
154                         ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
155                 }
156         }
157 }
158