69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package host
|
|
|
|
import (
|
|
"encoding/json"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateIANATimezone(t *testing.T) {
|
|
for _, tz := range []string{"UTC", "Asia/Shanghai", "America/Argentina/Buenos_Aires", "Etc/GMT+8"} {
|
|
if !ValidateIANATimezone(tz) {
|
|
t.Errorf("valid timezone %q was rejected", tz)
|
|
}
|
|
}
|
|
for _, tz := range []string{"", "Local", "Mars/Olympus", strings.Repeat("A", 64), "..", "Asia/../UTC", "/UTC", "-UTC", "Asia/", "Asia//Shanghai", "a/b/c/d",
|
|
"Asia/Hong Kong", "Asia/Shanghai\n", "UTC;date", "UTC$(date)", strings.Repeat("A", 65)} {
|
|
if ValidateIANATimezone(tz) {
|
|
t.Errorf("invalid timezone %q was accepted", tz)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsIANATimezoneMapping(t *testing.T) {
|
|
for id, want := range map[string]string{
|
|
"China Standard Time": "Asia/Shanghai",
|
|
"Tokyo Standard Time": "Asia/Tokyo",
|
|
"Pacific Standard Time": "America/Los_Angeles",
|
|
"UTC": "Etc/UTC",
|
|
"GMT Standard Time": "Europe/London",
|
|
"AUS Eastern Standard Time": "Australia/Sydney",
|
|
} {
|
|
if got := windowsIANATimezones[id]; got != want {
|
|
t.Errorf("%q mapped to %q, want %q", id, got, want)
|
|
}
|
|
}
|
|
for id, tz := range windowsIANATimezones {
|
|
if !ValidateIANATimezone(tz) {
|
|
t.Errorf("%q mapped to invalid timezone %q", id, tz)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDetectHostTimezone(t *testing.T) {
|
|
if runtime.GOOS != "windows" {
|
|
t.Setenv("TZ", "Asia/Shanghai")
|
|
}
|
|
tz := DetectHostTimezone()
|
|
if !ValidateIANATimezone(tz) || tz == "Local" || tz == "Etc/Unknown" {
|
|
t.Fatalf("DetectHostTimezone() = %q", tz)
|
|
}
|
|
t.Logf("detected host timezone %s", tz)
|
|
inspected := Inspect(t.TempDir())
|
|
if inspected.Timezone != tz {
|
|
t.Fatalf("Inspect().Timezone = %q, want %q", inspected.Timezone, tz)
|
|
}
|
|
data, err := json.Marshal(inspected)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var report Report
|
|
if err := json.Unmarshal(data, &report); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if report.Timezone != tz {
|
|
t.Errorf("report timezone = %q, want %q", report.Timezone, tz)
|
|
}
|
|
}
|