示例#1
0
        /// <summary>
        /// Performs SSH_FXP_OPENDIR request
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
        /// <returns>File handle.</returns>
        public byte[] RequestOpenDir(string path, bool nullOnError = false)
        {
            SshException exception = null;

            byte[] handle = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpOpenDirRequest(this.ProtocolVersion, this.NextRequestId, path, this.Encoding,
                                                     (response) =>
                {
                    handle = response.Handle;
                    wait.Set();
                },
                                                     (response) =>
                {
                    exception = this.GetSftpException(response);
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(handle);
        }
示例#2
0
        /// <summary>
        /// Performs SSH_FXP_READDIR request
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <returns></returns>
        public KeyValuePair <string, SftpFileAttributes>[] RequestReadDir(byte[] handle)
        {
            SshException exception = null;

            KeyValuePair <string, SftpFileAttributes>[] result = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadDirRequest(this.ProtocolVersion, this.NextRequestId, handle,
                                                     (response) =>
                {
                    result = response.Files;
                    wait.Set();
                },
                                                     (response) =>
                {
                    if (response.StatusCode != StatusCodes.Eof)
                    {
                        exception = this.GetSftpException(response);
                    }
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        public void RequestWrite(byte[] handle, UInt64 offset, byte[] data, EventWaitHandle wait, Action <SftpStatusResponse> writeCompleted = null)
        {
            SshException exception = null;

            var request = new SftpWriteRequest(this.ProtocolVersion, this.NextRequestId, handle, offset, data,
                                               (response) =>
            {
                if (writeCompleted != null)
                {
                    writeCompleted(response);
                }

                exception = this.GetSftpException(response);
                if (wait != null)
                {
                    wait.Set();
                }
            });

            this.SendRequest(request);

            if (wait != null)
            {
                this.WaitHandle(wait, this._operationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }
        }
示例#4
0
        /// <summary>
        /// Performs SSH_FXP_FSTAT request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
        /// <returns>
        /// File attributes
        /// </returns>
        public SftpFileAttributes RequestFStat(byte[] handle, bool nullOnError = false)
        {
            SshException       exception  = null;
            SftpFileAttributes attributes = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpFStatRequest(this.ProtocolVersion, this.NextRequestId, handle,
                                                   (response) =>
                {
                    attributes = response.Attributes;
                    wait.Set();
                },
                                                   (response) =>
                {
                    exception = this.GetSftpException(response);
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(attributes);
        }
示例#5
0
        /// <summary>
        /// Performs [email protected] extended request.
        /// </summary>
        /// <param name="oldPath">The old path.</param>
        /// <param name="newPath">The new path.</param>
        internal void HardLink(string oldPath, string newPath)
        {
            if (this.ProtocolVersion < 3)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "SSH_FXP_EXTENDED operation is not supported in {0} version that server operates in.", this.ProtocolVersion));
            }

            SshException exception = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new HardLinkRequest(this.ProtocolVersion, this.NextRequestId, oldPath, newPath,
                                                  (response) =>
                {
                    exception = this.GetSftpException(response);
                    wait.Set();
                });

                if (!this._supportedExtensions.ContainsKey(request.Name))
                {
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Extension method {0} currently not supported by the server.", request.Name));
                }

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }
        }
示例#6
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>data array; null if EOF</returns>
        public byte[] RequestRead(byte[] handle, UInt64 offset, UInt32 length)
        {
            SshException exception = null;

            byte[] data = new byte[0];

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadRequest(this.ProtocolVersion, this.NextRequestId, handle, offset, length,
                                                  (response) =>
                {
                    data = response.Data;
                    wait.Set();
                },
                                                  (response) =>
                {
                    if (response.StatusCode != StatusCodes.Eof)
                    {
                        exception = this.GetSftpException(response);
                    }
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(data);
        }
示例#7
0
        /// <summary>
        /// Performs SSH_FXP_SYMLINK request.
        /// </summary>
        /// <param name="linkpath">The linkpath.</param>
        /// <param name="targetpath">The targetpath.</param>
        public void RequestSymLink(string linkpath, string targetpath)
        {
            if (ProtocolVersion < 3)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "SSH_FXP_SYMLINK operation is not supported in {0} version that server operates in.", ProtocolVersion));
            }

            SshException exception = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpSymLinkRequest(ProtocolVersion, NextRequestId, linkpath, targetpath, Encoding,
                                                     response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }
        }
示例#8
0
        /// <summary>
        /// Performs [email protected] extended request.
        /// </summary>
        /// <param name="oldPath">The old path.</param>
        /// <param name="newPath">The new path.</param>
        public void RequestPosixRename(string oldPath, string newPath)
        {
            if (ProtocolVersion < 3)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "SSH_FXP_EXTENDED operation is not supported in {0} version that server operates in.", ProtocolVersion));
            }

            SshException exception = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new PosixRenameRequest(ProtocolVersion, NextRequestId, oldPath, newPath, Encoding,
                                                     response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                if (!_supportedExtensions.ContainsKey(request.Name))
                {
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Extension method {0} currently not supported by the server.", request.Name));
                }

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }
        }
