-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller_test.go
More file actions
205 lines (187 loc) · 8.3 KB
/
Copy pathinstaller_test.go
File metadata and controls
205 lines (187 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"regexp"
"strings"
"testing"
)
// The installer is a 32-bit NSIS executable and the program it installs is
// 64-bit, so the two halves do not see the same registry unless one of them says
// which view it means. Windows redirects a 32-bit process writing to
// HKLM\SOFTWARE\pingping into HKLM\SOFTWARE\WOW6432Node\pingping, and both
// appear as `SOFTWARE\pingping` in every screenshot, every error message and
// every `reg query` that does not pass /reg:32 or /reg:64.
//
// The consequence was two keys of the same name: the installer put InstallDir in
// one, `pingping.exe install` put Port and DataDir in the other, and each half
// read a key the other had never written. Nothing errored, because a missing
// value is indistinguishable from a value that was never set.
//
// So the invariant is SetRegView 64 before any HKLM write, and it is checked
// here rather than left to a comment, because the failure is invisible on the
// machine where it happens.
func TestInstallerPinsTheRegistryView(t *testing.T) {
src := readSource(t, "packaging/pingping.nsi")
for _, sec := range []string{`Section "pingping" SecMain`, `Section "Uninstall"`} {
i := strings.Index(src, sec)
if i < 0 {
t.Fatalf("%s is gone from the installer", sec)
}
body := src[i:]
if end := strings.Index(body, "\nSectionEnd"); end > 0 {
body = body[:end]
}
view := strings.Index(body, "SetRegView 64")
if view < 0 {
t.Errorf("%s never pins the registry view, so a 32-bit installer and a "+
"64-bit program write two keys of the same name", sec)
continue
}
// Every HKLM touch must come after it. A SetRegView 32 block in between is
// allowed — it is how leftovers from before this fix are cleaned up — as
// long as the view is put back, which the last-index check below covers.
for _, m := range regexp.MustCompile(`(?m)^\s*(Write|Delete)Reg\w*\s+HKLM`).FindAllStringIndex(body, -1) {
if m[0] < view {
t.Errorf("%s touches HKLM at offset %d, before SetRegView 64 at %d: "+
"that write lands in whichever view Windows chooses", sec, m[0], view)
}
}
// A SetRegView 32 must never be the last one standing.
if last32, last64 := strings.LastIndex(body, "SetRegView 32"), strings.LastIndex(body, "SetRegView 64"); last32 > last64 {
t.Errorf("%s leaves the registry view set to 32 for everything after "+
"offset %d", sec, last32)
}
}
}
// Add/Remove Programs shows an entry only if the entry is complete. A missing
// DisplayName or UninstallString is not an error anywhere — the program simply
// is not in the list, which reads as "it did not install" and sends everyone to
// look at the wrong thing.
//
// The list was in fact empty for several releases, and the reason was neither of
// those: the install section aborted at the file copy, before it got this far, so
// a failed install left no trace of itself to uninstall. That is fixed upstream
// of here by stopping the service before overwriting its executable. This test
// guards the other half — that when the section does run to completion, what it
// wrote is enough to be listed.
func TestUninstallEntryIsComplete(t *testing.T) {
src := readSource(t, "packaging/pingping.nsi")
i := strings.Index(src, `Section "pingping" SecMain`)
if i < 0 {
t.Fatal("the main section is gone")
}
body := src[i:]
if end := strings.Index(body, "\nSectionEnd"); end > 0 {
body = body[:end]
}
for _, want := range []string{"DisplayName", "DisplayVersion", "UninstallString", "DisplayIcon", "Publisher"} {
if !strings.Contains(body, `"`+want+`"`) {
t.Errorf("the uninstall entry has no %s; Windows hides an incomplete entry", want)
}
}
if !strings.Contains(body, "WriteUninstaller") {
t.Error("no uninstaller is written, so UninstallString points at nothing")
}
// These are the values that hide an entry, or move it out of the list, and not
// one of them should ever appear here.
for _, hides := range []string{"SystemComponent", "ParentKeyName", "ParentDisplayName", "ReleaseType", "WindowsInstaller"} {
if strings.Contains(body, `"`+hides+`"`) {
t.Errorf("the uninstall entry sets %s, which takes it out of the "+
"Add/Remove Programs list", hides)
}
}
}
// Overwriting a running executable is refused by Windows, and NSIS answers that
// refusal with a retry/abort dialog. Abort stops the section — which is upstream
// of the uninstall entry, the shortcuts and the service registration, so the
// install leaves a half-written directory and nothing in Add/Remove Programs.
//
// The service and every notification-area process hold the exe, so both have to
// be let go of before the copy. This checks the order, because the whole point is
// that the stop comes first.
func TestInstallerReleasesTheExeBeforeOverwritingIt(t *testing.T) {
src := readSource(t, "packaging/pingping.nsi")
i := strings.Index(src, `Section "pingping" SecMain`)
body := src[i:]
if end := strings.Index(body, "\nSectionEnd"); end > 0 {
body = body[:end]
}
copyAt := strings.Index(body, "File /oname=pingping.exe")
if copyAt < 0 {
t.Fatal("the section no longer copies pingping.exe")
}
stopAt := strings.Index(body, "sc stop pingping")
killAt := strings.Index(body, "taskkill /F /IM pingping.exe")
if stopAt < 0 || stopAt > copyAt {
t.Error("the service is not stopped before the executable is overwritten: " +
"the copy is refused, NSIS offers Abort, and Abort skips everything below")
}
if killAt < 0 || killAt > copyAt {
t.Error("the tray processes are not closed before the executable is " +
"overwritten; they hold the same file the service does")
}
if stopAt > killAt {
t.Error("taskkill runs before sc stop, so the service process is killed " +
"rather than stopped — the SCM reads that as a crash and applies the " +
"recovery action, which brings the service back mid-install")
}
}
// A shortcut named "pingping console" that passes no arguments does not open the
// console — a bare launch of an installed copy used to mean "serve in the
// foreground", so it started a second server on the port the service already
// held, failed the bind, and exited before the window could be read. The
// operator saw a console window flash and vanish, which is what a broken program
// looks like, and it was the only offered way back after the tray had been told
// to stop everything and exit.
func TestConsoleShortcutAsksForTheConsole(t *testing.T) {
src := readSource(t, "packaging/pingping.nsi")
i := strings.Index(src, `console.lnk"`)
if i < 0 {
t.Fatal("the console shortcut is gone from the installer")
}
// The arguments are the third field of CreateShortCut, on the continuation
// line after the .lnk name.
rest := src[i:]
if end := strings.Index(rest, "\n\n"); end > 0 {
rest = rest[:end]
}
if !strings.Contains(rest, `"console"`) {
t.Errorf("the console shortcut does not pass the console verb:\n%s", rest)
}
}
// Every verb the installer's shortcuts and the tray invoke has to be one main.go
// actually dispatches, or the program exits 2 with "unknown command" and the
// shortcut looks broken in exactly the same way.
func TestShortcutVerbsAreDispatched(t *testing.T) {
main := readSource(t, "main.go")
nsi := readSource(t, "packaging/pingping.nsi")
dispatch := main[strings.Index(main, "switch verb {"):]
if end := strings.Index(dispatch, "\n\t}"); end > 0 {
dispatch = dispatch[:end]
}
// Comments go first: this file explains the very bug being guarded against,
// in prose that mentions pingping.exe, and matching that prose found verbs
// like "is" and "and".
var live []string
for _, line := range strings.Split(nsi, "\n") {
if t := strings.TrimSpace(line); !strings.HasPrefix(t, ";") {
live = append(live, line)
}
}
// What the installer asks the exe to do: the argument field of a shortcut,
// and the command line of an nsExec call. Both put the verb straight after
// the closing quote of the exe path.
re := regexp.MustCompile(`pingping\.exe"[ \t]+"?([A-Za-z]\w*)`)
seen := map[string]bool{}
for _, m := range re.FindAllStringSubmatch(strings.Join(live, "\n"), -1) {
seen[m[1]] = true
}
if len(seen) == 0 {
t.Fatal("found no verbs in the installer; the pattern has drifted")
}
for verb := range seen {
if !strings.Contains(dispatch, `"`+verb+`"`) {
t.Errorf("the installer runs `pingping.exe %s`, but main.go does not "+
"dispatch that verb — it exits 2 with \"unknown command\"", verb)
}
}
}