2 // ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 using NUnit.Framework;
16 using System.Security.Cryptography;
18 namespace MonoTests.System.Security.Cryptography {
21 public class ProtectedMemoryTest {
23 private bool IsEmpty (byte[] array)
26 for (int i = 0; i < array.Length; i++)
31 private void ProtectUnprotect (MemoryProtectionScope scope)
34 byte[] data = new byte [16];
35 ProtectedMemory.Protect (data, scope);
36 Assert.IsFalse (IsEmpty (data), "Protect");
38 ProtectedMemory.Unprotect (data, scope);
39 Assert.IsTrue (IsEmpty (data), "Unprotect");
41 catch (PlatformNotSupportedException) {
42 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
47 public void ProtectSameProcess ()
49 // we're testing the MemoryProtectionScope definition but
50 // not if it's really limited to the scope specified
51 ProtectUnprotect (MemoryProtectionScope.SameProcess);
55 public void ProtectSameLogon ()
57 // we're testing the MemoryProtectionScope definition but
58 // not if it's really limited to the scope specified
59 ProtectUnprotect (MemoryProtectionScope.SameLogon);
63 public void ProtectCrossProcess ()
65 // we're testing the MemoryProtectionScope definition but
66 // not if it's really limited to the scope specified
67 ProtectUnprotect (MemoryProtectionScope.CrossProcess);
71 public void MemoryProtectionScope_All ()
73 byte[] data = new byte[16];
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");
82 catch (PlatformNotSupportedException) {
83 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
88 [ExpectedException (typeof (ArgumentException))]
89 public void Protect_InvalidMemoryProtectionScope ()
91 byte[] data = new byte[16];
92 ProtectedMemory.Protect (data, (MemoryProtectionScope) Int32.MinValue);
96 [ExpectedException (typeof (CryptographicException))]
97 public void ProtectBadDataLength ()
99 byte[] data = new byte [15];
101 ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
103 catch (PlatformNotSupportedException) {
104 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
109 [ExpectedException (typeof (ArgumentNullException))]
110 public void ProtectNull ()
112 ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
116 [ExpectedException (typeof (ArgumentException))]
117 public void Unprotect_InvalidMemoryProtectionScope ()
119 byte[] data = new byte[16];
120 ProtectedMemory.Unprotect (data, (MemoryProtectionScope) Int32.MinValue);
124 [ExpectedException (typeof (CryptographicException))]
125 public void UnprotectBadDataLength ()
127 byte[] data = new byte [15];
129 ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
131 catch (PlatformNotSupportedException) {
132 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
137 [ExpectedException (typeof (ArgumentNullException))]
138 public void UnprotectNull ()
140 ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);