Specialized MemoryStream for reading and writing data SSH data.
Наследование: System.IO.MemoryStream
Пример #1
0
        public void GetBytes()
        {
            var target = new PseudoTerminalRequestInfo(_environmentVariable, _columns, _rows, _width, _height, _terminalModeValues);

            var bytes = target.GetBytes();

            var expectedBytesLength = 1; // WantReply
            expectedBytesLength += 4; // EnvironmentVariable length
            expectedBytesLength += _environmentVariableBytes.Length; // EnvironmentVariable
            expectedBytesLength += 4; // Columns
            expectedBytesLength += 4; // Rows
            expectedBytesLength += 4; // PixelWidth
            expectedBytesLength += 4; // PixelHeight
            expectedBytesLength += 4; // Length of "encoded terminal modes"
            expectedBytesLength += _terminalModeValues.Count*(1 + 4) + 1; // encoded terminal modes

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(1, sshDataStream.ReadByte()); // WantReply
            Assert.AreEqual(_environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
            Assert.AreEqual(_columns, sshDataStream.ReadUInt32());
            Assert.AreEqual(_rows, sshDataStream.ReadUInt32());
            Assert.AreEqual(_width, sshDataStream.ReadUInt32());
            Assert.AreEqual(_height, sshDataStream.ReadUInt32());
            Assert.AreEqual((uint) (_terminalModeValues.Count * (1 + 4) + 1), sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.CS8, sshDataStream.ReadByte());
            Assert.AreEqual(_terminalModeValues[TerminalModes.CS8], sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.ECHO, sshDataStream.ReadByte());
            Assert.AreEqual(_terminalModeValues[TerminalModes.ECHO], sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.TTY_OP_END, sshDataStream.ReadByte());

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Пример #2
0
        public void GetBytes()
        {
            var request = new SftpMkDirRequest(_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
            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.MkDir, 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));

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

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Пример #3
0
        public void GetBytes()
        {
            var request = new SftpRemoveRequest(_protocolVersion, _requestId, _filename, _encoding, 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

            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.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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        public void Load()
        {
            var target = new SftpAttrsResponse(_protocolVersion);
            var attributes = CreateSftpFileAttributes();
            var attributesBytes = attributes.GetBytes();

            var sshDataStream = new SshDataStream(4 + 1 + 4 + attributesBytes.Length);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte) SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(attributesBytes, 0, attributesBytes.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Attributes);
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Attrs, target.SftpMessageType);

            // check attributes in detail
            Assert.AreEqual(attributes.GroupId, target.Attributes.GroupId);
            Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
            Assert.AreEqual(attributes.LastWriteTime, target.Attributes.LastWriteTime);
            Assert.AreEqual(attributes.UserId, target.Attributes.UserId);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        public void GetBytes()
        {
            var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 4; // Length
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            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.Rename, sshDataStream.ReadByte());
            Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());

            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);
        }
Пример #9
0
        /// <summary>
        /// Writes the current message to the specified <see cref="SshDataStream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>
        protected override void WriteBytes(SshDataStream stream)
        {
            const int sizeOfDataLengthBytes = 4;

            var startPosition = stream.Position;

            // skip 4 bytes for the length of the SFTP message data
            stream.Seek(sizeOfDataLengthBytes, SeekOrigin.Current);

            // write the SFTP message data to the stream
            base.WriteBytes(stream);

            // save where we were positioned when we finished writing the SSH message data
            var endPosition = stream.Position;

            // determine the length of the SSH message data
            var dataLength = endPosition - startPosition - sizeOfDataLengthBytes;

            // write the length of the SFTP message where we were positioned before we started
            // writing the SFTP message data
            stream.Position = startPosition;
            stream.Write((uint) dataLength);

            // move back to we were positioned when we finished writing the SFTP message data
            stream.Position = endPosition;
        }
 public byte[] Build()
 {
     var sshDataStream = new SshDataStream(0);
     sshDataStream.WriteByte(KeyExchangeDhGroupExchangeGroup.MessageNumber);
     sshDataStream.Write(_safePrime);
     sshDataStream.Write(_subGroup);
     return sshDataStream.ToArray();
 }
Пример #11
0
        /// <summary>
        /// Loads data bytes into internal buffer.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The zero-based offset in <paramref name="bytes"/> at which to begin reading SSH data.</param>
        /// <param name="count">The number of bytes to load.</param>
        private void LoadBytes(byte[] bytes, int offset, int count)
        {
            _loadedData = bytes;
            _offset     = offset;
            _count      = count;

            _stream = new SshDataStream(bytes, _offset, count);
        }
