Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Scripts/Attestation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ trap {
###################################################################################################
$global:ErrorActionPreference = "stop"
Set-StrictMode -Version Latest
$SCDM = ".\sdcm.exe"
$SCDM = Join-Path $PSScriptRoot "sdcm.exe"
if (-not (Test-Path -LiteralPath $SCDM -PathType Leaf)) {
Write-Output "sdcm.exe not found at expected location: $SCDM"
exit 1
}

function Assert-SdcmSuccess([string] $Step) {
if ($LASTEXITCODE -ne 0) {
Write-Output "sdcm.exe failed during '$Step' with exit code $LASTEXITCODE"
exit $LASTEXITCODE
}
}

###################################################################################################
# Functions
Expand Down Expand Up @@ -99,6 +110,8 @@ $json.createProduct.requestedSignatures = $Signatures
$json | ConvertTo-Json | Out-File -Encoding ASCII -FilePath "CreateAttest.json"
Write-Output " * Submit"
$output = & $SCDM -create CreateAttest.json
if ($LASTEXITCODE -ne 0) { Write-Output $output }
Assert-SdcmSuccess "Create Product"

if (-not ([string]$output -match "--- Product: (\d+)")) {
Write-Output "Did not find product ID"
Expand All @@ -115,6 +128,8 @@ $json.createSubmission.name = "$ProductName`_Attestation_Submission"
$json | ConvertTo-Json | Out-File -Encoding ASCII -FilePath "CreateSubmissionAttest.json"
Write-Output " * Submit"
$output = & $SCDM -create CreateSubmissionAttest.json -productid $SDCM_PID
if ($LASTEXITCODE -ne 0) { Write-Output $output }
Assert-SdcmSuccess "Create Submission"

if (-not ([string]$output -match "---- Submission: (\d+)")) {
Write-Output "Did not find submission ID"
Expand All @@ -126,18 +141,22 @@ Write-Output " * SID: $SDCM_SID"

Write-Output "> Upload File"
& $SCDM -upload $InputPath -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Upload File"

Write-Output "> Commit Submission"
& $SCDM -commit -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Commit Submission"

Write-Output "> Wait for Submission to complete"
Write-Output " * Dev Center URL: https://developer.microsoft.com/en-us/dashboard/hardware/driver/$SDCM_PID"
Write-Output " * PID: $SDCM_PID"
Write-Output " * SID: $SDCM_SID"
& $SCDM -wait -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Wait for Submission"

Write-Output "> Download File"
& $SCDM -productid $SDCM_PID -submissionid $SDCM_SID -download $InputPath`.signed`.zip
Assert-SdcmSuccess "Download File"

Write-Output "> Done"
Write-Output " * Output: $InputPath`.signed`.zip"
Expand Down
21 changes: 20 additions & 1 deletion Scripts/HLKx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ trap {
###################################################################################################
$global:ErrorActionPreference = "stop"
Set-StrictMode -Version Latest
$SCDM = ".\sdcm.exe"
$SCDM = Join-Path $PSScriptRoot "sdcm.exe"
if (-not (Test-Path -LiteralPath $SCDM -PathType Leaf)) {
Write-Output "sdcm.exe not found at expected location: $SCDM"
exit 1
}

function Assert-SdcmSuccess([string] $Step) {
if ($LASTEXITCODE -ne 0) {
Write-Output "sdcm.exe failed during '$Step' with exit code $LASTEXITCODE"
exit $LASTEXITCODE
}
}

###################################################################################################
# Functions
Expand Down Expand Up @@ -99,6 +110,8 @@ $json.createProduct.requestedSignatures = $Signatures
$json | ConvertTo-Json | Out-File -Encoding ASCII -FilePath "CreateHLK.json"
Write-Output " * Submit"
$output = & $SCDM -create CreateHLK.json
if ($LASTEXITCODE -ne 0) { Write-Output $output }
Assert-SdcmSuccess "Create Product"

if (-not ([string]$output -match "--- Product: (\d+)")) {
Write-Output "Did not find product ID"
Expand All @@ -115,6 +128,8 @@ $json.createSubmission.name = "$ProductName`_HLK_Submission"
$json | ConvertTo-Json | Out-File -Encoding ASCII -FilePath "CreateSubmissionHLK.json"
Write-Output " * Submit"
$output = & $SCDM -create CreateSubmissionHLK.json -productid $SDCM_PID
if ($LASTEXITCODE -ne 0) { Write-Output $output }
Assert-SdcmSuccess "Create Submission"

if (-not ([string]$output -match "---- Submission: (\d+)")) {
Write-Output "Did not find submission ID"
Expand All @@ -126,18 +141,22 @@ Write-Output " * SID: $SDCM_SID"

Write-Output "> Upload File"
& $SCDM -upload $InputFile -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Upload File"

Write-Output "> Commit Submission"
& $SCDM -commit -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Commit Submission"

Write-Output "> Wait for Submission to complete"
Write-Output " * Dev Center URL: https://developer.microsoft.com/en-us/dashboard/hardware/driver/$SDCM_PID"
Write-Output " * PID: $SDCM_PID"
Write-Output " * SID: $SDCM_SID"
& $SCDM -wait -productid $SDCM_PID -submissionid $SDCM_SID
Assert-SdcmSuccess "Wait for Submission"

Write-Output "> Download File"
& $SCDM -productid $SDCM_PID -submissionid $SDCM_SID -download $InputFile`.signed`.zip
Assert-SdcmSuccess "Download File"

Write-Output "> Done"
Write-Output " * Output: $InputFile`.signed`.zip"
Expand Down
25 changes: 23 additions & 2 deletions Scripts/ShippingLabel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

.PARAMETER Ceiling
Highest OS the driver is available for

.PARAMETER ProductName
Optional name used as the prefix for the Shipping Label name. Defaults to ProductId
#>
#Requires -Version 5.0

Expand All @@ -50,7 +53,10 @@ param(
[string] $Floor = "19H1",

[Parameter(Mandatory = $false, Position = 6)]
[string] $Ceiling = "19H1"
[string] $Ceiling = "19H1",

[Parameter(Mandatory = $false, Position = 7)]
[string] $ProductName = $ProductId
)

###################################################################################################
Expand All @@ -68,7 +74,18 @@ trap {
###################################################################################################
$global:ErrorActionPreference = "stop"
Set-StrictMode -Version Latest
$SDCM = ".\sdcm.exe"
$SDCM = Join-Path $PSScriptRoot "sdcm.exe"
if (-not (Test-Path -LiteralPath $SDCM -PathType Leaf)) {
Write-Output "sdcm.exe not found at expected location: $SDCM"
exit 1
}

function Assert-SdcmSuccess([string] $Step) {
if ($LASTEXITCODE -ne 0) {
Write-Output "sdcm.exe failed during '$Step' with exit code $LASTEXITCODE"
exit $LASTEXITCODE
}
}

###################################################################################################
# Functions
Expand All @@ -83,6 +100,7 @@ Write-Output ""

Write-Output "> Wait for Driver Metadata to be ready"
& $SDCM -wait -waitmetadata -productid $ProductId -submissionid $SubmissionId
Assert-SdcmSuccess "Wait for Driver Metadata"

Write-Output "> Generate Shipping Label json"
$CreateShippingLabelJson = @"
Expand Down Expand Up @@ -141,6 +159,8 @@ $CreateShippingLabelJson | Out-File -encoding ASCII CreateShippingLabel.json

Write-Output "> Create Shipping Label"
$output = & $SDCM -create CreateShippingLabel.json -productid $ProductId -submissionid $SubmissionId
if ($LASTEXITCODE -ne 0) { Write-Output $output }
Assert-SdcmSuccess "Create Shipping Label"
if (-not ([string]$output -match "---- Shipping Label: (\d+)")) {
Write-Output "Did not find shipping label ID"
Write-Output $output
Expand All @@ -151,6 +171,7 @@ Write-Output " * ShippingLabelId: $ShippingLabelId"

Write-Output "> Wait for Shipping Label"
& $SDCM -wait -productid $ProductId -submissionid $SubmissionId -shippinglabelid $ShippingLabelId
Assert-SdcmSuccess "Wait for Shipping Label"

Write-Output "> Done"

122 changes: 122 additions & 0 deletions Scripts/Tests/SdcmPath.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#-------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#-------------------------------------------------------------------------------
<#
.SYNOPSIS
Verifies the signing scripts invoke sdcm.exe only from the script directory.
Run with Windows PowerShell 5.1: powershell -File Scripts\Tests\SdcmPath.Tests.ps1
#>
#Requires -Version 5.0

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest

$ScriptsDir = Split-Path -Parent $PSScriptRoot
$Root = Join-Path ([IO.Path]::GetTempPath()) ("sdcm-tests-" + [Guid]::NewGuid())
$Failures = 0

function New-StubExe([string] $Path, [string] $LogName, [int] $ExitCode) {
$source = @"
using System;
using System.IO;
public static class Program {
public static int Main(string[] args) {
string log = Path.Combine(Environment.CurrentDirectory, "$LogName");
File.AppendAllText(log, string.Join(" ", args) + Environment.NewLine);
Console.WriteLine("--- Product: 111");
Console.WriteLine("---- Submission: 222");
Console.WriteLine("---- Shipping Label: 333");
return $ExitCode;
}
}
"@
Add-Type -TypeDefinition $source -OutputAssembly $Path -OutputType ConsoleApplication
}

function Assert-True([bool] $Condition, [string] $Message) {
if ($Condition) {
Write-Output " PASS: $Message"
}
else {
Write-Output " FAIL: $Message"
$script:Failures++
}
}

function Invoke-Script([string] $ScriptPath, [string[]] $Arguments, [string] $WorkDir) {
Push-Location $WorkDir
$ErrorActionPreference = "Continue"
try {
$out = & powershell.exe -NoProfile -ExecutionPolicy Bypass -File $ScriptPath @Arguments 2>&1
return [pscustomobject]@{ ExitCode = $LASTEXITCODE; Output = ($out | Out-String) }
}
finally {
Pop-Location
}
}

$Cases = @(
@{ Name = "Attestation.ps1"; Args = @("Prod", "WINDOWS_v100_X64_RS4_FULL", "INPUT"); Expected = @("-create CreateAttest.json", "-create CreateSubmissionAttest.json -productid 111", "-commit -productid 111 -submissionid 222", "-wait -productid 111 -submissionid 222") },
@{ Name = "HLKx.ps1"; Args = @("Prod", "WINDOWS_v100_X64_RS4_FULL", "INPUT"); Expected = @("-create CreateHLK.json", "-create CreateSubmissionHLK.json -productid 111", "-commit -productid 111 -submissionid 222", "-wait -productid 111 -submissionid 222") },
@{ Name = "ShippingLabel.ps1"; Args = @("111", "222", "chid"); Expected = @("-wait -waitmetadata -productid 111 -submissionid 222", "-create CreateShippingLabel.json -productid 111 -submissionid 222", "-wait -productid 111 -submissionid 222 -shippinglabelid 333") }
)

try {
$null = New-Item -ItemType Directory -Path $Root
$GoodDir = Join-Path $Root "good"
$FailDir = Join-Path $Root "fail"
$EmptyDir = Join-Path $Root "empty"
$WorkDir = Join-Path $Root "work"
$GoodDir, $FailDir, $EmptyDir, $WorkDir | ForEach-Object { $null = New-Item -ItemType Directory -Path $_ }

New-StubExe (Join-Path $GoodDir "sdcm.exe") "scriptdir.log" 0
New-StubExe (Join-Path $FailDir "sdcm.exe") "scriptdir.log" 5
New-StubExe (Join-Path $WorkDir "sdcm.exe") "workdir.log" 0

$InputFile = Join-Path $WorkDir "input.cab"
Set-Content -Path $InputFile -Value "x"

foreach ($case in $Cases) {
Write-Output "[$($case.Name)]"
$scriptArgs = $case.Args | ForEach-Object { if ($_ -eq "INPUT") { $InputFile } else { $_ } }
foreach ($dir in $GoodDir, $FailDir, $EmptyDir) {
Copy-Item (Join-Path $ScriptsDir $case.Name) $dir -Force
}
Remove-Item (Join-Path $WorkDir "*.log") -ErrorAction SilentlyContinue

# Runs from a different working directory that also contains an sdcm.exe
$r = Invoke-Script (Join-Path $GoodDir $case.Name) $scriptArgs $WorkDir
$scriptDirLog = Join-Path $WorkDir "scriptdir.log"
Assert-True ($r.ExitCode -eq 0) "succeeds from an unrelated working directory (exit $($r.ExitCode))"
Assert-True (-not (Test-Path (Join-Path $WorkDir "workdir.log"))) "sdcm.exe in the working directory is not used"
Assert-True (Test-Path $scriptDirLog) "sdcm.exe next to the script is invoked"
if (Test-Path $scriptDirLog) {
$log = Get-Content $scriptDirLog
foreach ($expected in $case.Expected) {
Assert-True ([bool]($log | Where-Object { $_ -like "$expected*" })) "arguments preserved: $expected"
}
}

# Missing executable next to the script
Remove-Item (Join-Path $WorkDir "*.log") -ErrorAction SilentlyContinue
$r = Invoke-Script (Join-Path $EmptyDir $case.Name) $scriptArgs $WorkDir
Assert-True ($r.ExitCode -ne 0) "missing sdcm.exe returns non-zero (exit $($r.ExitCode))"
Assert-True ($r.Output -match "sdcm.exe not found") "missing sdcm.exe reports a clear error"
Assert-True (-not (Test-Path (Join-Path $WorkDir "workdir.log"))) "no fallback to working directory when missing"

# Executable failure is detected
$r = Invoke-Script (Join-Path $FailDir $case.Name) $scriptArgs $WorkDir
Assert-True ($r.ExitCode -eq 5) "sdcm.exe failure propagates exit code (exit $($r.ExitCode))"
}
}
finally {
Remove-Item -Recurse -Force $Root -ErrorAction SilentlyContinue
}

if ($Failures -gt 0) {
Write-Output "$Failures test(s) failed"
exit 1
}
Write-Output "All tests passed"
exit 0
Loading