ProtectedDataTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // ProtectedDataTest.cs - NUnit Test Cases for ProtectedData
  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 ProtectedDataTest {
  17. private void ProtectUnprotect (byte[] entropy, DataProtectionScope scope)
  18. {
  19. byte[] data = new byte [16];
  20. byte[] encdata = ProtectedData.Protect (data, entropy, scope);
  21. int total = 0;
  22. for (int i=0; i < 16; i++)
  23. total += encdata [i];
  24. Assert.IsFalse ((total == 0), "Protect");
  25. byte[] decdata = ProtectedData.Unprotect (encdata, entropy, scope);
  26. total = 0;
  27. for (int i=0; i < 16; i++)
  28. total += decdata [i];
  29. Assert.IsTrue ((total == 0), "Unprotect");
  30. }
  31. [Test]
  32. public void ProtectCurrentUser ()
  33. {
  34. try {
  35. byte[] notMuchEntropy = new byte [16];
  36. // we're testing the DataProtectionScope definition but
  37. // not if it's really limited to the scope specified
  38. ProtectUnprotect (notMuchEntropy, DataProtectionScope.CurrentUser);
  39. }
  40. catch (PlatformNotSupportedException) {
  41. Assert.Ignore ("Only supported under Windows 2000 and later");
  42. }
  43. }
  44. [Test]
  45. public void ProtectLocalMachine ()
  46. {
  47. try {
  48. byte[] notMuchEntropy = new byte [16];
  49. // we're testing the DataProtectionScope definition but
  50. // not if it's really limited to the scope specified
  51. ProtectUnprotect (notMuchEntropy, DataProtectionScope.LocalMachine);
  52. }
  53. catch (PlatformNotSupportedException) {
  54. Assert.Ignore ("Only supported under Windows 2000 and later");
  55. }
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentNullException))]
  59. public void ProtectNull ()
  60. {
  61. byte[] notMuchEntropy = new byte [16];
  62. ProtectedData.Protect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  63. }
  64. [Test]
  65. public void ProtectNullEntropy ()
  66. {
  67. try {
  68. // we're testing the DataProtectionScope definition but
  69. // not if it's really limited to the scope specified
  70. ProtectUnprotect (null, DataProtectionScope.LocalMachine);
  71. }
  72. catch (PlatformNotSupportedException) {
  73. Assert.Ignore ("Only supported under Windows 2000 and later");
  74. }
  75. }
  76. [Test]
  77. [ExpectedException (typeof (CryptographicException))]
  78. public void UnprotectNotProtectedData ()
  79. {
  80. try {
  81. byte[] baddata = new byte [16];
  82. byte[] notMuchEntropy = new byte [16];
  83. ProtectedData.Unprotect (baddata, notMuchEntropy, DataProtectionScope.CurrentUser);
  84. }
  85. catch (PlatformNotSupportedException) {
  86. Assert.Ignore ("Only supported under Windows 2000 and later");
  87. }
  88. }
  89. [Test]
  90. [ExpectedException (typeof (ArgumentNullException))]
  91. public void UnprotectNull ()
  92. {
  93. byte[] notMuchEntropy = new byte [16];
  94. ProtectedData.Unprotect (null, notMuchEntropy, DataProtectionScope.CurrentUser);
  95. }
  96. }
  97. }
  98. #endif