Пример #12
0
        /// <summary>
        /// Gets data bytes array.
        /// </summary>
        /// <returns>
        /// A <see cref="Byte"/> array representation of data structure.
        /// </returns>
        public byte[] GetBytes()
        {
            var messageLength = BufferCapacity;
            var capacity      = messageLength != -1 ? messageLength : DefaultCapacity;
            var dataStream    = new SshDataStream(capacity);

            WriteBytes(dataStream);
            return(dataStream.ToArray());
        }
 private byte[] BuildExtensions()
 {
     var sshDataStream = new SshDataStream(0);
     foreach (var extensionPair in _extensions)
     {
         sshDataStream.Write(extensionPair.Key, Encoding.ASCII);
         sshDataStream.Write(extensionPair.Value, Encoding.ASCII);
     }
     return sshDataStream.ToArray();
 }
Пример #14
0
        /// <summary>
        /// Writes the message to the specified <see cref="SshDataStream"/>.
        /// </summary>
        protected override void WriteBytes(SshDataStream stream)
        {
            var messageAttribute = GetType().GetCustomAttributes(typeof(MessageAttribute), true).FirstOrDefault() as MessageAttribute;

            if (messageAttribute == null)
                throw new SshException(string.Format(CultureInfo.CurrentCulture, "Type '{0}' is not a valid message type.", GetType().AssemblyQualifiedName));

            stream.WriteByte(messageAttribute.Number);
            base.WriteBytes(stream);
        }
        public byte[] Build()
        {
            var extensions = BuildExtensions();

            var sshDataStream = new SshDataStream(4 + 1 + 4 + extensions.Length);
            sshDataStream.Write((uint)sshDataStream.Capacity - 4);
            sshDataStream.WriteByte((byte)SftpMessageTypes.Version);
            sshDataStream.Write(_version);
            sshDataStream.Write(extensions, 0, extensions.Length);
            return sshDataStream.ToArray();
        }
        public KeyExchangeDhGroupExchangeReplyBuilder WithHostKey(string hostKeyAlgorithm, params BigInteger[] hostKeys)
        {
            _hostKeyAlgorithm = Encoding.UTF8.GetBytes(hostKeyAlgorithm);

            var sshDataStream = new SshDataStream(0);
            foreach (var hostKey in hostKeys)
                sshDataStream.Write(hostKey);
            _hostKeys = sshDataStream.ToArray();

            return this;
        }
        public void GetReply_StatVfsReplyInfo()
        {
            var bsize = (ulong) _random.Next(0, int.MaxValue);
            var frsize = (ulong) _random.Next(0, int.MaxValue);
            var blocks = (ulong) _random.Next(0, int.MaxValue);
            var bfree = (ulong) _random.Next(0, int.MaxValue);
            var bavail = (ulong) _random.Next(0, int.MaxValue);
            var files = (ulong) _random.Next(0, int.MaxValue);
            var ffree = (ulong) _random.Next(0, int.MaxValue);
            var favail = (ulong) _random.Next(0, int.MaxValue);
            var sid = (ulong) _random.Next(0, int.MaxValue);
            var namemax = (ulong) _random.Next(0, int.MaxValue);

#if TUNING
            var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
#else
            var sshDataStream = new SshDataStream(1 + 4 + 88);
#endif
            sshDataStream.WriteByte((byte) SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(bsize);
            sshDataStream.Write(frsize);
            sshDataStream.Write(blocks);
            sshDataStream.Write(bfree);
            sshDataStream.Write(bavail);
            sshDataStream.Write(files);
            sshDataStream.Write(ffree);
            sshDataStream.Write(favail);
            sshDataStream.Write(sid);
            sshDataStream.Write((ulong) 0x2);
            sshDataStream.Write(namemax);

            var target = new SftpExtendedReplyResponse(_protocolVersion);
            target.Load(sshDataStream.ToArray());

            var reply = target.GetReply<StatVfsReplyInfo>();
            Assert.IsNotNull(reply);

            var information = reply.Information;
            Assert.IsNotNull(information);
            Assert.AreEqual(bavail, information.AvailableBlocks);
            Assert.AreEqual(favail, information.AvailableNodes);
            Assert.AreEqual(frsize, information.BlockSize);
            Assert.AreEqual(bsize, information.FileSystemBlockSize);
            Assert.AreEqual(bfree, information.FreeBlocks);
            Assert.AreEqual(ffree, information.FreeNodes);
            Assert.IsFalse(information.IsReadOnly);
            Assert.AreEqual(namemax, information.MaxNameLenght);
            Assert.AreEqual(sid, information.Sid);
            Assert.IsFalse(information.SupportsSetUid);
            Assert.AreEqual(blocks, information.TotalBlocks);
            Assert.AreEqual(files, information.TotalNodes);
        }
 public byte[] Build()
 {
     var sshDataStream = new SshDataStream(0);
     sshDataStream.WriteByte(KeyExchangeDhGroupExchangeReply.MessageNumber);
     sshDataStream.Write((uint)(4 + _hostKeyAlgorithm.Length + _hostKeys.Length));
     sshDataStream.Write((uint) _hostKeyAlgorithm.Length);
     sshDataStream.Write(_hostKeyAlgorithm, 0, _hostKeyAlgorithm.Length);
     sshDataStream.Write(_hostKeys, 0, _hostKeys.Length);
     sshDataStream.Write(_f);
     sshDataStream.WriteBinary(_signature);
     return sshDataStream.ToArray();
 }
Пример #19
0
        /// <summary>
        /// Loads data bytes into internal buffer.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="offset">The zero-based offset in <paramref name="bytes"/> at which to begin reading SSH data.</param>
        /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
        protected void LoadBytes(byte[] bytes, int offset)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }

            _loadedData = bytes;
            _offset     = offset;

            _stream = new SshDataStream(bytes);
            ResetReader();
        }
        public void Load()
        {
            var target = new SftpExtendedReplyResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4);
            sshDataStream.Write(_responseId);

            target.Load(sshDataStream.ToArray());

            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.ExtendedReply, target.SftpMessageType);
        }
        public void Load()
        {
            var target = new SftpExtendedReplyResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + 1 + 4);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte) SftpMessageTypes.ExtendedReply);
            sshDataStream.Write(_responseId);

            target.Load(sshDataStream.ToArray());

            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.ExtendedReply, target.SftpMessageType);
        }
