Skip to content

Commit

Permalink
Add tests for legacy parsers
Browse files Browse the repository at this point in the history
Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
  • Loading branch information
puerco committed Jul 22, 2023
1 parent 7acda14 commit ae81bcf
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pkg/vex/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2023 The OpenVEX Authors
SPDX-License-Identifier: Apache-2.0
*/

package vex

import (
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/google/go-cmp/cmp"
)

func TestParse001(t *testing.T) {
d, err := time.Parse(time.RFC3339, "2023-01-08T18:02:03.647787998-06:00")
require.NoError(t, err)
for msg, tc := range map[string]struct {
path string
shouldErr bool
expected *VEX
}{
"normal": {
"testdata/v0.0.1.json",
false,
&VEX{
Metadata: Metadata{
Context: "https://openvex.dev/ns/" + SpecVersion,
ID: "https://openvex.dev/docs/example/vex-9fb3463de1b57",
Author: "Wolfi J Inkinson",
AuthorRole: "Document Creator",
Timestamp: &d,
Version: 1,
},
Statements: []Statement{
{
Vulnerability: Vulnerability{Name: "CVE-2023-12345"},
Products: []Product{
{Component: Component{ID: "pkg:apk/wolfi/[email protected]?arch=armv7"}, Subcomponents: []Subcomponent{}},
{Component: Component{ID: "pkg:apk/wolfi/[email protected]?arch=x86_64"}, Subcomponents: []Subcomponent{}},
},
Status: "fixed",
},
},
},
},
} {
data, err := os.ReadFile(tc.path)
require.NoError(t, err, msg)
doc, err := parse001(data)
if tc.shouldErr {
require.Error(t, err, msg)
return
}

require.True(t, cmp.Equal(doc.Metadata, tc.expected.Metadata), fmt.Sprintf("%+v + %+v", doc.Metadata, tc.expected.Metadata))
require.Equal(t, doc.Statements, tc.expected.Statements, fmt.Sprintf("%+v + %+v", doc.Statements, tc.expected.Statements))
require.True(t, cmp.Equal(doc, tc.expected), msg)

require.NoError(t, err, msg)
}
}
18 changes: 18 additions & 0 deletions pkg/vex/testdata/v0.0.1-noversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"@context": "https://openvex.dev/ns",
"@id": "https://openvex.dev/docs/example/vex-9fb3463de1b57",
"author": "Wolfi J Inkinson",
"role": "Document Creator",
"timestamp": "2023-01-08T18:02:03.647787998-06:00",
"version": "1",
"statements": [
{
"vulnerability": "CVE-2023-12345",
"products": [
"pkg:apk/wolfi/[email protected]?arch=armv7",
"pkg:apk/wolfi/[email protected]?arch=x86_64"
],
"status": "fixed"
}
]
}
18 changes: 18 additions & 0 deletions pkg/vex/testdata/v0.0.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"@context": "https://openvex.dev/ns/v0.0.1",
"@id": "https://openvex.dev/docs/example/vex-9fb3463de1b57",
"author": "Wolfi J Inkinson",
"role": "Document Creator",
"timestamp": "2023-01-08T18:02:03.647787998-06:00",
"version": "1",
"statements": [
{
"vulnerability": "CVE-2023-12345",
"products": [
"pkg:apk/wolfi/[email protected]?arch=armv7",
"pkg:apk/wolfi/[email protected]?arch=x86_64"
],
"status": "fixed"
}
]
}
20 changes: 20 additions & 0 deletions pkg/vex/vex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,23 @@ func TestOpenCSAF(t *testing.T) {
require.Len(t, doc.Statements, tc.len)
}
}

func TestOpen(t *testing.T) {
for m, tc := range map[string]struct {
path string
shouldErr bool
}{
"OpenVEX v0.0.1": {"testdata/v0.0.1.json", false},
"OpenVEX v0.0.1 (no version)": {"testdata/v0.0.1-noversion.json", false},
"CSAF document": {"testdata/csaf.json", false},
} {
doc, err := Open(tc.path)
if tc.shouldErr {
require.Error(t, err, m)
continue
}

require.NoError(t, err, m)
require.NotNil(t, doc, m)
}
}

0 comments on commit ae81bcf

Please sign in to comment.