| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "encoding/binary" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/sylvinus/agent-vm/internal/cage" |
| 15 | ) |
| 16 | |
| 17 | func TestLinuxTimezoneRejectsStaleSystemFile(t *testing.T) { |
| 18 | if runtime.GOOS == "windows" { |
| 19 | t.Skip("Linux probe uses POSIX TZ semantics; native Windows settings are tested separately") |
| 20 | } |
| 21 | python, err := exec.LookPath("python3") |
| 22 | if err != nil { |
| 23 | t.Skip("python3 not installed") |
| 24 | } |
| 25 | directory := t.TempDir() |
| 26 | localtime := filepath.Join(directory, "localtime") |
| 27 | timezone := filepath.Join(directory, "timezone") |
| 28 | if err := os.WriteFile(timezone, []byte("Asia/Shanghai\n"), 0600); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | script := strings.ReplaceAll(guestSafetyProbe, "/etc/localtime", filepath.ToSlash(localtime)) |
| 34 | script = strings.ReplaceAll(script, "/etc/timezone", filepath.ToSlash(timezone)) |
| 35 | zoneDirectory := filepath.Join(directory, "zoneinfo", "Asia") |
| 36 | if err := os.MkdirAll(zoneDirectory, 0700); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | shanghai := fixedOffsetTZif(28800) |
| 40 | if err := os.WriteFile(filepath.Join(zoneDirectory, "Shanghai"), shanghai, 0600); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | zonePath, _ := json.Marshal([]string{filepath.Dir(zoneDirectory)}) |
| 44 | script = "import zoneinfo; zoneinfo.reset_tzpath(" + string(zonePath) + ")\n" + script |
| 45 | input, _ := json.Marshal(map[string]any{"stage": "environment", "timezone": "Asia/Shanghai"}) |
| 46 | config := cage.DefaultConfig() |
| 47 | config.Timezone = "Asia/Shanghai" |
| 48 | config.Locale = "C" |
| 49 | for _, tc := range []struct { |
| 50 | name string |
| 51 | data []byte |
| 52 | want bool |
| 53 | }{ |
| 54 | {"stale UTC file", fixedOffsetTZif(0), false}, |
| 55 | {"matching regular file", shanghai, true}, |
| 56 | {"unreadable zone data", []byte("invalid tzif"), false}, |
| 57 | {"missing file", nil, false}, |
| 58 | } { |
| 59 | t.Run(tc.name, func(t *testing.T) { |
| 60 | if tc.data == nil { |
| 61 | if err := os.Remove(localtime); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | } else if err := os.WriteFile(localtime, tc.data, 0600); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | command := exec.Command(python, "-I", "-c", script, base64.StdEncoding.EncodeToString(input)) |
| 68 | command.Env = append(os.Environ(), "TZ=Asia/Shanghai", "LANG=C", "LC_ALL=C") |
| 69 | output, err := command.CombinedOutput() |
| 70 | if err != nil { |
| 71 | t.Fatalf("probe failed: %v %s", err, output) |
| 72 | } |
| 73 | var observation guestObservation |
| 74 | if err := json.Unmarshal(output, &observation); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if got := guestSettingsMatch(observation, config); got != tc.want { |
| 78 | t.Fatalf("settings match=%t want=%t: %s", got, tc.want, output) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | func fixedOffsetTZif(offset int32) []byte { |
| 86 | data := make([]byte, 54) |
| 87 | copy(data, "TZif") |
| 88 | binary.BigEndian.PutUint32(data[36:40], 1) |
| 89 | binary.BigEndian.PutUint32(data[40:44], 4) |
| 90 | binary.BigEndian.PutUint32(data[44:48], uint32(offset)) |
| 91 | copy(data[50:], "FIX\x00") |
| 92 | return data |
| 93 | } |