Пример #22
0
        byte[] GetBytes()
        {
#if TUNING
            var messageLength = BufferCapacity;
            var capacity      = messageLength != -1 ? messageLength : DefaultCapacity;
            var dataStream    = new SshDataStream(capacity);
            WriteBytes(dataStream);
            return(dataStream.ToArray());
#else
            _data = new List <byte>();

            SaveData();

            return(_data.ToArray());
#endif
        }
Пример #23
0
        public void Load_Data()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(8);
            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();
            request.Load(sshData);

            Assert.AreEqual(one, request.ValueOne);
            Assert.AreEqual(two, request.ValueTwo);
        }
Пример #24
0
        public void Load()
        {
            var target = new SftpHandleResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + _handle.Length);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint) _handle.Length);
            sshDataStream.Write(_handle, 0, _handle.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Handle);
            Assert.IsTrue(target.Handle.SequenceEqual(_handle));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Handle, target.SftpMessageType);
        }
Пример #25
0
        public void Load()
        {
            var target = new SftpDataResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + _data.Length);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint) _data.Length);
            sshDataStream.Write(_data, 0, _data.Length);

            var sshData = sshDataStream.ToArray();

            target.Load(sshData);

            Assert.IsNotNull(target.Data);
            Assert.IsTrue(target.Data.SequenceEqual(_data));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Data, target.SftpMessageType);
        }
Пример #26
0
        public void Load()
        {
            var target = new SftpDataResponse(_protocolVersion);

            var sshDataStream = new SshDataStream(4 + 1 + 4 + _data.Length);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
            sshDataStream.WriteByte((byte)SftpMessageTypes.Attrs);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint) _data.Length);
            sshDataStream.Write(_data, 0, _data.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Data);
            Assert.IsTrue(target.Data.SequenceEqual(_data));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Data, target.SftpMessageType);
        }
Пример #27
0
        public void Load()
        {
            var target = new StatVfsReplyInfo();

#if TUNING
            var sshDataStream = new SshDataStream(4 + 1 + 4 + 88);
            sshDataStream.Write((uint) (sshDataStream.Capacity - 4));
#else
            var sshDataStream = new SshDataStream(1 + 4 + 88);
#endif
            sshDataStream.WriteByte((byte) SftpMessageTypes.ExtendedReply);
            sshDataStream.Write(_responseId);
            sshDataStream.Write(_bsize);
            sshDataStream.Write(_frsize);
            sshDataStream.Write(_blocks);
            sshDataStream.Write(_bfree);
            sshDataStream.Write(_bavail);
            sshDataStream.Write(_files);
            sshDataStream.Write(_ffree);
            sshDataStream.Write(_favail);
            sshDataStream.Write(_sid);
            sshDataStream.Write((ulong) 0x1);
            sshDataStream.Write(_namemax);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Information);

            var information = target.Information;
            Assert.AreEqual(_bavail, information.AvailableBlocks);
            Assert.AreEqual(_favail, information.AvailableNodes);
            Assert.AreEqual(_frsize, information.BlockSize);
            Assert.AreEqual(_bsize, information.FileSystemBlockSize);
            Assert.AreEqual(_bfree, information.FreeBlocks);
            Assert.AreEqual(_ffree, information.FreeNodes);
            Assert.IsTrue(information.IsReadOnly);
            Assert.AreEqual(_namemax, information.MaxNameLenght);
            Assert.AreEqual(_sid, information.Sid);
            Assert.IsTrue(information.SupportsSetUid);
            Assert.AreEqual(_blocks, information.TotalBlocks);
            Assert.AreEqual(_files, information.TotalNodes);
        }
