-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTimeHelperTests.cs
More file actions
80 lines (67 loc) · 2.61 KB
/
Copy pathFileTimeHelperTests.cs
File metadata and controls
80 lines (67 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace XISOSharp.Tests;
/// <summary>
/// Tests for <see cref="FileTimeHelper"/>, verifying that FILETIME values
/// are written correctly as 8-byte timestamps representing the current time.
/// </summary>
public class FileTimeHelperTests
{
/// <summary>
/// Verifies that <see cref="FileTimeHelper.WriteFileTimeNow"/> writes
/// a non-zero 8-byte value into the destination span.
/// </summary>
[Fact]
public void WriteFileTimeNow_WritesEightBytes()
{
Span<byte> dest = stackalloc byte[8];
FileTimeHelper.WriteFileTimeNow(dest);
var nonZero = false;
for (var i = 0; i < 8; i++)
{
if (dest[i] != 0)
{
nonZero = true;
break;
}
}
Assert.True(nonZero, "Expected non-zero FILETIME value");
}
/// <summary>
/// Verifies that repeated calls to <see cref="FileTimeHelper.WriteFileTimeNow"/>
/// within a short interval produce values that are equal or close together,
/// and are not all zeros.
/// </summary>
[Fact]
public void WriteFileTimeNow_IdempotentPerCall()
{
Span<byte> first = stackalloc byte[8];
Span<byte> second = stackalloc byte[8];
Span<byte> immediate = stackalloc byte[8];
FileTimeHelper.WriteFileTimeNow(first);
FileTimeHelper.WriteFileTimeNow(immediate);
FileTimeHelper.WriteFileTimeNow(second);
Assert.True(first.SequenceEqual(immediate) || first.SequenceEqual(second),
"FILETIME should be within 1-2 seconds");
Assert.False(first.SequenceEqual(stackalloc byte[8]),
"FILETIME should not be all zeros");
}
/// <summary>
/// Verifies that the FILETIME value produced by
/// <see cref="FileTimeHelper.WriteFileTimeNow"/> converts to a Unix timestamp
/// within 10 seconds of the current system time.
/// </summary>
[Fact]
public void WriteFileTimeNow_ReasonableRange()
{
Span<byte> dest = stackalloc byte[8];
FileTimeHelper.WriteFileTimeNow(dest);
var low = BitConverter.ToUInt32(dest);
var high = BitConverter.ToUInt32(dest[4..]);
Assert.NotEqual(0u, high | low);
var filetime = high * 4.0 * (1L << 30) + low;
var unixTime = filetime / 1.0e7 -
(369.0 * 365.25 * 24.0 * 60.0 * 60.0 - (3.0 * 24.0 * 60.0 * 60.0 + 6.0 * 60.0 * 60.0));
double now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Assert.True(unixTime >= now - 10 && unixTime <= now + 10,
$"FILETIME should represent current time. Got unix={unixTime}, now={now}");
}
}