1
0
mirror of https://github.com/golang/go synced 2024-11-17 21:04:43 -07:00

os: fix test failure when Workstation service is not enabled

TestNetworkSymbolicLink needs to enable the Workstation service, otherwise it will fail.

This CL avoids failure by skipping testing when the Workstation service is not enabled.

Fixes #61467

Change-Id: I395952fc18329e0b0dfdec55c8a18f4007ea91de

Change-Id: I395952fc18329e0b0dfdec55c8a18f4007ea91de
GitHub-Last-Rev: 7f089d1dff
GitHub-Pull-Request: golang/go#61564
Reviewed-on: https://go-review.googlesource.com/c/go/+/512736
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
qiulaidongfeng 2023-07-29 08:27:21 +00:00 committed by Alex Brainman
parent 088f953fd0
commit 0236686382
3 changed files with 71 additions and 0 deletions

View File

@ -401,3 +401,22 @@ type FILE_ID_BOTH_DIR_INFO struct {
//sys RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) = kernel32.RtlLookupFunctionEntry
//sys RtlVirtualUnwind(handlerType uint32, baseAddress uintptr, pc uintptr, entry uintptr, ctxt uintptr, data *uintptr, frame *uintptr, ctxptrs *byte) (ret uintptr) = kernel32.RtlVirtualUnwind
type SERVICE_STATUS struct {
ServiceType uint32
CurrentState uint32
ControlsAccepted uint32
Win32ExitCode uint32
ServiceSpecificExitCode uint32
CheckPoint uint32
WaitHint uint32
}
const (
SERVICE_RUNNING = 4
SERVICE_QUERY_STATUS = 4
)
//sys OpenService(mgr syscall.Handle, serviceName *uint16, access uint32) (handle syscall.Handle, err error) = advapi32.OpenServiceW
//sys QueryServiceStatus(hService syscall.Handle, lpServiceStatus *SERVICE_STATUS) (err error) = advapi32.QueryServiceStatus
//sys OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle syscall.Handle, err error) [failretval==0] = advapi32.OpenSCManagerW

View File

@ -49,7 +49,10 @@ var (
procDuplicateTokenEx = modadvapi32.NewProc("DuplicateTokenEx")
procImpersonateSelf = modadvapi32.NewProc("ImpersonateSelf")
procLookupPrivilegeValueW = modadvapi32.NewProc("LookupPrivilegeValueW")
procOpenSCManagerW = modadvapi32.NewProc("OpenSCManagerW")
procOpenServiceW = modadvapi32.NewProc("OpenServiceW")
procOpenThreadToken = modadvapi32.NewProc("OpenThreadToken")
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
@ -121,6 +124,24 @@ func LookupPrivilegeValue(systemname *uint16, name *uint16, luid *LUID) (err err
return
}
func OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procOpenSCManagerW.Addr(), 3, uintptr(unsafe.Pointer(machineName)), uintptr(unsafe.Pointer(databaseName)), uintptr(access))
handle = syscall.Handle(r0)
if handle == 0 {
err = errnoErr(e1)
}
return
}
func OpenService(mgr syscall.Handle, serviceName *uint16, access uint32) (handle syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procOpenServiceW.Addr(), 3, uintptr(mgr), uintptr(unsafe.Pointer(serviceName)), uintptr(access))
handle = syscall.Handle(r0)
if handle == 0 {
err = errnoErr(e1)
}
return
}
func OpenThreadToken(h syscall.Handle, access uint32, openasself bool, token *syscall.Token) (err error) {
var _p0 uint32
if openasself {
@ -133,6 +154,14 @@ func OpenThreadToken(h syscall.Handle, access uint32, openasself bool, token *sy
return
}
func QueryServiceStatus(hService syscall.Handle, lpServiceStatus *SERVICE_STATUS) (err error) {
r1, _, e1 := syscall.Syscall(procQueryServiceStatus.Addr(), 2, uintptr(hService), uintptr(unsafe.Pointer(lpServiceStatus)), 0)
if r1 == 0 {
err = errnoErr(e1)
}
return
}
func RevertToSelf() (err error) {
r1, _, e1 := syscall.Syscall(procRevertToSelf.Addr(), 0, 0, 0, 0)
if r1 == 0 {

View File

@ -427,6 +427,28 @@ func TestDirectorySymbolicLink(t *testing.T) {
testDirLinks(t, tests)
}
func mustHaveWorkstation(t *testing.T) {
mar, err := windows.OpenSCManager(nil, nil, windows.SERVICE_QUERY_STATUS)
if err != nil {
return
}
defer syscall.CloseHandle(mar)
//LanmanWorkstation is the service name, and Workstation is the display name.
srv, err := windows.OpenService(mar, syscall.StringToUTF16Ptr("LanmanWorkstation"), windows.SERVICE_QUERY_STATUS)
if err != nil {
return
}
defer syscall.CloseHandle(srv)
var state windows.SERVICE_STATUS
err = windows.QueryServiceStatus(srv, &state)
if err != nil {
return
}
if state.CurrentState != windows.SERVICE_RUNNING {
t.Skip("Requires the Windows service Workstation, but it is detected that it is not enabled.")
}
}
func TestNetworkSymbolicLink(t *testing.T) {
testenv.MustHaveSymlink(t)
@ -498,6 +520,7 @@ func TestNetworkSymbolicLink(t *testing.T) {
}
fi2, err := os.Stat(UNCPath)
if err != nil {
mustHaveWorkstation(t)
t.Fatal(err)
}
if !os.SameFile(fi1, fi2) {