Пример #28
0
        public void Load_DataAndOffsetAndCount()
        {
            const uint one = 123456u;
            const uint two = 456789u;

            var sshDataStream = new SshDataStream(11);
            sshDataStream.WriteByte(0x05);
            sshDataStream.WriteByte(0x07);
            sshDataStream.WriteByte(0x0f);
            sshDataStream.Write(one);
            sshDataStream.Write(two);

            var sshData = sshDataStream.ToArray();

            var request = new RequestSshData();
            request.Load(sshData, 3, sshData.Length - 3);

            Assert.AreEqual(one, request.ValueOne);
            Assert.AreEqual(two, request.ValueTwo);
        }
Пример #29
0
        public void Load()
        {
            var target = new SftpHandleResponse(_protocolVersion);

#if TUNING
            var sshDataStream = new SshDataStream(4 + 1 + 4 + _handle.Length);
            sshDataStream.Position = 4; // skip 4 bytes for SSH packet length
#else
            var sshDataStream = new SshDataStream(1 + 4 + _handle.Length);
#endif
            sshDataStream.WriteByte((byte) SftpMessageTypes.Handle);
            sshDataStream.Write(_responseId);
            sshDataStream.Write((uint) _handle.Length);
            sshDataStream.Write(_handle, 0, _handle.Length);

            target.Load(sshDataStream.ToArray());

            Assert.IsNotNull(target.Handle);
            Assert.IsTrue(target.Handle.SequenceEqual(_handle));
            Assert.AreEqual(_protocolVersion, target.ProtocolVersion);
            Assert.AreEqual(_responseId, target.ResponseId);
            Assert.AreEqual(SftpMessageTypes.Handle, target.SftpMessageType);
        }
        public void Test_KeyExchangeDhGroupExchangeRequest_GetBytes()
        {
            var request = new KeyExchangeDhGroupExchangeRequest(_minimum, _preferred, _maximum);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // Minimum
            expectedBytesLength += 4; // Preferred
            expectedBytesLength += 4; // Maximum

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(KeyExchangeDhGroupExchangeRequest.MessageNumber, sshDataStream.ReadByte());
            Assert.AreEqual(_minimum, sshDataStream.ReadUInt32());
            Assert.AreEqual(_preferred, sshDataStream.ReadUInt32());
            Assert.AreEqual(_maximum, sshDataStream.ReadUInt32());

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Пример #31
0
        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);
        }
Пример #32
0
 /// <summary>
 /// Writes the current message to the specified <see cref="SshDataStream"/>.
 /// </summary>
 /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>
 protected virtual void WriteBytes(SshDataStream stream)
 {
     _stream = stream;
     SaveData();
 }
Пример #33
0
 private void LoadInternal(byte[] value, int offset, int count)
 {
     _stream = new SshDataStream(value, offset, count);
     LoadData();
 }
Пример #34
0
            public byte[] Build()
            {
                var serviceName = _serviceName.ToArray();

                var sshDataStream = new SshDataStream(4 + 1 + 1 + 4 + serviceName.Length);
                sshDataStream.Write((uint)(sshDataStream.Capacity - 4)); // packet length
                sshDataStream.WriteByte(0); // padding length
                sshDataStream.WriteByte(ServiceAcceptMessage.MessageNumber);
                sshDataStream.WriteBinary(serviceName);
                return sshDataStream.ToArray();
            }
Пример #35
0
        public void GetBytes()
        {
            var request = new SftpLStatRequest(_protocolVersion, _requestId, _path, _encoding, null, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;
#if TUNING
            expectedBytesLength += 4; // Length
#endif
            expectedBytesLength += 1; // Type
            expectedBytesLength += 4; // RequestId
            expectedBytesLength += 4; // Pah length
            expectedBytesLength += _pathBytes.Length; // Path

            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.LStat, 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);
        }
Пример #36
0
        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);
        }