ScpClientTest_Upload_FileInfoAndPath_Success.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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).Setup(p => p.Connect());
  54. _serviceFactoryMock.InSequence(sequence).Setup(p => p.CreatePipeStream()).Returns(_pipeStreamMock.Object);
  55. _sessionMock.InSequence(sequence).Setup(p => p.CreateChannelSession()).Returns(_channelSessionMock.Object);
  56. _channelSessionMock.InSequence(sequence).Setup(p => p.Open());
  57. _remotePathTransformationMock.InSequence(sequence)
  58. .Setup(p => p.Transform(_remoteDirectory))
  59. .Returns(_transformedPath);
  60. _channelSessionMock.InSequence(sequence)
  61. .Setup(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)))
  62. .Returns(true);
  63. _pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
  64. _channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny<byte[]>()));
  65. _pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
  66. _channelSessionMock.InSequence(sequence)
  67. .Setup(p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(
  68. CreateData(string.Format("C0644 {0} {1}\n", _fileInfo.Length, _remoteFile),
  69. _connectionInfo.Encoding)))));
  70. _pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
  71. _channelSessionMock.InSequence(sequence)
  72. .Setup(
  73. p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(_fileContent.Take(_bufferSize))), 0, _bufferSize));
  74. _channelSessionMock.InSequence(sequence)
  75. .Setup(
  76. p => p.SendData(It.Is<byte[]>(b => b.Take(0, _fileContent.Length - _bufferSize).SequenceEqual(_fileContent.Take(_bufferSize, _fileContent.Length - _bufferSize))), 0, _fileContent.Length - _bufferSize));
  77. _channelSessionMock.InSequence(sequence)
  78. .Setup(
  79. p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(new byte[] {0}))));
  80. _pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
  81. _channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
  82. _pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());
  83. // On .NET Core, Dispose() in turn invokes Close() and since we're not mocking
  84. // an interface, we need to expect this call as well
  85. _pipeStreamMock.Setup(p => p.Close());
  86. }
  87. protected override void Arrange()
  88. {
  89. base.Arrange();
  90. _scpClient = new ScpClient(_connectionInfo, false, _serviceFactoryMock.Object)
  91. {
  92. BufferSize = (uint) _bufferSize
  93. };
  94. _scpClient.Uploading += (sender, args) => _uploadingRegister.Add(args);
  95. _scpClient.Connect();
  96. }
  97. protected override void TearDown()
  98. {
  99. base.TearDown();
  100. if (_fileName != null)
  101. {
  102. File.Delete(_fileName);
  103. _fileName = null;
  104. }
  105. }
  106. protected override void Act()
  107. {
  108. _scpClient.Upload(_fileInfo, _remotePath);
  109. }
  110. [TestMethod]
  111. public void SendExecRequestOnChannelSessionShouldBeInvokedOnce()
  112. {
  113. _channelSessionMock.Verify(p => p.SendExecRequest(string.Format("scp -t -d {0}", _transformedPath)), Times.Once);
  114. }
  115. [TestMethod]
  116. public void DisposeOnChannelShouldBeInvokedOnce()
  117. {
  118. _channelSessionMock.Verify(p => p.Dispose(), Times.Once);
  119. }
  120. [TestMethod]
  121. public void DisposeOnPipeStreamShouldBeInvokedOnce()
  122. {
  123. _pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once);
  124. }
  125. [TestMethod]
  126. public void UploadingShouldHaveFiredTwice()
  127. {
  128. Assert.AreEqual(2, _uploadingRegister.Count);
  129. var uploading = _uploadingRegister[0];
  130. Assert.IsNotNull(uploading);
  131. Assert.AreSame(_fileInfo.Name, uploading.Filename);
  132. Assert.AreEqual(_fileSize, uploading.Size);
  133. Assert.AreEqual(_bufferSize, uploading.Uploaded);
  134. uploading = _uploadingRegister[1];
  135. Assert.IsNotNull(uploading);
  136. Assert.AreSame(_fileInfo.Name, uploading.Filename);
  137. Assert.AreEqual(_fileSize, uploading.Size);
  138. Assert.AreEqual(_fileSize, uploading.Uploaded);
  139. }
  140. private static IEnumerable<byte> CreateData(string command, Encoding encoding)
  141. {
  142. return encoding.GetBytes(command);
  143. }
  144. private static byte[] CreateContent(int length)
  145. {
  146. var random = new Random();
  147. var content = new byte[length];
  148. for (var i = 0; i < length; i++)
  149. content[i] = (byte) random.Next(byte.MinValue, byte.MaxValue);
  150. return content;
  151. }
  152. private static string CreateTemporaryFile(byte[] content)
  153. {
  154. var tempFile = Path.GetTempFileName();
  155. using (var fs = File.OpenWrite(tempFile))
  156. {
  157. fs.Write(content, 0, content.Length);
  158. }
  159. return tempFile;
  160. }
  161. }
  162. }