File: test_stub.nsi

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (255 lines) | stat: -rw-r--r-- 8,474 bytes parent folder | download | duplicates (3)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Unicode true

; Tests use the silent install feature to bypass message box prompts.
SilentInstall silent

; This is the executable that will be run by an xpcshell test
OutFile "test_stub_installer.exe"


; The executable will write output to this file, and the xpcshell test
; will read the contents of the file to determine whether the tests passed
!define TEST_OUTPUT_FILENAME .\test_installer_output.txt


!include "stub_shared_defs.nsh"

; Some variables used only by tests and mocks
Var Stdout
Var FailureMessage
Var TestBreakpointNumber

; For building the test exectuable, this version of the IsTestBreakpointSet macro
; checks the value of the TestBreakpointNumber. For real installer executables,
; this macro is empty, and does nothing. (See stub.nsi for the that version.)
!macro IsTestBreakpointSet breakpointNumber
    ${If} ${breakpointNumber} == $TestBreakpointNumber
        Return
    ${EndIf}
!macroend

!include "LogicLib.nsh"
!include "FileFunc.nsh"
!include "TextFunc.nsh"
!include "WinVer.nsh"
!include "WordFunc.nsh"


Function ExitProcess
FunctionEnd

Function AttachConsole
    ; NSIS doesn't attach a console to the installer, so we'll do that now
    System::Call 'kernel32::AttachConsole(i -1)i.r0'
    ${If} $0 != 0
        ; Windows STD_OUTPUT_HANDLE is defined as -11
        System::Call 'kernel32::GetStdHandle(i -11)i.r0'
        Push $0
    ${Else}
        ; If there's no console, exit with a nonzero exit code
        System::Call 'kernel32::ExitProcess(i 65536)'
    ${EndIf}
FunctionEnd

; ${AtLeastWin10} is a macro provided by WinVer.nsh that can act as the predicate of a LogicLib
; control statement. This macro redefinition makes this test mockable
Var MockAtLeastWin10
!define /redef AtLeastWin10 `$MockAtLeastWin10 IsMockedAsTrue ""`

; See stub.nsi for the real version of CheckCpuSupportsSSE
Var MockCpuHasSSE
Function CheckCpuSupportsSSE
  StrCpy $CpuSupportsSSE "$MockCpuHasSSE"
FunctionEnd

; See stub.nsi for the real version of CanWrite
Var MockCanWrite
Function CanWrite
  StrCpy $CanWriteToInstallDir $MockCanWrite
FunctionEnd

!macro _IsMockedAsTrue _v _b _t _f
  ; If the mock value is the same as 'true', jump to the label specified in $_t
  ; Otherwise, jump to the label specified in $_f
  ; (This is compatible with LogicLib ${If} and similar tests)
  StrCmp `${_v}` 'true' `${_t}` `${_f}`
!macroend

; A helpful macro for testing that a variable is equal to a value.
; Provide the variable name (bare, without $ prefix) and the expected value.
; For example, to test that $MyVariable is equal to "hello there", you would write:
; !insertmacro AssertEqual MyVariable "hello there"
!macro AssertEqual _variableName _expectedValue
    ${If} $${_variableName} != ${_expectedValue}
        StrCpy $FailureMessage "At Line ${__LINE__}: Expected ${_variableName} of ${_expectedValue} , got $${_variableName}"
        SetErrors
        Return
    ${EndIf}
!macroend

Var TestFailureCount

!macro UnitTest _testFunctionName
    Call ${_testFunctionName}
    IfErrors 0 +3
    IntOp $TestFailureCount $TestFailureCount + 1
    FileWrite $Stdout "FAILURE: $FailureMessage; "
    ClearErrors
!macroend

; Redefine ElevateUAC as a no-op in this test exectuable
!define /redef ElevateUAC ``

!include stub.nsh


; .onInit is responsible for running the tests
Function .onInit
    Call AttachConsole
    Pop $Stdout
    IntOp $TestFailureCount 0 + 0
    !insertmacro UnitTest TestDontInstallOnOldWindows

    !insertmacro UnitTest TestDontInstallOnNewWindowsWithoutSSE

    !insertmacro UnitTest TestDontInstallOnOldWindowsWithoutSSE

    !insertmacro UnitTest TestGetArchToInstall

    !insertmacro UnitTest TestMaintServiceCfg

    !insertmacro UnitTest TestCanWriteToDirSuccess

    !insertmacro UnitTest TestCanWriteToDirFail

    ${If} $TestFailureCount = 0
        ; On success, write the success metric and jump to the end
        FileWrite $Stdout "All stub installer tests passed"
    ${Else}
        FileWrite $Stdout "$TestFailureCount stub installer tests failed"
    ${EndIf}
    FileClose $Stdout
    Return
OnError:
    Abort "Failed to run tests."
FunctionEnd

