mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
A planted <archive>.tmp set the archive's mode or redirected the write through a symlink. CoreData let 10000-01-01 through, which time.Time.MarshalJSON rejects, so a JSON export failed.
151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
package timeconv
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// anchorUnixSeconds is 2024-01-15T10:30:00Z.
|
|
const anchorUnixSeconds = int64(1705314600)
|
|
|
|
const anchorCoreDataSeconds = anchorUnixSeconds - 978307200
|
|
|
|
var (
|
|
anchorTime = time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC)
|
|
anchorChromiumMicros = (anchorUnixSeconds + 11644473600) * 1_000_000
|
|
)
|
|
|
|
func TestChromium_AnchorDate(t *testing.T) {
|
|
got := Chromium(anchorChromiumMicros)
|
|
assert.Equal(t, anchorTime, got)
|
|
assert.Equal(t, anchorUnixSeconds, got.Unix())
|
|
}
|
|
|
|
func TestChromium_ZeroReturnsZeroTime(t *testing.T) {
|
|
assert.True(t, Chromium(0).IsZero())
|
|
}
|
|
|
|
func TestChromium_NegativeReturnsZeroTime(t *testing.T) {
|
|
assert.True(t, Chromium(-1).IsZero())
|
|
}
|
|
|
|
func TestChromium_AlwaysUTC(t *testing.T) {
|
|
// assert.Same compares pointers, so it catches a dropped .UTC() even when the runner's TZ is UTC.
|
|
assert.Same(t, time.UTC, Chromium(anchorChromiumMicros).Location())
|
|
}
|
|
|
|
func TestChromium_MicrosecondPrecisionPreserved(t *testing.T) {
|
|
got := Chromium(anchorChromiumMicros + 123456)
|
|
assert.Equal(t, 123456*int64(time.Microsecond), int64(got.Nanosecond()))
|
|
}
|
|
|
|
func TestChromium_UnixEpochBoundary(t *testing.T) {
|
|
assert.Equal(t, time.Unix(0, 0).UTC(), Chromium(chromiumEpochOffsetMicros))
|
|
}
|
|
|
|
func TestChromium_OutOfJSONRangeReturnsZero(t *testing.T) {
|
|
jsonBytes, err := Chromium(1 << 62).MarshalJSON()
|
|
require.NoError(t, err)
|
|
assert.JSONEq(t, `"0001-01-01T00:00:00Z"`, string(jsonBytes))
|
|
}
|
|
|
|
func TestUnixMicro_AnchorDate(t *testing.T) {
|
|
assert.Equal(t, anchorTime, UnixMicro(anchorUnixSeconds*1_000_000))
|
|
}
|
|
|
|
func TestUnixMicro_PrecisionPreserved(t *testing.T) {
|
|
got := UnixMicro(anchorUnixSeconds*1_000_000 + 123456)
|
|
assert.Equal(t, 123456*int64(time.Microsecond), int64(got.Nanosecond()))
|
|
}
|
|
|
|
func TestUnixMilli_AnchorDate(t *testing.T) {
|
|
assert.Equal(t, anchorTime, UnixMilli(anchorUnixSeconds*1_000))
|
|
}
|
|
|
|
func TestUnixMilli_PrecisionPreserved(t *testing.T) {
|
|
got := UnixMilli(anchorUnixSeconds*1_000 + 789)
|
|
assert.Equal(t, 789*int64(time.Millisecond), int64(got.Nanosecond()))
|
|
}
|
|
|
|
func TestUnix_AnchorDate(t *testing.T) {
|
|
assert.Equal(t, anchorTime, Unix(anchorUnixSeconds))
|
|
}
|
|
|
|
func TestUnixUnits_ZeroReturnsZeroTime(t *testing.T) {
|
|
assert.True(t, UnixMicro(0).IsZero(), "micros")
|
|
assert.True(t, UnixMilli(0).IsZero(), "millis")
|
|
assert.True(t, Unix(0).IsZero(), "seconds")
|
|
}
|
|
|
|
func TestUnixUnits_NegativeReturnsZeroTime(t *testing.T) {
|
|
assert.True(t, UnixMicro(-1).IsZero(), "micros")
|
|
assert.True(t, UnixMilli(-1).IsZero(), "millis")
|
|
assert.True(t, Unix(-1).IsZero(), "seconds")
|
|
}
|
|
|
|
func TestUnixUnits_AlwaysUTC(t *testing.T) {
|
|
assert.Same(t, time.UTC, UnixMicro(anchorUnixSeconds*1_000_000).Location())
|
|
assert.Same(t, time.UTC, UnixMilli(anchorUnixSeconds*1_000).Location())
|
|
assert.Same(t, time.UTC, Unix(anchorUnixSeconds).Location())
|
|
}
|
|
|
|
func TestUnixUnits_SameMomentAcrossUnits(t *testing.T) {
|
|
fromMicros := UnixMicro(anchorUnixSeconds * 1_000_000)
|
|
fromMillis := UnixMilli(anchorUnixSeconds * 1_000)
|
|
fromSeconds := Unix(anchorUnixSeconds)
|
|
assert.True(t, fromMicros.Equal(fromMillis))
|
|
assert.True(t, fromMillis.Equal(fromSeconds))
|
|
}
|
|
|
|
func TestUnixUnits_OutOfJSONRangeReturnsZero(t *testing.T) {
|
|
for _, testCase := range []struct {
|
|
name string
|
|
got time.Time
|
|
}{
|
|
{"seconds", Unix(1 << 50)},
|
|
{"millis", UnixMilli(1 << 60)},
|
|
{"micros", UnixMicro(1 << 62)},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
encoded, err := testCase.got.MarshalJSON()
|
|
require.NoError(t, err)
|
|
assert.JSONEq(t, `"0001-01-01T00:00:00Z"`, string(encoded))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCoreData_AnchorDate(t *testing.T) {
|
|
assert.Equal(t, anchorTime, CoreData(float64(anchorCoreDataSeconds)))
|
|
}
|
|
|
|
func TestCoreData_KnownValue(t *testing.T) {
|
|
assert.Equal(t, int64(1678307200), CoreData(700000000).Unix())
|
|
}
|
|
|
|
func TestCoreData_EpochZero(t *testing.T) {
|
|
assert.True(t, CoreData(0).IsZero())
|
|
}
|
|
|
|
func TestCoreData_NegativeReturnsZeroTime(t *testing.T) {
|
|
assert.True(t, CoreData(-1).IsZero())
|
|
}
|
|
|
|
func TestCoreData_OutOfJSONRangeReturnsZero(t *testing.T) {
|
|
assert.True(t, CoreData(maxCoreDataSeconds).IsZero(), "10000-01-01 is past what time.Time.MarshalJSON accepts")
|
|
encoded, err := CoreData(maxCoreDataSeconds - 1).MarshalJSON()
|
|
require.NoError(t, err)
|
|
assert.JSONEq(t, `"9999-12-31T23:59:59Z"`, string(encoded))
|
|
}
|
|
|
|
func TestCoreData_FractionalSecondsPreserved(t *testing.T) {
|
|
got := CoreData(float64(anchorCoreDataSeconds) + 0.5)
|
|
assert.Equal(t, 500*int64(time.Millisecond), int64(got.Nanosecond()))
|
|
}
|
|
|
|
func TestCoreData_AlwaysUTC(t *testing.T) {
|
|
assert.Same(t, time.UTC, CoreData(float64(anchorCoreDataSeconds)).Location())
|
|
}
|