From 3f5a7d77e642aac6db74b610912712725431e082 Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Tue, 29 Nov 2022 21:30:43 +0100 Subject: [PATCH] Add tests for SftpFileStream.Seek(...) (#9) * Add tests for SftpFileStream.Seek(...) to cover https://github.com/sshnet/SSH.NET/pull/910. * Review feedback. --- src/SshNetTests/SftpTests.cs | 1255 ++++++++++++++++++++++++---------- 1 file changed, 891 insertions(+), 364 deletions(-) diff --git a/src/SshNetTests/SftpTests.cs b/src/SshNetTests/SftpTests.cs index 733250a..195a6ea 100644 --- a/src/SshNetTests/SftpTests.cs +++ b/src/SshNetTests/SftpTests.cs @@ -48,7 +48,6 @@ public void Sftp_Upload_DirectoryInfo_ExistingFile() #endif public void Sftp_UploadFile_FileStream(int size) { - const string remoteFile = "/home/sshnet/test"; var file = CreateTempFile(size); using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) @@ -56,6 +55,8 @@ public void Sftp_UploadFile_FileStream(int size) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.Delete(remoteFile); @@ -130,7 +131,6 @@ public void Sftp_ConnectDisconnect_Parallel() [TestMethod] public void Sftp_BeginUploadFile() { - const string remoteFile = "/home/sshnet/test"; const string content = "SftpBeginUploadFile"; var expectedByteCount = (ulong) Encoding.ASCII.GetByteCount(content); @@ -139,6 +139,8 @@ public void Sftp_BeginUploadFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.Delete(remoteFile); @@ -193,8 +195,6 @@ public void Sftp_BeginUploadFile() [TestMethod] public void Sftp_Create_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = Encoding.UTF8; var initialContent = "Gert & Ann & Lisa"; var newContent1 = "Sofie"; @@ -206,6 +206,8 @@ public void Sftp_Create_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -295,13 +297,13 @@ public void Sftp_Create_DirectoryDoesNotExist() [TestMethod] public void Sftp_Create_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.BufferSize = 512 * 1024; client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -316,9 +318,9 @@ public void Sftp_Create_FileDoesNotExist() byte[] buffer = new byte[Math.Min(client.BufferSize, imageStream.Length)]; int bytesRead; - while ((bytesRead = imageStream.Read(buffer, 0, buffer.Length)) > 0) + while ((bytesRead = imageStream.Read(buffer, offset: 0, buffer.Length)) > 0) { - fs.Write(buffer, 0, bytesRead); + fs.Write(buffer, offset: 0, bytesRead); } } @@ -346,8 +348,6 @@ public void Sftp_Create_FileDoesNotExist() [TestMethod] public void Sftp_AppendAllLines_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var initialContent = "\u0100ert & Ann"; IEnumerable linesToAppend = new[] { "Forever", "&", "\u0116ver" }; var expectedContent = initialContent + string.Join(Environment.NewLine, linesToAppend) + @@ -357,6 +357,8 @@ public void Sftp_AppendAllLines_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -422,8 +424,6 @@ public void Sftp_AppendAllLines_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendAllLines_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - IEnumerable linesToAppend = new[] { "\u0139isa", "&", "Sofie" }; var expectedContent = string.Join(Environment.NewLine, linesToAppend) + Environment.NewLine; @@ -431,6 +431,8 @@ public void Sftp_AppendAllLines_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -463,8 +465,6 @@ public void Sftp_AppendAllLines_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_AppendAllText_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var initialContent = "\u0100ert & Ann"; var contentToAppend = "Forever&\u0116ver"; var expectedContent = initialContent + contentToAppend; @@ -473,6 +473,8 @@ public void Sftp_AppendAllText_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -540,8 +542,6 @@ public void Sftp_AppendAllText_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendAllText_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var contentToAppend = "Forever&\u0116ver"; var expectedContent = contentToAppend; @@ -549,6 +549,8 @@ public void Sftp_AppendAllText_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -581,8 +583,6 @@ public void Sftp_AppendAllText_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_AppendText_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var initialContent = "\u0100ert & Ann"; var contentToAppend = "Forever&\u0116ver"; var expectedContent = initialContent + contentToAppend; @@ -591,8 +591,12 @@ public void Sftp_AppendText_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) + { client.DeleteFile(remoteFile); + } try { @@ -667,8 +671,6 @@ public void Sftp_AppendText_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendText_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var contentToAppend = "\u0100ert & Ann"; var expectedContent = contentToAppend; @@ -676,6 +678,8 @@ public void Sftp_AppendText_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -708,8 +712,6 @@ public void Sftp_AppendText_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_AppendAllLines_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var initialContent = "\u0100ert & Ann"; IEnumerable linesToAppend = new[] { "Forever", "&", "\u0116ver" }; var expectedContent = initialContent + string.Join(Environment.NewLine, linesToAppend) + @@ -721,6 +723,8 @@ public void Sftp_AppendAllLines_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -737,7 +741,7 @@ public void Sftp_AppendAllLines_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -790,8 +794,6 @@ public void Sftp_AppendAllLines_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendAllLines_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - IEnumerable linesToAppend = new[] { "\u0139isa", "&", "Sofie" }; var expectedContent = string.Join(Environment.NewLine, linesToAppend) + Environment.NewLine; var encoding = GetRandomEncoding(); @@ -801,6 +803,8 @@ public void Sftp_AppendAllLines_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -816,7 +820,7 @@ public void Sftp_AppendAllLines_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -833,8 +837,6 @@ public void Sftp_AppendAllLines_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_AppendAllText_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var initialContent = "\u0100ert & Ann"; var contentToAppend = "Forever&\u0116ver"; var expectedContent = initialContent + contentToAppend; @@ -845,6 +847,8 @@ public void Sftp_AppendAllText_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -861,7 +865,7 @@ public void Sftp_AppendAllText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -915,8 +919,6 @@ public void Sftp_AppendAllText_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendAllText_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - const string contentToAppend = "Forever&\u0116ver"; var expectedContent = contentToAppend; var encoding = GetRandomEncoding(); @@ -926,6 +928,8 @@ public void Sftp_AppendAllText_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -941,7 +945,7 @@ public void Sftp_AppendAllText_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -958,8 +962,6 @@ public void Sftp_AppendAllText_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_AppendText_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - const string initialContent = "\u0100ert & Ann"; const string contentToAppend = "Forever&\u0116ver"; var expectedContent = initialContent + contentToAppend; @@ -970,6 +972,8 @@ public void Sftp_AppendText_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -990,7 +994,7 @@ public void Sftp_AppendText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -1049,8 +1053,6 @@ public void Sftp_AppendText_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_AppendText_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); const string contentToAppend = "\u0100ert & Ann"; var expectedBytes = GetBytesWithPreamble(contentToAppend, encoding); @@ -1059,6 +1061,8 @@ public void Sftp_AppendText_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1074,7 +1078,7 @@ public void Sftp_AppendText_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -1091,8 +1095,6 @@ public void Sftp_AppendText_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_CreateText_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); const string initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -1104,6 +1106,8 @@ public void Sftp_CreateText_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1121,7 +1125,7 @@ public void Sftp_CreateText_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } @@ -1138,7 +1142,7 @@ public void Sftp_CreateText_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedContentBytes.IsEqualTo(actualBytes)); } } @@ -1196,8 +1200,6 @@ public void Sftp_CreateText_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_CreateText_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); var initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -1206,6 +1208,8 @@ public void Sftp_CreateText_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1237,7 +1241,7 @@ public void Sftp_CreateText_NoEncoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } } @@ -1254,8 +1258,6 @@ public void Sftp_CreateText_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_CreateText_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -1267,6 +1269,8 @@ public void Sftp_CreateText_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1284,7 +1288,7 @@ public void Sftp_CreateText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } @@ -1301,7 +1305,7 @@ public void Sftp_CreateText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedContentBytes.IsEqualTo(actualBytes)); } } @@ -1361,8 +1365,6 @@ public void Sftp_CreateText_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_CreateText_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var initialContent = "\u0100ert & Ann"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -1371,6 +1373,8 @@ public void Sftp_CreateText_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1402,7 +1406,7 @@ public void Sftp_CreateText_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } } @@ -1419,12 +1423,12 @@ public void Sftp_CreateText_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_DownloadFile_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1459,8 +1463,6 @@ public void Sftp_DownloadFile_FileDoesNotExist() [TestMethod] public void Sftp_ReadAllBytes_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var content = "\u0100ert & Ann"; var contentBytes = GetBytesWithPreamble(content, encoding); @@ -1469,6 +1471,8 @@ public void Sftp_ReadAllBytes_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1494,12 +1498,12 @@ public void Sftp_ReadAllBytes_ExistingFile() [TestMethod] public void Sftp_ReadAllBytes_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1531,8 +1535,6 @@ public void Sftp_ReadAllBytes_FileDoesNotExist() [TestMethod] public void Sftp_ReadAllLines_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; var linesBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, lines) + Environment.NewLine, @@ -1542,6 +1544,8 @@ public void Sftp_ReadAllLines_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1567,7 +1571,7 @@ public void Sftp_ReadAllLines_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesBytes.IsEqualTo(actualBytes)); } } @@ -1584,14 +1588,16 @@ public void Sftp_ReadAllLines_NoEncoding_ExistingFile() [TestMethod] public void Sftp_ReadAllLines_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) + { client.DeleteFile(remoteFile); + } try { @@ -1619,8 +1625,6 @@ public void Sftp_ReadAllLines_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_ReadAllLines_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; var linesBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, lines) + Environment.NewLine, @@ -1630,8 +1634,12 @@ public void Sftp_ReadAllLines_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) + { client.DeleteFile(remoteFile); + } try { @@ -1653,7 +1661,7 @@ public void Sftp_ReadAllLines_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesBytes.IsEqualTo(actualBytes)); } } @@ -1670,14 +1678,14 @@ public void Sftp_ReadAllLines_Encoding_ExistingFile() [TestMethod] public void Sftp_ReadAllLines_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1709,8 +1717,6 @@ public void Sftp_ReadAllLines_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_ReadAllText_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; var expectedText = string.Join(Environment.NewLine, lines) + Environment.NewLine; @@ -1720,6 +1726,8 @@ public void Sftp_ReadAllText_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1739,7 +1747,7 @@ public void Sftp_ReadAllText_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -1756,14 +1764,16 @@ public void Sftp_ReadAllText_NoEncoding_ExistingFile() [TestMethod] public void Sftp_ReadAllText_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) + { client.DeleteFile(remoteFile); + } try { @@ -1791,8 +1801,6 @@ public void Sftp_ReadAllText_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_ReadAllText_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; var expectedText = string.Join(Environment.NewLine, lines) + Environment.NewLine; @@ -1802,6 +1810,8 @@ public void Sftp_ReadAllText_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1821,7 +1831,7 @@ public void Sftp_ReadAllText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes.IsEqualTo(actualBytes)); } } @@ -1838,16 +1848,18 @@ public void Sftp_ReadAllText_Encoding_ExistingFile() [TestMethod] public void Sftp_ReadAllText_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) + { client.DeleteFile(remoteFile); + } try { @@ -1875,14 +1887,14 @@ public void Sftp_ReadAllText_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_ReadLines_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1922,12 +1934,12 @@ public void Sftp_ReadLines_NoEncoding_ExistingFile() [TestMethod] public void Sftp_ReadLines_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -1959,8 +1971,6 @@ public void Sftp_ReadLines_NoEncoding_FileDoesNotExist() [TestMethod] public void Sftp_ReadLines_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var lines = new[] { "\u0100ert & Ann", "Forever", "&", "\u0116ver" }; @@ -1968,6 +1978,8 @@ public void Sftp_ReadLines_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2010,14 +2022,14 @@ public void Sftp_ReadLines_Encoding_ExistingFile() [TestMethod] public void Sftp_ReadLines_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2051,7 +2063,7 @@ public void Sftp_WriteAllBytes_DirectoryDoesNotExist() { const string remoteFile = "/home/sshnet/directorydoesnotexist/test"; - var content = GenerateRandom(5); + var content = GenerateRandom(size: 5); using (var client = new SftpClient(_connectionInfoFactory.Create())) { @@ -2085,19 +2097,19 @@ public void Sftp_WriteAllBytes_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllBytes_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - - var initialContent = GenerateRandom(13); - var newContent1 = GenerateRandom(5); + var initialContent = GenerateRandom(size: 13); + var newContent1 = GenerateRandom(size: 5); var expectedContent1 = new ArrayBuilder().Add(newContent1) .Add(initialContent, newContent1.Length, initialContent.Length - newContent1.Length) .Build(); - var newContent2 = GenerateRandom(50000); + var newContent2 = GenerateRandom(size: 50000); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2107,7 +2119,7 @@ public void Sftp_WriteAllBytes_ExistingFile() { using (var fs = client.Create(remoteFile)) { - fs.Write(initialContent, 0, initialContent.Length); + fs.Write(initialContent, offset: 0, initialContent.Length); } #region Write less bytes than the current content, overwriting part of that content @@ -2141,14 +2153,14 @@ public void Sftp_WriteAllBytes_ExistingFile() [TestMethod] public void Sftp_WriteAllBytes_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - - var content = GenerateRandom(13); + var content = GenerateRandom(size: 13); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2161,7 +2173,7 @@ public void Sftp_WriteAllBytes_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(content.IsEqualTo(actualBytes)); } } @@ -2215,8 +2227,6 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); var initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -2237,6 +2247,8 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2254,7 +2266,7 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2267,7 +2279,7 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -2286,8 +2298,6 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_ExistingFile() [TestMethod] public void Sftp_WriteAllLines_IEnumerable_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); IEnumerable linesToWrite = new[] { "\u0139isa", "&", "Sofie" }; var linesToWriteBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite) + Environment.NewLine, encoding); @@ -2296,6 +2306,8 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2308,7 +2320,7 @@ public void Sftp_WriteAllLines_IEnumerable_NoEncoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesToWriteBytes.IsEqualTo(actualBytes)); } } @@ -2362,8 +2374,6 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -2383,6 +2393,8 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2400,7 +2412,7 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2413,7 +2425,7 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -2432,8 +2444,6 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_ExistingFile() [TestMethod] public void Sftp_WriteAllLines_IEnumerable_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); IEnumerable linesToWrite = new[] { "\u0139isa", "&", "Sofie" }; var linesToWriteBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite) + Environment.NewLine, encoding); @@ -2442,6 +2452,8 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2454,7 +2466,7 @@ public void Sftp_WriteAllLines_IEnumerable_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesToWriteBytes.IsEqualTo(actualBytes)); } } @@ -2507,8 +2519,6 @@ public void Sftp_WriteAllLines_Array_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); @@ -2525,6 +2535,8 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2542,7 +2554,7 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2555,7 +2567,7 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -2574,8 +2586,6 @@ public void Sftp_WriteAllLines_Array_NoEncoding_ExistingFile() [TestMethod] public void Sftp_WriteAllLines_Array_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = new UTF8Encoding(false, true); var linesToWrite = new[] { "\u0139isa", "&", "Sofie" }; var linesToWriteBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite) + Environment.NewLine, encoding); @@ -2584,6 +2594,8 @@ public void Sftp_WriteAllLines_Array_NoEncoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2596,7 +2608,7 @@ public void Sftp_WriteAllLines_Array_NoEncoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesToWriteBytes.IsEqualTo(actualBytes)); } } @@ -2650,10 +2662,9 @@ public void Sftp_WriteAllLines_Array_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; + const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var encoding = GetRandomEncoding(); - const string initialContent = "\u0100ert & Ann Forever & Ever Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); var linesToWrite1 = new[] { "Forever", "&", "\u0116ver" }; var linesToWrite1Bytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite1) + Environment.NewLine, encoding); @@ -2668,6 +2679,8 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2685,7 +2698,7 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2698,7 +2711,7 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -2717,8 +2730,6 @@ public void Sftp_WriteAllLines_Array_Encoding_ExistingFile() [TestMethod] public void Sftp_WriteAllLines_Array_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var encoding = GetRandomEncoding(); var linesToWrite = new[] { "\u0139isa", "&", "Sofie" }; var linesToWriteBytes = GetBytesWithPreamble(string.Join(Environment.NewLine, linesToWrite) + Environment.NewLine, encoding); @@ -2727,6 +2738,8 @@ public void Sftp_WriteAllLines_Array_Encoding_FileDoesNotExist() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2739,7 +2752,7 @@ public void Sftp_WriteAllLines_Array_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(linesToWriteBytes.IsEqualTo(actualBytes)); } } @@ -2793,12 +2806,11 @@ public void Sftp_WriteAllText_NoEncoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllText_NoEncoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; + const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; + const string newContent1 = "For\u0116ver & Ever"; var encoding = new UTF8Encoding(false, true); - const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); - const string newContent1 = "For\u0116ver & Ever"; var newContent1Bytes = GetBytesWithPreamble(newContent1, encoding); var expectedBytes1 = new ArrayBuilder().Add(newContent1Bytes) .Add(initialContentBytes, newContent1Bytes.Length, initialContentBytes.Length - newContent1Bytes.Length) @@ -2811,6 +2823,8 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2827,7 +2841,7 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2840,7 +2854,7 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -2859,16 +2873,17 @@ public void Sftp_WriteAllText_NoEncoding_ExistingFile() [TestMethod] public void Sftp_WriteAllText_NoEncoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; + const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var encoding = new UTF8Encoding(false, true); - const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2881,7 +2896,7 @@ public void Sftp_WriteAllText_NoEncoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } } @@ -2935,17 +2950,16 @@ public void Sftp_WriteAllText_Encoding_DirectoryDoesNotExist() [TestMethod] public void Sftp_WriteAllText_Encoding_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; + const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; + const string newContent1 = "For\u0116ver & Ever"; + const string newContent2 = "Sofie & Lisa For\u0116ver & Ever with \u0100ert & Ann"; var encoding = GetRandomEncoding(); - const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); - const string newContent1 = "For\u0116ver & Ever"; var newContent1Bytes = GetBytesWithPreamble(newContent1, encoding); var expectedBytes1 = new ArrayBuilder().Add(newContent1Bytes) .Add(initialContentBytes, newContent1Bytes.Length, initialContentBytes.Length - newContent1Bytes.Length) .Build(); - const string newContent2 = "Sofie & Lisa For\u0116ver & Ever with \u0100ert & Ann"; var newContent2Bytes = GetBytesWithPreamble(newContent2, encoding); var expectedBytes2 = newContent2Bytes; @@ -2953,6 +2967,8 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -2969,7 +2985,7 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes1.IsEqualTo(actualBytes)); } @@ -2982,7 +2998,7 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(expectedBytes2.IsEqualTo(actualBytes)); } @@ -3001,16 +3017,17 @@ public void Sftp_WriteAllText_Encoding_ExistingFile() [TestMethod] public void Sftp_WriteAllText_Encoding_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; + const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var encoding = GetRandomEncoding(); - const string initialContent = "\u0100ert & Ann Forever & \u0116ver Lisa & Sofie"; var initialContentBytes = GetBytesWithPreamble(initialContent, encoding); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3023,7 +3040,7 @@ public void Sftp_WriteAllText_Encoding_FileDoesNotExist() using (var fs = client.OpenRead(remoteFile)) { var actualBytes = new byte[fs.Length]; - fs.Read(actualBytes, 0, actualBytes.Length); + fs.Read(actualBytes, offset: 0, actualBytes.Length); Assert.IsTrue(initialContentBytes.IsEqualTo(actualBytes)); } } @@ -3040,12 +3057,12 @@ public void Sftp_WriteAllText_Encoding_FileDoesNotExist() [TestMethod] public void Sftp_BeginDownloadFile_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3156,12 +3173,12 @@ public void Sftp_BeginUploadFile_InputAndPath_DirectoryDoesNotExist() [TestMethod] public void Sftp_BeginUploadFile_InputAndPath_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3204,12 +3221,12 @@ public void Sftp_BeginUploadFile_InputAndPath_FileDoesNotExist() [TestMethod] public void Sftp_BeginUploadFile_InputAndPath_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3290,12 +3307,12 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsFalse_D [TestMethod] public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsFalse_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3340,12 +3357,12 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsFalse_F [TestMethod] public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsFalse_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3425,12 +3442,12 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_Di [TestMethod] public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3475,12 +3492,12 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_Fi [TestMethod] public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -3528,12 +3545,13 @@ public void Sftp_BeginUploadFile_InputAndPathAndCanOverride_CanOverrideIsTrue_Ex public void Sftp_UploadAndDownloadBigFile() { const int size = 50 * 1024 * 1024; - const string remoteFile = "/home/sshnet/test"; using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.Delete(remoteFile); @@ -3909,13 +3927,14 @@ public void Sftp_ChangeDirectory_DirectoryExists() [TestMethod] public void Sftp_DownloadFile_MemoryStream() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 500 * 1024; using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + SftpCreateRemoteFile(client, remoteFile, fileSize); try @@ -3995,12 +4014,12 @@ public void Sftp_SubsystemExecution_Failed() [TestMethod] public void Sftp_SftpFileStream_ReadAndWrite() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4032,9 +4051,11 @@ public void Sftp_SftpFileStream_ReadAndWrite() Assert.AreEqual(6, s.Length); var buffer = new byte[s.Length]; - Assert.AreEqual(6, s.Read(buffer, 0, buffer.Length)); + Assert.AreEqual(6, s.Read(buffer, offset: 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 4, 3, 7, 9, 10, 11 }, buffer); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); } @@ -4049,6 +4070,8 @@ public void Sftp_SftpFileStream_ReadAndWrite() s.Write(new byte[] { 0, 1, 6, 4 }, 1, 2); Assert.AreEqual(11, s.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); s.WriteByte(12); @@ -4069,9 +4092,11 @@ public void Sftp_SftpFileStream_ReadAndWrite() Assert.AreEqual(7, s.Length); var buffer = new byte[s.Length]; - Assert.AreEqual(7, s.Read(buffer, 0, buffer.Length)); + Assert.AreEqual(7, s.Read(buffer, offset: 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 5, 3, 13, 1, 6, 11, 12 }, buffer); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); } } @@ -4088,39 +4113,41 @@ public void Sftp_SftpFileStream_ReadAndWrite() [TestMethod] public void Sftp_SftpFileStream_SetLength_ReduceLength() { - const string path = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); - if (client.Exists(path)) + var remoteFile = GenerateUniqueRemoteFileName(); + + if (client.Exists(remoteFile)) { - client.DeleteFile(path); + client.DeleteFile(remoteFile); } try { - using (var s = client.Open(path, FileMode.CreateNew, FileAccess.Write)) + using (var s = client.Open(remoteFile, FileMode.CreateNew, FileAccess.Write)) { s.Write(new byte[] { 5, 4, 3, 2, 1 }, 1, 3); } // reduce length while in write mode, with data in write buffer, and before // current position - using (var s = client.Open(path, FileMode.Append, FileAccess.Write)) + using (var s = client.Open(remoteFile, FileMode.Append, FileAccess.Write)) { s.Position = 3; - s.Write(new byte[] { 6, 7, 8, 9 }, 0, 4); + s.Write(new byte[] { 6, 7, 8, 9 }, offset: 0, count: 4); Assert.AreEqual(7, s.Position); // verify buffer has not yet been flushed - using (var fs = client.Open(path, FileMode.Open, FileAccess.Read)) + using (var fs = client.Open(remoteFile, FileMode.Open, FileAccess.Read)) { Assert.AreEqual(4, fs.ReadByte()); Assert.AreEqual(3, fs.ReadByte()); Assert.AreEqual(2, fs.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4129,13 +4156,15 @@ public void Sftp_SftpFileStream_SetLength_ReduceLength() Assert.AreEqual(5, s.Position); // verify that buffer was flushed and size has been modified - using (var fs = client.Open(path, FileMode.Open, FileAccess.Read)) + using (var fs = client.Open(remoteFile, FileMode.Open, FileAccess.Read)) { Assert.AreEqual(4, fs.ReadByte()); Assert.AreEqual(3, fs.ReadByte()); Assert.AreEqual(2, fs.ReadByte()); Assert.AreEqual(6, fs.ReadByte()); Assert.AreEqual(7, fs.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4143,30 +4172,32 @@ public void Sftp_SftpFileStream_SetLength_ReduceLength() } // verify that last byte was correctly written to the file - using (var s = client.Open(path, FileMode.Open, FileAccess.Read)) + using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.Read)) { Assert.AreEqual(6, s.Length); var buffer = new byte[s.Length + 2]; - Assert.AreEqual(6, s.Read(buffer, 0, buffer.Length)); + Assert.AreEqual(6, s.Read(buffer, offset: 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 4, 3, 2, 6, 7, 1, 0, 0 }, buffer); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); } // reduce length while in read mode, but beyond current position - using (var s = client.Open(path, FileMode.Open, FileAccess.ReadWrite)) + using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.ReadWrite)) { var buffer = new byte[1]; - Assert.AreEqual(1, s.Read(buffer, 0, buffer.Length)); + Assert.AreEqual(1, s.Read(buffer, offset: 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 4 }, buffer); s.SetLength(3); - using (var w = client.Open(path, FileMode.Open, FileAccess.Write)) + using (var w = client.Open(remoteFile, FileMode.Open, FileAccess.Write)) { - w.Write(new byte[] { 8, 1, 6, 2 }, 0, 4); + w.Write(new byte[] { 8, 1, 6, 2 }, offset: 0, count: 4); } // verify that position was not changed @@ -4176,16 +4207,18 @@ public void Sftp_SftpFileStream_SetLength_ReduceLength() Assert.AreEqual(1, s.ReadByte()); Assert.AreEqual(6, s.ReadByte()); Assert.AreEqual(2, s.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); Assert.AreEqual(4, s.Length); } // reduce length while in read mode, but before current position - using (var s = client.Open(path, FileMode.Open, FileAccess.ReadWrite)) + using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.ReadWrite)) { var buffer = new byte[4]; - Assert.AreEqual(4, s.Read(buffer, 0, buffer.Length)); + Assert.AreEqual(4, s.Read(buffer, offset: 0, buffer.Length)); CollectionAssert.AreEqual(new byte[] { 8, 1, 6, 2 }, buffer); @@ -4196,39 +4229,42 @@ public void Sftp_SftpFileStream_SetLength_ReduceLength() // verify that position was moved to last byte Assert.AreEqual(3, s.Position); - using (var w = client.Open(path, FileMode.Open, FileAccess.Read)) + using (var w = client.Open(remoteFile, FileMode.Open, FileAccess.Read)) { Assert.AreEqual(3, w.Length); Assert.AreEqual(8, w.ReadByte()); Assert.AreEqual(1, w.ReadByte()); Assert.AreEqual(6, w.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, w.ReadByte()); } + // Ensure we've reached end of the stream Assert.AreEqual(-1, s.ReadByte()); } } finally { - if (client.Exists(path)) + if (client.Exists(remoteFile)) { - client.DeleteFile(path); + client.DeleteFile(remoteFile); } } } } [TestMethod] - public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() + public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginBegin() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.BufferSize = 500; client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4242,18 +4278,22 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() fs.WriteByte(0x04); } - // seek beyond EOF but not beyond buffer size // do not write anything using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(3L, SeekOrigin.Begin); + var newPosition = fs.Seek(offset: 3L, SeekOrigin.Begin); + + Assert.AreEqual(3, newPosition); + Assert.AreEqual(3, fs.Position); } using (var fs = client.OpenRead(remoteFile)) { Assert.AreEqual(1, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4269,13 +4309,18 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() // do not write anything using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(700L, SeekOrigin.Begin); + var newPosition = fs.Seek(offset: 700L, SeekOrigin.Begin); + + Assert.AreEqual(700, newPosition); + Assert.AreEqual(700, fs.Position); } using (var fs = client.OpenRead(remoteFile)) { Assert.AreEqual(1, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4290,12 +4335,18 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() // seek beyond EOF but not beyond buffer size // write less bytes than buffer size var seekOffset = 3L; - var writeBuffer = GenerateRandom(7); + + // buffer holding the data that we'll write to the file + var writeBuffer = GenerateRandom(size: 7); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(seekOffset, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4303,14 +4354,15 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[seekOffset - 1]; - Assert.AreEqual(emptyBuffer.Length, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[emptyBuffer.Length].IsEqualTo(emptyBuffer)); + var soughtOverReadBufferffer = new byte[seekOffset - 1]; + Assert.AreEqual(soughtOverReadBufferffer.Length, fs.Read(soughtOverReadBufferffer, offset: 0, soughtOverReadBufferffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBufferffer.Length].IsEqualTo(soughtOverReadBufferffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4325,12 +4377,18 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() // seek beyond EOF and beyond buffer size // write less bytes than buffer size seekOffset = 700L; - writeBuffer = GenerateRandom(4); + + // buffer holding the data that we'll write to the file + writeBuffer = GenerateRandom(size: 4); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(seekOffset, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4338,14 +4396,15 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[seekOffset - 1]; - Assert.AreEqual(emptyBuffer.Length, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[emptyBuffer.Length].IsEqualTo(emptyBuffer)); + var soughtOverReadBufferffer = new byte[seekOffset - 1]; + Assert.AreEqual(soughtOverReadBufferffer.Length, fs.Read(soughtOverReadBufferffer, offset: 0, soughtOverReadBufferffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBufferffer.Length].IsEqualTo(soughtOverReadBufferffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4359,12 +4418,16 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() // seek beyond EOF but not beyond buffer size // write more bytes than buffer size - writeBuffer = GenerateRandom(600); + writeBuffer = GenerateRandom(size: 600); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(3L, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(offset: 3L, SeekOrigin.Begin); + + Assert.AreEqual(3, newPosition); + Assert.AreEqual(3, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4375,9 +4438,10 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() Assert.AreEqual(0x00, fs.ReadByte()); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4391,12 +4455,16 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() // seek beyond EOF and beyond buffer size // write more bytes than buffer size - writeBuffer = GenerateRandom(600); + writeBuffer = GenerateRandom(size: 600); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(550, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(offset: 550, SeekOrigin.Begin); + + Assert.AreEqual(550, newPosition); + Assert.AreEqual(550, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4405,14 +4473,15 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[550 - 1]; - Assert.AreEqual(550 - 1, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[550 - 1].IsEqualTo(emptyBuffer)); + var soughtOverReadBuffer = new byte[550 - 1]; + Assert.AreEqual(550 - 1, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[550 - 1].IsEqualTo(soughtOverReadBuffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } } @@ -4426,18 +4495,16 @@ public void Sftp_SftpFileStream_Seek_BeyondEndOfFile() } } - /// https://github.com/sshnet/SSH.NET/issues/253 [TestMethod] - public void Sftp_SftpFileStream_Seek_Issue253() + public void Sftp_SftpFileStream_Seek_BeyondEndOfFile_SeekOriginEnd() { - const string remoteFile = "/home/sshnet/test"; - - var buf = Encoding.UTF8.GetBytes("123456"); - using (var client = new SftpClient(_connectionInfoFactory.Create())) { + client.BufferSize = 500; client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4445,163 +4512,602 @@ public void Sftp_SftpFileStream_Seek_Issue253() try { - using (var ws = client.OpenWrite(remoteFile)) - { - ws.Write(buf, 0, 3); - } - - using (var ws = client.OpenWrite(remoteFile)) - { - ws.Seek(3, SeekOrigin.Begin); - ws.Write(buf, 3, 3); - } - - var actual = client.ReadAllText(remoteFile, Encoding.UTF8); - Assert.AreEqual("123456", actual); - } - finally - { - if (client.Exists(remoteFile)) + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) { - client.DeleteFile(remoteFile); + fs.WriteByte(0x04); } - } - } - } - - [TestMethod] - public void Sftp_SftpFileStream_Seek_WithinReadBuffer() - { - const string remoteFile = "/home/sshnet/test"; - - var originalContent = GenerateRandom(800); - - using (var client = new SftpClient(_connectionInfoFactory.Create())) - { - client.BufferSize = 500; - client.Connect(); - - if (client.Exists(remoteFile)) - { - client.DeleteFile(remoteFile); - } - try - { + // seek beyond EOF but not beyond buffer size + // do not write anything using (var fs = client.OpenWrite(remoteFile)) { - fs.Write(originalContent, 0, originalContent.Length); + var newPosition = fs.Seek(offset: 3L, SeekOrigin.End); + + Assert.AreEqual(4, newPosition); + Assert.AreEqual(4, fs.Position); } using (var fs = client.OpenRead(remoteFile)) { - var readBuffer = new byte[200]; - - Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(1, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); - fs.Seek(3L, SeekOrigin.Begin); + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); } client.DeleteFile(remoteFile); - #region Seek beyond EOF and beyond buffer size do not write anything - // create single-byte file using (var fs = client.OpenWrite(remoteFile)) { fs.WriteByte(0x04); } - using (var fs = client.OpenRead(remoteFile)) - { - Assert.AreEqual(1, fs.Length); - Assert.AreEqual(0x04, fs.ReadByte()); - Assert.AreEqual(-1, fs.ReadByte()); - } - + // seek beyond EOF and beyond buffer size + // do not write anything using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(700L, SeekOrigin.Begin); + var newPosition = fs.Seek(offset: 700L, SeekOrigin.End); + + Assert.AreEqual(701, newPosition); + Assert.AreEqual(701, fs.Position); } using (var fs = client.OpenRead(remoteFile)) { Assert.AreEqual(1, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); + + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } client.DeleteFile(remoteFile); - #endregion Seek beyond EOF and beyond buffer size do not write anything - - #region Seek beyond EOF but not beyond buffer size and write less bytes than buffer size - // create single-byte file using (var fs = client.OpenWrite(remoteFile)) { fs.WriteByte(0x04); } + // seek beyond EOF but not beyond buffer size + // write less bytes than buffer size var seekOffset = 3L; - var writeBuffer = GenerateRandom(7); + + // buffer holding the data that we'll write to the file + var writeBuffer = GenerateRandom(size: 7); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(seekOffset, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(seekOffset, SeekOrigin.End); + + Assert.AreEqual(4, newPosition); + Assert.AreEqual(4, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) { - Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); + Assert.AreEqual(1 + seekOffset + writeBuffer.Length, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[seekOffset - 1]; - Assert.AreEqual(emptyBuffer.Length, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[emptyBuffer.Length].IsEqualTo(emptyBuffer)); + var soughtOverReadBuffer = new byte[seekOffset]; + Assert.AreEqual(soughtOverReadBuffer.Length, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBuffer.Length].IsEqualTo(soughtOverReadBuffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } client.DeleteFile(remoteFile); - #endregion Seek beyond EOF but not beyond buffer size and write less bytes than buffer size - - #region Seek beyond EOF and beyond buffer size and write less bytes than buffer size - // create single-byte file using (var fs = client.OpenWrite(remoteFile)) { fs.WriteByte(0x04); } + // seek beyond EOF and beyond buffer size + // write less bytes than buffer size seekOffset = 700L; - writeBuffer = GenerateRandom(4); + + // buffer holding the data that we'll write to the file + writeBuffer = GenerateRandom(size: 4); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(seekOffset, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); - } + var newPosition = fs.Seek(seekOffset, SeekOrigin.End); + + Assert.AreEqual(1 + seekOffset, newPosition); + Assert.AreEqual(1 + seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(1 + seekOffset + writeBuffer.Length, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + + var soughtOverReadBuffer = new byte[seekOffset]; + Assert.AreEqual(soughtOverReadBuffer.Length, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBuffer.Length].IsEqualTo(soughtOverReadBuffer)); + + var readBuffer = new byte[writeBuffer.Length]; + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + } + + // seek beyond EOF but not beyond buffer size + // write more bytes than buffer size + seekOffset = 3L; + writeBuffer = GenerateRandom(size: 600); + + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(seekOffset, SeekOrigin.End); + + Assert.AreEqual(1 + seekOffset, newPosition); + Assert.AreEqual(1 + seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(1 + seekOffset + writeBuffer.Length, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + + var soughtOverReadBuffer = new byte[seekOffset]; + Assert.AreEqual(soughtOverReadBuffer.Length, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBuffer.Length].IsEqualTo(soughtOverReadBuffer)); + + var readBuffer = new byte[writeBuffer.Length]; + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + } + + // seek beyond EOF and beyond buffer size + // write more bytes than buffer size + seekOffset = 550L; + writeBuffer = GenerateRandom(size: 600); + + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(seekOffset, SeekOrigin.End); + + Assert.AreEqual(1 + seekOffset, newPosition); + Assert.AreEqual(1 + seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(1 + seekOffset + writeBuffer.Length, fs.Length); + + Assert.AreEqual(0x04, fs.ReadByte()); + + var soughtOverReadBuffer = new byte[seekOffset]; + Assert.AreEqual(soughtOverReadBuffer.Length, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBuffer.Length].IsEqualTo(soughtOverReadBuffer)); + + var readBuffer = new byte[writeBuffer.Length]; + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + } + finally + { + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + } + } + } + + [TestMethod] + public void Sftp_SftpFileStream_Seek_NegativeOffSet_SeekOriginEnd() + { + using (var client = new SftpClient(_connectionInfoFactory.Create())) + { + client.BufferSize = 500; + client.Connect(); + + var remoteFile = GenerateUniqueRemoteFileName(); + + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + + try + { + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + fs.WriteByte(0x07); + fs.WriteByte(0x05); + } + + // seek within file and not beyond buffer size + // do not write anything + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(offset: -2L, SeekOrigin.End); + + Assert.AreEqual(1, newPosition); + Assert.AreEqual(1, fs.Position); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(3, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + Assert.AreEqual(0x07, fs.ReadByte()); + Assert.AreEqual(0x05, fs.ReadByte()); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + // buffer holding the data that we'll write to the file + var writeBuffer = GenerateRandom(size: (int) client.BufferSize + 200); + + using (var fs = client.OpenWrite(remoteFile)) + { + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } + + // seek within EOF and beyond buffer size + // do not write anything + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(offset: -100L, SeekOrigin.End); + + Assert.AreEqual(600, newPosition); + Assert.AreEqual(600, fs.Position); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(writeBuffer.Length, fs.Length); + + var readBuffer = new byte[writeBuffer.Length]; + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + // seek within EOF and within buffer size + // write less bytes than buffer size + using (var fs = client.OpenWrite(remoteFile)) + { + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + + var newPosition = fs.Seek(offset: -3, SeekOrigin.End); + + Assert.AreEqual(697, newPosition); + Assert.AreEqual(697, fs.Position); + + fs.WriteByte(0x01); + fs.WriteByte(0x05); + fs.WriteByte(0x04); + fs.WriteByte(0x07); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(writeBuffer.Length + 1, fs.Length); + + var readBuffer = new byte[writeBuffer.Length - 3]; + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(readBuffer.SequenceEqual(writeBuffer.Take(readBuffer.Length))); + + Assert.AreEqual(0x01, fs.ReadByte()); + Assert.AreEqual(0x05, fs.ReadByte()); + Assert.AreEqual(0x04, fs.ReadByte()); + Assert.AreEqual(0x07, fs.ReadByte()); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + // buffer holding the data that we'll write to the file + writeBuffer = GenerateRandom(size: (int) client.BufferSize * 4); + + // seek within EOF and beyond buffer size + // write less bytes than buffer size + using (var fs = client.OpenWrite(remoteFile)) + { + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + + var newPosition = fs.Seek(offset: -(client.BufferSize * 2), SeekOrigin.End); + + Assert.AreEqual(1000, newPosition); + Assert.AreEqual(1000, fs.Position); + + fs.WriteByte(0x01); + fs.WriteByte(0x05); + fs.WriteByte(0x04); + fs.WriteByte(0x07); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(writeBuffer.Length, fs.Length); + + // First part of file should not have been touched + var readBuffer = new byte[(int) client.BufferSize * 2]; + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(readBuffer.SequenceEqual(writeBuffer.Take(readBuffer.Length))); + + // Check part that should have been updated + Assert.AreEqual(0x01, fs.ReadByte()); + Assert.AreEqual(0x05, fs.ReadByte()); + Assert.AreEqual(0x04, fs.ReadByte()); + Assert.AreEqual(0x07, fs.ReadByte()); + + // Remaining bytes should not have been touched + readBuffer = new byte[((int) client.BufferSize * 2) - 4]; + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(readBuffer.SequenceEqual(writeBuffer.Skip(((int)client.BufferSize * 2) + 4).Take(readBuffer.Length))); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + } + finally + { + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + } + } + } + + /// https://github.com/sshnet/SSH.NET/issues/253 + [TestMethod] + public void Sftp_SftpFileStream_Seek_Issue253() + { + var buf = Encoding.UTF8.GetBytes("123456"); + + using (var client = new SftpClient(_connectionInfoFactory.Create())) + { + client.Connect(); + + var remoteFile = GenerateUniqueRemoteFileName(); + + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + + try + { + using (var ws = client.OpenWrite(remoteFile)) + { + ws.Write(buf, offset: 0, count: 3); + } + + using (var ws = client.OpenWrite(remoteFile)) + { + var newPosition = ws.Seek(offset: 3, SeekOrigin.Begin); + + Assert.AreEqual(3, newPosition); + Assert.AreEqual(3, ws.Position); + + ws.Write(buf, 3, 3); + } + + var actual = client.ReadAllText(remoteFile, Encoding.UTF8); + Assert.AreEqual("123456", actual); + } + finally + { + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + } + } + } + + [TestMethod] + public void Sftp_SftpFileStream_Seek_WithinReadBuffer() + { + var originalContent = GenerateRandom(size: 800); + + using (var client = new SftpClient(_connectionInfoFactory.Create())) + { + client.BufferSize = 500; + client.Connect(); + + var remoteFile = GenerateUniqueRemoteFileName(); + + if (client.Exists(remoteFile)) + { + client.DeleteFile(remoteFile); + } + + try + { + using (var fs = client.OpenWrite(remoteFile)) + { + fs.Write(originalContent, offset: 0, originalContent.Length); + } + + using (var fs = client.OpenRead(remoteFile)) + { + var readBuffer = new byte[200]; + + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + + var newPosition = fs.Seek(offset: 3L, SeekOrigin.Begin); + + Assert.AreEqual(3L, newPosition); + Assert.AreEqual(3L, fs.Position); + } + + client.DeleteFile(remoteFile); + + #region Seek beyond EOF and beyond buffer size do not write anything + + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(1, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(offset: 700L, SeekOrigin.Begin); + + Assert.AreEqual(700L, newPosition); + Assert.AreEqual(700L, fs.Position); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(1, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + #endregion Seek beyond EOF and beyond buffer size do not write anything + + #region Seek beyond EOF but not beyond buffer size and write less bytes than buffer size + + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + } + + var seekOffset = 3L; + var writeBuffer = GenerateRandom(size: 7); + + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } + + using (var fs = client.OpenRead(remoteFile)) + { + Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); + Assert.AreEqual(0x04, fs.ReadByte()); + + var soughtOverReadBuffer = new byte[seekOffset - 1]; + Assert.AreEqual(soughtOverReadBuffer.Length, fs.Read(soughtOverReadBuffer, offset: 0, soughtOverReadBuffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBuffer.Length].IsEqualTo(soughtOverReadBuffer)); + + var readBuffer = new byte[writeBuffer.Length]; + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); + Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + + // Ensure we've reached end of the stream + Assert.AreEqual(-1, fs.ReadByte()); + } + + client.DeleteFile(remoteFile); + + #endregion Seek beyond EOF but not beyond buffer size and write less bytes than buffer size + + #region Seek beyond EOF and beyond buffer size and write less bytes than buffer size + + // create single-byte file + using (var fs = client.OpenWrite(remoteFile)) + { + fs.WriteByte(0x04); + } + + seekOffset = 700L; + writeBuffer = GenerateRandom(size: 4); + + using (var fs = client.OpenWrite(remoteFile)) + { + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); + } using (var fs = client.OpenRead(remoteFile)) { Assert.AreEqual(seekOffset + writeBuffer.Length, fs.Length); Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[seekOffset - 1]; - Assert.AreEqual(emptyBuffer.Length, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[emptyBuffer.Length].IsEqualTo(emptyBuffer)); + var soughtOverReadBufferffer = new byte[seekOffset - 1]; + Assert.AreEqual(soughtOverReadBufferffer.Length, fs.Read(soughtOverReadBufferffer, offset: 0, soughtOverReadBufferffer.Length)); + Assert.IsTrue(new byte[soughtOverReadBufferffer.Length].IsEqualTo(soughtOverReadBufferffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(readBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4618,12 +5124,16 @@ public void Sftp_SftpFileStream_Seek_WithinReadBuffer() } seekOffset = 3L; - writeBuffer = GenerateRandom(600); + writeBuffer = GenerateRandom(size: 600); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(seekOffset, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4634,9 +5144,10 @@ public void Sftp_SftpFileStream_Seek_WithinReadBuffer() Assert.AreEqual(0x00, fs.ReadByte()); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4653,12 +5164,16 @@ public void Sftp_SftpFileStream_Seek_WithinReadBuffer() } seekOffset = 550L; - writeBuffer = GenerateRandom(600); + writeBuffer = GenerateRandom(size: 600); using (var fs = client.OpenWrite(remoteFile)) { - fs.Seek(550, SeekOrigin.Begin); - fs.Write(writeBuffer, 0, writeBuffer.Length); + var newPosition = fs.Seek(seekOffset, SeekOrigin.Begin); + + Assert.AreEqual(seekOffset, newPosition); + Assert.AreEqual(seekOffset, fs.Position); + + fs.Write(writeBuffer, offset: 0, writeBuffer.Length); } using (var fs = client.OpenRead(remoteFile)) @@ -4667,14 +5182,15 @@ public void Sftp_SftpFileStream_Seek_WithinReadBuffer() Assert.AreEqual(0x04, fs.ReadByte()); - var emptyBuffer = new byte[seekOffset - 1]; - Assert.AreEqual(seekOffset - 1, fs.Read(emptyBuffer, 0, emptyBuffer.Length)); - Assert.IsTrue(new byte[seekOffset - 1].IsEqualTo(emptyBuffer)); + var soughtOverReadBufferffer = new byte[seekOffset - 1]; + Assert.AreEqual(seekOffset - 1, fs.Read(soughtOverReadBufferffer, offset: 0, soughtOverReadBufferffer.Length)); + Assert.IsTrue(new byte[seekOffset - 1].IsEqualTo(soughtOverReadBufferffer)); var readBuffer = new byte[writeBuffer.Length]; - Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, 0, readBuffer.Length)); + Assert.AreEqual(writeBuffer.Length, fs.Read(readBuffer, offset: 0, readBuffer.Length)); Assert.IsTrue(writeBuffer.IsEqualTo(readBuffer)); + // Ensure we've reached end of the stream Assert.AreEqual(-1, fs.ReadByte()); } @@ -4695,14 +5211,14 @@ public void Sftp_SftpFileStream_Seek_WithinReadBuffer() [TestMethod] public void Sftp_SftpFileStream_SetLength_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - var size = new Random().Next(500, 5000); using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4741,21 +5257,22 @@ public void Sftp_SftpFileStream_SetLength_FileDoesNotExist() [TestMethod] public void Sftp_Open_Append_Write_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024; using (var client = new SftpClient(_connectionInfoFactory.Create())) using (var input = CreateMemoryStream(fileSize)) { + input.Position = 0; + client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); } - input.Position = 0; - try { client.UploadFile(input, remoteFile); @@ -4763,8 +5280,8 @@ public void Sftp_Open_Append_Write_ExistingFile() using (var s = client.Open(remoteFile, FileMode.Append, FileAccess.Write)) { var buffer = new byte[] { 0x05, 0x0f, 0x0d, 0x0a, 0x04 }; - s.Write(buffer, 0, buffer.Length); - input.Write(buffer, 0, buffer.Length); + s.Write(buffer, offset: 0, buffer.Length); + input.Write(buffer, offset: 0, buffer.Length); } using (var downloaded = new MemoryStream()) @@ -4789,12 +5306,12 @@ public void Sftp_Open_Append_Write_ExistingFile() [TestMethod] public void Sftp_Open_Append_Write_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4820,11 +5337,11 @@ public void Sftp_Open_Append_Write_FileDoesNotExist() #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.Append, FileAccess.Write)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -4851,12 +5368,12 @@ public void Sftp_Open_Append_Write_FileDoesNotExist() [TestMethod] public void Sftp_Open_PathAndMode_ModeIsCreate_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4882,11 +5399,11 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_FileDoesNotExist() #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.Create)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -4911,7 +5428,6 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_FileDoesNotExist() [TestMethod] public void Sftp_Open_PathAndMode_ModeIsCreate_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024; var newContent = new byte[] { 0x07, 0x03, 0x02, 0x0b }; @@ -4920,6 +5436,8 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4937,7 +5455,7 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_ExistingFile() Assert.IsTrue(attributes.IsRegularFile); Assert.AreEqual(0L, attributes.Size); - stream.Write(newContent, 0, newContent.Length); + stream.Write(newContent, offset: 0, newContent.Length); stream.Position = 0; Assert.AreEqual(CreateHash(newContent), CreateHash(stream)); @@ -4956,12 +5474,12 @@ public void Sftp_Open_PathAndMode_ModeIsCreate_ExistingFile() [TestMethod] public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -4987,11 +5505,11 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_FileDo #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.Create, FileAccess.ReadWrite)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -5016,7 +5534,6 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_FileDo [TestMethod] public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024; var newContent = new byte[] { 0x07, 0x03, 0x02, 0x0b }; @@ -5025,6 +5542,8 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_Existi { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5042,7 +5561,7 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_Existi Assert.IsTrue(attributes.IsRegularFile); Assert.AreEqual(0L, attributes.Size); - stream.Write(newContent, 0, newContent.Length); + stream.Write(newContent, offset: 0, newContent.Length); stream.Position = 0; Assert.AreEqual(CreateHash(newContent), CreateHash(stream)); @@ -5061,8 +5580,6 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsReadWrite_Existi [TestMethod] public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - // use new content that contains less bytes than original content to // verify whether file is first truncated var originalContent = new byte[] { 0x05, 0x0f, 0x0d, 0x0a, 0x04 }; @@ -5072,6 +5589,8 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_ExistingFi { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5083,7 +5602,7 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_ExistingFi using (var s = client.Open(remoteFile, FileMode.Create, FileAccess.Write)) { - s.Write(newContent, 0, newContent.Length); + s.Write(newContent, offset: 0, newContent.Length); } using (var downloaded = new MemoryStream()) @@ -5107,12 +5626,12 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_ExistingFi [TestMethod] public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5138,11 +5657,11 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_FileDoesNo #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.Create, FileAccess.Write)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -5167,7 +5686,6 @@ public void Sftp_Open_PathAndModeAndAccess_ModeIsCreate_AccessIsWrite_FileDoesNo [TestMethod] public void Sftp_Open_CreateNew_Write_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024; using (var client = new SftpClient(_connectionInfoFactory.Create())) @@ -5175,6 +5693,8 @@ public void Sftp_Open_CreateNew_Write_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5224,12 +5744,12 @@ public void Sftp_Open_CreateNew_Write_ExistingFile() [TestMethod] public void Sftp_Open_CreateNew_Write_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5255,11 +5775,11 @@ public void Sftp_Open_CreateNew_Write_FileDoesNotExist() #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.CreateNew, FileAccess.Write)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -5284,8 +5804,6 @@ public void Sftp_Open_CreateNew_Write_FileDoesNotExist() [TestMethod] public void Sftp_Open_Open_Write_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - // use new content that contains less bytes than original content to // verify whether file is first truncated var originalContent = new byte[] { 0x05, 0x0f, 0x0d, 0x0a, 0x04 }; @@ -5296,6 +5814,8 @@ public void Sftp_Open_Open_Write_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5307,7 +5827,7 @@ public void Sftp_Open_Open_Write_ExistingFile() using (var s = client.Open(remoteFile, FileMode.Open, FileAccess.Write)) { - s.Write(newContent, 0, newContent.Length); + s.Write(newContent, offset: 0, newContent.Length); } using (var downloaded = new MemoryStream()) @@ -5331,12 +5851,12 @@ public void Sftp_Open_Open_Write_ExistingFile() [TestMethod] public void Sftp_Open_Open_Write_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5374,8 +5894,6 @@ public void Sftp_Open_Open_Write_FileDoesNotExist() [TestMethod] public void Sftp_Open_OpenOrCreate_Write_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; - // use new content that contains less bytes than original content to // verify whether file is first truncated var originalContent = new byte[] { 0x05, 0x0f, 0x0d, 0x0a, 0x04 }; @@ -5386,6 +5904,8 @@ public void Sftp_Open_OpenOrCreate_Write_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5397,7 +5917,7 @@ public void Sftp_Open_OpenOrCreate_Write_ExistingFile() using (var s = client.Open(remoteFile, FileMode.OpenOrCreate, FileAccess.Write)) { - s.Write(newContent, 0, newContent.Length); + s.Write(newContent, offset: 0, newContent.Length); } using (var downloaded = new MemoryStream()) @@ -5421,12 +5941,12 @@ public void Sftp_Open_OpenOrCreate_Write_ExistingFile() [TestMethod] public void Sftp_Open_OpenOrCreate_Write_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5452,11 +5972,11 @@ public void Sftp_Open_OpenOrCreate_Write_FileDoesNotExist() #region Verify if content is actually written to the file - var content = GenerateRandom(100); + var content = GenerateRandom(size: 100); using (var s = client.Open(remoteFile, FileMode.OpenOrCreate, FileAccess.Write)) { - s.Write(content, 0, content.Length); + s.Write(content, offset: 0, content.Length); } using (var downloaded = new MemoryStream()) @@ -5487,7 +6007,6 @@ public void Sftp_Open_OpenOrCreate_Write_FileDoesNotExist() [TestMethod] public void Sftp_Open_Truncate_Write_ExistingFile() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024; // use new content that contains less bytes than original content to @@ -5500,6 +6019,8 @@ public void Sftp_Open_Truncate_Write_ExistingFile() { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5513,7 +6034,7 @@ public void Sftp_Open_Truncate_Write_ExistingFile() using (var s = client.Open(remoteFile, FileMode.Truncate, FileAccess.Write)) { - s.Write(newContent, 0, newContent.Length); + s.Write(newContent, offset: 0, newContent.Length); } using (var downloaded = new MemoryStream()) @@ -5538,12 +6059,12 @@ public void Sftp_Open_Truncate_Write_ExistingFile() [TestMethod] public void Sftp_Open_Truncate_Write_FileDoesNotExist() { - const string remoteFile = "/home/sshnet/test"; - using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + if (client.Exists(remoteFile)) { client.DeleteFile(remoteFile); @@ -5577,13 +6098,14 @@ public void Sftp_Open_Truncate_Write_FileDoesNotExist() [TestMethod] public void Sftp_OpenRead() { - const string remoteFile = "/home/sshnet/test"; const int fileSize = 5 * 1024 * 1024; using (var client = new SftpClient(_connectionInfoFactory.Create())) { client.Connect(); + var remoteFile = GenerateUniqueRemoteFileName(); + SftpCreateRemoteFile(client, remoteFile, fileSize); try @@ -5595,7 +6117,7 @@ public void Sftp_OpenRead() var stopwatch = new Stopwatch(); stopwatch.Start(); - var bytesRead = s.Read(buffer, 0, buffer.Length); + var bytesRead = s.Read(buffer, offset: 0, buffer.Length); stopwatch.Stop(); @@ -5646,8 +6168,8 @@ private static byte[] GetBytesWithPreamble(string text, Encoding encoding) if (preamble.Length != 0) { var textAndPreambleBytes = new byte[preamble.Length + textBytes.Length]; - Buffer.BlockCopy(preamble, 0, textAndPreambleBytes, 0, preamble.Length); - Buffer.BlockCopy(textBytes, 0, textAndPreambleBytes, preamble.Length, textBytes.Length); + Buffer.BlockCopy(preamble, srcOffset: 0, textAndPreambleBytes, dstOffset: 0, preamble.Length); + Buffer.BlockCopy(textBytes, srcOffset: 0, textAndPreambleBytes, preamble.Length, textBytes.Length); return textAndPreambleBytes; } @@ -5704,5 +6226,10 @@ private static Stream CreateStreamWithContent(string content) memoryStream.Position = 0; return memoryStream; } + + private static string GenerateUniqueRemoteFileName() + { + return $"/home/sshnet/{Guid.NewGuid():D}"; + } } }