示例#9
0
        /// <summary>
        /// Performs SSH_FXP_REALPATH request
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
        /// <returns></returns>
        internal KeyValuePair <string, SftpFileAttributes>[] RequestRealPath(string path, bool nullOnError = false)
        {
            SshException exception = null;

            KeyValuePair <string, SftpFileAttributes>[] result = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpRealPathRequest(ProtocolVersion, NextRequestId, path, Encoding,
                                                      response =>
                {
                    result = response.Files;
                    wait.Set();
                },
                                                      response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(result);
        }
示例#10
0
        /// <summary>
        /// Performs SSH_FXP_STAT request.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
        /// <returns>
        /// File attributes
        /// </returns>
        internal SftpFileAttributes RequestStat(string path, bool nullOnError = false)
        {
            SshException exception = null;

            SftpFileAttributes attributes = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpStatRequest(ProtocolVersion, NextRequestId, path, Encoding,
                                                  response =>
                {
                    attributes = response.Attributes;
                    wait.Set();
                },
                                                  response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(attributes);
        }
示例#11
0
        /// <summary>
        /// Performs SSH_FXP_FSTAT request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <returns>
        /// File attributes
        /// </returns>
        public SftpFileAttributes RequestFStat(byte[] handle)
        {
            SshException       exception  = null;
            SftpFileAttributes attributes = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpFStatRequest(ProtocolVersion, NextRequestId, handle,
                                                   response =>
                {
                    attributes = response.Attributes;
                    wait.Set();
                },
                                                   response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(attributes);
        }
示例#12
0
        /// <summary>
        /// Performs SSH_FXP_OPEN request
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="flags">The flags.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns <c>null</c> instead of throwing an exception.</param>
        /// <returns>File handle.</returns>
        public byte[] RequestOpen(string path, Flags flags, bool nullOnError = false)
        {
            byte[]       handle    = null;
            SshException exception = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpOpenRequest(ProtocolVersion, NextRequestId, path, Encoding, flags,
                                                  response =>
                {
                    handle = response.Handle;
                    wait.Set();
                },
                                                  response =>
                {
                    exception = GetSftpException(response);
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(handle);
        }
示例#13
0
        public void SshExceptionConstructorTest1()
        {
            string       message = string.Empty; // TODO: Initialize to an appropriate value
            SshException target  = new SshException(message);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
示例#14
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>data array; null if EOF</returns>
        public byte[] RequestRead(byte[] handle, ulong offset, uint length)
        {
            SshException exception = null;

            var data = new byte[0];

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadRequest(ProtocolVersion, NextRequestId, handle, offset, length,
                                                  response =>
                {
                    data = response.Data;
                    wait.Set();
                },
                                                  response =>
                {
                    if (response.StatusCode != StatusCodes.Eof)
                    {
                        exception = GetSftpException(response);
                    }
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(data);
        }
示例#15
0
        /// <summary>
        /// Performs SSH_FXP_REALPATH request
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="nullOnError">if set to <c>true</c> returns null instead of throwing an exception.</param>
        /// <returns></returns>
        public KeyValuePair <string, SftpFileAttributes>[] RequestRealPath(string path, bool nullOnError = false)
        {
            SshException exception = null;

            KeyValuePair <string, SftpFileAttributes>[] result = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpRealPathRequest(this.ProtocolVersion, this.NextRequestId, path, this.Encoding,
                                                      (response) =>
                {
                    result = response.Files;
                    wait.Set();
                },
                                                      (response) =>
                {
                    exception = this.GetSftpException(response);
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            if (!nullOnError && exception != null)
            {
                throw exception;
            }

            return(result);
        }
示例#16
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        /// <param name="writeCompleted">The callback to invoke when the write has completed.</param>
        public void RequestWrite(byte[] handle, ulong offset, byte[] data, AutoResetEvent wait, Action <SftpStatusResponse> writeCompleted = null)
        {
            SshException exception = null;

            var request = new SftpWriteRequest(ProtocolVersion, NextRequestId, handle, offset, data,
                                               response =>
            {
                if (writeCompleted != null)
                {
                    writeCompleted(response);
                }

                exception = GetSftpException(response);
                if (wait != null)
                {
                    wait.Set();
                }
            });

            SendRequest(request);

            if (wait != null)
            {
                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }
        }
        private void Arrange()
        {
            var random = new Random();

            _localChannelNumber           = (uint)random.Next(0, int.MaxValue);
            _localWindowSize              = (uint)random.Next(2000, 3000);
            _localPacketSize              = (uint)random.Next(1000, 2000);
            _initialSessionSemaphoreCount = random.Next(10, 20);
            _sessionSemaphore             = new SemaphoreLight(_initialSessionSemaphoreCount);
            _channelClosedRegister        = new List <ChannelEventArgs>();
            _channelExceptionRegister     = new List <ExceptionEventArgs>();
            _actualException              = null;

            _failureReasonCode  = (uint)random.Next(0, int.MaxValue);
            _failureDescription = random.Next().ToString(CultureInfo.InvariantCulture);
            _failureLanguage    = random.Next().ToString(CultureInfo.InvariantCulture);

            _sessionMock        = new Mock <ISession>(MockBehavior.Strict);
            _connectionInfoMock = new Mock <IConnectionInfo>(MockBehavior.Strict);

            var sequence = new MockSequence();

            _sessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
            _connectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(1);
            _sessionMock.Setup(p => p.SessionSemaphore).Returns(_sessionSemaphore);
            _sessionMock.InSequence(sequence)
            .Setup(
                p =>
                p.SendMessage(
                    It.Is <ChannelOpenMessage>(
                        m =>
                        m.LocalChannelNumber == _localChannelNumber &&
                        m.InitialWindowSize == _localWindowSize && m.MaximumPacketSize == _localPacketSize &&
                        m.Info is SessionChannelOpenInfo)));
            _sessionMock.InSequence(sequence)
            .Setup(p => p.WaitOnHandle(It.IsNotNull <WaitHandle>()))
            .Callback <WaitHandle>(
                w =>
            {
                _sessionMock.Raise(
                    s => s.ChannelOpenFailureReceived += null,
                    new MessageEventArgs <ChannelOpenFailureMessage>(
                        new ChannelOpenFailureMessage(
                            _localChannelNumber,
                            _failureDescription,
                            _failureReasonCode,
                            _failureLanguage
                            )));
                w.WaitOne();
            });
            _sessionMock.InSequence(sequence).Setup(p => p.ConnectionInfo).Returns(_connectionInfoMock.Object);
            _connectionInfoMock.InSequence(sequence).Setup(p => p.RetryAttempts).Returns(1);

            _channel            = new ChannelSession(_sessionMock.Object, _localChannelNumber, _localWindowSize, _localPacketSize);
            _channel.Closed    += (sender, args) => _channelClosedRegister.Add(args);
            _channel.Exception += (sender, args) => _channelExceptionRegister.Add(args);
        }
示例#18
0
        [Ignore] // placeholder for actual test
        public void GetObjectDataTest()
        {
            SshException      target  = new SshException();     // TODO: Initialize to an appropriate value
            SerializationInfo info    = null;                   // TODO: Initialize to an appropriate value
            StreamingContext  context = new StreamingContext(); // TODO: Initialize to an appropriate value

            target.GetObjectData(info, context);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
 protected override void Act()
 {
     try
     {
         _channel.Open();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
 private void Act()
 {
     try
     {
         _channel.Open();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
示例#21
0
 protected override void Act()
 {
     try
     {
         _scpClient.Download(_path, _destination);
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
 protected virtual void Act()
 {
     try
     {
         _scpClient.Upload(_fileInfo, _path);
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
示例#23
0
 protected override void Act()
 {
     try
     {
         _reader.Read();
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
示例#24
0
        protected override void SetupData()
        {
            var random = new Random();

            _handle           = CreateByteArray(random, 5);
            _fileSize         = random.Next();
            _waitHandleArray  = new WaitHandle[2];
            _operationTimeout = random.Next(10000, 20000);
            _closeAsyncResult = new SftpCloseAsyncResult(null, null);

            _exception = new SshException();
        }
 protected override void Act()
 {
     try
     {
         _scpClient.Upload(_directoryInfo, _path);
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
示例#26
0
 protected override void Act()
 {
     try
     {
         _scpClient.Upload(_source, _remotePath);
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
 protected void Act()
 {
     try
     {
         _subsystemSession.Connect();
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }
        private void SetupData()
        {
            var random = new Random();

            _terminalName       = random.Next().ToString();
            _columns            = (uint)random.Next();
            _rows               = (uint)random.Next();
            _width              = (uint)random.Next();
            _height             = (uint)random.Next();
            _terminalModeValues = new Dictionary <TerminalModes, uint>();
            _bufferSize         = random.Next();
            _actualException    = null;
        }
示例#29
0
        protected override void SetupData()
        {
            var random = new Random();

            _handle           = CreateByteArray(random, 5);
            _chunk1           = CreateByteArray(random, ChunkLength);
            _chunk2           = CreateByteArray(random, ChunkLength);
            _fileSize         = _chunk1.Length + _chunk2.Length + 1;
            _waitHandleArray  = new WaitHandle[2];
            _operationTimeout = random.Next(10000, 20000);
            _closeAsyncResult = new SftpCloseAsyncResult(null, null);

            _exception         = new SshException();
            _exceptionSignaled = new ManualResetEvent(false);
        }
示例#30
0
 protected override void Act()
 {
     // wait for the exception to be signaled by the second call to WaitAny
     _exceptionSignaled.WaitOne(5000);
     // allow a little time to allow SftpFileReader to process exception
     Thread.Sleep(100);
     try
     {
         _reader.Read();
         Assert.Fail();
     }
     catch (SshException ex)
     {
         _actualException = ex;
     }
 }