fix(windows): start a responsive tray immediately after installation
This commit is contained in:
1 parent
14cb3570c1
commit
d59b2d5463
13 files changed
+850
-106
No files matched your search
@@ -0,0 +1,242 @@
|
||||
//go:build windows
|
||||
|
||||
package windowstray
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
winapi "golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
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 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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user