-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign3.ps1
248 lines (224 loc) · 9.69 KB
/
assign3.ps1
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
Param (
[string]$Command,
[int]$ProxyPort = 8888,
[string]$Url = "http://cs260.meancat.com/delay"
)
$Command = $Command.ToLower()
# powershell version check --- print this out up front so we can figure out problems...
Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)" -ForegroundColor Green
$DetectedArch = if ($ENV:PROCESSOR_ARCHITECTURE -eq "AMD64") { "x64" } else { $ENV:PROCESSOR_ARCHITECTURE }
Write-Host "Architecture Detected: $DetectedArch" -ForegroundColor Green
# constants to some important files:
$MSBuildExe = "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe"
$ClientExe = Join-Path $PSScriptRoot "FakeHttpClient/$DetectedArch/Release/CS260_FakeHttpClient.exe"
$ResultsDir = Join-Path $PSScriptRoot "results/"
# create results dir if needed
New-Item -Path $ResultsDir -ItemType Directory -Force | Out-Null
function BuildClient {
# requires the correct path to MSBuild (see above)
VerifyMSBuildExists
# Path to the C++ solution
$solutionPath = Join-Path $PSScriptRoot "FakeHttpClient\FakeHttpClient.sln"
# Build the solution
Write-Host "Building FakeHttpClient ($DetectedArch)..." -ForegroundColor Blue
& "$MSBuildExe" $solutionPath /p:Configuration=Release /p:Platform=$DetectedArch
if ($LASTEXITCODE -ne 0) {
Write-Error "Build failed!" -ForegroundColor Red
exit $LASTEXITCODE
} else {
Write-Host "Build succeeded!" -ForegroundColor Green
}
}
function VerifyMSBuildExists {
if (-not (Test-Path $MSBuildExe)) {
Write-Error "msbuild.exe not found (tried $MSBuildExe)"
exit 1
}
}
function VerifyClientExists {
if (-not (Test-Path $ClientExe)) {
Write-Error "Client Executable not found at: $ClientExe"
return $false
}
return $true
}
function CreateClientProcess {
param([string]$url)
$procInfo = New-Object System.Diagnostics.ProcessStartInfo
$procInfo.FileName = $ClientExe
$procInfo.Arguments = "$url $ProxyPort"
$procInfo.RedirectStandardOutput = $true
$procInfo.RedirectStandardError = $true
$procInfo.UseShellExecute = $false
$procInfo.CreateNoWindow = $true
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $procInfo
return $proc
}
function PrintStreamToScreen {
Param($stream, [bool]$isError)
#while ($stream.Peek() -ge 0) {
while(-not $stream.EndOfStream) {
$c = [char]$stream.Read()
if ($isError) {
Write-Host -NoNewLine $c -ForegroundColor Red
} else {
Write-Host -NoNewLine $c
}
}
}
function ExecuteTest {
Param([string]$TestName, [string]$Url)
$proc = CreateClientProcess $Url
if (-not $proc.Start()) {
Write-Output "Error: $ClientExe failed to start"
exit 1
}
$stdoutStream = $proc.StandardOutput
$stderrStream = $proc.StandardError
# print in "real time" (no buffering)
while (-not $proc.HasExited) {
PrintStreamToScreen -stream $stdoutStream -isError $false
PrintStreamToScreen -stream $stderrStream -isError $true
}
PrintStreamToScreen -stream $stdoutStream -isError $false
PrintStreamToScreen -stream $stderrStream -isError $true
Write-Host "$TestName completed."
}
function ExecuteTestJob {
Param([string]$TestName, [string]$Url)
Start-Job -ScriptBlock {
Param($testName, $url, $port, $resultsDir, $clientExe)
Write-Output "Begin Test $testName $url $(Get-Date)"
# Ensure logs are created
$testLog = Join-Path $resultsDir "$TestName.txt"
$errorLog = Join-Path $resultsDir "$TestName-error.txt"
New-Item -Path $testLog -ItemType File -Force | Out-Null
New-Item -Path $errorLog -ItemType File -Force | Out-Null
# Use Start-Process with output redirection
$proxyArgs = "$url $port"
$process = Start-Process -FilePath $clientExe `
-ArgumentList $proxyArgs `
-NoNewWindow `
-RedirectStandardOutput $testLog `
-RedirectStandardError $errorLog `
-PassThru
# Wait for the process to finish or timeout after 1 minute
$timeout = 30
$process | Wait-Process -Timeout $timeout
Write-Host "End Test $TestName $(Get-Date)"
} -ArgumentList $test.TestName, $test.Url, $ProxyPort, $ResultsDir, $ClientExe
}
function IsUserPrivileged {
if ($IsMacOs -eq $true) {
return true;
}
return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")
}
switch -Regex ($Command) {
"buildClient" {
BuildClient
if (-not (VerifyClientExists)) {
Write-Host "No output found; Build failed?" -ForegroundColor Red
exit 1
}
}
"runOne" {
# this assumes your assignment is already running and listening to $ProxyPort
# start the fake http client and capture input and output
if (-not (VerifyClientExists)) {
BuildClient
}
# just run vs. one url
ExecuteTest -TestName "run-one" -Url $Url
exit 0
}
"runAll" {
Write-Output "Run all the tests!"
$testCases = @(
@{ TestName = "Valid-Delay-1-DNS"; Url = "http://cs260.meancat.com/delay" },
@{ TestName = "Valid-Delay-2-DNS"; Url = "http://cs260.meancat.com/delay" }
@{ TestName = "Valid-Delay-3-DNS"; Url = "http://cs260.meancat.com/delay" },
@{ TestName = "Valid-Delay-4-DNS"; Url = "http://cs260.meancat.com/delay" },
@{ TestName = "Valid-Delay-IP"; Url = "http://52.12.14.56/delay" }
@{ TestName = "Valid-Google-NoSlash"; Url = "http://www.google.com" },
@{ TestName = "Valid-Google-Slash"; Url = "http://www.google.com/" }
)
foreach($test in $testCases) {
Write-Output "Run $test.TestName"
ExecuteTestJob -TestName $test.TestName -Url $test.Url
Write-Output "Done"
}
Write-Host "Waiting for all jobs ..."
Wait-Job -State Running
Get-Job | ForEach-Object {
Receive-Job -Job $_
Remove-Job -Job $_
}
Write-Host "Complete!"
}
"create-cert" {
New-SelfSignedCertificate -Type CodeSigningCert -Subject "CN=DigiPen-CS260-ScriptSigning" -CertStoreLocation Cert:\CurrentUser\My
}
"export-cert" {
$cert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.Subject -match "DigiPen-CS260-ScriptSigning" }
Export-Certificate -Cert $cert -FilePath "DigiPen-CS260-ScriptSigning.cer"
}
"sign-script" {
$cert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.Subject -match "DigiPen-CS260-ScriptSigning" }
Set-AuthenticodeSignature -FilePath ".\assign3.ps1" -Certificate $cert
Write-Host "Signed"
}
"import-cert" {
# you must be admin for this to work
$isAdmin = IsUserPrivileged
if (-not $isAdmin) {
Write-Host "You must be running as administrator to do that!"
return
}
Import-Certificate -FilePath ".\DigiPen-CS260-ScriptSigning.cer" -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
Write-Host "Import done"
}
"set-execution-policy" {
$isAdmin = IsUserPrivileged
if (-not $isAdmin) {
Write-Host "You must be running as administrator to do that!"
return
}
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Write-Host "Done!"
}
}
# SIG # Begin signature block
# MIIFtgYJKoZIhvcNAQcCoIIFpzCCBaMCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCDZIhH4Vv/GAWLu
# FykFgwQ+cNZn57gA0FJp9v/ZJXfAa6CCAyAwggMcMIICBKADAgECAhBs1zNr0OVC
# rUrmSV3omiDS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