Skip to content

Fix SFTP file UTC time handling #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ public void AuthenticateOnPublicKeyAuthenticationMethodShouldHaveBeenInvokedTwic
PublicKeyAuthenticationMethodMock.Verify(p => p.Authenticate(SessionMock.Object), Times.Exactly(2));
}
}
}
}
2 changes: 2 additions & 0 deletions src/Renci.SshNet.Tests/Classes/Common/BigIntegerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,10 +1506,12 @@ public void DefaultCtorWorks()
Assert.AreEqual("0", a.ToString(), "#4");

a = new BigInteger();
#pragma warning disable CS1718 // Comparison made to same variable
Assert.AreEqual(true, a == a, "#5");

a = new BigInteger();
Assert.AreEqual(false, a < a, "#6");
#pragma warning restore CS1718 // Comparison made to same variable

a = new BigInteger();
Assert.AreEqual(true, a < 10L, "#7");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ChannelExtendedDataMessageConstructorTest()
[Ignore] // placeholder
public void ChannelExtendedDataMessageConstructorTest1()
{
uint localChannelNumber = 0; // TODO: Initialize to an appropriate value
//uint localChannelNumber = 0; // TODO: Initialize to an appropriate value
//ChannelExtendedDataMessage target = new ChannelExtendedDataMessage(localChannelNumber, null, null);
Assert.Inconclusive("TODO: Implement code to verify target");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void SetUp()

protected static SftpFileAttributes CreateSftpFileAttributes(long size)
{
return new SftpFileAttributes(default(DateTime), default(DateTime), size, default(int), default(int), default(uint), null);
var utcDefault = DateTime.SpecifyKind(default(DateTime), DateTimeKind.Utc);
return new SftpFileAttributes(utcDefault, utcDefault, size, default(int), default(int), default(uint), null);
}

protected static byte[] CreateByteArray(Random random, int length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected override void SetupData()
_readBufferSize = (uint) _random.Next(5, 1000);
_writeBufferSize = (uint) _random.Next(5, 1000);
_handle = GenerateRandom(_random.Next(1, 10), _random);
_fileAttributes = new SftpFileAttributesBuilder().WithLastAccessTime(DateTime.Now.AddSeconds(_random.Next()))
.WithLastWriteTime(DateTime.Now.AddSeconds(_random.Next()))
_fileAttributes = new SftpFileAttributesBuilder().WithLastAccessTime(DateTime.UtcNow.AddSeconds(_random.Next()))
.WithLastWriteTime(DateTime.UtcNow.AddSeconds(_random.Next()))
.WithSize(_random.Next())
.WithUserId(_random.Next())
.WithGroupId(_random.Next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ protected override void SetupData()
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
.WithExtension("V", "VValue")
.WithGroupId(random.Next())
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithPermissions((uint) random.Next())
.WithSize(_length + 100)
.WithUserId(random.Next())
Expand Down Expand Up @@ -201,4 +201,4 @@ public void WriteShouldStartFromSamePositionAsBeforeSetLength()
SftpSessionMock.Verify(p => p.IsOpen, Times.Exactly(4));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected override void SetupData()
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
.WithExtension("V", "VValue")
.WithGroupId(random.Next())
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithPermissions((uint)random.Next())
.WithSize(_length + 100)
.WithUserId(random.Next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected override void SetupData()
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
.WithExtension("V", "VValue")
.WithGroupId(random.Next())
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithPermissions((uint)random.Next())
.WithSize(_length + 100)
.WithUserId(random.Next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected override void SetupData()
_fileAttributes = new SftpFileAttributesBuilder().WithExtension("X", "ABC")
.WithExtension("V", "VValue")
.WithGroupId(random.Next())
.WithLastAccessTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.Now.AddSeconds(random.Next()))
.WithLastAccessTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithLastWriteTime(DateTime.UtcNow.AddSeconds(random.Next()))
.WithPermissions((uint) random.Next())
.WithSize(_length + 100)
.WithUserId(random.Next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected void Arrange()
_writeBufferSize = (uint) random.Next(0, 1000);
_length = random.Next();

_fileAttributesLastAccessTime = DateTime.Now.AddSeconds(random.Next());
_fileAttributesLastWriteTime = DateTime.Now.AddSeconds(random.Next());
_fileAttributesLastAccessTime = DateTime.UtcNow.AddSeconds(random.Next());
_fileAttributesLastWriteTime = DateTime.UtcNow.AddSeconds(random.Next());
_fileAttributesSize = random.Next();
_fileAttributesUserId = random.Next();
_fileAttributesGroupId = random.Next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected void Arrange()
_writeBufferSize = (uint) random.Next(0, 1000);
_length = random.Next();

_fileAttributesLastAccessTime = DateTime.Now.AddSeconds(random.Next());
_fileAttributesLastWriteTime = DateTime.Now.AddSeconds(random.Next());
_fileAttributesLastAccessTime = DateTime.UtcNow.AddSeconds(random.Next());
_fileAttributesLastWriteTime = DateTime.UtcNow.AddSeconds(random.Next());
_fileAttributesSize = random.Next();
_fileAttributesUserId = random.Next();
_fileAttributesGroupId = random.Next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public void PositionShouldBeNumberOfBytesWrittenToFileAndNUmberOfBytesInBuffer()
[TestMethod]
public void LengthShouldFlushBufferAndReturnSizeOfFile()
{
var lengthFileAttributes = new SftpFileAttributes(DateTime.Now,
DateTime.Now,
var lengthFileAttributes = new SftpFileAttributes(DateTime.UtcNow,
DateTime.UtcNow,
_random.Next(),
_random.Next(),
_random.Next(),
Expand Down
10 changes: 9 additions & 1 deletion src/Renci.SshNet.Tests/Classes/SftpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,9 @@ public void SetLastAccessTimeTest()
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
string path = string.Empty; // TODO: Initialize to an appropriate value
DateTime lastAccessTime = new DateTime(); // TODO: Initialize to an appropriate value
#pragma warning disable CS0618 // Type or member is obsolete
target.SetLastAccessTime(path, lastAccessTime);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}

Expand All @@ -1091,7 +1093,9 @@ public void SetLastAccessTimeUtcTest()
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
string path = string.Empty; // TODO: Initialize to an appropriate value
DateTime lastAccessTimeUtc = new DateTime(); // TODO: Initialize to an appropriate value
#pragma warning disable CS0618 // Type or member is obsolete
target.SetLastAccessTimeUtc(path, lastAccessTimeUtc);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}

Expand All @@ -1106,7 +1110,9 @@ public void SetLastWriteTimeTest()
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
string path = string.Empty; // TODO: Initialize to an appropriate value
DateTime lastWriteTime = new DateTime(); // TODO: Initialize to an appropriate value
#pragma warning disable CS0618 // Type or member is obsolete
target.SetLastWriteTime(path, lastWriteTime);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}

Expand All @@ -1121,7 +1127,9 @@ public void SetLastWriteTimeUtcTest()
SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
string path = string.Empty; // TODO: Initialize to an appropriate value
DateTime lastWriteTimeUtc = new DateTime(); // TODO: Initialize to an appropriate value
#pragma warning disable CS0618 // Type or member is obsolete
target.SetLastWriteTimeUtc(path, lastWriteTimeUtc);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}

Expand Down Expand Up @@ -1403,4 +1411,4 @@ private class TestInfo
public SftpDownloadAsyncResult DownloadResult { get; set; }
}
}
}
}
4 changes: 2 additions & 2 deletions src/Renci.SshNet.Tests/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ internal static SftpFileAttributes Clone(this SftpFileAttributes value)
clonedExtensions = null;
}

