Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OSV export golden test #138

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/hsec-tools/hsec-tools.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ test-suite spec
main-is: Spec.hs
other-modules: Spec.QueriesSpec
build-depends:
, aeson-pretty <2
, base <5
, Cabal-syntax
, cvss
Expand Down
14 changes: 12 additions & 2 deletions code/hsec-tools/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Main where

import Data.Aeson.Encode.Pretty (encodePretty)
import Data.List (isSuffixOf)
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as LText
Expand All @@ -13,6 +14,7 @@ import Test.Tasty
import Test.Tasty.Golden (goldenVsString)
import Text.Pretty.Simple (pShowNoColor)

import qualified Security.Advisories.Convert.OSV as OSV
import Security.Advisories.Parse
import qualified Spec.QueriesSpec as QueriesSpec

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

doGoldenTest :: FilePath -> TestTree
doGoldenTest fp = goldenVsString fp (fp <> ".golden") (flip mappend "\n" . LText.encodeUtf8 <$> doCheck)
doGoldenTest fp = goldenVsString fp (fp <> ".golden") (LText.encodeUtf8 <$> doCheck)
where
doCheck :: IO LText.Text
doCheck = do
Expand All @@ -46,4 +48,12 @@ doGoldenTest fp = goldenVsString fp (fp <> ".golden") (flip mappend "\n" . LText
, oobModified = Just fakeDate
}
res = parseAdvisory NoOverrides attr input
pure . pShowNoColor $ res
osvExport = case res of
Right adv ->
let osv = OSV.convert adv
in LText.unlines
[ pShowNoColor osv
, LText.decodeUtf8 (encodePretty osv)
]
Left _ -> ""
pure (LText.unlines [pShowNoColor res, osvExport])
103 changes: 103 additions & 0 deletions code/hsec-tools/test/golden/EXAMPLE_ADVISORY.md.golden
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,106 @@ Right
"
}
)
Model
{ modelSchemaVersion = "1.5.0"
, modelId = "HSEC-0000-0000"
, modelModified = 1970-01-01 00:00:00 UTC
, modelPublished = Just 1970-01-01 00:00:00 UTC
, modelWithdrawn = Nothing
, modelAliases = [ "CVE-2022-XXXX" ]
, modelRelated =
[ "CVE-2022-YYYY"
, "CVE-2022-ZZZZ"
]
, modelSummary = Just "Advisory Template - Title Goes Here"
, modelDetails = Just "# Advisory Template - Title Goes Here

This is an example template.

* Markdown
* TOML "front matter".

> Acme Broken.
"
, modelSeverity = []
, modelAffected =
[ Affected
{ affectedRanges =
[ RangeEcosystem
[ EventIntroduced "1.0.8"
, EventFixed "1.1"
, EventIntroduced "1.1.2"
] Nothing
]
, affectedPackage = Package
{ packageName = "package-name"
, packageEcosystem = "Hackage"
, packagePurl = Nothing
}
, affectedSeverity =
[ Severity CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H ]
, affectedEcosystemSpecific = Nothing
, affectedDatabaseSpecific = Nothing
}
]
, modelReferences =
[ Reference
{ referencesType = ReferenceTypeArticle
, referencesUrl = "https://example.com"
}
]
, modelCredits = []
, modelDatabaseSpecific = Nothing
}
{
"affected": [
{
"package": {
"ecosystem": "Hackage",
"name": "package-name"
},
"ranges": [
{
"events": [
{
"introduced": "1.0.8"
},
{
"fixed": "1.1"
},
{
"introduced": "1.1.2"
}
],
"type": "ECOSYSTEM"
}
],
"severity": [
{
"score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
"type": "CVSS_V3"
}
]
}
],
"aliases": [
"CVE-2022-XXXX"
],
"details": "# Advisory Template - Title Goes Here\n\nThis is an example template.\n\n * Markdown\n * TOML \"front matter\".\n\n > Acme Broken.\n",
"id": "HSEC-0000-0000",
"modified": "1970-01-01T00:00:00Z",
"published": "1970-01-01T00:00:00Z",
"references": [
{
"type": "ARTICLE",
"url": "https://example.com"
}
],
"related": [
"CVE-2022-YYYY",
"CVE-2022-ZZZZ"
],
"schema_version": "1.5.0",
"summary": "Advisory Template - Title Goes Here"
}

1 change: 1 addition & 0 deletions code/hsec-tools/test/golden/MISSING_AFFECTED.md.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Left
] "missing key: affected in top
"
)

1 change: 1 addition & 0 deletions code/hsec-tools/test/golden/MISSING_TITLE.md.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Left
( MarkdownFormatError "Does not have summary heading" )

Loading