Skip to content

Commit 1caaaa0

Browse files
committed
oval: add Test interface and implementation
Signed-off-by: Hank Donnay <[email protected]>
1 parent 1f6f813 commit 1caaaa0

File tree

8 files changed

+91
-46
lines changed

8 files changed

+91
-46
lines changed

oval/dpkginfo.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66

77
// DpkgInfoTest : >tests>dpkginfo_test
88
type DpkgInfoTest struct {
9-
XMLName xml.Name `xml:"dpkginfo_test"`
10-
ID string `xml:"id,attr"`
11-
Comment string `xml:"comment,attr"`
12-
Check string `xml:"check,attr"`
13-
Version int `xml:"version,attr"`
14-
ObjectRefs []ObjectRef `xml:"object"`
15-
StateRefs []StateRef `xml:"state"`
9+
XMLName xml.Name `xml:"dpkginfo_test"`
10+
ID string `xml:"id,attr"`
11+
Comment string `xml:"comment,attr"`
12+
Check string `xml:"check,attr"`
13+
Version int `xml:"version,attr"`
14+
testRef
1615
}
1716

17+
var _ Test = (*DpkgInfoTest)(nil)
18+
1819
// DpkgName : >objects>dpkginfo_object>name
1920
//
2021
// when parsing ubuntu var_ref is a reference

oval/line.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import "encoding/xml"
44

55
// LineTest : >tests>line_test
66
type LineTest struct {
7-
XMLName xml.Name `xml:"line_test"`
8-
ID string `xml:"id,attr"`
9-
StateOperator string `xml:"state_operator,attr"`
10-
ObjectRefs []ObjectRef `xml:"object"`
11-
StateRefs []StateRef `xml:"state"`
12-
Comment string `xml:"comment,attr"`
7+
XMLName xml.Name `xml:"line_test"`
8+
ID string `xml:"id,attr"`
9+
StateOperator string `xml:"state_operator,attr"`
10+
Comment string `xml:"comment,attr"`
11+
testRef
1312
}
1413

