2 // ProtectedMemoryCas.cs
3 // - CAS unit tests for System.Security.Cryptography.ProtectedMemory
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit
.Framework
;
34 using System
.Reflection
;
35 using System
.Security
;
36 using System
.Security
.Cryptography
;
37 using System
.Security
.Permissions
;
39 using MonoTests
.System
.Security
.Cryptography
;
41 namespace MonoCasTests
.System
.Security
.Cryptography
{
45 // problem with CSC when an assembly use permissions defined within itself
46 [Category ("NotWorking")]
47 public class ProtectedMemoryCas
{
50 public virtual void SetUp ()
52 if (!SecurityManager
.SecurityEnabled
)
53 Assert
.Ignore ("SecurityManager.SecurityEnabled is OFF");
56 private bool IsEmpty (byte[] array
)
59 for (int i
= 0; i
< array
.Length
; i
++)
65 [DataProtectionPermission (SecurityAction
.PermitOnly
, ProtectMemory
= true, UnprotectMemory
= true)]
66 public void UnitTestReuse ()
68 ProtectedMemoryTest unit
= new ProtectedMemoryTest ();
69 unit
.ProtectSameProcess ();
70 unit
.ProtectSameLogon ();
71 unit
.ProtectCrossProcess ();
72 unit
.MemoryProtectionScope_All ();
76 [DataProtectionPermission (SecurityAction
.PermitOnly
, ProtectMemory
= true)]
77 // note: this implies that UnmanagedCode isn't allowed
78 public void Protect_PermitOnly_Protect ()
81 byte[] data
= new byte[16];
82 ProtectedMemory
.Protect (data
, MemoryProtectionScope
.SameProcess
);
83 Assert
.IsFalse (IsEmpty (data
), "SameProcess");
86 ProtectedMemory
.Protect (data
, MemoryProtectionScope
.SameLogon
);
87 Assert
.IsFalse (IsEmpty (data
), "SameLogon");
90 ProtectedMemory
.Protect (data
, MemoryProtectionScope
.CrossProcess
);
91 Assert
.IsFalse (IsEmpty (data
), "CrossProcess");
93 catch (PlatformNotSupportedException
) {
94 Assert
.Ignore ("Only supported under Windows 2000 SP3 and later");
99 [DataProtectionPermission (SecurityAction
.Deny
, ProtectMemory
= true)]
100 [ExpectedException (typeof (SecurityException
))]
101 public void Protect_Deny_Protect ()
104 ProtectedMemory
.Protect (new byte[16], MemoryProtectionScope
.SameProcess
);
106 catch (PlatformNotSupportedException
) {
107 Assert
.Ignore ("Only supported under Windows 2000 SP3 and later");
112 [DataProtectionPermission (SecurityAction
.PermitOnly
, UnprotectMemory
= true)]
113 // note: this implies that UnmanagedCode isn't allowed
114 public void Unprotect_PermitOnly_Unprotect ()
117 byte[] data
= new byte[16];
118 ProtectedMemory
.Unprotect (data
, MemoryProtectionScope
.SameProcess
);
119 Assert
.IsFalse (IsEmpty (data
), "Unprotect unprotected");
121 catch (PlatformNotSupportedException
) {
122 Assert
.Ignore ("Only supported under Windows 2000 SP3 and later");
127 [DataProtectionPermission (SecurityAction
.Deny
, UnprotectMemory
= true)]
128 [ExpectedException (typeof (SecurityException
))]
129 public void Unprotect_Deny_Unprotect ()
132 ProtectedMemory
.Unprotect (new byte[16], MemoryProtectionScope
.SameProcess
);
134 catch (PlatformNotSupportedException
) {
135 Assert
.Ignore ("Only supported under Windows 2000 SP3 and later");
140 [DataProtectionPermission (SecurityAction
.PermitOnly
, ProtectMemory
= true, UnprotectMemory
= true)]
141 public void LinkDemand_PermitOnly_DataProtection ()
143 Type pm
= typeof (ProtectedMemory
);
144 byte[] data
= new byte[16];
145 object[] parameters
= new object[2] { data, MemoryProtectionScope.SameProcess }
;
148 MethodInfo mi
= pm
.GetMethod ("Protect");
149 Assert
.IsNotNull (mi
, "Protect");
150 mi
.Invoke (null, parameters
);
151 Assert
.IsFalse (IsEmpty (data
), "Encrypted");
153 mi
= pm
.GetMethod ("Unprotect");
154 Assert
.IsNotNull (mi
, "Unprotect");
155 mi
.Invoke (null, parameters
);
156 Assert
.IsTrue (IsEmpty (data
), "Decrypted");
158 // so no LinkDemand are required (Demand are enough) and
159 // no check for UnmanagedCode are required
161 catch (TargetInvocationException tie
) {
162 if (tie
.InnerException
is PlatformNotSupportedException
)
163 Assert
.Ignore ("Only supported under Windows 2000 SP 3 and later");