ProtectedMemoryTest.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // ProtectedMemoryTest.cs - NUnit Test Cases for ProtectedMemory
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. #if NET_2_0
  11. using NUnit.Framework;
  12. using System;
  13. using System.Security.Cryptography;
  14. namespace MonoTests.System.Security.Cryptography {
  15. [TestFixture]
  16. public class ProtectedMemoryTest {
  17. private void ProtectUnprotect (MemoryProtectionScope scope)
  18. {
  19. byte[] data = new byte [16];
  20. ProtectedMemory.Protect (data, scope);
  21. int total = 0;
  22. for (int i=0; i < 16; i++)
  23. total += data [i];
  24. Assert.IsFalse ((total == 0), "Protect");
  25. ProtectedMemory.Unprotect (data, scope);
  26. total = 0;
  27. for (int i=0; i < 16; i++)
  28. total += data [i];
  29. Assert.IsTrue ((total == 0), "Unprotect");
  30. }
  31. [Test]
  32. public void ProtectSameProcess ()
  33. {
  34. try {
  35. // we're testing the MemoryProtectionScope definition but
  36. // not if it's really limited to the scope specified
  37. ProtectUnprotect (MemoryProtectionScope.SameProcess);
  38. }
  39. catch (PlatformNotSupportedException) {
  40. Assert.Ignore ("Only supported under Windows 2000 and later");
  41. }
  42. }
  43. [Test]
  44. public void ProtectSameLogon ()
  45. {
  46. try {
  47. // we're testing the MemoryProtectionScope definition but
  48. // not if it's really limited to the scope specified
  49. ProtectUnprotect (MemoryProtectionScope.SameLogon);
  50. }
  51. catch (PlatformNotSupportedException) {
  52. Assert.Ignore ("Only supported under Windows 2000 and later");
  53. }
  54. }
  55. [Test]
  56. public void ProtectCrossProcess ()
  57. {
  58. try {
  59. // we're testing the MemoryProtectionScope definition but
  60. // not if it's really limited to the scope specified
  61. ProtectUnprotect (MemoryProtectionScope.CrossProcess);
  62. }
  63. catch (PlatformNotSupportedException) {
  64. Assert.Ignore ("Only supported under Windows 2000 and later");
  65. }
  66. }
  67. [Test]
  68. [ExpectedException (typeof (CryptographicException))]
  69. public void ProtectBadDataLength ()
  70. {
  71. byte[] data = new byte [15];
  72. ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentNullException))]
  76. public void ProtectNull ()
  77. {
  78. ProtectedMemory.Protect (null, MemoryProtectionScope.SameProcess);
  79. }
  80. [Test]
  81. [ExpectedException (typeof (CryptographicException))]
  82. public void UnprotectBadDataLength ()
  83. {
  84. byte[] data = new byte [15];
  85. ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public void UnprotectNull ()
  90. {
  91. ProtectedMemory.Unprotect (null, MemoryProtectionScope.SameProcess);
  92. }
  93. }
  94. }
  95. #endif