152 lines
4.3 KiB
Go
152 lines
4.3 KiB
Go
//go:build windows
|
|
|
|
package packetbridge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func TestStartGuardSharedReadsAndExclusiveInstall(t *testing.T) {
|
|
dir := t.TempDir()
|
|
lockPath := filepath.Join(dir, "npcap-driver.lock")
|
|
statePath := filepath.Join(dir, "npcap-installer.json")
|
|
|
|
// 1. Multiple bridged VM starts (same process & cross-process) share the read lock concurrently.
|
|
rel1, err := acquireGuard(lockPath, statePath, false)
|
|
if err != nil {
|
|
t.Fatalf("first shared acquire: %v", err)
|
|
}
|
|
defer func() {
|
|
if rel1 != nil {
|
|
rel1()
|
|
}
|
|
}()
|
|
rel2, err := acquireGuard(lockPath, statePath, false)
|
|
if err != nil {
|
|
t.Fatalf("second concurrent shared acquire must succeed: %v", err)
|
|
}
|
|
defer func() {
|
|
if rel2 != nil {
|
|
rel2()
|
|
}
|
|
}()
|
|
|
|
// 2. While bridge starts hold shared locks, exclusive Npcap install is rejected with E_NETWORK_NETWORK_BUSY.
|
|
if relEx, err := acquireGuard(lockPath, statePath, true); err == nil {
|
|
relEx()
|
|
t.Fatal("exclusive install lock succeeded while shared start locks were held")
|
|
} else {
|
|
var coded *i18n.CodedError
|
|
if !errors.As(err, &coded) || coded.Code != "E_NETWORK_NETWORK_BUSY" {
|
|
t.Fatalf("expected E_NETWORK_NETWORK_BUSY, got %v", err)
|
|
}
|
|
}
|
|
|
|
// Cross-process shared start succeeds while shared lock is held.
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
helper := exec.CommandContext(ctx, exe, "-test.run=^TestStartGuardHelperProcess$")
|
|
helper.Env = append(os.Environ(),
|
|
"REIMS_TEST_GUARD_LOCK="+lockPath,
|
|
"REIMS_TEST_GUARD_STATE="+statePath,
|
|
"REIMS_TEST_GUARD_EXPECT=shared_ok",
|
|
)
|
|
if output, err := helper.CombinedOutput(); err != nil {
|
|
t.Fatalf("cross-process shared acquire failed: %v %s", err, output)
|
|
}
|
|
|
|
// Release both shared locks; exclusive install lock now succeeds.
|
|
rel1()
|
|
rel1 = nil
|
|
rel2()
|
|
rel2 = nil
|
|
|
|
relInstall, err := acquireGuard(lockPath, statePath, true)
|
|
if err != nil {
|
|
t.Fatalf("exclusive install acquire after release: %v", err)
|
|
}
|
|
defer func() {
|
|
if relInstall != nil {
|
|
relInstall()
|
|
}
|
|
}()
|
|
|
|
// 3. While exclusive install lock is held, cross-process bridged start is rejected with E_NETWORK_NPCAP_INSTALL_PENDING.
|
|
helper2 := exec.CommandContext(ctx, exe, "-test.run=^TestStartGuardHelperProcess$")
|
|
helper2.Env = append(os.Environ(),
|
|
"REIMS_TEST_GUARD_LOCK="+lockPath,
|
|
"REIMS_TEST_GUARD_STATE="+statePath,
|
|
"REIMS_TEST_GUARD_EXPECT=blocked_pending",
|
|
)
|
|
if output, err := helper2.CombinedOutput(); err != nil {
|
|
t.Fatalf("cross-process blocked-by-install check failed: %v %s", err, output)
|
|
}
|
|
|
|
relInstall()
|
|
relInstall = nil
|
|
|
|
// 4. Tracked installer PID persists block across manager restarts until that exact process exits.
|
|
selfH, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION|windows.SYNCHRONIZE, false, uint32(os.Getpid()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recordTrackedInstaller(statePath, selfH, exe)
|
|
_ = windows.CloseHandle(selfH)
|
|
|
|
if rel, err := acquireGuard(lockPath, statePath, false); err == nil {
|
|
rel()
|
|
t.Fatal("tracked live installer failed to block bridged start")
|
|
} else {
|
|
var coded *i18n.CodedError
|
|
if !errors.As(err, &coded) || coded.Code != "E_NETWORK_NPCAP_INSTALL_PENDING" {
|
|
t.Fatalf("expected E_NETWORK_NPCAP_INSTALL_PENDING, got %v", err)
|
|
}
|
|
}
|
|
clearTrackedInstaller(statePath)
|
|
|
|
relFinal, err := acquireGuard(lockPath, statePath, false)
|
|
if err != nil {
|
|
t.Fatalf("acquire after clearing tracked installer: %v", err)
|
|
}
|
|
relFinal()
|
|
}
|
|
|
|
func TestStartGuardHelperProcess(t *testing.T) {
|
|
lockPath := os.Getenv("REIMS_TEST_GUARD_LOCK")
|
|
statePath := os.Getenv("REIMS_TEST_GUARD_STATE")
|
|
expect := os.Getenv("REIMS_TEST_GUARD_EXPECT")
|
|
if lockPath == "" || expect == "" {
|
|
return
|
|
}
|
|
rel, err := acquireGuard(lockPath, statePath, false)
|
|
switch expect {
|
|
case "shared_ok":
|
|
if err != nil {
|
|
t.Fatalf("expected concurrent shared acquire to succeed, got %v", err)
|
|
}
|
|
rel()
|
|
case "blocked_pending":
|
|
if err == nil {
|
|
rel()
|
|
t.Fatal("expected bridged start to be blocked while exclusive install lock is held")
|
|
}
|
|
var coded *i18n.CodedError
|
|
if !errors.As(err, &coded) || coded.Code != "E_NETWORK_NPCAP_INSTALL_PENDING" {
|
|
t.Fatalf("expected E_NETWORK_NPCAP_INSTALL_PENDING, got %v", err)
|
|
}
|
|
}
|
|
}
|