Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: mock RPM DB #5567

Merged
merged 2 commits into from
Nov 15, 2023
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
19 changes: 18 additions & 1 deletion pkg/fanal/analyzer/pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var osVendors = []string{

type rpmPkgAnalyzer struct{}

type RPMDB interface {
ListPackages() ([]*rpmdb.PackageInfo, error)
}

func (a rpmPkgAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
parsedPkgs, installedFiles, err := a.parsePkgInfo(input.Content)
if err != nil {
Expand Down Expand Up @@ -93,7 +97,12 @@ func (a rpmPkgAnalyzer) parsePkgInfo(rc io.Reader) (types.Packages, []string, er
if err != nil {
return nil, nil, xerrors.Errorf("failed to open RPM DB: %w", err)
}
defer db.Close()

return a.listPkgs(db)
}

func (a rpmPkgAnalyzer) listPkgs(db RPMDB) (types.Packages, []string, error) {
// equivalent:
// new version: rpm -qa --qf "%{NAME} %{EPOCHNUM} %{VERSION} %{RELEASE} %{SOURCERPM} %{ARCH}\n"
// old version: rpm -qa --qf "%{NAME} %{EPOCH} %{VERSION} %{RELEASE} %{SOURCERPM} %{ARCH}\n"
Expand Down Expand Up @@ -129,6 +138,9 @@ func (a rpmPkgAnalyzer) parsePkgInfo(rc io.Reader) (types.Packages, []string, er
if err != nil {
return nil, nil, xerrors.Errorf("unable to get installed files: %w", err)
}
files = lo.Map(files, func(file string, _ int) string {
return filepath.ToSlash(file)
})
}

// RPM DB uses MD5 digest
Expand All @@ -138,6 +150,11 @@ func (a rpmPkgAnalyzer) parsePkgInfo(rc io.Reader) (types.Packages, []string, er
d = digest.NewDigestFromString(digest.MD5, pkg.SigMD5)
}

var licenses []string
if pkg.License != "" {
licenses = []string{pkg.License}
}

p := types.Package{
ID: fmt.Sprintf("%s@%s-%s.%s", pkg.Name, pkg.Version, pkg.Release, pkg.Arch),
Name: pkg.Name,
Expand All @@ -150,7 +167,7 @@ func (a rpmPkgAnalyzer) parsePkgInfo(rc io.Reader) (types.Packages, []string, er
SrcVersion: srcVer,
SrcRelease: srcRel,
Modularitylabel: pkg.Modularitylabel,
Licenses: []string{pkg.License},
Licenses: licenses,
DependsOn: pkg.Requires, // Will be replaced with package IDs
Maintainer: pkg.Vendor,
Digest: d,
Expand Down
Loading