Inheritance: SftpRequest
        public void Constructor()
        {
            var request = new SftpOpenRequest(_protocolVersion, _requestId, _filename, _encoding, _flags, null, null);

            Assert.AreSame(_encoding, request.Encoding);
            Assert.AreEqual(_filename, request.Filename);
            Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
            Assert.AreEqual(_requestId, request.RequestId);
            Assert.AreEqual(SftpMessageTypes.Open, request.SftpMessageType);
        }
        public void Complete_SftpHandleResponse()
        {
            var statusActionInvocations = new List<SftpStatusResponse>();
            var handleActionInvocations = new List<SftpHandleResponse>();

            Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
            Action<SftpHandleResponse> handleAction = handleActionInvocations.Add;
            var handleResponse = new SftpHandleResponse(_protocolVersion);

            var request = new SftpOpenRequest(
                _protocolVersion,
                _requestId,
                _filename,
                _encoding,
                _flags,
                handleAction,
                statusAction);

            request.Complete(handleResponse);

            Assert.AreEqual(0, statusActionInvocations.Count);
            Assert.AreEqual(1, handleActionInvocations.Count);
            Assert.AreSame(handleResponse, handleActionInvocations[0]);
        }
Beispiel #3
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 null instead of throwing an exception.</param>
        /// <returns>File handle.</returns>
        internal 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(this.ProtocolVersion, this.NextRequestId, path, this.Encoding, flags,
                    (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;
        }
        //#define SSH_FXP_INIT                1
        //#define SSH_FXP_VERSION             2
        /// <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 null instead of throwing an exception.</param>
        /// <returns></returns>
        internal byte[] RequestOpen(string path, Flags flags, bool nullOnError = false)
        {
            byte[] handle = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpOpenRequest(this.NextRequestId, path, flags,
                    (response) =>
                    {
                        handle = response.Handle;
                        wait.Set();
                    },
                    (response) =>
                    {
                        if (nullOnError)
                        {
                            wait.Set();
                        }
                        else
                        {
                            this.ThrowSftpException(response);
                        }
                    });

                this.SendRequest(request);

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

            return handle;
        }
Beispiel #5
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;
        }
        public void GetBytes()
        {
            var request = new SftpOpenRequest(_protocolVersion, _requestId, _filename, _encoding, _flags, null, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
#if TUNING
            expectedBytesLength += 4; // Length
#endif
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Filename length
            expectedBytesLength += _filenameBytes.Length; // Filename
            expectedBytesLength += 4; // Flags
            expectedBytesLength += _attributesBytes.Length; // Attributes

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

#if TUNING
            Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
#endif
            Assert.AreEqual((byte)SftpMessageTypes.Open, sshDataStream.ReadByte());
            Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());

            Assert.AreEqual((uint)_filenameBytes.Length, sshDataStream.ReadUInt32());
            var actualPath = new byte[_filenameBytes.Length];
            sshDataStream.Read(actualPath, 0, actualPath.Length);
            Assert.IsTrue(_filenameBytes.SequenceEqual(actualPath));

            Assert.AreEqual((uint) _flags, sshDataStream.ReadUInt32());

            var actualAttributes = new byte[_attributesBytes.Length];
            sshDataStream.Read(actualAttributes, 0, actualAttributes.Length);
            Assert.IsTrue(_attributesBytes.SequenceEqual(actualAttributes));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }