ToBase64TransformTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // ToBase64TransformTest
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. using System;
  8. using System.Security.Cryptography;
  9. using NUnit.Framework;
  10. namespace System.Security.Cryptography.Test {
  11. /// <summary>
  12. /// ToBase64Transform test suite.
  13. /// </summary>
  14. public class ToBase64TransformTest {
  15. public static ITest Suite {
  16. get {
  17. TestSuite suite = new TestSuite("ToBase64Transform tests");
  18. suite.AddTest(ToBase64TransformTestCase.Suite);
  19. return suite;
  20. }
  21. }
  22. public class ToBase64TransformTestCase : TestCase {
  23. public ToBase64TransformTestCase (String name) : base(name)
  24. {
  25. }
  26. protected override void SetUp ()
  27. {
  28. }
  29. public static ITest Suite
  30. {
  31. get {
  32. Console.WriteLine("Testing " + (new ToBase64Transform ()));
  33. return new TestSuite(typeof(ToBase64TransformTestCase));
  34. }
  35. }
  36. //
  37. // Tests
  38. //
  39. public void TestProperties ()
  40. {
  41. ToBase64Transform encoder = new ToBase64Transform ();
  42. Assert ("Wrong input block size!", encoder.InputBlockSize == 3);
  43. Assert ("Wrong output block size!", encoder.OutputBlockSize == 4);
  44. Assert ("Should be unable to transform multiple blocks!", !encoder.CanTransformMultipleBlocks);
  45. }
  46. /*
  47. // FIXME: Well, according to Beta2 docs an exception should be thrown here,
  48. // since "data size is not valid". Nothing happens in Beta2 though.
  49. // So just ignore this for now.
  50. public void TestException ()
  51. {
  52. ToBase64Transform encoder = new ToBase64Transform ();
  53. byte [] res = new byte [4];
  54. bool thrown = false;
  55. try {
  56. encoder.TransformBlock (new byte [] {1,2,3,4,5},0,5,res,0);
  57. } catch (CryptographicException) {
  58. thrown = true;
  59. }
  60. Assert (thrown);
  61. }
  62. */
  63. public void TestFullBlockEncoding ()
  64. {
  65. ToBase64Transform encoder = new ToBase64Transform ();
  66. byte [] res = new byte [encoder.OutputBlockSize];
  67. foreach (Base64TestCase testCase in fullBlockTests) {
  68. Base64TestCase tmp = new Base64TestCase ();
  69. tmp.flat = testCase.flat;
  70. int n = encoder.TransformBlock (testCase.GetFlat(),0,3,res,0);
  71. AssertEquals(n, encoder.OutputBlockSize);
  72. tmp.SetEncoded(res);
  73. AssertEquals(testCase,tmp);
  74. }
  75. }
  76. public void TestFinalBlockEncoding ()
  77. {
  78. ToBase64Transform encoder = new ToBase64Transform ();
  79. foreach (Base64TestCase testCase in finalBlockTests) {
  80. Base64TestCase tmp = new Base64TestCase ();
  81. tmp.flat = testCase.flat;
  82. byte [] res = encoder.TransformFinalBlock (testCase.GetFlat(),0,testCase.flat.Length);
  83. tmp.SetEncoded(res);
  84. AssertEquals(testCase,tmp);
  85. }
  86. }
  87. Base64TestCase [] fullBlockTests = {
  88. new Base64TestCase("ABC","QUJD"),
  89. new Base64TestCase("123","MTIz"),
  90. new Base64TestCase(new byte [] {125,24,215},"fRjX")
  91. };
  92. Base64TestCase [] finalBlockTests = {
  93. new Base64TestCase("AB","QUI="),
  94. new Base64TestCase("1","MQ=="),
  95. new Base64TestCase(new byte [] {125,24},"fRg=")
  96. };
  97. //
  98. // Test helper
  99. //
  100. private class Base64TestCase {
  101. public string flat;
  102. public string encoded;
  103. public Base64TestCase () : this ("","")
  104. {
  105. }
  106. public Base64TestCase (Base64TestCase that)
  107. {
  108. this.flat = that.flat;
  109. this.encoded = that.encoded;
  110. }
  111. public Base64TestCase (byte [] flat, string encoded)
  112. {
  113. SetFlat (flat);
  114. this.encoded = encoded;
  115. }
  116. public Base64TestCase (string flat, string encoded)
  117. {
  118. this.flat = flat;
  119. this.encoded = encoded;
  120. }
  121. public void SetEncoded (byte [] encoded)
  122. {
  123. String enc="";
  124. for (int i = 0; i < encoded.Length; i++)
  125. enc += (char) encoded[i];
  126. this.encoded = enc;
  127. }
  128. public void SetFlat (byte [] flat)
  129. {
  130. String flt="";
  131. for (int i=0; i < flat.Length; i++)
  132. flt += (char) flat[i];
  133. this.flat = flt;
  134. }
  135. public byte [] GetFlat ()
  136. {
  137. byte [] res = new byte [flat.Length];
  138. for (int i = 0; i < flat.Length; i++)
  139. res [i] = (byte) flat [i];
  140. return res;
  141. }
  142. public static bool operator == (Base64TestCase t1, Base64TestCase t2)
  143. {
  144. return (t1.flat.Equals (t2.flat) &&
  145. t1.encoded.Equals (t2.encoded));
  146. }
  147. public static bool operator != (Base64TestCase t1, Base64TestCase t2)
  148. {
  149. return !(t1 == t2);
  150. }
  151. public override bool Equals (object o)
  152. {
  153. return (o is Base64TestCase)
  154. && (this == (o as Base64TestCase));
  155. }
  156. public override int GetHashCode ()
  157. {
  158. return flat.GetHashCode () ^ (encoded.GetHashCode () << 2);
  159. }
  160. public override string ToString ()
  161. {
  162. return "Flat = " + flat + ", encoded = " + encoded;
  163. }
  164. }
  165. } // ToBase64TransformTestCase
  166. } // ToBase64TransformTest
  167. } // System.Security.Cryptography.Test