Skip to content

Commit e9e9b4e

Browse files
authored
Update ScpClient to work with revent versions of OpenSSH (#625)
Update ScpClient to work with versions of OpenSSH that include fix for CVE-2018-20685.
1 parent 546e2b9 commit e9e9b4e

12 files changed

+517
-52
lines changed
1.93 MB
Binary file not shown.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Renci.SshNet.Common;
3+
using System;
4+
5+
namespace Renci.SshNet.Tests.Classes.Common
6+
{
7+
[TestClass]
8+
public class PosixPathTest_CreateAbsoluteOrRelativeFilePath
9+
{
10+
[TestMethod]
11+
public void Path_Null()
12+
{
13+
const string path = null;
14+
15+
try
16+
{
17+
PosixPath.CreateAbsoluteOrRelativeFilePath(path);
18+
Assert.Fail();
19+
}
20+
catch (ArgumentNullException ex)
21+
{
22+
Assert.IsNull(ex.InnerException);
23+
Assert.AreEqual("path", ex.ParamName);
24+
}
25+
}
26+
27+
[TestMethod]
28+
public void Path_Empty()
29+
{
30+
var path = string.Empty;
31+
32+
try
33+
{
34+
PosixPath.CreateAbsoluteOrRelativeFilePath(path);
35+
Assert.Fail();
36+
}
37+
catch (ArgumentException ex)
38+
{
39+
Assert.IsNull(ex.InnerException);
40+
Assert.AreEqual(string.Format("The path is a zero-length string.{0}Parameter name: {1}", Environment.NewLine, ex.ParamName), ex.Message);
41+
Assert.AreEqual("path", ex.ParamName);
42+
}
43+
}
44+
45+
[TestMethod]
46+
public void Path_TrailingForwardSlash()
47+
{
48+
var path = "/abc/";
49+
50+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
51+
52+
Assert.IsNotNull(actual);
53+
Assert.AreEqual("/abc", actual.Directory);
54+
Assert.IsNull(actual.File);
55+
}
56+
57+
[TestMethod]
58+
public void Path_FileWithoutNoDirectory()
59+
{
60+
var path = "abc.log";
61+
62+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
63+
64+
Assert.IsNotNull(actual);
65+
Assert.AreEqual(".", actual.Directory);
66+
Assert.AreSame(path, actual.File);
67+
}
68+
69+
[TestMethod]
70+
public void Path_FileInRootDirectory()
71+
{
72+
var path = "/abc.log";
73+
74+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
75+
76+
Assert.IsNotNull(actual);
77+
Assert.AreEqual("/", actual.Directory);
78+
Assert.AreEqual("abc.log", actual.File);
79+
}
80+
81+
[TestMethod]
82+
public void Path_RootDirectoryOnly()
83+
{
84+
var path = "/";
85+
86+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
87+
88+
Assert.IsNotNull(actual);
89+
Assert.AreEqual("/", actual.Directory);
90+
Assert.IsNull(actual.File);
91+
}
92+
93+
[TestMethod]
94+
public void Path_FileInNonRootDirectory()
95+
{
96+
var path = "/home/sshnet/xyz";
97+
98+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
99+
100+
Assert.IsNotNull(actual);
101+
Assert.AreEqual("/home/sshnet", actual.Directory);
102+
Assert.AreEqual("xyz", actual.File);
103+
}
104+
105+
[TestMethod]
106+
public void Path_BackslashIsNotConsideredDirectorySeparator()
107+
{
108+
var path = "/home\\abc.log";
109+
110+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
111+
112+
Assert.IsNotNull(actual);
113+
Assert.AreEqual("/", actual.Directory);
114+
Assert.AreEqual("home\\abc.log", actual.File);
115+
}
116+
117+
[TestMethod]
118+
public void Path_ColonIsNotConsideredPathSeparator()
119+
{
120+
var path = "/home:abc.log";
121+
122+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
123+
124+
Assert.IsNotNull(actual);
125+
Assert.AreEqual("/", actual.Directory);
126+
Assert.AreEqual("home:abc.log", actual.File);
127+
}
128+
129+
[TestMethod]
130+
public void Path_LeadingWhitespace()
131+
{
132+
var path = " / \tabc";
133+
134+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
135+
136+
Assert.IsNotNull(actual);
137+
Assert.AreEqual(" ", actual.Directory);
138+
Assert.AreEqual(" \tabc", actual.File);
139+
}
140+
141+
[TestMethod]
142+
public void Path_TrailingWhitespace()
143+
{
144+
var path = "/abc \t ";
145+
146+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
147+
148+
Assert.IsNotNull(actual);
149+
Assert.AreEqual("/", actual.Directory);
150+
Assert.AreEqual("abc \t ", actual.File);
151+
}
152+
153+
[TestMethod]
154+
public void Path_OnlyWhitespace()
155+
{
156+
var path = " ";
157+
158+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
159+
160+
Assert.IsNotNull(actual);
161+
Assert.AreEqual(".", actual.Directory);
162+
Assert.AreSame(path, actual.File);
163+
}
164+
165+
[TestMethod]
166+
public void Path_FileNameOnlyWhitespace()
167+
{
168+
var path = "/home/\t ";
169+
170+
var actual = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
171+
172+
Assert.IsNotNull(actual);
173+
Assert.AreEqual("/home", actual.Directory);
174+
Assert.AreEqual("\t ", actual.File);
175+
}
176+
}
177+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Renci.SshNet.Common;
4+
5+
namespace Renci.SshNet.Tests.Classes.Common
6+
{
7+
[TestClass]
8+
public class PosixPathTest_GetDirectoryName
9+
{
10+
[TestMethod]
11+
public void Path_Null()
12+
{
13+
const string path = null;
14+
15+
try
16+
{
17+
PosixPath.GetDirectoryName(path);
18+
Assert.Fail();
19+
}
20+
catch (ArgumentNullException ex)
21+
{
22+
Assert.IsNull(ex.InnerException);
23+
Assert.AreEqual("path", ex.ParamName);
24+
}
25+
}
26+
27+
[TestMethod]
28+
public void Path_Empty()
29+
{
30+
var path = string.Empty;
31+
32+
var actual = PosixPath.GetDirectoryName(path);
33+
34+
Assert.IsNotNull(actual);
35+
Assert.AreEqual(".", actual);
36+
}
37+
38+
[TestMethod]
39+
public void Path_TrailingForwardSlash()
40+
{
41+
var path = "/abc/";
42+
43+
var actual = PosixPath.GetDirectoryName(path);
44+
45+
Assert.IsNotNull(actual);
46+
Assert.AreEqual("/abc", actual);
47+
}
48+
49+
[TestMethod]
50+
public void Path_FileWithoutNoDirectory()
51+
{
52+
var path = "abc.log";
53+
54+
var actual = PosixPath.GetDirectoryName(path);
55+
56+
Assert.IsNotNull(actual);
57+
Assert.AreEqual(".", actual);
58+
}
59+
60+
[TestMethod]
61+
public void Path_FileInRootDirectory()
62+
{
63+
var path = "/abc.log";
64+
65+
var actual = PosixPath.GetDirectoryName(path);
66+
67+
Assert.IsNotNull(actual);
68+
Assert.AreEqual("/", actual);
69+
}
70+
71+
[TestMethod]
72+
public void Path_RootDirectoryOnly()
73+
{
74+
var path = "/";
75+
76+
var actual = PosixPath.GetDirectoryName(path);
77+
78+
Assert.IsNotNull(actual);
79+
Assert.AreEqual("/", actual);
80+
}
81+
82+
[TestMethod]
83+
public void Path_FileInNonRootDirectory()
84+
{
85+
var path = "/home/sshnet/xyz";
86+
87+
var actual = PosixPath.GetDirectoryName(path);
88+
89+
Assert.IsNotNull(actual);
90+
Assert.AreEqual("/home/sshnet", actual);
91+
}
92+
93+
[TestMethod]
94+
public void Path_BackslashIsNotConsideredDirectorySeparator()
95+
{
96+
var path = "/home\\abc.log";
97+
98+
var actual = PosixPath.GetDirectoryName(path);
99+
100+
Assert.IsNotNull(actual);
101+
Assert.AreEqual("/", actual);
102+
}
103+
104+
[TestMethod]
105+
public void Path_ColonIsNotConsideredPathSeparator()
106+
{
107+
var path = "/home:abc.log";
108+
109+
var actual = PosixPath.GetDirectoryName(path);
110+
111+
Assert.IsNotNull(actual);
112+
Assert.AreEqual("/", actual);
113+
}
114+
115+
[TestMethod]
116+
public void Path_LeadingWhitespace()
117+
{
118+
var path = " / \tabc";
119+
120+
var actual = PosixPath.GetDirectoryName(path);
121+
122+
Assert.IsNotNull(actual);
123+
Assert.AreEqual(" ", actual);
124+
}
125+
126+
[TestMethod]
127+
public void Path_TrailingWhitespace()
128+
{
129+
var path = "/abc \t ";
130+
131+
var actual = PosixPath.GetDirectoryName(path);
132+
133+
Assert.IsNotNull(actual);
134+
Assert.AreEqual("/", actual);
135+
}
136+
137+
[TestMethod]
138+
public void Path_OnlyWhitespace()
139+
{
140+
var path = " ";
141+
142+
var actual = PosixPath.GetDirectoryName(path);
143+
144+
Assert.IsNotNull(actual);
145+
Assert.AreEqual(".", actual);
146+
}
147+
148+
[TestMethod]
149+
public void Path_FileNameOnlyWhitespace()
150+
{
151+
var path = "/home/\t ";
152+
153+
var actual = PosixPath.GetDirectoryName(path);
154+
155+
Assert.IsNotNull(actual);
156+
Assert.AreEqual("/home", actual);
157+
}
158+
}
159+
}

src/Renci.SshNet.Tests/Classes/Common/PosixPathTest_GetFileName.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ public void Path_Null()
1717
PosixPath.GetFileName(path);
1818
Assert.Fail();
1919
}
20-
catch (NullReferenceException)
20+
catch (ArgumentNullException ex)
2121
{
22+
Assert.IsNull(ex.InnerException);
23+
Assert.AreEqual("path", ex.ParamName);
2224
}
2325
}
2426

