67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestVersionNewer(t *testing.T) {
|
||
|
|
if !versionNewer("2.0.1", "2.0.0") {
|
||
|
|
t.Fatal("2.0.1 should be newer than 2.0.0")
|
||
|
|
}
|
||
|
|
if versionNewer("2.0.0", "2.0.0") {
|
||
|
|
t.Fatal("same version is not newer")
|
||
|
|
}
|
||
|
|
if versionNewer("1.9.9", "2.0.0") {
|
||
|
|
t.Fatal("1.9.9 should not be newer than 2.0.0")
|
||
|
|
}
|
||
|
|
if !versionNewer("v2.1.0", "2.0.9") {
|
||
|
|
t.Fatal("v prefix should be ignored")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParseSemver(t *testing.T) {
|
||
|
|
got := parseSemver("v2.3.4-beta")
|
||
|
|
if got != [3]int{2, 3, 4} {
|
||
|
|
t.Fatalf("got %#v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestUpdateLaunchScript(t *testing.T) {
|
||
|
|
s := updateLaunchScript(4242, `C:\Temp\2.0.1-installer.exe`)
|
||
|
|
for _, want := range []string{
|
||
|
|
"set \"PID=4242\"",
|
||
|
|
`start "" "C:\Temp\2.0.1-installer.exe"`,
|
||
|
|
"tasklist /FI \"PID eq %PID%\"",
|
||
|
|
":wait",
|
||
|
|
":launch",
|
||
|
|
} {
|
||
|
|
if !strings.Contains(s, want) {
|
||
|
|
t.Fatalf("script missing %q\n%s", want, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if strings.Contains(s, " /S") {
|
||
|
|
t.Fatal("installer must not be started silently; UAC/file lock would fail")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestIsWindowsPE(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
pe := filepath.Join(dir, "ok.exe")
|
||
|
|
html := filepath.Join(dir, "page.html")
|
||
|
|
if err := os.WriteFile(pe, []byte("MZ\x90\x00fake"), 0644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(html, []byte("<!doctype html>"), 0644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !isWindowsPE(pe) {
|
||
|
|
t.Fatal("MZ header should pass")
|
||
|
|
}
|
||
|
|
if isWindowsPE(html) {
|
||
|
|
t.Fatal("html should not pass as installer")
|
||
|
|
}
|
||
|
|
}
|