|
| 1 | +{-# LANGUAGE FlexibleInstances #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | + |
| 5 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 6 | + |
| 7 | +module Test.DMQ.Protocol.SigSubmission where |
| 8 | + |
| 9 | +import Control.Monad.ST (runST) |
| 10 | +import Data.Bifunctor (second) |
| 11 | +import Data.List.NonEmpty qualified as NonEmpty |
| 12 | +import Data.Word (Word32) |
| 13 | + |
| 14 | +import Network.TypedProtocol.Codec hiding (prop_codec) |
| 15 | + |
| 16 | +import Ouroboros.Network.SizeInBytes |
| 17 | + |
| 18 | +import DMQ.Protocol.SigSubmission.Type |
| 19 | +import DMQ.Protocol.SigSubmission.Codec |
| 20 | + |
| 21 | +import Ouroboros.Network.Protocol.TxSubmission2.Test (labelMsg) |
| 22 | + |
| 23 | +import Test.Ouroboros.Network.Protocol.Utils (prop_codec_cborM, |
| 24 | + prop_codec_valid_cbor_encoding, splits2, splits3) |
| 25 | + |
| 26 | +import Test.Tasty |
| 27 | +import Test.Tasty.QuickCheck |
| 28 | +import Test.QuickCheck as QC |
| 29 | +import Test.QuickCheck.Instances.ByteString () |
| 30 | + |
| 31 | + |
| 32 | +tests :: TestTree |
| 33 | +tests = |
| 34 | + testGroup "DMQ.Protocol" |
| 35 | + [ testGroup "SigSubmission" |
| 36 | + [ testProperty "codec" prop_codec |
| 37 | + , testProperty "codec id" prop_codec_id |
| 38 | + , testProperty "codec 2-splits" prop_codec_splits2 |
| 39 | + , testProperty "codec 3-splits" $ withMaxSize 10 |
| 40 | + prop_codec_splits3 |
| 41 | + , testProperty "codec cbor" prop_codec_cbor |
| 42 | + , testProperty "codec valid cbor" prop_codec_valid_cbor |
| 43 | + ] |
| 44 | + ] |
| 45 | + |
| 46 | +instance Arbitrary SigHash where |
| 47 | + arbitrary = SigHash <$> arbitrary |
| 48 | + shrink = map SigHash . shrink . getSigHash |
| 49 | + |
| 50 | +instance Arbitrary SigId where |
| 51 | + arbitrary = SigId <$> arbitrary |
| 52 | + shrink = map SigId . shrink . getSigId |
| 53 | + |
| 54 | +instance Arbitrary SigBody where |
| 55 | + arbitrary = SigBody <$> arbitrary |
| 56 | + shrink = map SigBody . shrink . getSigBody |
| 57 | + |
| 58 | +instance Arbitrary SigTTL where |
| 59 | + -- generate only whole seconds (this is what we receive on the wire) |
| 60 | + arbitrary = SigTTL . realToFrac @Word32 <$> arbitrary |
| 61 | + -- shrink via Word32 (e.g. in seconds) |
| 62 | + shrink (SigTTL posix) = SigTTL . realToFrac |
| 63 | + <$> shrink (floor @_ @Word32 posix) |
| 64 | + |
| 65 | +instance Arbitrary SigKesSignature where |
| 66 | + arbitrary = SigKesSignature <$> arbitrary |
| 67 | + shrink = map SigKesSignature . shrink . getSigKesSignature |
| 68 | + |
| 69 | +instance Arbitrary SigOpCertificate where |
| 70 | + arbitrary = SigOpCertificate <$> arbitrary |
| 71 | + shrink = map SigOpCertificate . shrink . getSigOpCertificate |
| 72 | + |
| 73 | +instance Arbitrary Sig where |
| 74 | + arbitrary = Sig <$> arbitrary |
| 75 | + <*> arbitrary |
| 76 | + <*> arbitrary |
| 77 | + <*> arbitrary |
| 78 | + shrink sig@Sig { sigId, sigBody, sigTTL, sigOpCertificate, sigKesSignature } = |
| 79 | + [ sig { sigId = sigId' } |
| 80 | + | sigId' <- shrink sigId |
| 81 | + ] |
| 82 | + ++ |
| 83 | + [ sig { sigBody = sigBody' } |
| 84 | + | sigBody' <- shrink sigBody |
| 85 | + ] |
| 86 | + ++ |
| 87 | + [ sig { sigTTL = sigTTL' } |
| 88 | + | sigTTL' <- shrink sigTTL |
| 89 | + ] |
| 90 | + ++ |
| 91 | + [ sig { sigOpCertificate = sigOpCertificate' } |
| 92 | + | sigOpCertificate' <- shrink sigOpCertificate |
| 93 | + ] |
| 94 | + ++ |
| 95 | + [ sig { sigKesSignature = sigKesSignature' } |
| 96 | + | sigKesSignature' <- shrink sigKesSignature |
| 97 | + ] |
| 98 | + |
| 99 | + |
| 100 | +instance Arbitrary (AnyMessage SigSubmission) where |
| 101 | + arbitrary = oneof |
| 102 | + [ pure $ AnyMessage MsgInit |
| 103 | + , AnyMessage <$> |
| 104 | + (MsgRequestTxIds SingBlocking |
| 105 | + <$> arbitrary |
| 106 | + <*> arbitrary) |
| 107 | + |
| 108 | + , AnyMessage <$> |
| 109 | + (MsgRequestTxIds SingNonBlocking |
| 110 | + <$> arbitrary |
| 111 | + <*> arbitrary) |
| 112 | + |
| 113 | + , AnyMessage <$> |
| 114 | + MsgReplyTxIds <$> (BlockingReply . NonEmpty.fromList |
| 115 | + . map (second SizeInBytes) |
| 116 | + . QC.getNonEmpty |
| 117 | + <$> arbitrary) |
| 118 | + |
| 119 | + , AnyMessage <$> MsgReplyTxIds <$> (NonBlockingReply . map (second SizeInBytes) <$> arbitrary) |
| 120 | + |
| 121 | + , AnyMessage <$> MsgRequestTxs <$> arbitrary |
| 122 | + |
| 123 | + , AnyMessage <$> MsgReplyTxs <$> arbitrary |
| 124 | + |
| 125 | + , AnyMessage <$> pure MsgDone |
| 126 | + ] |
| 127 | + |
| 128 | +prop_codec :: AnyMessage SigSubmission -> Bool |
| 129 | +prop_codec msg = |
| 130 | + runST (prop_codecM codecSigSubmission msg) |
| 131 | + |
| 132 | +prop_codec_id :: AnyMessage SigSubmission -> Bool |
| 133 | +prop_codec_id msg = |
| 134 | + runST (prop_codecM codecSigSubmissionId msg) |
| 135 | + |
| 136 | +prop_codec_splits2 :: AnyMessage SigSubmission -> Bool |
| 137 | +prop_codec_splits2 msg = |
| 138 | + runST (prop_codec_splitsM splits2 codecSigSubmission msg) |
| 139 | + |
| 140 | +prop_codec_splits3 :: AnyMessage SigSubmission -> Property |
| 141 | +prop_codec_splits3 msg = |
| 142 | + labelMsg msg $ |
| 143 | + runST (prop_codec_splitsM splits3 codecSigSubmission msg) |
| 144 | + |
| 145 | + |
| 146 | +prop_codec_cbor |
| 147 | + :: AnyMessage SigSubmission |
| 148 | + -> Property |
| 149 | +prop_codec_cbor msg = |
| 150 | + runST (prop_codec_cborM codecSigSubmission msg) |
| 151 | + |
| 152 | +prop_codec_valid_cbor |
| 153 | + :: AnyMessage SigSubmission |
| 154 | + -> Property |
| 155 | +prop_codec_valid_cbor = prop_codec_valid_cbor_encoding codecSigSubmission |
0 commit comments