forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSftpRenameRequestTest.cs
More file actions
107 lines (86 loc) · 4.04 KB
/
Copy pathSftpRenameRequestTest.cs
File metadata and controls
107 lines (86 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Sftp.Requests;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Sftp.Requests
{
[TestClass]
public class SftpRenameRequestTest : TestBase
{
private uint _protocolVersion;
private uint _requestId;
private Encoding _encoding;
private string _oldPath;
private byte[] _oldPathBytes;
private string _newPath;
private byte[] _newPathBytes;
protected override void OnInit()
{
var random = new Random();
_protocolVersion = (uint)random.Next(0, int.MaxValue);
_requestId = (uint)random.Next(0, int.MaxValue);
_encoding = Encoding.Unicode;
_oldPath = random.Next().ToString(CultureInfo.InvariantCulture);
_oldPathBytes = _encoding.GetBytes(_oldPath);
_newPath = random.Next().ToString(CultureInfo.InvariantCulture);
_newPathBytes = _encoding.GetBytes(_newPath);
}
[TestMethod]
public void Constructor()
{
var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, null);
Assert.AreSame(_encoding, request.Encoding);
Assert.AreEqual(_newPath, request.NewPath);
Assert.AreEqual(_oldPath, request.OldPath);
Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
Assert.AreEqual(_requestId, request.RequestId);
Assert.AreEqual(SftpMessageTypes.Rename, request.SftpMessageType);
}
[TestMethod]
public void Complete_SftpStatusResponse()
{
var statusActionInvocations = new List<SftpStatusResponse>();
Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
var statusResponse = new SftpStatusResponse(_protocolVersion);
var request = new SftpRenameRequest(_protocolVersion, _requestId, _oldPath, _newPath, _encoding, statusAction);
request.Complete(statusResponse);
Assert.AreEqual(1, statusActionInvocations.Count);
Assert.AreSame(statusResponse, statusActionInvocations[0]);
}
[TestMethod]
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);
}
}
}