ProtectedDataTest.cs 2.8 KB

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