File size: 5,287 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Describe "Verify SBOMs" {
    BeforeAll {
        Write-Verbose "In Describe BeforeAll" -Verbose
        Import-Module $PSScriptRoot/../../../build.psm1
        Import-Module $PSScriptRoot/../packaging.psd1 -Force
        $matchCases = @()
        $testCases = @()
        $missingFromPackageCases = @()
        $missingFromManifestCases = @()
        Write-Verbose "${env:PACKAGE_FOLDER}" -Verbose
        Get-ChildItem $env:PACKAGE_FOLDER -Filter *.zip |
            Where-Object { $_.Name -notlike 'powershell-symbols*' } |
            ForEach-Object {
                Write-Verbose "Found $($_.Name)..." -Verbose
                $testCases += @{
                    FilePath = $_.FullName
                    Name = $_.Name
                    Extension = $_.Extension
            }
        }

        if ($IsLinux) {
            Get-ChildItem $env:PACKAGE_FOLDER -Filter *.rpm | ForEach-Object {
                Write-Verbose "Found $($_.Name)..." -Verbose
                $testCases += @{
                    FilePath  = $_.FullName
                    Name      = $_.Name
                    Extension = $_.Extension
                }
            }
        }

        foreach($case in $testCases) {
            $skip = $null
            $name = $case.Name
            Write-Verbose "Testing $name..." -Verbose
            $extractedPath = Join-Path Testdrive:\ -ChildPath ([System.io.path]::GetRandomFileName())
            $null = New-Item -Path $extractedPath -ItemType Directory -Force
            $resolvedPath = (Resolve-Path -Path $extractedPath).ProviderPath
            switch ($case.Extension) {
                '.zip' {
                    Expand-Archive -Path $case.FilePath -DestinationPath $extractedPath
                    $manifestPath = Join-Path $extractedPath -ChildPath '/_manifest/spdx_2.2/manifest.spdx.json'
                }
                '.rpm' {
                    $skip = "rpm test is not stable"
                }
                Default {
                    throw "Unknown extension $($case.Extension)"
                }
            }

            It "$name has a BOM" {
                if ($skip) {
                    Set-ItResult -Pending -Because $skip
                }
                $manifestPath | Should -Exist
            }

            # RPM hashes are broken, skip that
            if ($case.Extension -in '.zip') {
                Test-PackageManifest -PackagePath $extractedPath | ForEach-Object {
                    $status = $_.Status
                    $expectedHash = $_.ExpectedHash
                    $actual = $_.ActualHash
                    $file = $_.File

                    switch ($status) {
                        # cover match and mismatch
                        default {
                            $matchCases += @{
                                Name         = $name
                                File         = $file
                                ActualHash   = $actual
                                ExpectedHash = $ExpectedHash
                                Status       = $status
                            }
                        }
                        "MissingFromPackage" {
                            $missingFromPackageCases = @{
                                Name         = $name
                                File         = $file
                                ActualHash   = $actual
                                ExpectedHash = $ExpectedHash
                                Status       = $status
                            }
                        }
                        "MissingFromManifest" {
                            $missingFromManifestCases = @{
                                Name         = $name
                                File         = $file
                                ActualHash   = $actual
                                ExpectedHash = $ExpectedHash
                                Status       = $status
                            }
                        }
                    }
                }
            }
        }
    }

    Context "Package files" {
        It "<name> should have <file> with matching hash" -TestCases $matchCases {
            param(
                $Name,
                $File,
                $ActualHash,
                $ExpectedHash,
                $Status
            )

            $status | Should -Be "Match" -Because  "$actualHash should be $expectedHash"
        }

        It "<name> should have <file> with matching hash" -TestCases $missingFromPackageCases -Skip:($missingFromPackageCases.Count -eq 0)  {
            param(
                $Name,
                $File,
                $ActualHash,
                $ExpectedHash,
                $Status
            )

            $status | Should -Be "Match" -Because "All files in manifest should exist in package"
        }

        It "Manifest for <name> should have <file>" -TestCases $missingFromManifestCases -Skip:($missingFromManifestCases.Count -eq 0) {
            param(
                $Name,
                $File,
                $ActualHash,
                $ExpectedHash,
                $Status
            )

            $status | Should -Be "Match" -Because "All files in package should exist in manifest"
        }
    }
}