Exemple #1
0
        /// <summary>
        /// Reads all data left in internal buffer at current position.
        /// </summary>
        /// <returns>An array of bytes containing the remaining data in the internal buffer.</returns>
        protected byte[] ReadBytes()
        {
            var bytesLength = (int)(_stream.Length - _stream.Position);
            var data        = new byte[bytesLength];

            _stream.Read(data, 0, bytesLength);
            return(data);
        }
        /// <summary>
        /// Reads all data left in internal buffer at current position.
        /// </summary>
        /// <returns>An array of bytes containing the remaining data in the internal buffer.</returns>
        protected byte[] ReadBytes()
        {
#if TUNING
            var bytesLength = (int)(_stream.Length - _stream.Position);
            var data        = new byte[bytesLength];
            _stream.Read(data, 0, bytesLength);
            return(data);
#else
            var data = new byte[_data.Count - _readerIndex];
            _data.CopyTo(_readerIndex, data, 0, data.Length);
            return(data);
#endif
        }
        public void GetBytes()
        {
            var request = new SftpFSetStatRequest(_protocolVersion, _requestId, _handle, _attributes, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Handle length
            expectedBytesLength += _handle.Length; // Handle
            expectedBytesLength += _attributesBytes.Length; // Attributes

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new SftpCloseRequest(_protocolVersion, _requestId, _handle, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
#if TUNING
            expectedBytesLength += 4; // Length
#endif
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Handle length
            expectedBytesLength += _handle.Length; // Handle

            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.Close, sshDataStream.ReadByte());
            Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new IgnoreMessage(_data);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // Data length
            expectedBytesLength += _data.Length; // Data

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(IgnoreMessage.MessageNumber, sshDataStream.ReadByte());
            Assert.AreEqual((uint) _data.Length, sshDataStream.ReadUInt32());

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new SftpOpenDirRequest(_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.OpenDir, 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);
        }
        public void GetBytes()
        {
            var statusActionInvocations = new List<SftpStatusResponse>();
            var nameActionInvocations = new List<SftpNameResponse>();
            Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
            var request = new SftpRmDirRequest(_protocolVersion, _requestId, _path, _encoding, 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.RmDir, 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);
        }
        public void GetBytes()
        {
            var request = new PosixRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Name length
            expectedBytesLength += _nameBytes.Length; // Name
            expectedBytesLength += 4; // OldPath length
            expectedBytesLength += _oldPathBytes.Length; // OldPath
            expectedBytesLength += 4; // NewPath length
            expectedBytesLength += _newPathBytes.Length; // NewPath

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

            Assert.AreEqual((uint) _oldPathBytes.Length, sshDataStream.ReadUInt32());

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

            Assert.AreEqual((uint) _newPathBytes.Length, sshDataStream.ReadUInt32());

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var localChannelNumber = (uint)_random.Next(0, int.MaxValue);
            var initialWindowSize = (uint)_random.Next(0, int.MaxValue);
            var maximumPacketSize = (uint)_random.Next(0, int.MaxValue);
            var info = new DirectTcpipChannelInfo("host", 22, "originator", 25);
            var infoBytes = info.GetBytes();
            var target = new ChannelOpenMessage(localChannelNumber, initialWindowSize, maximumPacketSize, info);

            var bytes = target.GetBytes();

            var expectedBytesLength = 1; // Type
            expectedBytesLength += 4; // ChannelType length
            expectedBytesLength += target.ChannelType.Length; // ChannelType
            expectedBytesLength += 4; // LocalChannelNumber
            expectedBytesLength += 4; // InitialWindowSize
            expectedBytesLength += 4; // MaximumPacketSize
            expectedBytesLength += infoBytes.Length; // Info

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(ChannelOpenMessage.MessageNumber, sshDataStream.ReadByte());

            var actualChannelTypeLength = sshDataStream.ReadUInt32();
            Assert.AreEqual((uint) target.ChannelType.Length, actualChannelTypeLength);

            var actualChannelType = new byte[actualChannelTypeLength];
            sshDataStream.Read(actualChannelType, 0, (int) actualChannelTypeLength);
            Assert.IsTrue(target.ChannelType.SequenceEqual(actualChannelType));

            Assert.AreEqual(localChannelNumber, sshDataStream.ReadUInt32());
            Assert.AreEqual(initialWindowSize, sshDataStream.ReadUInt32());
            Assert.AreEqual(maximumPacketSize, sshDataStream.ReadUInt32());

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        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);
        }
        public void GetBytes()
        {
            var request = new SftpRemoveRequest(_protocolVersion, _requestId, _filename, _encoding, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Filename length
            expectedBytesLength += _filenameBytes.Length; // Filename

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new SftpLinkRequest(_protocolVersion, _requestId, _newLinkPath, _existingPath, true, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // NewLinkPath length
            expectedBytesLength += _newLinkPathBytes.Length; // NewLinkPath
            expectedBytesLength += 4; // ExistingPath length
            expectedBytesLength += _existingPathBytes.Length; // ExistingPath
            expectedBytesLength += 1; // IsSymLink

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

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

            Assert.AreEqual(1, sshDataStream.ReadByte());

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new SftpBlockRequest(_protocolVersion, _requestId, _handle, _offset, _length, _lockMask, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Handle length
            expectedBytesLength += _handle.Length; // Handle
            expectedBytesLength += 8; // Offset
            expectedBytesLength += 8; // Length
            expectedBytesLength += 4; // LockMask

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

            Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
            Assert.AreEqual(_length, sshDataStream.ReadUInt64());
            Assert.AreEqual(_lockMask, sshDataStream.ReadUInt32());
            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var random = new Random();

            var localChannelNumber = (uint) random.Next(0, int.MaxValue);
            var data = new byte[random.Next(10, 20)];
            random.NextBytes(data);
            var offset = random.Next(2, 4);
            var size = random.Next(5, 9);

            var target = new ChannelDataMessage(localChannelNumber, data, offset, size);

            var bytes = target.GetBytes();

            var expectedBytesLength = 1; // Type
            expectedBytesLength += 4; // LocalChannelNumber
            expectedBytesLength += 4; // Data length
            expectedBytesLength += size; // Data

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(ChannelDataMessage.MessageNumber, sshDataStream.ReadByte());
            Assert.AreEqual(localChannelNumber, sshDataStream.ReadUInt32());
            Assert.AreEqual((uint) size, sshDataStream.ReadUInt32());

            var actualData = new byte[size];
            sshDataStream.Read(actualData, 0, size);
            Assert.IsTrue(actualData.SequenceEqual(data.Take(offset, size)));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        public void GetBytes()
        {
            var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _serverFileOffset, _data, _offset, _length, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Handle length
            expectedBytesLength += _handle.Length; // Handle
            expectedBytesLength += 8; // ServerFileOffset
            expectedBytesLength += 4; // Data length
            expectedBytesLength += _length; // Data

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

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

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

            Assert.AreEqual(_serverFileOffset, sshDataStream.ReadUInt64());

            Assert.AreEqual((uint) _length, sshDataStream.ReadUInt32());
            var actualData = new byte[_length];
            sshDataStream.Read(actualData, 0, actualData.Length);
            Assert.IsTrue(_data.Take(_offset, _length).SequenceEqual(actualData));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }