Skip to content

Commit

Permalink
(chocolatey#1144) Add Pester test to cover hash validation
Browse files Browse the repository at this point in the history
In the next release, when possible, Chocolatey CLI will now include
package hash validation to ensure that the package being installed is
the one that the server thinks it is. This is a feature flag that needs
to be toggled on.

This commit adds tests to cover that hash validation is being attempted
when it can be.
  • Loading branch information
gep13 authored and corbob committed May 27, 2024
1 parent 2cb1c2f commit 320cfc1
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion tests/pester-tests/commands/choco-install.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,79 @@

$PackageUnderTest = "installpackage"

$Output = Invoke-Choco install $PackageUnderTest --confirm
$Output = Invoke-Choco install $PackageUnderTest --debug --confirm
}

It "Exits with Success (0)" {
$Output.ExitCode | Should -Be 0 -Because $Output.String
}

It "Installed a package to the lib directory" {
"$env:ChocolateyInstall\lib\$PackageUnderTest" | Should -Exist
}

# We are skipping this for now, until we have stabilized the directory
# path reporting functionality. There are times that this test will
# fail due to Chocolatey not reporting the path.
# This failure seems to happen randomly, and is therefore not a
# reliable test we can make.
It "Outputs the installation directory (which should exist)" -Skip {
$directoryPath = "$env:ChocolateyInstall\lib\$PackageUnderTest"
$lineRegex = [regex]::Escape($directoryPath)

$foundPath = $Output.Lines -match $lineRegex
$foundPath | Should -Not -BeNullOrEmpty
$foundPath | Should -Exist
}

It "Installs the expected version of the package" {
"$env:ChocolateyInstall\lib\$PackageUnderTest\$PackageUnderTest.nuspec" | Should -Exist
[xml]$XML = Get-Content "$env:ChocolateyInstall\lib\$PackageUnderTest\$PackageUnderTest.nuspec"
$XML.package.metadata.version | Should -Be "1.0.0"
}

It "Creates a Console Shim in the Bin Directory" {
"$env:ChocolateyInstall\bin\console.exe" | Should -Exist
}

It "Creates a Graphical Shim in the Bin Directory" {
"$env:ChocolateyInstall\bin\graphical.exe" | Should -Exist
}

It "Does not create a Shim for Ignored Executable in the Bin Directory" {
"$env:ChocolateyInstall\bin\not.installed.exe" | Should -Not -Exist
}

It "Does not create a Shim for Ignored Executable (with mismatched case) in the Bin Directory" {
"$env:ChocolateyInstall\bin\casemismatch.exe" | Should -Not -Exist
}

It "Does not create an extensions folder for the package" {
"$env:ChocolateyInstall\extensions\$PackageUnderTest" | Should -Not -Exist
}

It "Contains the output of the ChocolateyInstall.ps1 script" {
$Output.Lines | Should -Contain "Ya!"
}

It "Outputs a message showing that installation was successful" {
$Output.String | Should -Match "Chocolatey installed 1/1 packages\."
}

It "Should mention that package hash verification has been skipped" {
$Output.Lines | Should -Contain "Skipping package hash validation as feature 'usePackageHashValidation' is not enabled." -Because $Output.String
}
}

Context "Installing a Package (Happy Path) with hash validation enabled" {
BeforeAll {
Restore-ChocolateyInstallSnapshot

$null = Enable-ChocolateyFeature -Name "usePackageHashValidation"

$PackageUnderTest = "installpackage"

$Output = Invoke-Choco install $PackageUnderTest --debug --confirm
}

It "Exits with Success (0)" {
Expand Down Expand Up @@ -141,6 +213,14 @@
It "Outputs a message showing that installation was successful" {
$Output.String | Should -Match "Chocolatey installed 1/1 packages\."
}

It "Should mention that package hash verification was skipped since local folder source is being used" {
if ((Test-HasNuGetV3Source) -or (-not $env:TEST_KITCHEN)) {
$Output.Lines | Should -Contain "Source does not provide a package hash, skipping package hash validation." -Because $Output.String
} else {
$Output.Lines | Should -Contain "Package hash matches expected hash." -Because $Output.String
}
}
}

Context "Installing a Package (Happy Path) including configured sources" {
Expand Down

0 comments on commit 320cfc1

Please sign in to comment.