Skip to content

Commit 6c9ec32

Browse files
TristanCacquerayblackheaven
authored andcommitted
Add OSV export golden test
This change prevents regression in the OSV output.
1 parent 0cf102f commit 6c9ec32

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

code/hsec-tools/hsec-tools.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ test-suite spec
103103
main-is: Spec.hs
104104
other-modules: Spec.QueriesSpec
105105
build-depends:
106+
, aeson-pretty <2
106107
, base <5
107108
, Cabal-syntax
108109
, cvss

code/hsec-tools/test/Spec.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Main where
44

5+
import Data.Aeson.Encode.Pretty (encodePretty)
56
import Data.List (isSuffixOf)
67
import qualified Data.Text.IO as T
78
import qualified Data.Text.Lazy as LText
@@ -13,6 +14,7 @@ import Test.Tasty
1314
import Test.Tasty.Golden (goldenVsString)
1415
import Text.Pretty.Simple (pShowNoColor)
1516

17+
import qualified Security.Advisories.Convert.OSV as OSV
1618
import Security.Advisories.Parse
1719
import qualified Spec.QueriesSpec as QueriesSpec
1820

@@ -34,7 +36,7 @@ goldenTestsSpec :: [FilePath] -> TestTree
3436
goldenTestsSpec goldenFiles = testGroup "Golden test" $ map doGoldenTest goldenFiles
3537

3638
doGoldenTest :: FilePath -> TestTree
37-
doGoldenTest fp = goldenVsString fp (fp <> ".golden") (flip mappend "\n" . LText.encodeUtf8 <$> doCheck)
39+
doGoldenTest fp = goldenVsString fp (fp <> ".golden") (LText.encodeUtf8 <$> doCheck)
3840
where
3941
doCheck :: IO LText.Text
4042
doCheck = do
@@ -46,4 +48,12 @@ doGoldenTest fp = goldenVsString fp (fp <> ".golden") (flip mappend "\n" . LText
4648
, oobModified = Just fakeDate
4749
}
4850
res = parseAdvisory NoOverrides attr input
49-
pure . pShowNoColor $ res
51+
osvExport = case res of
52+
Right adv ->
53+
let osv = OSV.convert adv
54+
in LText.unlines
55+
[ pShowNoColor osv
56+
, LText.decodeUtf8 (encodePretty osv)
57+
]
58+
Left _ -> ""
59+
pure (LText.unlines [pShowNoColor res, osvExport])

code/hsec-tools/test/golden/EXAMPLE_ADVISORY.md.golden

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,106 @@ Right
153153
"
154154
}
155155
)
156+
Model
157+
{ modelSchemaVersion = "1.5.0"
158+
, modelId = "HSEC-0000-0000"
159+
, modelModified = 1970-01-01 00:00:00 UTC
160+
, modelPublished = Just 1970-01-01 00:00:00 UTC
161+
, modelWithdrawn = Nothing
162+
, modelAliases = [ "CVE-2022-XXXX" ]
163+
, modelRelated =
164+
[ "CVE-2022-YYYY"
165+
, "CVE-2022-ZZZZ"
166+
]
167+
, modelSummary = Just "Advisory Template - Title Goes Here"
168+
, modelDetails = Just "# Advisory Template - Title Goes Here
169+
170+
This is an example template.
171+
172+
* Markdown
173+
* TOML "front matter".
174+
175+
> Acme Broken.
176+
"
177+
, modelSeverity = []
178+
, modelAffected =
179+
[ Affected
180+
{ affectedRanges =
181+
[ RangeEcosystem
182+
[ EventIntroduced "1.0.8"
183+
, EventFixed "1.1"
184+
, EventIntroduced "1.1.2"
185+
] Nothing
186+
]
187+
, affectedPackage = Package
188+
{ packageName = "package-name"
189+
, packageEcosystem = "Hackage"
190+
, packagePurl = Nothing
191+
}
192+
, affectedSeverity =
193+
[ Severity CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H ]
194+
, affectedEcosystemSpecific = Nothing
195+
, affectedDatabaseSpecific = Nothing
196+
}
197+
]
198+
, modelReferences =
199+
[ Reference
200+
{ referencesType = ReferenceTypeArticle
201+
, referencesUrl = "https://example.com"
202+
}
203+
]
204+
, modelCredits = []
205+
, modelDatabaseSpecific = Nothing
206+
}
207+
{
208+
"affected": [
209+
{
210+
"package": {
211+
"ecosystem": "Hackage",
212+
"name": "package-name"
213+
},
214+
"ranges": [
215+
{
216+
"events": [
217+
{
218+
"introduced": "1.0.8"
219+
},
220+
{
221+
"fixed": "1.1"
222+
},
223+
{
224+
"introduced": "1.1.2"
225+
}
226+
],
227+
"type": "ECOSYSTEM"
228+
}
229+
],
230+
"severity": [
231+
{
232+
"score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
233+
"type": "CVSS_V3"
234+
}
235+
]
236+
}
237+
],
238+
"aliases": [
239+
"CVE-2022-XXXX"
240+
],
241+
"details": "# Advisory Template - Title Goes Here\n\nThis is an example template.\n\n * Markdown\n * TOML \"front matter\".\n\n > Acme Broken.\n",
242+
"id": "HSEC-0000-0000",
243+
"modified": "1970-01-01T00:00:00Z",
244+
"published": "1970-01-01T00:00:00Z",
245+
"references": [
246+
{
247+
"type": "ARTICLE",
248+
"url": "https://example.com"
249+
}
250+
],
251+
"related": [
252+
"CVE-2022-YYYY",
253+
"CVE-2022-ZZZZ"
254+
],
255+
"schema_version": "1.5.0",
256+
"summary": "Advisory Template - Title Goes Here"
257+
}
258+

code/hsec-tools/test/golden/MISSING_AFFECTED.md.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Left
77
] "missing key: affected in top
88
"
99
)
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Left
22
( MarkdownFormatError "Does not have summary heading" )
3+

0 commit comments

Comments
 (0)