301 lines
10 KiB
Go
301 lines
10 KiB
Go
//go:build windows
|
|
|
|
package windowstray
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/rvbox/rvbox/internal/client/windowsservice"
|
|
winapi "golang.org/x/sys/windows"
|
|
)
|
|
|
|
func TestNativeTrayReadOnlyServiceQuery_HP_WINTRAY_07(t *testing.T) {
|
|
status, err := windowsservice.Query()
|
|
if err != nil {
|
|
t.Fatalf("unprivileged read-only SCM query: %v", err)
|
|
}
|
|
if !status.Installed && status.State != windowsservice.StateStopped {
|
|
t.Fatalf("missing service state=%#v", status)
|
|
}
|
|
if status.Installed && status.State == windowsservice.StateUnknown {
|
|
t.Fatalf("installed service state=%#v", status)
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayMenuFlagsAndRefresh_HP_WINTRAY_08(t *testing.T) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
menu, _, err := procCreatePopup.Call()
|
|
if menu == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
defer procDestroyMenu.Call(menu)
|
|
w := &trayWindow{menu: menu, status: MenuStatus{Label: "Status: running", AutoStart: true, StartupKnown: true, CanStop: true}}
|
|
for _, item := range trayMenu(w.status) {
|
|
flags, text := menuItemFlags(item)
|
|
if ok, _, err := procAppendMenu.Call(menu, flags, uintptr(item.Action), uintptr(unsafe.Pointer(text))); ok == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
getState := user32Tray.NewProc("GetMenuState")
|
|
banner, _, _ := getState.Call(menu, 0, trayMFByPosition)
|
|
auto, _, _ := getState.Call(menu, 7, trayMFByPosition)
|
|
if banner == ^uintptr(0) || banner&trayMFDisabled == 0 || auto&trayMFChecked == 0 {
|
|
t.Fatalf("initial menu flags banner=%#x auto=%#x", banner, auto)
|
|
}
|
|
w.status = MenuStatus{Label: "Status: stopped", StartupKnown: true, CanStart: true}
|
|
w.refreshMenu()
|
|
auto, _, _ = getState.Call(menu, 7, trayMFByPosition)
|
|
start, _, _ := getState.Call(menu, 5, trayMFByPosition)
|
|
stop, _, _ := getState.Call(menu, 6, trayMFByPosition)
|
|
if auto&trayMFChecked != 0 || start&trayMFDisabled != 0 || stop&trayMFDisabled == 0 {
|
|
t.Fatalf("updated menu flags start=%#x stop=%#x auto=%#x", start, stop, auto)
|
|
}
|
|
}
|
|
|
|
func trayTestName() string {
|
|
return fmt.Sprintf(`\\.\pipe\rvbox-tray-test-%d-%d`, winapi.GetCurrentProcessId(), time.Now().UnixNano())
|
|
}
|
|
|
|
func trayTestServer(t *testing.T, handler func(context.Context, winapi.Handle)) string {
|
|
t.Helper()
|
|
name := trayTestName()
|
|
pipe, err := newTrayPipe(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
defer winapi.CloseHandle(pipe)
|
|
if connectTrayPipe(ctx, pipe) == nil {
|
|
handler(ctx, pipe)
|
|
}
|
|
}()
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Error("pipe server did not shut down")
|
|
}
|
|
})
|
|
return name
|
|
}
|
|
|
|
func TestNativeTrayRequestRoundTrip_HP_WINTRAY_02(t *testing.T) {
|
|
var session uint32
|
|
if err := winapi.ProcessIdToSessionId(winapi.GetCurrentProcessId(), &session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := make(chan Peer, 1)
|
|
name := trayTestServer(t, func(ctx context.Context, pipe winapi.Handle) {
|
|
// serveTrayPipe owns its handle; the test server owns the original.
|
|
var duplicate winapi.Handle
|
|
process := winapi.CurrentProcess()
|
|
if err := winapi.DuplicateHandle(process, pipe, process, &duplicate, 0, false, winapi.DUPLICATE_SAME_ACCESS); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
serveTrayPipe(ctx, duplicate, func(_ context.Context, peer Peer, _ Frame) (Frame, error) {
|
|
seen <- peer
|
|
return Frame{Action: ActionStatus, Payload: []byte("native status OK")}, nil
|
|
})
|
|
})
|
|
response, err := requestNamedTrayPipe(context.Background(), name, Frame{Action: ActionStatus})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if session == 0 {
|
|
if !strings.Contains(string(response.Payload), "not interactive") {
|
|
t.Fatalf("Session 0 rejection = %q", response.Payload)
|
|
}
|
|
return
|
|
}
|
|
if string(response.Payload) != "native status OK" {
|
|
t.Fatalf("response = %q", response.Payload)
|
|
}
|
|
peer := <-seen
|
|
if !peer.TokenVerified || !peer.Interactive || peer.SessionID != session || peer.PID != winapi.GetCurrentProcessId() {
|
|
t.Fatalf("peer = %#v", peer)
|
|
}
|
|
var token winapi.Token
|
|
if err := winapi.OpenProcessToken(winapi.CurrentProcess(), winapi.TOKEN_QUERY, &token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer token.Close()
|
|
if peer.Administrator != token.IsElevated() {
|
|
t.Fatalf("enabled administrator membership=%t, elevated=%t", peer.Administrator, token.IsElevated())
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayStalledServerDeadline_BH_WINTRAY_02(t *testing.T) {
|
|
name := trayTestServer(t, func(ctx context.Context, _ winapi.Handle) { <-ctx.Done() })
|
|
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
|
|
defer cancel()
|
|
start := time.Now()
|
|
_, err := requestNamedTrayPipe(ctx, name, Frame{Action: ActionStatus})
|
|
if !errors.Is(err, context.DeadlineExceeded) || time.Since(start) > 2*time.Second {
|
|
t.Fatalf("stalled request = %v after %s", err, time.Since(start))
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayMalformedResponse_BH_WINTRAY_03(t *testing.T) {
|
|
name := trayTestServer(t, func(ctx context.Context, pipe winapi.Handle) {
|
|
if _, err := readTrayFrame(ctx, pipe); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
_, _ = transferTrayPipe(ctx, pipe, []byte("invalid frame"), true)
|
|
_, _ = transferTrayPipe(ctx, pipe, make([]byte, 1), false)
|
|
})
|
|
_, err := requestNamedTrayPipe(context.Background(), name, Frame{Action: ActionStatus})
|
|
if !errors.Is(err, ErrInvalidFrame) {
|
|
t.Fatalf("malformed response = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayRejectionBeforeRead_BH_WINTRAY_05(t *testing.T) {
|
|
name := trayTestServer(t, func(ctx context.Context, pipe winapi.Handle) {
|
|
// Reproduce an identity rejection before reading the request. A client
|
|
// FlushFileBuffers here would wait forever for a server read, while the
|
|
// server's flush waits for a client response read.
|
|
writeTrayResponse(ctx, pipe, ErrInvalidPeer)
|
|
buffer := make([]byte, maxFrameBytes)
|
|
for {
|
|
if _, err := transferTrayPipe(ctx, pipe, buffer, false); err != nil {
|
|
return
|
|
}
|
|
}
|
|
})
|
|
response, err := requestNamedTrayPipe(context.Background(), name, Frame{Action: ActionStatus})
|
|
if err != nil || !strings.Contains(string(response.Payload), ErrInvalidPeer.Error()) {
|
|
t.Fatalf("pre-read rejection = %q, %v", response.Payload, err)
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayIdlePeersBoundedShutdown_BH_WINTRAY_04(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
name := trayTestName()
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- serveNamedTrayPipe(ctx, name, func(context.Context, Peer, Frame) (Frame, error) { return Frame{Action: ActionStatus}, nil })
|
|
}()
|
|
var clients []winapi.Handle
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
for _, handle := range clients {
|
|
_ = winapi.CloseHandle(handle)
|
|
}
|
|
})
|
|
path, _ := winapi.UTF16PtrFromString(name)
|
|
for i := 0; i < pipeInstances; i++ {
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for {
|
|
handle, err := winapi.CreateFile(path, winapi.GENERIC_READ|winapi.GENERIC_WRITE, 0, nil, winapi.OPEN_EXISTING, winapi.FILE_FLAG_OVERLAPPED, 0)
|
|
if err == nil {
|
|
clients = append(clients, handle)
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|
|
requestCtx, requestCancel := context.WithTimeout(ctx, 150*time.Millisecond)
|
|
_, err := requestNamedTrayPipe(requestCtx, name, Frame{Action: ActionStatus})
|
|
requestCancel()
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("ninth stalled peer = %v", err)
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("service did not cancel its accept and idle reads")
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayIconLayout_HP_WINTRAY_03(t *testing.T) {
|
|
if runtime.GOARCH != "amd64" {
|
|
t.Skip("v1 Windows executable is amd64")
|
|
}
|
|
var icon trayIconData
|
|
if unsafe.Sizeof(icon) != 976 || unsafe.Offsetof(icon.InfoData) != 304 || unsafe.Offsetof(icon.TimeoutOrVersion) != 816 || unsafe.Offsetof(icon.InfoTitle) != 820 || unsafe.Offsetof(icon.InfoFlags) != 948 || unsafe.Offsetof(icon.Guid) != 952 {
|
|
t.Fatalf("NOTIFYICONDATAW layout mismatch: size=%d info=%d title=%d flags=%d", unsafe.Sizeof(icon), unsafe.Offsetof(icon.InfoData), unsafe.Offsetof(icon.InfoTitle), unsafe.Offsetof(icon.InfoFlags))
|
|
}
|
|
}
|
|
|
|
func TestNativeInstalledTrayReadiness_BH_WINTRAY_07(t *testing.T) {
|
|
class, _ := winapi.UTF16PtrFromString("RVBoxTrayWindowV1")
|
|
hwnd, _, _ := procFindWindow.Call(uintptr(unsafe.Pointer(class)), 0)
|
|
if hwnd == 0 {
|
|
t.Skip("requires an installed tray on this interactive desktop")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
if err := WaitReady(ctx); err != nil {
|
|
t.Fatalf("existing installed icon was falsely reported unready: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNativeTrayWindowLifecycle_HP_WINTRAY_04(t *testing.T) {
|
|
var session uint32
|
|
if err := winapi.ProcessIdToSessionId(winapi.GetCurrentProcessId(), &session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if session == 0 {
|
|
t.Skip("requires the real interactive Windows fixture desktop")
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
ready := make(chan *trayWindow, 1)
|
|
done := make(chan error, 1)
|
|
go runTrayMessageLoop(ctx, io.Discard, "RVBox native tray test", ready, done, "")
|
|
var window *trayWindow
|
|
select {
|
|
case window = <-ready:
|
|
case err := <-done:
|
|
t.Fatalf("tray startup failed: %v", err)
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("tray startup blocked before creating its icon")
|
|
}
|
|
defer procPostMessage.Call(window.hwnd, wmClose, 0, 0)
|
|
readyCtx, readyCancel := context.WithTimeout(ctx, time.Second)
|
|
defer readyCancel()
|
|
if err := WaitReady(readyCtx); err != nil {
|
|
t.Fatalf("Shell did not confirm its registered icon: %v", err)
|
|
}
|
|
send := user32Tray.NewProc("SendMessageTimeoutW")
|
|
for i := 0; i < 10; i++ {
|
|
runtime.GC()
|
|
var result uintptr
|
|
ok, _, err := send.Call(window.hwnd, 0, 0, 0, 2, 1000, uintptr(unsafe.Pointer(&result)))
|
|
if ok == 0 {
|
|
t.Fatalf("window message loop stopped responding after GC: %v", err)
|
|
}
|
|
}
|
|
_, _, _ = procPostMessage.Call(window.hwnd, wmClose, 0, 0)
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("tray did not process close on its owning thread")
|
|
}
|
|
}
|