Skip to content

Commit

Permalink
test: mock RPM DB (#5567)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 authored Nov 15, 2023
1 parent a96ec35 commit d005f5a
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 546 deletions.
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

0 comments on commit d005f5a

Please sign in to comment.