-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTimeHelperTests.cs
More file actions
103 lines (87 loc) · 3.67 KB
/
Copy pathFileTimeHelperTests.cs
File metadata and controls
103 lines (87 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
bool nonZero = false;
for (int 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 agree within a tight tolerance,
/// and are not all zeros.
/// </summary>
[Fact]
public void WriteFileTimeNow_IdempotentPerCall()
{
Span<byte> first = stackalloc byte[8];
Span<byte> immediate = stackalloc byte[8];
Span<byte> second = stackalloc byte[8];
FileTimeHelper.WriteFileTimeNow(first);
FileTimeHelper.WriteFileTimeNow(immediate);
FileTimeHelper.WriteFileTimeNow(second);
ulong f = FileTimeHelper.ReadFileTimeRaw(first);
ulong i = FileTimeHelper.ReadFileTimeRaw(immediate);
ulong s = FileTimeHelper.ReadFileTimeRaw(second);
// Precise integer conversion (BUG-LIB-038): consecutive calls can
// straddle a clock tick, so assert a tight tolerance (5 s) instead of
// the exact equality the old second-truncated formula gave for free.
Assert.InRange(i >= f ? i - f : f - i, 0UL, 50_000_000UL);
Assert.InRange(s >= i ? s - i : i - s, 0UL, 50_000_000UL);
Assert.NotEqual(0UL, f);
}
/// <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);
uint low = BitConverter.ToUInt32(dest);
uint high = BitConverter.ToUInt32(dest[4..]);
Assert.NotEqual(0u, high | low);
double filetime = (high * 4.0 * (1L << 30)) + low;
double 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}");
}
/// <summary>
/// Verifies that <see cref="FileTimeHelper.WriteFileTimeNow"/> agrees with the
/// precise integer conversion (BUG-LIB-038: the old double formula truncated
/// to whole seconds, so the same "now" differed between paths).
/// </summary>
[Fact]
public void WriteFileTimeNow_MatchesIntegerConversionOfNow()
{
DateTimeOffset before = DateTimeOffset.UtcNow;
Span<byte> dest = stackalloc byte[8];
FileTimeHelper.WriteFileTimeNow(dest);
DateTimeOffset after = DateTimeOffset.UtcNow;
ulong ft = FileTimeHelper.ReadFileTimeRaw(dest);
Assert.InRange(ft, FileTimeHelper.ToFileTimeRaw(before), FileTimeHelper.ToFileTimeRaw(after));
}
}