| 1 | package managedproxy |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | |
| 11 | func TestStopWindowsCoreForgetsReusedPID(t *testing.T) { |
| 12 | exe, err := os.Executable() |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | core := filepath.Join(filepath.Dir(exe), "mihomo.exe") |
| 17 | if err := os.WriteFile(core, []byte("placeholder"), 0700); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | t.Cleanup(func() { _ = os.Remove(core) }) |
| 21 | for name, pid := range map[string]uint32{"same user": uint32(os.Getpid()), "system": 4} { |
| 22 | t.Run(name, func(t *testing.T) { |
| 23 | profile := t.TempDir() |
| 24 | b, _ := json.Marshal(windowsCoreRecord{PID: pid, Executable: core, Created: 1}) |
| 25 | if err := Write(windowsCoreRecordPath(profile), b); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if err := stopWindowsCore(profile); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if _, err := os.Lstat(windowsCoreRecordPath(profile)); !os.IsNotExist(err) { |
| 32 | t.Fatal("stale core record was kept") |
| 33 | } |
| 34 | }) |
| 35 | } |
| 36 | } |