Arc4CipherTest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Text;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Security.Cryptography.Ciphers;
  5. using Renci.SshNet.Tests.Common;
  6. using Renci.SshNet.Tests.Properties;
  7. namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
  8. {
  9. /// <summary>
  10. /// Implements ARCH4 cipher algorithm
  11. /// </summary>
  12. [TestClass]
  13. public class Arc4CipherTest : TestBase
  14. {
  15. /// <summary>
  16. ///A test for Arc4Cipher Constructor
  17. ///</summary>
  18. [TestMethod]
  19. [Ignore] // placeholder for actual test
  20. public void Arc4CipherConstructorTest()
  21. {
  22. byte[] key = null; // TODO: Initialize to an appropriate value
  23. Arc4Cipher target = new Arc4Cipher(key, true);
  24. Assert.Inconclusive("TODO: Implement code to verify target");
  25. }
  26. [TestMethod]
  27. public void Decrypt_DischargeFirstBytes_False1()
  28. {
  29. const string key = "Key";
  30. const string expectedPlainText = "Plaintext";
  31. var encoding = Encoding.ASCII;
  32. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  33. var cipherText = new byte[] { 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3 };
  34. var actualPlainText = cipher.Decrypt(cipherText);
  35. Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
  36. }
  37. [TestMethod]
  38. public void Decrypt_DischargeFirstBytes_False2()
  39. {
  40. const string key = "Wiki";
  41. const string expectedPlainText = "pedia";
  42. var encoding = Encoding.ASCII;
  43. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  44. var cipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
  45. var actualPlainText = cipher.Decrypt(cipherText);
  46. Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
  47. }
  48. /// <summary>
  49. ///A test for DecryptBlock
  50. ///</summary>
  51. [TestMethod]
  52. [Ignore] // placeholder for actual test
  53. public void DecryptBlockTest()
  54. {
  55. byte[] key = null; // TODO: Initialize to an appropriate value
  56. Arc4Cipher target = new Arc4Cipher(key, true); // TODO: Initialize to an appropriate value
  57. byte[] inputBuffer = null; // TODO: Initialize to an appropriate value
  58. int inputOffset = 0; // TODO: Initialize to an appropriate value
  59. int inputCount = 0; // TODO: Initialize to an appropriate value
  60. byte[] outputBuffer = null; // TODO: Initialize to an appropriate value
  61. int outputOffset = 0; // TODO: Initialize to an appropriate value
  62. int expected = 0; // TODO: Initialize to an appropriate value
  63. int actual;
  64. actual = target.DecryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  65. Assert.AreEqual(expected, actual);
  66. Assert.Inconclusive("Verify the correctness of this test method.");
  67. }
  68. [TestMethod]
  69. public void Encrypt_DischargeFirstBytes_False1()
  70. {
  71. const string key = "Key";
  72. const string plainText = "Plaintext";
  73. var encoding = Encoding.ASCII;
  74. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  75. var expectedCipherText = new byte[] { 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3 };
  76. var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
  77. Assert.IsNotNull(actualCipherText);
  78. Assert.AreEqual(expectedCipherText.Length, actualCipherText.Length);
  79. Assert.AreEqual(expectedCipherText[0], actualCipherText[0]);
  80. Assert.AreEqual(expectedCipherText[1], actualCipherText[1]);
  81. Assert.AreEqual(expectedCipherText[2], actualCipherText[2]);
  82. Assert.AreEqual(expectedCipherText[3], actualCipherText[3]);
  83. Assert.AreEqual(expectedCipherText[4], actualCipherText[4]);
  84. Assert.AreEqual(expectedCipherText[5], actualCipherText[5]);
  85. Assert.AreEqual(expectedCipherText[6], actualCipherText[6]);
  86. Assert.AreEqual(expectedCipherText[7], actualCipherText[7]);
  87. Assert.AreEqual(expectedCipherText[8], actualCipherText[8]);
  88. }
  89. [TestMethod]
  90. public void Encrypt_DischargeFirstBytes_False2()
  91. {
  92. const string key = "Wiki";
  93. const string plainText = "pedia";
  94. var encoding = Encoding.ASCII;
  95. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  96. var expectedCipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
  97. var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
  98. Assert.IsNotNull(actualCipherText);
  99. Assert.AreEqual(expectedCipherText.Length, actualCipherText.Length);
  100. Assert.AreEqual(expectedCipherText[0], actualCipherText[0]);
  101. Assert.AreEqual(expectedCipherText[1], actualCipherText[1]);
  102. Assert.AreEqual(expectedCipherText[2], actualCipherText[2]);
  103. Assert.AreEqual(expectedCipherText[3], actualCipherText[3]);
  104. Assert.AreEqual(expectedCipherText[4], actualCipherText[4]);
  105. }
  106. /// <summary>
  107. ///A test for EncryptBlock
  108. ///</summary>
  109. [TestMethod]
  110. [Ignore] // placeholder for actual test
  111. public void EncryptBlockTest()
  112. {
  113. byte[] key = null; // TODO: Initialize to an appropriate value
  114. Arc4Cipher target = new Arc4Cipher(key, true); // TODO: Initialize to an appropriate value
  115. byte[] inputBuffer = null; // TODO: Initialize to an appropriate value
  116. int inputOffset = 0; // TODO: Initialize to an appropriate value
  117. int inputCount = 0; // TODO: Initialize to an appropriate value
  118. byte[] outputBuffer = null; // TODO: Initialize to an appropriate value
  119. int outputOffset = 0; // TODO: Initialize to an appropriate value
  120. int expected = 0; // TODO: Initialize to an appropriate value
  121. int actual;
  122. actual = target.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  123. Assert.AreEqual(expected, actual);
  124. Assert.Inconclusive("Verify the correctness of this test method.");
  125. }
  126. [TestMethod]
  127. [Owner("olegkap")]
  128. [TestCategory("Cipher")]
  129. [TestCategory("integration")]
  130. public void Test_Cipher_Arcfour128_Connection()
  131. {
  132. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  133. connectionInfo.Encryptions.Clear();
  134. connectionInfo.Encryptions.Add("arcfour128", new CipherInfo(128, (key, iv) => { return new Arc4Cipher(key, true); }));
  135. using (var client = new SshClient(connectionInfo))
  136. {
  137. client.Connect();
  138. client.Disconnect();
  139. }
  140. }
  141. [TestMethod]
  142. [Owner("olegkap")]
  143. [TestCategory("Cipher")]
  144. [TestCategory("integration")]
  145. public void Test_Cipher_Arcfour256_Connection()
  146. {
  147. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  148. connectionInfo.Encryptions.Clear();
  149. connectionInfo.Encryptions.Add("arcfour256", new CipherInfo(256, (key, iv) => { return new Arc4Cipher(key, true); }));
  150. using (var client = new SshClient(connectionInfo))
  151. {
  152. client.Connect();
  153. client.Disconnect();
  154. }
  155. }
  156. }
  157. }