Debian Codegraph @

src/os/exec_windows_test.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build windows
6
7 package os_test
8
9 import (
10 "internal/testenv"
11 "io"
12 . "os"
13 "path/filepath"
14 "sync"
15 "syscall"
16 "testing"
17 "time"
18 )
19
20 func TestRemoveAllWithExecutedProcess(t *testing.T) {
21 // Regression test for golang.org/issue/25965.
22 if testing.Short() {
23 t.Skip("slow test; skipping")
24 }
25 testenv.MustHaveExec(t)
26
27 name, err := Executable()
28 if err != nil {
29 t.Fatal(err)
30 }
31 r, err := Open(name)
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer r.Close()
36 const n = 100
37 var execs [n]string
38 // First create n executables.
39 for i := 0; i < n; i++ {
40 // Rewind r.
41 if _, err := r.Seek(0, io.SeekStart); err != nil {
42 t.Fatal(err)
43 }
44 name := filepath.Join(t.TempDir(), "test.exe")
45 execs[i] = name
46 w, err := Create(name)
47 if err != nil {
48 t.Fatal(err)
49 }
50 if _, err = io.Copy(w, r); err != nil {
51 w.Close()
52 t.Fatal(err)
53 }
54 if err := w.Sync(); err != nil {
55 w.Close()
56 t.Fatal(err)
57 }
58 if err = w.Close(); err != nil {
59 t.Fatal(err)
60 }
61 }
62 // Then run each executable and remove its directory.
63 // Run each executable in a separate goroutine to add some load
64 // and increase the chance of triggering the bug.
65 var wg sync.WaitGroup
66 wg.Add(n)
67 for i := 0; i < n; i++ {
68 go func(i int) {
69 defer wg.Done()
70 name := execs[i]
71 dir := filepath.Dir(name)
72 // Run test.exe without executing any test, just to make it do something.
73 cmd := testenv.Command(t, name, "-test.run=^$")
74 if err := cmd.Run(); err != nil {
75 t.Errorf("exec failed: %v", err)
76 }
77 // Remove dir and check that it doesn't return `ERROR_ACCESS_DENIED`.
78 err = RemoveAll(dir)
79 if err != nil {
80 t.Errorf("RemoveAll failed: %v", err)
81 }
82 }(i)
83 }
84 wg.Wait()
85 }
86
87 func TestProcessWithHandleWindows(t *testing.T) {
88 const envVar = "OSTEST_PROCESS_WITH_HANDLE"
89 if Getenv(envVar) != "" {
90 time.Sleep(1 * time.Minute)
91 return
92 }
93
94 cmd := testenv.CommandContext(t, t.Context(), testenv.Executable(t), "-test.run=^"+t.Name()+"$")
95 cmd = testenv.CleanCmdEnv(cmd)
96 cmd.Env = append(cmd.Env, envVar+"=1")
97 if err := cmd.Start(); err != nil {
98 t.Fatal(err)
99 }
100 defer func() {
101 cmd.Process.Kill()
102 cmd.Wait()
103 }()
104
105 called := false
106 err := cmd.Process.WithHandle(func(handle uintptr) {
107 called = true
108 // Check the handle is valid.
109 var u syscall.Rusage
110 e := syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime)
111 if e != nil {
112 t.Errorf("Using process handle failed: %v", NewSyscallError("GetProcessTimes", e))
113 }
114 })
115 if err != nil {
116 t.Fatalf("WithHandle: got error %v, want nil", err)
117 }
118 if !called {
119 t.Fatal("WithHandle did not call the callback function")
120 }
121 }