Skip to content

Commit f64bf33

Browse files
committed
Improve .cbor control validation failure message
1 parent bb822fa commit f64bf33

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

golden/cborControlBad.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
unsatisfied control:
2+
.cbor simpleRule

src/Codec/CBOR/Cuddle/CBOR/Validator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ controlBytes cddl bs Bits ctrl = do
624624
controlBytes cddl bs Cbor ctrl =
625625
case deserialiseFromBytes decodeTerm (BSL.fromStrict bs) of
626626
Right (BSL.null -> True, term) -> isValid $ validateTerm cddl term ctrl
627-
_ -> error "Not yet implemented"
627+
_ -> False
628628
controlBytes cddl bs Cborseq ctrl =
629629
case deserialiseFromBytes decodeTerm (BSL.fromStrict (BS.snoc (BS.cons 0x9f bs) 0xff)) of
630630
Right (BSL.null -> True, TListI terms) -> isValid $ validateTerm cddl (TList terms) (Array [Occur ctrl OIZeroOrMore])

test/Test/Codec/CBOR/Cuddle/CDDL/Examples/Huddle.hs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle (
66
huddleArray,
77
huddleMap,
88
huddleRangeMap,
9-
simpleRule,
10-
simpleTermExample,
9+
customGenRule,
10+
customGenExample,
1111
refTermExample,
1212
bytesExample,
1313
opCertExample,
@@ -17,6 +17,7 @@ module Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle (
1717
rangeMapExample,
1818
optionalMapExample,
1919
choicesExample,
20+
cborControlExample,
2021
) where
2122

2223
import Codec.CBOR.Cuddle.CDDL (Name)
@@ -95,18 +96,21 @@ huddleRangeMap =
9596
]
9697

9798
simpleRule :: Name -> Rule
98-
simpleRule n = withGenerator (\_ -> S . C.TInt <$> choose (4, 6)) $ n =:= arr [1, 2, 3]
99+
simpleRule n = n =:= arr [1, 2, 3]
99100

100-
simpleTermExample :: Huddle
101-
simpleTermExample =
101+
customGenRule :: Name -> Rule
102+
customGenRule = withGenerator (\_ -> S . C.TInt <$> choose (4, 6)) . simpleRule
103+
104+
customGenExample :: Huddle
105+
customGenExample =
102106
collectFrom
103-
[ HIRule $ simpleRule "root"
107+
[ HIRule $ customGenRule "root"
104108
]
105109

106110
refTermExample :: Huddle
107111
refTermExample =
108112
collectFrom
109-
[ HIRule $ "root" =:= arr [0, a $ simpleRule "bar"]
113+
[ HIRule $ "root" =:= arr [0, a $ customGenRule "bar"]
110114
]
111115

112116
bytesExample :: Huddle
@@ -177,3 +181,12 @@ choicesExample =
177181
H./ arr [1, a VBool, 6]
178182
H./ arr [1, a VText]
179183
]
184+
185+
cborControlExample :: Huddle
186+
cborControlExample =
187+
collectFrom
188+
[ HIRule $
189+
"root"
190+
=:= VBytes
191+
`H.cbor` simpleRule "simpleRule"
192+
]

test/Test/Codec/CBOR/Cuddle/CDDL/GeneratorSpec.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import Data.Text.Lazy qualified as TL
2020
import Test.AntiGen (runAntiGen, zapAntiGen)
2121
import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle (
2222
bytesExample,
23+
customGenExample,
2324
opCertExample,
2425
optionalMapExample,
2526
rangeListExample,
2627
rangeMapExample,
2728
refTermExample,
28-
simpleTermExample,
2929
sizeBytesExample,
3030
sizeTextExample,
3131
)
@@ -66,7 +66,7 @@ spec :: Spec
6666
spec = do
6767
describe "Negative generator" $ do
6868
describe "Zapped value fails to validate" $ do
69-
zapInvalidatesHuddle "simpleTerm" simpleTermExample
69+
zapInvalidatesHuddle "customGen" customGenExample
7070
zapInvalidatesHuddle "refTerm" refTermExample
7171
zapInvalidatesHuddle "opCert" opCertExample
7272
zapInvalidatesHuddle "sizeText" sizeTextExample
@@ -77,7 +77,7 @@ spec = do
7777

7878
describe "Custom generators" $ do
7979
describe "Huddle" $ do
80-
simpleTermExampleCddl <- tryResolveHuddle simpleTermExample
80+
simpleTermExampleCddl <- tryResolveHuddle customGenExample
8181
prop "If a term has a custom generator then it is used" $ do
8282
res <- generateCDDL $ mapIndex simpleTermExampleCddl
8383
pure $

test/Test/Codec/CBOR/Cuddle/CDDL/Validator/Golden.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Codec.CBOR.Cuddle.CDDL.Resolve (fullResolveCDDL)
1414
import Codec.CBOR.Cuddle.Huddle (Huddle, toCDDL)
1515
import Codec.CBOR.Cuddle.IndexMappable (mapCDDLDropExt, mapIndex)
1616
import Codec.CBOR.Term (Term (..), encodeTerm)
17+
import Codec.CBOR.Write (toStrictByteString)
1718
import Codec.CBOR.Write qualified as CBOR
1819
import Control.Monad ((<=<))
1920
import Data.Either (fromRight)
@@ -24,6 +25,7 @@ import Prettyprinter (defaultLayoutOptions, layoutPretty)
2425
import Prettyprinter.Render.Terminal qualified as Ansi
2526
import System.FilePath ((</>))
2627
import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle (
28+
cborControlExample,
2729
choicesExample,
2830
huddleRangeArray,
2931
refTermExample,
@@ -60,6 +62,9 @@ choiceAlmostSecond =
6062
, TInt 1
6163
]
6264

65+
cborControlBad :: Term
66+
cborControlBad = TBytes . toStrictByteString $ encodeTerm (TList [TInt 1, TInt 2, TInt 4])
67+
6368
validatorPrettyGolden :: String -> Huddle -> Name -> Term -> Spec
6469
validatorPrettyGolden testName huddle n term =
6570
it testName $
@@ -102,3 +107,8 @@ spec = describe "golden" $ do
102107
choicesExample
103108
"root"
104109
choiceAlmostSecond
110+
validatorPrettyGolden
111+
"cborControlBad"
112+
cborControlExample
113+
"root"
114+
cborControlBad

0 commit comments

Comments
 (0)