1 // Copyright 2012 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.
18 "golang.org/x/sys/windows/svc/mgr"
21 func TestOpenLanManServer(t *testing.T) {
22 m, err := mgr.Connect()
24 if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
25 t.Skip("Skipping test: we don't have rights to manage services.")
27 t.Fatalf("SCM connection failed: %s", err)
31 s, err := m.OpenService("LanmanServer")
33 t.Fatalf("OpenService(lanmanserver) failed: %s", err)
39 t.Fatalf("Config failed: %s", err)
43 func install(t *testing.T, m *mgr.Mgr, name, exepath string, c mgr.Config) {
44 // Sometimes it takes a while for the service to get
45 // removed after previous test run.
47 s, err := m.OpenService(name)
54 t.Fatalf("service %s already exists", name)
56 time.Sleep(300 * time.Millisecond)
59 s, err := m.CreateService(name, exepath, c)
61 t.Fatalf("CreateService(%s) failed: %v", name, err)
66 func depString(d []string) string {
71 d[i] = strings.ToLower(d[i])
73 ss := sort.StringSlice(d)
75 return strings.Join([]string(ss), " ")
78 func testConfig(t *testing.T, s *mgr.Service, should mgr.Config) mgr.Config {
81 t.Fatalf("Config failed: %s", err)
83 if should.DisplayName != is.DisplayName {
84 t.Fatalf("config mismatch: DisplayName is %q, but should have %q", is.DisplayName, should.DisplayName)
86 if should.StartType != is.StartType {
87 t.Fatalf("config mismatch: StartType is %v, but should have %v", is.StartType, should.StartType)
89 if should.Description != is.Description {
90 t.Fatalf("config mismatch: Description is %q, but should have %q", is.Description, should.Description)
92 if depString(should.Dependencies) != depString(is.Dependencies) {
93 t.Fatalf("config mismatch: Dependencies is %v, but should have %v", is.Dependencies, should.Dependencies)
98 func remove(t *testing.T, s *mgr.Service) {
101 t.Fatalf("Delete failed: %s", err)
105 func TestMyService(t *testing.T) {
107 t.Skip("skipping test in short mode - it modifies system services")
110 const name = "myservice"
112 m, err := mgr.Connect()
114 if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
115 t.Skip("Skipping test: we don't have rights to manage services.")
117 t.Fatalf("SCM connection failed: %s", err)
122 StartType: mgr.StartDisabled,
123 DisplayName: "my service",
124 Description: "my service is just a test",
125 Dependencies: []string{"LanmanServer", "W32Time"},
128 exename := os.Args[0]
129 exepath, err := filepath.Abs(exename)
131 t.Fatalf("filepath.Abs(%s) failed: %s", exename, err)
134 install(t, m, name, exepath, c)
136 s, err := m.OpenService(name)
138 t.Fatalf("service %s is not installed", name)
142 c.BinaryPathName = exepath
143 c = testConfig(t, s, c)
145 c.StartType = mgr.StartManual
146 err = s.UpdateConfig(c)
148 t.Fatalf("UpdateConfig failed: %v", err)
153 svcnames, err := m.ListServices()
155 t.Fatalf("ListServices failed: %v", err)
157 var myserviceIsInstalled bool
158 for _, sn := range svcnames {
160 myserviceIsInstalled = true
164 if !myserviceIsInstalled {
165 t.Errorf("ListServices failed to find %q service", name)