Skip to content

Commit 1911637

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
windows/svc: use separate (and more descriptive) service names in tests
Notably, the DisplayName field was set to the same thing in both sys.TestExample and mrg.TestMyService, which may explain the collision reported in golang/go#59298. Moreover, the adjective ”my” conveys no information whatsoever — we shouldn't use it in tests or examples. Also skip the tests that install services if GO_BUILDER_NAME is not set, to reduce the likelihood of 'go test all' in a user's working directory being mistaken for a malicious or compromised program. Fixes golang/go#59298. Change-Id: Ib00bf7400bfaa34e1a1d49125c43b97019b53c82 Reviewed-on: https://go-review.googlesource.com/c/sys/+/481015 Reviewed-by: Carlos Amedee <carlos@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
1 parent ca59eda commit 1911637

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

windows/svc/example/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"flag"
1819
"fmt"
1920
"log"
2021
"os"
@@ -33,8 +34,11 @@ func usage(errmsg string) {
3334
os.Exit(2)
3435
}
3536

37+
var svcName = "exampleservice"
38+
3639
func main() {
37-
const svcName = "myservice"
40+
flag.StringVar(&svcName, "name", svcName, "name of the service")
41+
flag.Parse()
3842

3943
inService, err := svc.IsWindowsService()
4044
if err != nil {
@@ -55,7 +59,7 @@ func main() {
5559
runService(svcName, true)
5660
return
5761
case "install":
58-
err = installService(svcName, "my service")
62+
err = installService(svcName, "example service")
5963
case "remove":
6064
err = removeService(svcName)
6165
case "start":

windows/svc/example/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919

2020
var elog debug.Log
2121

22-
type myservice struct{}
22+
type exampleService struct{}
2323

24-
func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
24+
func (m *exampleService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
2525
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
2626
changes <- svc.Status{State: svc.StartPending}
2727
fasttick := time.Tick(500 * time.Millisecond)
@@ -79,7 +79,7 @@ func runService(name string, isDebug bool) {
7979
if isDebug {
8080
run = debug.Run
8181
}
82-
err = run(name, &myservice{})
82+
err = run(name, &exampleService{})
8383
if err != nil {
8484
elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err))
8585
return

windows/svc/mgr/mgr_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,26 @@ func remove(t *testing.T, s *mgr.Service) {
227227
}
228228

229229
func TestMyService(t *testing.T) {
230+
if os.Getenv("GO_BUILDER_NAME") == "" {
231+
// Don't install services on arbitrary users' machines.
232+
t.Skip("skipping test that modifies system services: GO_BUILDER_NAME not set")
233+
}
230234
if testing.Short() {
231-
t.Skip("skipping test in short mode - it modifies system services")
235+
t.Skip("skipping test in short mode that modifies system services")
232236
}
233237

234-
const name = "mymgrservice"
238+
const name = "mgrtestservice"
235239

236240
m, err := mgr.Connect()
237241
if err != nil {
238-
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
239-
t.Skip("Skipping test: we don't have rights to manage services.")
240-
}
241242
t.Fatalf("SCM connection failed: %s", err)
242243
}
243244
defer m.Disconnect()
244245

245246
c := mgr.Config{
246247
StartType: mgr.StartDisabled,
247-
DisplayName: "my service",
248-
Description: "my service is just a test",
248+
DisplayName: "x-sys mgr test service",
249+
Description: "x-sys mgr test service is just a test",
249250
Dependencies: []string{"LanmanServer", "W32Time"},
250251
}
251252

@@ -288,14 +289,14 @@ func TestMyService(t *testing.T) {
288289
if err != nil {
289290
t.Fatalf("ListServices failed: %v", err)
290291
}
291-
var myserviceIsInstalled bool
292+
var serviceIsInstalled bool
292293
for _, sn := range svcnames {
293294
if sn == name {
294-
myserviceIsInstalled = true
295+
serviceIsInstalled = true
295296
break
296297
}
297298
}
298-
if !myserviceIsInstalled {
299+
if !serviceIsInstalled {
299300
t.Errorf("ListServices failed to find %q service", name)
300301
}
301302

windows/svc/svc_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ func stopAndDeleteIfInstalled(t *testing.T, m *mgr.Mgr, name string) {
7777
}
7878

7979
func TestExample(t *testing.T) {
80-
if testing.Short() && os.Getenv("GO_BUILDER_NAME") != "" {
81-
t.Skip("skipping test in short mode - it modifies system services")
80+
if os.Getenv("GO_BUILDER_NAME") == "" {
81+
// Don't install services on arbitrary users' machines.
82+
t.Skip("skipping test that modifies system services: GO_BUILDER_NAME not set")
83+
}
84+
if testing.Short() {
85+
t.Skip("skipping test in short mode that modifies system services")
8286
}
8387

84-
const name = "myservice"
88+
const name = "svctestservice"
8589

8690
m, err := mgr.Connect()
8791
if err != nil {
@@ -103,7 +107,7 @@ func TestExample(t *testing.T) {
103107

104108
stopAndDeleteIfInstalled(t, m, name)
105109

106-
s, err := m.CreateService(name, exepath, mgr.Config{DisplayName: "my service"}, "is", "auto-started")
110+
s, err := m.CreateService(name, exepath, mgr.Config{DisplayName: "x-sys svc test service"}, "-name", name)
107111
if err != nil {
108112
t.Fatalf("CreateService(%s) failed: %v", name, err)
109113
}
@@ -141,15 +145,15 @@ func TestExample(t *testing.T) {
141145
t.Fatalf("Delete failed: %s", err)
142146
}
143147

144-
out, err := exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='myservice']]]", "/rd:true", "/c:10").CombinedOutput()
148+
out, err := exec.Command("wevtutil.exe", "qe", "Application", "/q:*[System[Provider[@Name='"+name+"']]]", "/rd:true", "/c:10").CombinedOutput()
145149
if err != nil {
146150
t.Fatalf("wevtutil failed: %v\n%v", err, string(out))
147151
}
148152
want := strings.Join(append([]string{name}, args...), "-")
149153
// Test context passing (see servicemain in sys_386.s and sys_amd64.s).
150154
want += "-123456"
151155
if !strings.Contains(string(out), want) {
152-
t.Errorf("%q string does not contain %q", string(out), want)
156+
t.Errorf("%q string does not contain %q", out, want)
153157
}
154158
}
155159

0 commit comments

Comments
 (0)