* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Security / Test / System.Security.Cryptography / ProtectedMemoryTest.cs
1 //
2 // ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
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 #if NET_2_0
12
13 using NUnit.Framework;
14
15 using System;
16 using System.Security.Cryptography;
17
18 namespace MonoTests.System.Security.Cryptography {
19
20         [TestFixture]
21         public class ProtectedMemoryTest {
22
23                 private bool IsEmpty (byte[] array)
24                 {
25                         int total = 0;
26                         for (int i = 0; i < array.Length; i++)
27                                 total += array [i];
28                         return (total == 0);
29                 }
30
31                 private void ProtectUnprotect (MemoryProtectionScope scope) 
32                 {
33                         try {
34                                 byte[] data = new byte [16];
35                                 ProtectedMemory.Protect (data, scope);
36                                 Assert.IsFalse (IsEmpty (data), "Protect");
37
38                                 ProtectedMemory.Unprotect (data, scope);
39                                 Assert.IsTrue (IsEmpty (data), "Unprotect");
40                         }
41                         catch (PlatformNotSupportedException) {
42                                 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
43                         }
44                 }
45
46                 [Test]
47                 public void ProtectSameProcess () 
48                 {
49                         // we're testing the MemoryProtectionScope definition but
50                         // not if it's really limited to the scope specified
51                         ProtectUnprotect (MemoryProtectionScope.SameProcess);
52                 }
53
54                 [Test]
55                 public void ProtectSameLogon () 
56                 {
57                         // we're testing the MemoryProtectionScope definition but
58                         // not if it's really limited to the scope specified
59                         ProtectUnprotect (MemoryProtectionScope.SameLogon);
60                 }
61
62                 [Test]
63                 public void ProtectCrossProcess () 
64                 {
65                         // we're testing the MemoryProtectionScope definition but
66                         // not if it's really limited to the scope specified
67                         ProtectUnprotect (MemoryProtectionScope.CrossProcess);
68                 }
69
70                 [Test]
71                 public void MemoryProtectionScope_All ()
72                 {
73                         byte[] data = new byte[16];
74                         try {
75                                 foreach (MemoryProtectionScope mps in Enum.GetValues (typeof (MemoryProtectionScope))) {
76                                         ProtectedMemory.Protect (data, mps);
77                                         Assert.IsFalse (IsEmpty (data), "Protect");
78                                         ProtectedMemory.Unprotect (data, mps);
79                                         Assert.IsTrue (IsEmpty (data), "Unprotect");
80                                 }
81                         }
82                         catch (PlatformNotSupportedException) {
83                                 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
84                         }
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (ArgumentException))]
89                 public void Protect_InvalidMemoryProtectionScope ()
90                 {
91                         byte[] data = new byte[16];
92                         ProtectedMemory.Protect (data, (MemoryProtectionScope) Int32.MinValue);
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (CryptographicException))]
97                 public void ProtectBadDataLength () 
98                 {
99                         byte[] data = new byte [15];
100                         try {
101                                 ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
102                         }
103                         catch (PlatformNotSupportedException) {
104                                 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
105                         }
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentNullException))]
110                 public void ProtectNull () 
111                 {
112                         ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentException))]
117                 public void Unprotect_InvalidMemoryProtectionScope ()
118                 {
119                         byte[] data = new byte[16];
120                         ProtectedMemory.Unprotect (data, (MemoryProtectionScope) Int32.MinValue);
121                 }
122
123                 [Test]
124                 [ExpectedException (typeof (CryptographicException))]
125                 public void UnprotectBadDataLength () 
126                 {
127                         byte[] data = new byte [15];
128                         try {
129                                 ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
130                         }
131                         catch (PlatformNotSupportedException) {
132                                 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
133                         }
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentNullException))]
138                 public void UnprotectNull () 
139                 {
140                         ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);
141                 }
142         }
143 }
144
145 #endif