refactor(debian): use txtar format for test data (#9957)

This commit is contained in:
Teppei Fukuda
2025-12-23 15:55:13 +09:00
committed by GitHub
parent 7a6594c745
commit 517365caa3
9 changed files with 99 additions and 96 deletions

View File

@@ -1,31 +1,27 @@
package dpkg
import (
"os"
"path/filepath"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/trivy/internal/testutil"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/mapfs"
)
func Test_dpkgAnalyzer_Analyze(t *testing.T) {
tests := []struct {
name string
// testFiles contains path in testdata and path in OS
// e.g. tar.md5sums => var/lib/dpkg/info/tar.md5sums
testFiles map[string]string
want *analyzer.AnalysisResult
wantErr bool
name string
txtar string
want *analyzer.AnalysisResult
wantErr bool
}{
{
name: "valid",
testFiles: map[string]string{"./testdata/dpkg": "var/lib/dpkg/status"},
name: "valid",
txtar: "testdata/valid.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1395,8 +1391,8 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
},
},
{
name: "corrupsed",
testFiles: map[string]string{"./testdata/corrupsed": "var/lib/dpkg/status"},
name: "corrupsed",
txtar: "testdata/corrupsed.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1457,8 +1453,8 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
},
},
{
name: "only apt",
testFiles: map[string]string{"./testdata/dpkg_apt": "var/lib/dpkg/status"},
name: "only apt",
txtar: "testdata/only-apt.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1476,11 +1472,8 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
},
},
{
name: "happy path with digests",
testFiles: map[string]string{
"./testdata/digest-status": "var/lib/dpkg/status",
"./testdata/digest-available": "var/lib/dpkg/available",
},
name: "happy path with digests",
txtar: "testdata/digest.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1517,11 +1510,8 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
},
},
{
name: "md5sums",
testFiles: map[string]string{
"./testdata/tar-status": "var/lib/dpkg/status",
"./testdata/tar.md5sums": "var/lib/dpkg/info/tar.md5sums",
},
name: "md5sums",
txtar: "testdata/md5sums.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1575,8 +1565,8 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
},
},
{
name: "third-party package",
testFiles: map[string]string{"./testdata/dpkg-third-party": "var/lib/dpkg/status"},
name: "third-party package",
txtar: "testdata/third-party.txtar",
want: &analyzer.AnalysisResult{
PackageInfos: []types.PackageInfo{
{
@@ -1616,18 +1606,10 @@ func Test_dpkgAnalyzer_Analyze(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
a, err := newDpkgAnalyzer(analyzer.AnalyzerOptions{})
require.NoError(t, err)
ctx := t.Context()
mfs := mapfs.New()
for testPath, osPath := range tt.testFiles {
err = mfs.MkdirAll(filepath.Dir(osPath), os.ModePerm)
require.NoError(t, err)
err = mfs.WriteFile(osPath, testPath)
require.NoError(t, err)
}
got, err := a.PostAnalyze(ctx, analyzer.PostAnalysisInput{
FS: mfs,
fsys := testutil.TxtarToFS(t, tt.txtar)
got, err := a.PostAnalyze(t.Context(), analyzer.PostAnalysisInput{
FS: fsys,
})
require.NoError(t, err)

View File

@@ -1,3 +1,6 @@
Test data for corrupted dpkg status file parsing.
-- var/lib/dpkg/status --
Package: libpam-runtime
Status: install ok installed
Priority: required

View File

@@ -1,41 +0,0 @@
Package: sed
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 320
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 4.4-2
Pre-Depends: libc6 (>= 2.14), libselinux1 (>= 1.32)
Description: GNU stream editor for filtering/transforming text
sed reads the specified files or the standard input if no
files are specified, makes editing changes according to a
list of commands, and writes the results to the standard
output.
Original-Maintainer: Clint Adams <clint@debian.org>
Homepage: https://www.gnu.org/software/sed/
Package: tar
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 3152
Maintainer: Janos Lenart <ocsi@debian.org>
Architecture: amd64
Multi-Arch: foreign
Version: 1.34+dfsg-1
Replaces: cpio (<< 2.4.2-39)
Pre-Depends: libacl1 (>= 2.2.23), libc6 (>= 2.28), libselinux1 (>= 3.1~)
Suggests: bzip2, ncompress, xz-utils, tar-scripts, tar-doc
Breaks: dpkg-dev (<< 1.14.26)
Conflicts: cpio (<= 2.4.2-38)
Description: GNU version of the tar archiving utility
Tar is a program for packaging a set of files as a single archive in tar
format. The function it performs is conceptually similar to cpio, and to
things like PKZIP in the DOS world. It is heavily used by the Debian package
management system, and is useful for performing system backups and exchanging
sets of files with others.
Homepage: https://www.gnu.org/software/tar/

View File

@@ -1,3 +1,49 @@
Test data for dpkg status with digests from available file.
-- var/lib/dpkg/status --
Package: sed
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 320
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 4.4-2
Pre-Depends: libc6 (>= 2.14), libselinux1 (>= 1.32)
Description: GNU stream editor for filtering/transforming text
sed reads the specified files or the standard input if no
files are specified, makes editing changes according to a
list of commands, and writes the results to the standard
output.
Original-Maintainer: Clint Adams <clint@debian.org>
Homepage: https://www.gnu.org/software/sed/
Package: tar
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 3152
Maintainer: Janos Lenart <ocsi@debian.org>
Architecture: amd64
Multi-Arch: foreign
Version: 1.34+dfsg-1
Replaces: cpio (<< 2.4.2-39)
Pre-Depends: libacl1 (>= 2.2.23), libc6 (>= 2.28), libselinux1 (>= 3.1~)
Suggests: bzip2, ncompress, xz-utils, tar-scripts, tar-doc
Breaks: dpkg-dev (<< 1.14.26)
Conflicts: cpio (<= 2.4.2-38)
Description: GNU version of the tar archiving utility
Tar is a program for packaging a set of files as a single archive in tar
format. The function it performs is conceptually similar to cpio, and to
things like PKZIP in the DOS world. It is heavily used by the Debian package
management system, and is useful for performing system backups and exchanging
sets of files with others.
Homepage: https://www.gnu.org/software/tar/
-- var/lib/dpkg/available --
Package: tar
Version: 1.34+dfsg-1
Essential: yes

View File

@@ -1,3 +1,24 @@
Test data for dpkg with md5sums file.
-- var/lib/dpkg/status --
Package: tar
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 864
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 1.29b-2
Replaces: cpio (<< 2.4.2-39)
Pre-Depends: libacl1 (>= 2.2.51-8), libc6 (>= 2.17), libselinux1 (>= 1.32)
Suggests: bzip2, ncompress, xz-utils, tar-scripts, tar-doc
Breaks: dpkg-dev (<< 1.14.26)
Conflicts: cpio (<= 2.4.2-38)
Description: GNU version of the tar archiving utility
-- var/lib/dpkg/info/tar.md5sums --
25de5fcdc3c8ebd9c9f599fb7a899b40 usr/bin/tar
5bf0e62990e0b668830ceb2c8615b497 usr/lib/mime/packages/tar
de1096fbccdc14324196fc6829324ebc usr/sbin/rmt-tar

View File

@@ -1,3 +1,6 @@
Test data for single apt package.
-- var/lib/dpkg/status --
Package: apt
Status: install ok installed
Priority: important

View File

@@ -1,17 +0,0 @@
Package: tar
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 864
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 1.29b-2
Replaces: cpio (<< 2.4.2-39)
Pre-Depends: libacl1 (>= 2.2.51-8), libc6 (>= 2.17), libselinux1 (>= 1.32)
Suggests: bzip2, ncompress, xz-utils, tar-scripts, tar-doc
Breaks: dpkg-dev (<< 1.14.26)
Conflicts: cpio (<= 2.4.2-38)
Description: GNU version of the tar archiving utility

View File

@@ -1,3 +1,6 @@
Test data for third-party package detection.
-- var/lib/dpkg/status --
Package: docker-ce
Status: install ok installed
Priority: optional

View File

@@ -1,3 +1,6 @@
Test data for valid dpkg status file parsing.
-- var/lib/dpkg/status --
Package: fdisk
Status: install ok installed
Priority: important