[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Security / Test / System.Security.Cryptography / ProtectedMemoryCas.cs
blobd81ae91a002a1713591a361ee689734bb46e9189
1 //
2 // ProtectedMemoryCas.cs
3 // - CAS unit tests for System.Security.Cryptography.ProtectedMemory
4 //
5 // Author:
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
29 #if !MOBILE
31 using NUnit.Framework;
33 using System;
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 {
43 [TestFixture]
44 [Category ("CAS")]
45 // problem with CSC when an assembly use permissions defined within itself
46 [Category ("NotWorking")]
47 public class ProtectedMemoryCas {
49 [SetUp]
50 public virtual void SetUp ()
52 if (!SecurityManager.SecurityEnabled)
53 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
56 private bool IsEmpty (byte[] array)
58 int total = 0;
59 for (int i = 0; i < array.Length; i++)
60 total += array[i];
61 return (total == 0);
64 [Test]
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 ();
75 [Test]
76 [DataProtectionPermission (SecurityAction.PermitOnly, ProtectMemory = true)]
77 // note: this implies that UnmanagedCode isn't allowed
78 public void Protect_PermitOnly_Protect ()
80 try {
81 byte[] data = new byte[16];
82 ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
83 Assert.IsFalse (IsEmpty (data), "SameProcess");
85 data = new byte[16];
86 ProtectedMemory.Protect (data, MemoryProtectionScope.SameLogon);
87 Assert.IsFalse (IsEmpty (data), "SameLogon");
89 data = new byte[16];
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");
98 [Test]
99 [DataProtectionPermission (SecurityAction.Deny, ProtectMemory = true)]
100 [ExpectedException (typeof (SecurityException))]
101 public void Protect_Deny_Protect ()
103 try {
104 ProtectedMemory.Protect (new byte[16], MemoryProtectionScope.SameProcess);
106 catch (PlatformNotSupportedException) {
107 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
111 [Test]
112 [DataProtectionPermission (SecurityAction.PermitOnly, UnprotectMemory = true)]
113 // note: this implies that UnmanagedCode isn't allowed
114 public void Unprotect_PermitOnly_Unprotect ()
116 try {
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");
126 [Test]
127 [DataProtectionPermission (SecurityAction.Deny, UnprotectMemory = true)]
128 [ExpectedException (typeof (SecurityException))]
129 public void Unprotect_Deny_Unprotect ()
131 try {
132 ProtectedMemory.Unprotect (new byte[16], MemoryProtectionScope.SameProcess);
134 catch (PlatformNotSupportedException) {
135 Assert.Ignore ("Only supported under Windows 2000 SP3 and later");
139 [Test]
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 };
147 try {
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");
168 #endif