Skip to content

Commit c281658

Browse files
TristanCacquerayfrasertweedale
authored andcommitted
tools: add golden tests to validate the parser
Fixes #100
1 parent e443352 commit c281658

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed

.github/workflows/haskell-ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ jobs:
219219
- name: build
220220
run: |
221221
$CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --write-ghc-environment-files=always
222+
- name: tests
223+
run: |
224+
$CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct
222225
- name: hlint
223226
run: |
224227
if [ $((HCNUMVER >= 90400 && HCNUMVER < 90600)) -ne 0 ] ; then (cd ${PKGDIR_hsec_tools} && hlint -XHaskell2010 src) ; fi

code/hsec-tools/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
## Building
66

77
We aim to support both regular cabal-based and nix-based builds.
8+
9+
## Testing
10+
11+
Run (and auto update) the golden test:
12+
13+
```ShellSession
14+
cabal test -O0 --test-show-details=direct --test-option=--accept
15+
```

code/hsec-tools/hsec-tools.cabal

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ maintainer: [email protected]
2121
-- copyright:
2222
category: Data
2323
extra-doc-files: CHANGELOG.md
24+
extra-source-files: test/golden/*.md
25+
test/golden/*.golden
2426

2527
tested-with:
2628
GHC ==8.10.7 || ==9.0.2 || ==9.2.7 || ==9.4.5 || ==9.6.2
@@ -86,3 +88,24 @@ executable hsec-tools
8688
-Wincomplete-uni-patterns
8789
-Wpartial-fields
8890
-Wredundant-constraints
91+
92+
test-suite spec
93+
type: exitcode-stdio-1.0
94+
hs-source-dirs: test
95+
main-is: Spec.hs
96+
build-depends: base < 5
97+
, directory
98+
, hsec-tools
99+
, pretty-simple < 5
100+
, tasty < 1.5
101+
, tasty-golden < 2.4
102+
, time
103+
, text
104+
default-language: Haskell2010
105+
ghc-options: -Wall
106+
-Wcompat
107+
-Widentities
108+
-Wincomplete-record-updates
109+
-Wincomplete-uni-patterns
110+
-Wpartial-fields
111+
-Wredundant-constraints

code/hsec-tools/test/Spec.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Main where
4+
5+
import Data.List (isSuffixOf)
6+
import qualified Data.Text.IO as T
7+
import qualified Data.Text.Lazy as LText
8+
import qualified Data.Text.Lazy.Encoding as LText
9+
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)
10+
import Data.Time.LocalTime
11+
import System.Directory (listDirectory)
12+
import Test.Tasty
13+
import Test.Tasty.Golden (goldenVsString)
14+
import Text.Pretty.Simple (pShowNoColor)
15+
16+
import Security.Advisories.Parse
17+
18+
main :: IO ()
19+
main = do
20+
goldenFiles <- listGoldenFiles
21+
defaultMain (testGroup "Tests" (tests goldenFiles))
22+
23+
listGoldenFiles :: IO [FilePath]
24+
listGoldenFiles = map (mappend dpath) . filter (not . isSuffixOf ".golden") <$> listDirectory dpath
25+
where
26+
dpath = "test/golden/"
27+
28+
tests :: [FilePath] -> [TestTree]
29+
tests goldenFiles =
30+
[ testGroup "Golden test" $ map doGoldenTest goldenFiles
31+
]
32+
33+
doGoldenTest :: FilePath -> TestTree
34+
doGoldenTest fp = goldenVsString fp (fp <> ".golden") (flip mappend "\n" . LText.encodeUtf8 <$> doCheck)
35+
where
36+
doCheck :: IO LText.Text
37+
doCheck = do
38+
input <- T.readFile fp
39+
let fakeDate = ZonedTime (LocalTime (fromOrdinalDate 1970 0) midnight) utc
40+
attr =
41+
emptyOutOfBandAttributes
42+
{ oobPublished = Just fakeDate
43+
, oobModified = Just fakeDate
44+
}
45+
res = parseAdvisory NoOverrides attr input
46+
pure . pShowNoColor $ res
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
```toml
2+
3+
[advisory]
4+
id = "HSEC-0000-0000"
5+
cwe = []
6+
keywords = ["example", "freeform", "keywords"]
7+
aliases = ["CVE-2022-XXXX"]
8+
related = ["CVE-2022-YYYY", "CVE-2022-ZZZZ"]
9+
10+
[[affected]]
11+
package = "package-name"
12+
cvss = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H"
13+
14+
[[affected.versions]]
15+
introduced = "1.0.8"
16+
fixed = "1.1"
17+
[[affected.versions]]
18+
introduced = "1.1.2"
19+
20+
[[references]]
21+
type = "ARTICLE"
22+
url = "https://example.com"
23+
```
24+
25+
# Advisory Template - Title Goes Here
26+
27+
This is an example template.
28+
29+
* Markdown
30+
* TOML "front matter".
31+
32+
> Acme Broken.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
Right
2+
( Advisory
3+
{ advisoryId = HSEC-0000-0000
4+
, advisoryModified = 1970-01-01 00:00:00 UTC
5+
, advisoryPublished = 1970-01-01 00:00:00 UTC
6+
, advisoryCWEs = []
7+
, advisoryKeywords =
8+
[ "example"
9+
, "freeform"
10+
, "keywords"
11+
]
12+
, advisoryAliases = [ "CVE-2022-XXXX" ]
13+
, advisoryRelated =
14+
[ "CVE-2022-YYYY"
15+
, "CVE-2022-ZZZZ"
16+
]
17+
, advisoryAffected =
18+
[ Affected
19+
{ affectedPackage = "package-name"
20+
, affectedCVSS = "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H"
21+
, affectedVersions =
22+
[ AffectedVersionRange
23+
{ affectedVersionRangeIntroduced = "1.0.8"
24+
, affectedVersionRangeFixed = Just "1.1"
25+
}
26+
, AffectedVersionRange
27+
{ affectedVersionRangeIntroduced = "1.1.2"
28+
, affectedVersionRangeFixed = Nothing
29+
}
30+
]
31+
, affectedArchitectures = Nothing
32+
, affectedOS = Nothing
33+
, affectedDeclarations = []
34+
}
35+
]
36+
, advisoryReferences =
37+
[ Reference
38+
{ referencesType = ReferenceTypeArticle
39+
, referencesUrl = "https://example.com"
40+
}
41+
]
42+
, advisoryPandoc = Pandoc
43+
( Meta
44+
{ unMeta = fromList [] }
45+
)
46+
[ Header 1
47+
( ""
48+
, []
49+
, []
50+
)
51+
[ Str "Advisory"
52+
, Space
53+
, Str "Template"
54+
, Space
55+
, Str "-"
56+
, Space
57+
, Str "Title"
58+
, Space
59+
, Str "Goes"
60+
, Space
61+
, Str "Here"
62+
]
63+
, Para
64+
[ Str "This"
65+
, Space
66+
, Str "is"
67+
, Space
68+
, Str "an"
69+
, Space
70+
, Str "example"
71+
, Space
72+
, Str "template."
73+
]
74+
, BulletList
75+
[
76+
[ Plain
77+
[ Str "Markdown" ]
78+
]
79+
,
80+
[ Plain
81+
[ Str "TOML"
82+
, Space
83+
, Str ""front"
84+
, Space
85+
, Str "matter"."
86+
]
87+
]
88+
]
89+
, BlockQuote
90+
[ Para
91+
[ Str "Acme"
92+
, Space
93+
, Str "Broken."
94+
]
95+
]
96+
]
97+
, advisoryHtml = "<pre><code class="language-toml">
98+
[advisory]
99+
id = &quot;HSEC-0000-0000&quot;
100+
cwe = []
101+
keywords = [&quot;example&quot;, &quot;freeform&quot;, &quot;keywords&quot;]
102+
aliases = [&quot;CVE-2022-XXXX&quot;]
103+
related = [&quot;CVE-2022-YYYY&quot;, &quot;CVE-2022-ZZZZ&quot;]
104+
105+
[[affected]]
106+
package = &quot;package-name&quot;
107+
cvss = &quot;CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H&quot;
108+
109+
[[affected.versions]]
110+
introduced = &quot;1.0.8&quot;
111+
fixed = &quot;1.1&quot;
112+
[[affected.versions]]
113+
introduced = &quot;1.1.2&quot;
114+
115+
[[references]]
116+
type = &quot;ARTICLE&quot;
117+
url = &quot;https://example.com&quot;
118+
</code></pre>
119+
<h1>Advisory Template - Title Goes Here</h1>
120+
<p>This is an example template.</p>
121+
<ul>
122+
<li>Markdown
123+
</li>
124+
<li>TOML &quot;front matter&quot;.
125+
</li>
126+
</ul>
127+
<blockquote>
128+
<p>Acme Broken.</p>
129+
</blockquote>
130+
"
131+
, advisorySummary = "Advisory Template - Title Goes Here"
132+
, advisoryDetails = "# Advisory Template - Title Goes Here
133+
134+
This is an example template.
135+
136+
* Markdown
137+
* TOML "front matter".
138+
139+
> Acme Broken.
140+
"
141+
}
142+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```toml
2+
[advisory]
3+
id = "HSEC-0000-0000"
4+
cwe = []
5+
```
6+
7+
## Title
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Left
2+
( AdvisoryError
3+
( MissingKey "affected" ) "MissingKey "affected""
4+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```toml
2+
[advisory]
3+
id = "HSEC-0000-0000"
4+
cwe = []
5+
date = 1970-01-01
6+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Left
2+
( MarkdownFormatError "Does not have summary heading" )

0 commit comments

Comments
 (0)