return new SftpFileAttributes(value.LastAccessTime,
value.LastWriteTime,
return new SftpFileAttributes(value.LastAccessTimeUtc,
value.LastWriteTimeUtc,
value.Size,
value.UserId,
value.GroupId,
Expand Down
10 changes: 8 additions & 2 deletions src/Renci.SshNet.Tests/Common/SftpFileAttributesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ public SftpFileAttributesBuilder WithExtension(string name, string value)
public SftpFileAttributes Build()
{
if (_lastAccessTime == null)
_lastAccessTime = DateTime.MinValue;
_lastAccessTime = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
else if (_lastAccessTime.Value.Kind != DateTimeKind.Utc)
_lastAccessTime = _lastAccessTime.Value.ToUniversalTime();

if (_lastWriteTime == null)
_lastWriteTime = DateTime.MinValue;
_lastWriteTime = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
else if (_lastWriteTime.Value.Kind != DateTimeKind.Utc)
_lastWriteTime = _lastWriteTime.Value.ToUniversalTime();

if (_size == null)
_size = 0;
if (_userId == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/Sftp/SftpFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public DateTime LastAccessTimeUtc
{
get
{
return Attributes.LastAccessTime.ToUniversalTime();
return Attributes.LastAccessTimeUtc;
}
set
{
Attributes.LastAccessTime = value.ToLocalTime();
Attributes.LastAccessTimeUtc = value;
}
}

Expand All @@ -117,11 +117,11 @@ public DateTime LastWriteTimeUtc
{
get
{
return Attributes.LastWriteTime.ToUniversalTime();
return Attributes.LastWriteTimeUtc;
}
set
{
Attributes.LastWriteTime = value.ToLocalTime();
Attributes.LastWriteTimeUtc = value;
}
}

Expand Down
Loading