internal static SftpFileAttributes FromBytes(SshDataStream stream) { var flag = stream.ReadUInt32(); long size = -1; var userId = -1; var groupId = -1; uint permissions = 0; var accessTime = DateTime.MinValue; var modifyTime = DateTime.MinValue; IDictionary<string, string> extensions = null; if ((flag & 0x00000001) == 0x00000001) // SSH_FILEXFER_ATTR_SIZE { size = (long) stream.ReadUInt64(); } if ((flag & 0x00000002) == 0x00000002) // SSH_FILEXFER_ATTR_UIDGID { userId = (int) stream.ReadUInt32(); groupId = (int) stream.ReadUInt32(); } if ((flag & 0x00000004) == 0x00000004) // SSH_FILEXFER_ATTR_PERMISSIONS { permissions = stream.ReadUInt32(); } if ((flag & 0x00000008) == 0x00000008) // SSH_FILEXFER_ATTR_ACMODTIME { var time = stream.ReadUInt32(); accessTime = DateTime.FromFileTime((time + 11644473600) * 10000000); time = stream.ReadUInt32(); modifyTime = DateTime.FromFileTime((time + 11644473600) * 10000000); } if ((flag & 0x80000000) == 0x80000000) // SSH_FILEXFER_ATTR_EXTENDED { var extendedCount = (int) stream.ReadUInt32(); extensions = new Dictionary<string, string>(extendedCount); for (var i = 0; i < extendedCount; i++) { var extensionName = stream.ReadString(SshData.Utf8); var extensionData = stream.ReadString(SshData.Utf8); extensions.Add(extensionName, extensionData); } } return new SftpFileAttributes(accessTime, modifyTime, size, userId, groupId, permissions, extensions); }
public void GetBytes() { var request = new SftpReadRequest(_protocolVersion, _requestId, _handle, _offset, _length, null, 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 += 4; // Length Assert.AreEqual(expectedBytesLength, bytes.Length); var sshDataStream = new SshDataStream(bytes); Assert.AreEqual((uint) bytes.Length - 4, sshDataStream.ReadUInt32()); Assert.AreEqual((byte) SftpMessageTypes.Read, 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.ReadUInt32()); Assert.IsTrue(sshDataStream.IsEndOfData); }
public void GetBytes() { #if TUNING var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, _length, null); #else var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, null); #endif 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 expectedBytesLength += 8; // Offset expectedBytesLength += 4; // Data length expectedBytesLength += _length; // Data 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.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(_offset, sshDataStream.ReadUInt64()); Assert.AreEqual((uint) _length, sshDataStream.ReadUInt32()); var actualData = new byte[_length]; sshDataStream.Read(actualData, 0, actualData.Length); Assert.IsTrue(_data.Take(_length).SequenceEqual(actualData)); Assert.IsTrue(sshDataStream.IsEndOfData); }