14+
var _ Test = (*LineTest)(nil)
15+
1516
// LineObject : >objects>line_object
1617
type LineObject struct {
1718
XMLName xml.Name `xml:"line_object"`

oval/rpminfo.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@ import (
66

77
// RPMInfoTest : >tests>rpminfo_test
88
type RPMInfoTest struct {
9-
XMLName xml.Name `xml:"rpminfo_test"`
10-
ID string `xml:"id,attr"`
11-
Comment string `xml:"comment,attr"`
12-
Check string `xml:"check,attr"`
13-
Version int `xml:"version,attr"`
14-
ObjectRefs []ObjectRef `xml:"object"`
15-
StateRefs []StateRef `xml:"state"`
9+
XMLName xml.Name `xml:"rpminfo_test"`
10+
ID string `xml:"id,attr"`
11+
Comment string `xml:"comment,attr"`
12+
Check string `xml:"check,attr"`
13+
Version int `xml:"version,attr"`
14+
testRef
1615
}
1716

17+
var _ Test = (*RPMInfoTest)(nil)
18+
1819
// RPMVerifyFileTest : >tests>rpmverifyfile_test
1920
type RPMVerifyFileTest struct {
20-
XMLName xml.Name `xml:"rpmverifyfile_test"`
21-
ID string `xml:"id,attr"`
22-
Comment string `xml:"comment,attr"`
23-
Check string `xml:"check,attr"`
24-
Version int `xml:"version,attr"`
25-
ObjectRefs []ObjectRef `xml:"object"`
26-
StateRefs []StateRef `xml:"state"`
21+
XMLName xml.Name `xml:"rpmverifyfile_test"`
22+
ID string `xml:"id,attr"`
23+
Comment string `xml:"comment,attr"`
24+
Check string `xml:"check,attr"`
25+
Version int `xml:"version,attr"`
26+
testRef
2727
}
2828

29+
var _ Test = (*RPMVerifyFileTest)(nil)
30+
2931
// RPMInfoObject : >objects>RPMInfo_object
3032
type RPMInfoObject struct {
3133
XMLName xml.Name `xml:"rpminfo_object"`

oval/rpminfo_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package oval
33
import (
44
"encoding/xml"
55
"os"
6+
"strings"
67
"testing"
78
)
89

@@ -61,22 +62,44 @@ func TestLookupRPMTest(t *testing.T) {
6162
t.Errorf("got: %q, want %q", got, want)
6263
}
6364
var name string
65+
var test Test
6466
switch kind {
6567
case "rpmverifyfile_test":
6668
obj := &root.Tests.RPMVerifyFileTests[i]
6769
name = obj.Comment
70+
test = obj
6871
t.Logf("%s: %s (%#+v)", tc.Ref, obj.Comment, obj)
6972
case "uname_test":
7073
obj := &root.Tests.UnameTests[i]
7174
name = obj.Comment
75+
test = obj
7276
t.Logf("%s: %s (%#+v)", tc.Ref, obj.Comment, obj)
7377
case "textfilecontent54_test":
7478
obj := &root.Tests.TextfileContent54Tests[i]
7579
name = obj.Comment
80+
test = obj
7681
t.Logf("%s: %s (%#+v)", tc.Ref, obj.Comment, obj)
7782
default:
7883
t.Fatalf("unknown test kind %q", kind)
7984
}
85+
var ss, os strings.Builder
86+
ss.WriteByte('[')
87+
for i, ref := range test.StateRef() {
88+
if i != 0 {
89+
ss.WriteString(", ")
90+
}
91+
ss.WriteString(ref.StateRef)
92+
}
93+
ss.WriteByte(']')
94+
os.WriteByte('[')
95+
for i, ref := range test.ObjectRef() {
96+
if i != 0 {
97+
os.WriteString(", ")
98+
}
99+
os.WriteString(ref.ObjectRef)
100+
}
101+
os.WriteByte(']')
102+
t.Logf("states: %s; objects: %s", ss.String(), os.String())
80103
if got, want := name, tc.Name; got != want {
81104
t.Fatalf("got: %q, want: %q", got, want)
82105
}

oval/tests.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,18 @@ func (t *Tests) Lookup(ref string) (kind string, index int, err error) {
107107
}
108108
return "", -1, ErrNotFound(ref)
109109
}
110+
111+
// Test is an interface for OVAL objects that reports Object and State refs.
112+
type Test interface {
113+
ObjectRef() []ObjectRef
114+
StateRef() []StateRef
115+
}
116+
117+
// TestRef is an embeddable struct that implements the Test interface.
118+
type testRef struct {
119+
ObjectRefs []ObjectRef `xml:"object"`
120+
StateRefs []StateRef `xml:"state"`
121+
}
122+
123+
func (t *testRef) ObjectRef() []ObjectRef { return t.ObjectRefs }
124+
func (t *testRef) StateRef() []StateRef { return t.StateRefs }

oval/textfilecontent54.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import "encoding/xml"
44

55
// TextfileContent54Test : >tests>textfilecontent54_test
66
type TextfileContent54Test struct {
7-
XMLName xml.Name `xml:"textfilecontent54_test"`
8-
ID string `xml:"id,attr"`
9-
StateOperator string `xml:"state_operator,attr"`
10-
ObjectRefs []ObjectRef `xml:"object"`
11-
StateRefs []StateRef `xml:"state"`
12-
Comment string `xml:"comment,attr"`
7+
XMLName xml.Name `xml:"textfilecontent54_test"`
8+
ID string `xml:"id,attr"`
9+
StateOperator string `xml:"state_operator,attr"`
10+
Comment string `xml:"comment,attr"`
11+
testRef
1312
}
13+
14+
var _ Test = (*TextfileContent54Test)(nil)

oval/uname.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import "encoding/xml"
44

55
// UnameTest : >tests>uname_test
66
type UnameTest struct {
7-
XMLName xml.Name `xml:"uname_test"`
8-
ID string `xml:"id,attr"`
9-
Comment string `xml:"comment,attr"`
10-
Check string `xml:"check,attr"`
11-
Version int `xml:"version,attr"`
12-
ObjectRefs []ObjectRef `xml:"object"`
13-
StateRefs []StateRef `xml:"state"`
7+
XMLName xml.Name `xml:"uname_test"`
8+
ID string `xml:"id,attr"`
9+
Comment string `xml:"comment,attr"`
10+
Check string `xml:"check,attr"`
11+
Version int `xml:"version,attr"`
12+
testRef
1413
}
14+
15+
var _ Test = (*UnameTest)(nil)

oval/version55.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import "encoding/xml"
44

55
// Version55Test : >tests>version55_test
66
type Version55Test struct {
7-
XMLName xml.Name `xml:"version55_test"`
8-
ID string `xml:"id,attr"`
9-
StateOperator string `xml:"state_operator,attr"`
10-
ObjectRefs []ObjectRef `xml:"object"`
11-
StateRefs []StateRef `xml:"state"`
12-
Comment string `xml:"comment,attr"`
7+
XMLName xml.Name `xml:"version55_test"`
8+
ID string `xml:"id,attr"`
9+
StateOperator string `xml:"state_operator,attr"`
10+
Comment string `xml:"comment,attr"`
11+
testRef
1312
}
1413

14+
var _ Test = (*Version55Test)(nil)
15+
1516
// Version55Object : >objects>version55_object
1617
type Version55Object struct {
1718
XMLName xml.Name `xml:"version55_object"`

0 commit comments

Comments
 (0)