110 lines
4.9 KiB
Go
110 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/rvbox/rvbox/internal/client/windowsservice"
|
|
)
|
|
|
|
func TestInstallerVersionAwareLifecycle_HP_WINCLI_10(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
state installerSnapshot
|
|
want []string
|
|
installed bool
|
|
}{
|
|
{"fresh", installerSnapshot{}, []string{"inspect", "install", "tray"}, true},
|
|
{"upgrade", installerSnapshot{Installed: true, ServiceState: windowsservice.StateRunning, TrayReady: true}, []string{"inspect", "install", "tray"}, true},
|
|
{"healthy same version", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateRunning, TrayReady: true}, []string{"inspect"}, false},
|
|
{"missing tray only", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateRunning}, []string{"inspect", "tray"}, false},
|
|
{"stopped with tray", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateStopped, TrayReady: true}, []string{"inspect", "start"}, false},
|
|
{"stopped without tray", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateStopped}, []string{"inspect", "start", "tray"}, false},
|
|
{"starting", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateStartPending, TrayReady: true}, []string{"inspect", "start"}, false},
|
|
{"stopping", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StateStopPending}, []string{"inspect", "start", "tray"}, false},
|
|
{"paused", installerSnapshot{Installed: true, SameVersion: true, ServiceState: windowsservice.StatePaused}, []string{"inspect", "start", "tray"}, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var calls []string
|
|
installed, err := ensureInstallerRunning(installerActions{
|
|
Inspect: func() (installerSnapshot, error) { calls = append(calls, "inspect"); return tc.state, nil },
|
|
Install: func() error { calls = append(calls, "install"); return nil },
|
|
StartService: func() error { calls = append(calls, "start"); return nil },
|
|
StartTray: func() error { calls = append(calls, "tray"); return nil },
|
|
})
|
|
if err != nil || installed != tc.installed || !reflect.DeepEqual(calls, tc.want) {
|
|
t.Fatalf("installed=%v calls=%v err=%v", installed, calls, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInstallerActivationFailureStopsBeforeTray_BH_WINCLI_03(t *testing.T) {
|
|
want := errors.New("UAC cancelled")
|
|
trayCalled := false
|
|
_, err := ensureInstallerRunning(installerActions{
|
|
Inspect: func() (installerSnapshot, error) { return installerSnapshot{Installed: true, SameVersion: true}, nil },
|
|
StartService: func() error { return want },
|
|
StartTray: func() error { trayCalled = true; return nil },
|
|
})
|
|
if !errors.Is(err, want) || trayCalled {
|
|
t.Fatalf("err=%v trayCalled=%v", err, trayCalled)
|
|
}
|
|
}
|
|
|
|
func TestInstallerInspectionFailureDoesNotMutate_BH_WINCLI_04(t *testing.T) {
|
|
want := errors.New("cannot inspect service")
|
|
mutated := false
|
|
mutate := func() error { mutated = true; return nil }
|
|
_, err := ensureInstallerRunning(installerActions{Inspect: func() (installerSnapshot, error) { return installerSnapshot{}, want }, Install: mutate, StartService: mutate, StartTray: mutate})
|
|
if !errors.Is(err, want) || mutated {
|
|
t.Fatalf("err=%v mutated=%v", err, mutated)
|
|
}
|
|
}
|
|
|
|
func TestInstallerVersionMetadataAndHashFallback_HP_WINCLI_11(t *testing.T) {
|
|
source, target := filepath.Join(t.TempDir(), "source.exe"), filepath.Join(t.TempDir(), "target.exe")
|
|
if err := os.WriteFile(source, []byte("candidate"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name, sourceVersion, targetVersion, body string
|
|
metadataErr bool
|
|
want bool
|
|
}{
|
|
{"same release", "1.2.3", "1.2.3", "signed differently", false, true},
|
|
{"different release", "1.2.4", "1.2.3", "candidate", false, false},
|
|
{"missing metadata identical", "", "", "candidate", true, true},
|
|
{"missing metadata differs", "", "", "old binary", true, false},
|
|
{"dev identical", "dev", "dev", "candidate", false, true},
|
|
{"dev differs", "dev", "dev", "old binary", false, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if err := os.WriteFile(target, []byte(tc.body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
same, err := sameInstallerVersion(source, target, func(path string) (string, error) {
|
|
if tc.metadataErr {
|
|
return "", errors.New("no version resource")
|
|
}
|
|
if path == source {
|
|
return tc.sourceVersion, nil
|
|
}
|
|
return tc.targetVersion, nil
|
|
})
|
|
if err != nil || same != tc.want {
|
|
t.Fatalf("same=%v err=%v", same, err)
|
|
}
|
|
})
|
|
}
|
|
if err := os.Remove(target); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if same, err := sameInstallerVersion(source, target, func(string) (string, error) { t.Fatal("missing file metadata read"); return "", nil }); err != nil || same {
|
|
t.Fatalf("missing file same=%v err=%v", same, err)
|
|
}
|
|
}
|