; Expect installation to abort if windows version < 10
Function TestDontInstallOnOldWindows
    StrCpy $MockAtLeastWin10 'false'
    StrCpy $MockCpuHasSSE '1'
    StrCpy $AbortInstallation ''
    StrCpy $ExitCode "Unknown"
    Call CommonOnInit
    !insertmacro AssertEqual ExitCode "${ERR_PREINSTALL_SYS_OS_REQ}"
    !insertmacro AssertEqual AbortInstallation "true"
    !insertmacro AssertEqual R7 "$(WARN_MIN_SUPPORTED_OSVER_MSG)"
FunctionEnd


; Expect installation to abort on processor without SSE, WIndows 10+ version
Function TestDontInstallOnNewWindowsWithoutSSE
    StrCpy $MockAtLeastWin10 'true'
    StrCpy $MockCpuHasSSE '0' 
    StrCpy $AbortInstallation ''
    StrCpy $ExitCode "Unknown"
    Call CommonOnInit
    !insertmacro AssertEqual ExitCode "${ERR_PREINSTALL_SYS_HW_REQ}"
    !insertmacro AssertEqual AbortInstallation "true"
FunctionEnd

; Expect installation to abort on processor without SSE, Windows <10 version
Function TestDontInstallOnOldWindowsWithoutSSE
    StrCpy $MockAtLeastWin10 'false'
    StrCpy $MockCpuHasSSE '0' 
    StrCpy $AbortInstallation ''
    StrCpy $ExitCode "Unknown"
    Call CommonOnInit
    !insertmacro AssertEqual ExitCode "${ERR_PREINSTALL_SYS_OS_REQ}"
    !insertmacro AssertEqual AbortInstallation "true"
    !insertmacro AssertEqual R7 "$(WARN_MIN_SUPPORTED_OSVER_CPU_MSG)"
FunctionEnd

; Expect to find a known supported architecture for Windows
Function TestGetArchToInstall
    StrCpy $TestBreakpointNumber "${TestBreakpointArchToInstall}"
    StrCpy $MockAtLeastWin10 'true'
    StrCpy $MockCpuHasSSE '1'
    StrCpy $INSTDIR "Unknown"
    StrCpy $ArchToInstall "Unknown"

    Call CommonOnInit
    ${Switch} $ArchToInstall
        ${Case} "${ARCH_X86}"
            ; OK, fall through
        ${Case} "${ARCH_AMD64}"
            ; OK, fall through
        ${Case} "${ARCH_AARCH64}"
            ; OK
            ${Break}
        ${Default}
            StrCpy $FailureMessage "Unexpected value for ArchToInstall: $ArchToInstall"
            SetErrors
            Return
    ${EndSwitch}

    ${Switch} $INSTDIR
        ${Case} "${DefaultInstDir64bit}"
            ; OK, fall through
        ${Case} "${DefaultInstDir32bit}"
            ; OK
            ${Break}
        ${Default}
            StrCpy $FailureMessage "Unexpected value for INSTDIR: $INSTDIR"
            SetErrors
            Return
    ${EndSwitch}
FunctionEnd

; Since we're not actually elevating permissions, expect not to enable installing maintenance service
Function TestMaintServiceCfg
    StrCpy $TestBreakpointNumber "${TestBreakpointMaintService}"
    StrCpy $CheckboxInstallMaintSvc 'Unknown'
    StrCpy $MockAtLeastWin10 'true'
    StrCpy $MockCpuHasSSE '1'

    Call CommonOnInit

    !insertmacro AssertEqual CheckboxInstallMaintSvc "0"
FunctionEnd

; Expect success if we can write to installation directory
Function TestCanWriteToDirSuccess
    StrCpy $TestBreakpointNumber "${TestBreakpointCanWriteToDir}"
    StrCpy $MockCanWrite 'true'
    StrCpy $MockAtLeastWin10 'true'
    StrCpy $MockCpuHasSSE '1'
    StrCpy $CanWriteToInstallDir "Unknown"
    StrCpy $AbortInstallation "false"

    Call CommonOnInit

    ; Since we're not running as admin, expect directory to be under user's own home directory, so it should be writable
    !insertmacro AssertEqual CanWriteToInstallDir "true"
    !insertmacro AssertEqual AbortInstallation "false"
FunctionEnd

; Expect failure if we can't write to installation directory
Function TestCanWriteToDirFail
    StrCpy $TestBreakpointNumber "${TestBreakpointCanWriteToDir}"
    StrCpy $MockCanWrite 'false'
    StrCpy $MockAtLeastWin10 'true'
    StrCpy $MockCpuHasSSE '1'
    StrCpy $CanWriteToInstallDir "Unknown"
    StrCpy $AbortInstallation "false"

    Call CommonOnInit

    ; Since we're not running as admin, expect directory to be under user's own home directory, so it should be writable
    !insertmacro AssertEqual CanWriteToInstallDir "false"
    !insertmacro AssertEqual ExitCode "${ERR_PREINSTALL_NOT_WRITABLE}"
    !insertmacro AssertEqual AbortInstallation "true"
FunctionEnd

Section
SectionEnd