2
0

ScpClientTest_Upload_FileInfoAndPath_Success.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Moq;
  8. using Renci.SshNet.Common;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. [TestClass]
  12. public class ScpClientTest_Upload_FileInfoAndPath_Success : ScpClientTestBase
  13. {
  14. private ConnectionInfo _connectionInfo;
  15. private ScpClient _scpClient;
  16. private FileInfo _fileInfo;
  17. private string _remoteDirectory;
  18. private string _remoteFile;
  19. private string _remotePath;
  20. private string _transformedPath;
  21. private int _bufferSize;
  22. private byte[] _fileContent;
  23. private string _fileName;
  24. private int _fileSize;
  25. private IList<ScpUploadEventArgs> _uploadingRegister;
  26. protected override void SetupData()
  27. {
  28. var random = new Random();
  29. _bufferSize = random.Next(5, 15);
  30. _fileSize = _bufferSize + 2; //force uploading 2 chunks
  31. _fileContent = CreateContent(_fileSize);
  32. _fileName = CreateTemporaryFile(_fileContent);
  33. _connectionInfo = new ConnectionInfo("host", 22, "user", new PasswordAuthenticationMethod("user", "pwd"));
  34. _fileInfo = new FileInfo(_fileName);
  35. _remoteDirectory = "/home/sshnet";
  36. _remoteFile = random.Next().ToString();
  37. _remotePath = _remoteDirectory + "/" + _remoteFile;
  38. _transformedPath = random.Next().ToString();
  39. _uploadingRegister = new List<ScpUploadEventArgs>();
  40. }
  41. protected override void SetupMocks()
  42. {
  43. var sequence = new MockSequence();
  44. _ = ServiceFactoryMock.InSequence(sequence)
  45. .Setup(p => p.CreateRemotePathDoubleQuoteTransformation())
  46. .Returns(_remotePathTransformationMock.Object);
  47. _ = ServiceFactoryMock.InSequence(sequence)
  48. .Setup(p => p.CreateSocketFactory())
  49. .Returns(SocketFactoryMock.Object);
  50. _ = ServiceFactoryMock.InSequence(sequence)
  51. .Setup(p => p.CreateSession(_connectionInfo, SocketFactoryMock.Object))
  52. .Returns(SessionMock.Object);
  53. _ = SessionMock.InSequence(sequence)
  54. .Setup(p => p.Connect());
  55. _ = ServiceFactoryMock.InSequence(sequence)
  56. .Setup(p => p.CreatePipeStream())
  57. .Returns(_pipeStreamMock.Object);
  58. _ = SessionMock.InSequence(sequence)
  59. .Setup(p => p.CreateChannelSession())
  60. .Returns(_channelSessionMock.Object);
  61. _ = _channelSessionMock.InSequence(sequence)
  62. .Setup(p => p.Open());
  63. _ = _remotePathTransformationMock.InSequence(sequence)
  64. .Setup(p => p.Transform(_remoteDirectory))
  65. .Returns(_transformedPath);
  66. _ = _channelSessionMock.InSequence(sequence)
  67. .Setup(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)))
  68. .Returns(true);
  69. _ = _pipeStreamMock.InSequence(sequence)
  70. .Setup(p => p.ReadByte())
  71. .Returns(0);
  72. _ = _channelSessionMock.InSequence(sequence)
  73. .Setup(p => p.SendData(It.IsAny<byte[]>()));
  74. _ = _pipeStreamMock.InSequence(sequence)
  75. .Setup(p => p.ReadByte())
  76. .Returns(0);
  77. _ = _channelSessionMock.InSequence(sequence)
  78. .Setup(p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(CreateData(string.Format("C0644 {0} {1}\n", _fileInfo.Length, _remoteFile), _connectionInfo.Encoding)))));
  79. _ = _pipeStreamMock.InSequence(sequence)
  80. .Setup(p => p.ReadByte())
  81. .Returns(0);
  82. _ = _channelSessionMock.InSequence(sequence)
  83. .Setup(p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(_fileContent.Take(_bufferSize))), 0, _bufferSize));
  84. _ = _channelSessionMock.InSequence(sequence)
  85. .Setup(p => p.SendData(It.Is<byte[]>(b => b.Take(0, _fileContent.Length - _bufferSize).SequenceEqual(_fileContent.Take(_bufferSize, _fileContent.Length - _bufferSize))), 0, _fileContent.Length - _bufferSize));
  86. _ = _channelSessionMock.InSequence(sequence)
  87. .Setup(p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(new byte[] {0}))));
  88. _ = _pipeStreamMock.InSequence(sequence)
  89. .Setup(p => p.ReadByte())
  90. .Returns(0);
  91. _ = _channelSessionMock.InSequence(sequence)
  92. .Setup(p => p.Dispose());
  93. _ = _pipeStreamMock.InSequence(sequence)
  94. .Setup(p => p.Close());
  95. }
  96. protected override void Arrange()
  97. {
  98. base.Arrange();
  99. _scpClient = new ScpClient(_connectionInfo, false, ServiceFactoryMock.Object)
  100. {
  101. BufferSize = (uint) _bufferSize
  102. };
  103. _scpClient.Uploading += (sender, args) => _uploadingRegister.Add(args);
  104. _scpClient.Connect();
  105. }
  106. protected override void TearDown()
  107. {
  108. base.TearDown();
  109. if (_fileName != null)
  110. {
  111. File.Delete(_fileName);
  112. _fileName = null;
  113. }
  114. }
  115. protected override void Act()
  116. {
  117. _scpClient.Upload(_fileInfo, _remotePath);
  118. }
  119. [TestMethod]
  120. public void SendExecRequestOnChannelSessionShouldBeInvokedOnce()
  121. {
  122. _channelSessionMock.Verify(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)), Times.Once);
  123. }
  124. [TestMethod]
  125. public void DisposeOnChannelShouldBeInvokedOnce()
  126. {
  127. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  128. }
  129. [TestMethod]
  130. public void DisposeOnPipeStreamShouldBeInvokedOnce()
  131. {
  132. _pipeStreamMock.Verify(p => p.Close(), Times.Once);
  133. }
  134. [TestMethod]
  135. public void UploadingShouldHaveFiredTwice()
  136. {
  137. Assert.AreEqual(2, _uploadingRegister.Count);
  138. var uploading = _uploadingRegister[0];
  139. Assert.IsNotNull(uploading);
  140. Assert.AreSame(_fileInfo.Name, uploading.Filename);
  141. Assert.AreEqual(_fileSize, uploading.Size);
  142. Assert.AreEqual(_bufferSize, uploading.Uploaded);
  143. uploading = _uploadingRegister[1];
  144. Assert.IsNotNull(uploading);
  145. Assert.AreSame(_fileInfo.Name, uploading.Filename);
  146. Assert.AreEqual(_fileSize, uploading.Size);
  147. Assert.AreEqual(_fileSize, uploading.Uploaded);
  148. }
  149. private static IEnumerable<byte> CreateData(string command, Encoding encoding)
  150. {
  151. return encoding.GetBytes(command);
  152. }
  153. private static byte[] CreateContent(int length)
  154. {
  155. var random = new Random();
  156. var content = new byte[length];
  157. for (var i = 0; i < length; i++)
  158. {
  159. content[i] = (byte) random.Next(byte.MinValue, byte.MaxValue);
  160. }
  161. return content;
  162. }
  163. private static string CreateTemporaryFile(byte[] content)
  164. {
  165. var tempFile = Path.GetTempFileName();
  166. using (var fs = File.OpenWrite(tempFile))
  167. {
  168. fs.Write(content, 0, content.Length);
  169. }
  170. return tempFile;
  171. }
  172. }
  173. }