mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
* [wip] Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * distinct the package metadata functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove metadata type from package core model Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate review feedback for names Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add RPM archive metadata and split parser helpers Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * clarify the python package metadata type Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename the KB metadata type Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * break hackage and composer types by use case Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * linting fix Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix encoding and decoding for syft-json and cyclonedx Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump json schema to 11 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cyclonedx-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cyclonedx-xml snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update spdx-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update spdx-tv snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update syft-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct metadata type in stack yaml parser test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix bom-ref redactor for cyclonedx-xml Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add tests for legacy package metadata names Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * regenerate json schema v11 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix legacy HackageMetadataType reflect type value check Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * packagemetadata discovery should account for type shadowing Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump json schema version to v12 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update json schema to incorporate changes from main Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add syft-json legacy config option Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add tests around v11-v12 json decoding Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add docs for SYFT_JSON_LEGACY Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename structs to be compliant with new naming scheme Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
164 lines
3.9 KiB
Go
164 lines
3.9 KiB
Go
package pkg
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestApkMetadata_UnmarshalJSON(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want ApkDBEntry
|
|
wantErr require.ErrorAssertionFunc
|
|
}{
|
|
{
|
|
name: "empty",
|
|
input: "{}",
|
|
want: ApkDBEntry{},
|
|
},
|
|
{
|
|
name: "string array dependencies",
|
|
input: `{
|
|
"package": "scanelf",
|
|
"originPackage": "pax-utils",
|
|
"maintainer": "Natanael Copa <ncopa@alpinelinux.org>",
|
|
"version": "1.3.4-r0",
|
|
"license": "GPL-2.0-only",
|
|
"architecture": "x86_64",
|
|
"url": "https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities",
|
|
"description": "Scan ELF binaries for stuff",
|
|
"size": 36745,
|
|
"installedSize": 94208,
|
|
"pullChecksum": "Q1Gcqe+ND8DFOlhM3R0o5KyZjR2oE=",
|
|
"gitCommitOfApkPort": "d7ae612a3cc5f827289d915783b4cbf8c7207947",
|
|
"files": [
|
|
{
|
|
"path": "/usr"
|
|
}
|
|
],
|
|
"pullDependencies": ["foo", "bar"]
|
|
}`,
|
|
want: ApkDBEntry{
|
|
Package: "scanelf",
|
|
OriginPackage: "pax-utils",
|
|
Maintainer: "Natanael Copa <ncopa@alpinelinux.org>",
|
|
Version: "1.3.4-r0",
|
|
Architecture: "x86_64",
|
|
URL: "https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities",
|
|
Description: "Scan ELF binaries for stuff",
|
|
Size: 36745,
|
|
InstalledSize: 94208,
|
|
Dependencies: []string{"foo", "bar"},
|
|
Checksum: "Q1Gcqe+ND8DFOlhM3R0o5KyZjR2oE=",
|
|
GitCommit: "d7ae612a3cc5f827289d915783b4cbf8c7207947",
|
|
Files: []ApkFileRecord{{Path: "/usr"}},
|
|
},
|
|
},
|
|
{
|
|
name: "single string dependencies",
|
|
input: `{
|
|
"package": "scanelf",
|
|
"originPackage": "pax-utils",
|
|
"maintainer": "Natanael Copa <ncopa@alpinelinux.org>",
|
|
"version": "1.3.4-r0",
|
|
"license": "GPL-2.0-only",
|
|
"architecture": "x86_64",
|
|
"url": "https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities",
|
|
"description": "Scan ELF binaries for stuff",
|
|
"size": 36745,
|
|
"installedSize": 94208,
|
|
"pullChecksum": "Q1Gcqe+ND8DFOlhM3R0o5KyZjR2oE=",
|
|
"gitCommitOfApkPort": "d7ae612a3cc5f827289d915783b4cbf8c7207947",
|
|
"files": [
|
|
{
|
|
"path": "/usr"
|
|
}
|
|
],
|
|
"pullDependencies": "foo bar"
|
|
}`,
|
|
want: ApkDBEntry{
|
|
Package: "scanelf",
|
|
OriginPackage: "pax-utils",
|
|
Maintainer: "Natanael Copa <ncopa@alpinelinux.org>",
|
|
Version: "1.3.4-r0",
|
|
Architecture: "x86_64",
|
|
URL: "https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities",
|
|
Description: "Scan ELF binaries for stuff",
|
|
Size: 36745,
|
|
InstalledSize: 94208,
|
|
Dependencies: []string{"foo", "bar"},
|
|
Checksum: "Q1Gcqe+ND8DFOlhM3R0o5KyZjR2oE=",
|
|
GitCommit: "d7ae612a3cc5f827289d915783b4cbf8c7207947",
|
|
Files: []ApkFileRecord{{Path: "/usr"}},
|
|
},
|
|
},
|
|
{
|
|
name: "null pullDependencies",
|
|
input: `{
|
|
"pullDependencies": null
|
|
}`,
|
|
want: ApkDBEntry{
|
|
Dependencies: nil,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.wantErr == nil {
|
|
tt.wantErr = require.NoError
|
|
}
|
|
var got ApkDBEntry
|
|
err := json.Unmarshal([]byte(tt.input), &got)
|
|
tt.wantErr(t, err)
|
|
if err != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSpaceDelimitedStringSlice_UnmarshalJSON(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data string
|
|
want []string
|
|
wantErr require.ErrorAssertionFunc
|
|
}{
|
|
{
|
|
name: "empty string",
|
|
data: `""`,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "single string with one elements",
|
|
data: `"foo"`,
|
|
want: []string{"foo"},
|
|
},
|
|
{
|
|
name: "single string with multiple elements",
|
|
data: `"foo bar"`,
|
|
want: []string{"foo", "bar"},
|
|
},
|
|
{
|
|
name: "string array",
|
|
data: `["foo", "bar"]`,
|
|
want: []string{"foo", "bar"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.wantErr == nil {
|
|
tt.wantErr = require.NoError
|
|
}
|
|
element := spaceDelimitedStringSlice{}
|
|
tt.wantErr(t, element.UnmarshalJSON([]byte(tt.data)))
|
|
assert.Equal(t, tt.want, []string(element))
|
|
})
|
|
}
|
|
}
|