src/Renci.SshNet.Tests/Classes/ScpClientTest_Upload_DirectoryInfoAndPath_SendExecRequestReturnsFalse.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Globalization;
43
using System.IO;
54
using Microsoft.VisualStudio.TestTools.UnitTesting;
65
using Moq;
@@ -25,7 +24,7 @@ protected override void SetupData()
2524

2625
_connectionInfo = new ConnectionInfo("host", 22, "user", new PasswordAuthenticationMethod("user", "pwd"));
2726
_directoryInfo = new DirectoryInfo("source");
28-
_path = "/home/sshnet/" + random.Next().ToString(CultureInfo.InvariantCulture);
27+
_path = "/home/sshnet/" + random.Next().ToString();
2928
_transformedPath = random.Next().ToString();
3029
_uploadingRegister = new List<ScpUploadEventArgs>();
3130
}
@@ -48,7 +47,7 @@ protected override void SetupMocks()
4847
.Setup(p => p.Transform(_path))
4948
.Returns(_transformedPath);
5049
_channelSessionMock.InSequence(sequence)
51-
.Setup(p => p.SendExecRequest(string.Format("scp -rt {0}", _transformedPath)))
50+
.Setup(p => p.SendExecRequest(string.Format("scp -r -p -d -t {0}", _transformedPath)))
5251
.Returns(false);
5352
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
5453
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());
@@ -87,7 +86,7 @@ public void UploadShouldHaveThrownSshException()
8786
[TestMethod]
8887
public void SendExecREquestOnChannelSessionShouldBeInvokedOnce()
8988
{
90-
_channelSessionMock.Verify(p => p.SendExecRequest(string.Format("scp -rt {0}", _transformedPath)), Times.Once);
89+
_channelSessionMock.Verify(p => p.SendExecRequest(string.Format("scp -r -p -d -t {0}", _transformedPath)), Times.Once);
9190
}
9291

9392
[TestMethod]

0 commit comments

Comments
 (0)