2004-05-06 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / ProtectedMemoryTest.cs
1 //
2 // ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
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 ProtectedMemoryTest : Assertion {
24
25                 private void ProtectUnprotect (MemoryProtectionScope scope) 
26                 {
27                         byte[] data = new byte [16];
28                         ProtectedMemory.Protect (data, scope);
29                         int total = 0;
30                         for (int i=0; i < 16; i++)
31                                 total += data [i];
32                         Assert ("Protect", (total != 0));
33
34                         ProtectedMemory.Unprotect (data, scope);
35                         total = 0;
36                         for (int i=0; i < 16; i++)
37                                 total += data [i];
38                         Assert ("Unprotect", (total == 0));
39                 }
40
41                 [Test]
42                 public void ProtectSameProcess () 
43                 {
44                         try {
45                                 // we're testing the MemoryProtectionScope definition but
46                                 // not if it's really limited to the scope specified
47                                 ProtectUnprotect (MemoryProtectionScope.SameProcess);
48                         }
49                         catch (PlatformNotSupportedException) {
50                                 Console.WriteLine ("Only supported under Windows XP and later");
51                         }
52                 }
53
54                 [Test]
55                 public void ProtectSameLogon () 
56                 {
57                         try {
58                                 // we're testing the MemoryProtectionScope definition but
59                                 // not if it's really limited to the scope specified
60                                 ProtectUnprotect (MemoryProtectionScope.SameLogon);
61                         }
62                         catch (PlatformNotSupportedException) {
63                                 Console.WriteLine ("Only supported under Windows XP and later");
64                         }
65                 }
66
67                 [Test]
68                 public void ProtectCrossProcess () 
69                 {
70                         try {
71                                 // we're testing the MemoryProtectionScope definition but
72                                 // not if it's really limited to the scope specified
73                                 ProtectUnprotect (MemoryProtectionScope.CrossProcess);
74                         }
75                         catch (PlatformNotSupportedException) {
76                                 Console.WriteLine ("Only supported under Windows XP and later");
77                         }
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (CryptographicException))]
82                 public void ProtectBadDataLength () 
83                 {
84                         byte[] data = new byte [15];
85                         ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentNullException))]
90                 public void ProtectNull () 
91                 {
92                         ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (CryptographicException))]
97                 public void UnprotectBadDataLength () 
98                 {
99                         byte[] data = new byte [15];
100                         ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentNullException))]
105                 public void UnprotectNull () 
106                 {
107                         ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);
108                 }
109         }
110 }
111
112 #endif