os/user: test that Current does not depend on netapi32.dll

Updates #21867.

Change-Id: I1eb923ef66aa0f338bfa0d683159edc1d8ae2a6c
Reviewed-on: https://go-review.googlesource.com/c/go/+/604415
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
qmuntal 2024-08-09 10:57:51 +02:00 committed by Quim Muntal
parent 36b45bca66
commit 5d82dbb59c
3 changed files with 50 additions and 0 deletions

View File

@ -500,3 +500,5 @@ func QueryPerformanceCounter() int64 // Implemented in runtime package.
//
//go:linkname QueryPerformanceFrequency
func QueryPerformanceFrequency() int64 // Implemented in runtime package.
//sys GetModuleHandle(modulename *uint16) (handle syscall.Handle, err error) = kernel32.GetModuleHandleW

View File

@ -69,6 +69,7 @@ var (
procGetFileInformationByHandleEx = modkernel32.NewProc("GetFileInformationByHandleEx")
procGetFinalPathNameByHandleW = modkernel32.NewProc("GetFinalPathNameByHandleW")
procGetModuleFileNameW = modkernel32.NewProc("GetModuleFileNameW")
procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW")
procGetTempPath2W = modkernel32.NewProc("GetTempPath2W")
procGetVolumeInformationByHandleW = modkernel32.NewProc("GetVolumeInformationByHandleW")
procGetVolumeNameForVolumeMountPointW = modkernel32.NewProc("GetVolumeNameForVolumeMountPointW")
@ -287,6 +288,15 @@ func GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32,
return
}
func GetModuleHandle(modulename *uint16) (handle syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modulename)), 0, 0)
handle = syscall.Handle(r0)
if handle == 0 {
err = errnoErr(e1)
}
return
}
func GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) {
r0, _, e1 := syscall.Syscall(procGetTempPath2W.Addr(), 2, uintptr(buflen), uintptr(unsafe.Pointer(buf)), 0)
n = uint32(r0)

View File

@ -8,7 +8,11 @@ import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"internal/syscall/windows"
"internal/testenv"
"os"
"os/exec"
"runtime"
"strconv"
"syscall"
@ -143,3 +147,37 @@ func TestImpersonated(t *testing.T) {
}
compare(t, want, got)
}
func TestCurrentNetapi32(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
// Test that Current does not load netapi32.dll.
// First call Current.
Current()
// Then check if netapi32.dll is loaded.
netapi32, err := syscall.UTF16PtrFromString("netapi32.dll")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
os.Exit(9)
return
}
mod, _ := windows.GetModuleHandle(netapi32)
if mod != 0 {
fmt.Fprintf(os.Stderr, "netapi32.dll is loaded\n")
os.Exit(9)
return
}
os.Exit(0)
return
}
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
cmd := testenv.CleanCmdEnv(exec.Command(exe, "-test.run=^TestCurrentNetapi32$"))
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%v\n%s", err, out)
}
}