public void GetBytes()
        {
            var request = new SftpReadLinkRequest(_protocolVersion, _requestId, _path, _encoding, null, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;

            expectedBytesLength += 4;                 // Length
            expectedBytesLength += 1;                 // Type
            expectedBytesLength += 4;                 // RequestId
            expectedBytesLength += 4;                 // Path length
            expectedBytesLength += _pathBytes.Length; // Path

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
            Assert.AreEqual((byte)SftpMessageTypes.ReadLink, sshDataStream.ReadByte());
            Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());

            Assert.AreEqual((uint)_pathBytes.Length, sshDataStream.ReadUInt32());
            var actualPath = new byte[_pathBytes.Length];

            sshDataStream.Read(actualPath, 0, actualPath.Length);
            Assert.IsTrue(_pathBytes.SequenceEqual(actualPath));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Example #2
0
        /// <summary>
        /// Performs SSH_FXP_READLINK 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>[] RequestReadLink(string path, bool nullOnError = false)
        {
            KeyValuePair <string, SftpFileAttributes>[] result = null;

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadLinkRequest(this.NextRequestId, path,
                                                      (response) =>
                {
                    result = response.Files;

                    wait.Set();
                },
                                                      (response) =>
                {
                    if (nullOnError)
                    {
                        wait.Set();
                    }
                    else
                    {
                        this.ThrowSftpException(response);
                    }
                });

                this.SendRequest(request);

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

            return(result);
        }
        public void Constructor()
        {
            var request = new SftpReadLinkRequest(_protocolVersion, _requestId, _path, _encoding, null, null);

            Assert.AreSame(_encoding, request.Encoding);
            Assert.AreEqual(_path, request.Path);
            Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
            Assert.AreEqual(_requestId, request.RequestId);
            Assert.AreEqual(SftpMessageTypes.ReadLink, request.SftpMessageType);
        }
Example #4
0
        /// <summary>
        /// Performs SSH_FXP_READLINK 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>[] RequestReadLink(string path, bool nullOnError = false)
        {
            if (this.ProtocolVersion < 3)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "SSH_FXP_READLINK operation is not supported in {0} version that server operates in.", this.ProtocolVersion));
            }

            SshException exception = null;

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

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadLinkRequest(this.ProtocolVersion, this.NextRequestId, path, this.Encoding,
                                                      (response) =>
                {
                    result = response.Files;

                    wait.Set();
                },
                                                      (response) =>
                {
                    exception = this.GetSftpException(response);
                    wait.Set();
                });

                this.SendRequest(request);

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

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

            return(result);
        }
        public void Complete_SftpStatusResponse()
        {
            var statusActionInvocations = new List <SftpStatusResponse>();
            var nameActionInvocations   = new List <SftpNameResponse>();

            Action <SftpStatusResponse> statusAction = statusActionInvocations.Add;
            Action <SftpNameResponse>   nameAction   = nameActionInvocations.Add;
            var statusResponse = new SftpStatusResponse(_protocolVersion);

            var request = new SftpReadLinkRequest(
                _protocolVersion,
                _requestId,
                _path,
                _encoding,
                nameAction,
                statusAction);

            request.Complete(statusResponse);

            Assert.AreEqual(1, statusActionInvocations.Count);
            Assert.AreSame(statusResponse, statusActionInvocations[0]);
            Assert.AreEqual(0, nameActionInvocations.Count);
        }