Skip to content

Commit c055a28

Browse files
committed
Invalid fields testing
1 parent e9efa95 commit c055a28

File tree

4 files changed

+98
-17
lines changed
  • eras
    • alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary
    • conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary
    • dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary
  • libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Binary

4 files changed

+98
-17
lines changed

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary/Golden.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Test.Cardano.Ledger.Alonzo.Binary.Golden (
1010
spec,
1111
witsEmptyField,
1212
expectFailureOnTxWitsEmptyField,
13+
txWitsDecodingFailsOnInvalidField,
1314
module Test.Cardano.Ledger.Allegra.Binary.Golden,
1415
) where
1516

@@ -30,11 +31,11 @@ import Test.Cardano.Ledger.Alonzo.Era (AlonzoEraTest)
3031
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc (..))
3132
import Test.Cardano.Ledger.Common (
3233
Expectation,
33-
NonNegative (..),
3434
Spec,
3535
describe,
3636
it,
3737
prop,
38+
(==>),
3839
)
3940
import Test.Cardano.Ledger.Imp.Common (forEachEraVersion)
4041

@@ -65,6 +66,16 @@ expectSuccessOnEmptyFieldRaw ::
6566
expectSuccessOnEmptyFieldRaw version k =
6667
expectDecoderSuccessAnnWith eqRaw version (witsEmptyField k) (mkBasicTxWits @era)
6768

69+
txWitsDecodingFailsOnInvalidField :: forall era. AlonzoEraTest era => Version -> [Int] -> Spec
70+
txWitsDecodingFailsOnInvalidField version validFields =
71+
prop "Invalid field" $ \n ->
72+
n `notElem` validFields ==> expectFailureOnTxWitsEmptyField @era version n $
73+
DecoderErrorDeserialiseFailure
74+
(Binary.label $ Proxy @(Annotator (TxWits era)))
75+
( DeserialiseFailure 2 $
76+
"An error occurred while decoding (Int,Void) not a valid key:.\nError: " <> show n
77+
)
78+
6879
spec ::
6980
forall era.
7081
(AlonzoEraTest era, ShelleyEraTxCert era) =>
@@ -84,13 +95,5 @@ spec = do
8495
-- from there onwards
8596
it "plutusV2Script" $ expectSuccessOnEmptyFieldRaw @era version 6
8697
it "plutusV3Script" $ expectSuccessOnEmptyFieldRaw @era version 7
87-
prop "Invalid field" $ \(NonNegative n) ->
88-
let invalidTag = n + 8
89-
in expectFailureOnTxWitsEmptyField @era version invalidTag $
90-
DecoderErrorDeserialiseFailure
91-
(Binary.label $ Proxy @(Annotator (TxWits era)))
92-
( DeserialiseFailure 2 $
93-
"An error occurred while decoding (Int,Void) not a valid key:.\nError: " <> show invalidTag
94-
)
9598
describe "TxCerts" $ do
9699
forEachEraVersion @era $ allegraDecodeDuplicateDelegCertSucceeds @era

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary/Golden.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ spec = do
150150
DecoderErrorDeserialiseFailure
151151
(Binary.label $ Proxy @(Annotator (TxWits era)))
152152
(DeserialiseFailure 4 "Empty list of scripts is not allowed")
153-
it "8th field" . expectFailureOnTxWitsEmptyField @era version 8 $
154-
DecoderErrorDeserialiseFailure
155-
(Binary.label $ Proxy @(Annotator (TxWits era)))
156-
(DeserialiseFailure 2 "An error occurred while decoding (Int,Void) not a valid key:.\nError: 8")
153+
txWitsDecodingFailsOnInvalidField @era version [0 .. 7]
157154
describe "Tagged" $ do
158155
it "addrTxWits" . expectFailureOnTxWitsEmptyField @era version 0 $
159156
DecoderErrorDeserialiseFailure

eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary/Golden.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ goldenEmptyFields version =
9292
"Annotator (MemoBytes (AlonzoTxWitsRaw DijkstraEra))"
9393
(DeserialiseFailure 4 "Empty list of scripts is not allowed")
9494
-- TODO replace this with `plutusV4Script` once that is added
95-
it "8th field" . expectFailureOnTxWitsEmptyField @era version 8 $
96-
DecoderErrorDeserialiseFailure
97-
(Binary.label $ Proxy @(Annotator (TxWits era)))
98-
(DeserialiseFailure 2 "An error occurred while decoding (Int,Void) not a valid key:.\nError: 8")
95+
txWitsDecodingFailsOnInvalidField @era version [0 .. 7]
9996
describe "Tagged" $ do
10097
let
10198
it "addrTxWits" . expectFailureOnTxWitsEmptyField @era version 0 $
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE TypeApplications #-}
5+
6+
module Test.Cardano.Ledger.Core.Binary.Golden (
7+
decodeEnc,
8+
expectDecoderSuccessAnn,
9+
expectDecoderSuccessAnnWith,
10+
expectDecoderFailureAnn,
11+
expectDecoderResultOn,
12+
) where
13+
14+
import Cardano.Ledger.Binary (
15+
Annotator,
16+
DecCBOR (..),
17+
DecoderError,
18+
ToCBOR (..),
19+
Version,
20+
decodeFullAnnotator,
21+
toLazyByteString,
22+
)
23+
import qualified Cardano.Ledger.Binary as Binary
24+
import Data.Typeable (Proxy (..))
25+
import GHC.Stack (HasCallStack)
26+
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc)
27+
import Test.Cardano.Ledger.Binary.RoundTrip (embedTripAnnExpectation)
28+
import Test.Cardano.Ledger.Common (
29+
Expectation,
30+
diffExprString,
31+
expectationFailure,
32+
shouldBe,
33+
showExpr,
34+
)
35+
import Test.Cardano.Ledger.TreeDiff (ToExpr, expectExprEqualWithMessage)
36+
37+
decodeEnc :: forall a. DecCBOR (Annotator a) => Version -> Enc -> Either DecoderError a
38+
decodeEnc version enc = decodeFullAnnotator @a version (Binary.label $ Proxy @(Annotator a)) decCBOR bytes
39+
where
40+
bytes = toLazyByteString $ toCBOR enc
41+
42+
expectDecoderSuccessAnnWith ::
43+
forall a.
44+
(ToExpr a, DecCBOR (Annotator a), HasCallStack) =>
45+
(a -> a -> Bool) ->
46+
Version ->
47+
Enc ->
48+
a ->
49+
Expectation
50+
expectDecoderSuccessAnnWith equals version enc expected =
51+
case decodeEnc @a version enc of
52+
Left err -> expectationFailure $ "Unexpected decoder failure: " <> show err
53+
Right x | x `equals` expected -> pure ()
54+
Right result -> expectationFailure $ diffExprString expected result
55+
56+
expectDecoderSuccessAnn ::
57+
(ToExpr a, DecCBOR (Annotator a), Eq a, HasCallStack) => Version -> Enc -> a -> Expectation
58+
expectDecoderSuccessAnn = expectDecoderSuccessAnnWith (==)
59+
60+
expectDecoderFailureAnn ::
61+
forall a.
62+
(ToExpr a, DecCBOR (Annotator a), HasCallStack) =>
63+
Version ->
64+
Enc ->
65+
DecoderError ->
66+
Expectation
67+
expectDecoderFailureAnn version enc expectedErr =
68+
case decodeEnc @a version enc of
69+
Left err -> expectedErr `shouldBe` err
70+
Right x ->
71+
expectationFailure $
72+
"Expected a failure, but decoder succeeded:\n"
73+
<> showExpr x
74+
75+
expectDecoderResultOn ::
76+
forall a b.
77+
(ToExpr b, DecCBOR (Annotator a), Eq b, HasCallStack) =>
78+
Version -> Enc -> a -> (a -> b) -> Expectation
79+
expectDecoderResultOn version enc expected f =
80+
embedTripAnnExpectation
81+
version
82+
version
83+
(\x _ -> expectExprEqualWithMessage "" (f x) (f expected))
84+
enc

0 commit comments

Comments
 (0)