Skip to content

Commit de0b85b

Browse files
committed
add remote pact test for send error messages
Change-Id: I2046ab2a28a2951c71ef4909e580e51256673716
1 parent d80eaf4 commit de0b85b

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

test/unit/Chainweb/Test/Pact/RemotePactTest.hs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import Pact.Types.Continuation
7575
import Pact.Types.Exp
7676
import Pact.Types.Gas
7777
import Pact.Types.Hash (Hash(..))
78+
import Pact.Types.Hash qualified as Pact
7879
import Pact.Types.Info (noInfo)
7980
import qualified Pact.Types.PactError as Pact
8081
import Pact.Types.PactValue
@@ -184,6 +185,7 @@ tests rdb = testGroup "Chainweb.Test.Pact.RemotePactTest"
184185
join $ webAuthnSignatureTest <$> iot <*> cenv
185186
]
186187
, testCase "txlogsCompactionTest" $ txlogsCompactionTest rdb
188+
, testCase "invalid command test" $ invalidCommandTest rdb
187189
]
188190

189191
responseGolden :: ClientEnv -> RequestKeys -> IO LBS.ByteString
@@ -193,6 +195,116 @@ responseGolden cenv rks = do
193195
(NEL.toList $ _rkRequestKeys rks)
194196
return $ foldMap J.encode values
195197

198+
invalidCommandTest :: RocksDb -> IO ()
199+
invalidCommandTest rdb = runResourceT $ do
200+
nodeDbDirs <- withNodeDbDirs rdb nNodes
201+
net <- withNodesAtLatestBehavior v id nodeDbDirs
202+
let cenv = _getServiceClientEnv net
203+
204+
let sendExpect :: [Command Text] -> (Text -> Bool) -> ResourceT IO ()
205+
sendExpect txs p = do
206+
e <- liftIO $ flip runClientM cenv $
207+
pactSendApiClient v cid $ SubmitBatch $ NEL.fromList txs
208+
case e of
209+
Right _ -> do
210+
liftIO $ assertFailure "Expected an error message from /send, but didn't get it"
211+
Left clientErr -> do
212+
case clientErr of
213+
FailureResponse _request resp -> do
214+
let respBody = T.decodeUtf8 (LBS.toStrict (responseBody resp))
215+
if p respBody
216+
then pure ()
217+
else liftIO $ assertFailure $ "Predicate failed, responseBody was: " ++ T.unpack respBody
218+
_ -> do
219+
liftIO $ assertFailure "Expected 'FailureResponse', got a different ClientError"
220+
221+
iot <- liftIO $ toTxCreationTime @Integer <$> getCurrentTimeIntegral
222+
223+
cmdParseFailure <- liftIO $ buildTextCmd "bare-command" v
224+
$ set cbSigners [mkEd25519Signer' sender00 []]
225+
$ set cbTTL defaultMaxTTL
226+
$ set cbCreationTime iot
227+
$ set cbChainId cid
228+
$ set cbRPC (mkExec "(+ 1" (mkKeySetData "sender00" [sender00]))
229+
$ defaultCmd
230+
-- Why does pact just return 'mzero' here...
231+
sendExpect [cmdParseFailure] (== "Validation of command at position 0 failed: Pact parsing error: Failed reading: mzero")
232+
233+
cmdInvalidPayloadHash <- liftIO $ do
234+
bareCmd <- buildTextCmd "bare-command" v
235+
$ set cbSigners [mkEd25519Signer' sender00 []]
236+
$ set cbTTL defaultMaxTTL
237+
$ set cbCreationTime iot
238+
$ set cbChainId cid
239+
$ set cbRPC (mkExec "(+ 1 2)" (mkKeySetData "sender00" [sender00]))
240+
$ defaultCmd
241+
pure $ bareCmd
242+
{ _cmdHash = Pact.hash "fakehash"
243+
}
244+
sendExpect [cmdInvalidPayloadHash] (== "Validation of command at position 0 failed: Command failed validation: The hash of the payload was invalid.")
245+
246+
cmdSignersSigsLengthMismatch1 <- liftIO $ do
247+
bareCmd <- buildTextCmd "bare-command" v
248+
$ set cbSigners [mkEd25519Signer' sender00 []]
249+
$ set cbTTL defaultMaxTTL
250+
$ set cbCreationTime iot
251+
$ set cbChainId cid
252+
$ set cbRPC (mkExec "(+ 1 2)" (mkKeySetData "sender00" [sender00]))
253+
$ defaultCmd
254+
pure $ bareCmd
255+
{ _cmdSigs = []
256+
}
257+
sendExpect [cmdSignersSigsLengthMismatch1] (== "Validation of command at position 0 failed: Command failed validation: The number of signers and signatures do not match. Number of signers: 1. Number of signatures: 0.")
258+
259+
cmdSignersSigsLengthMismatch2 <- liftIO $ do
260+
bareCmd <- buildTextCmd "bare-command" v
261+
$ set cbSigners []
262+
$ set cbTTL defaultMaxTTL
263+
$ set cbCreationTime iot
264+
$ set cbChainId cid
265+
$ set cbRPC (mkExec "(+ 1 2)" (mkKeySetData "sender00" [sender00]))
266+
$ defaultCmd
267+
pure $ bareCmd
268+
{ -- This is an invalid ED25519 signature, but length signers == length signatures is checked first
269+
_cmdSigs = [ED25519Sig "fakeSig"]
270+
}
271+
sendExpect [cmdSignersSigsLengthMismatch2] (== "Validation of command at position 0 failed: Command failed validation: The number of signers and signatures do not match. Number of signers: 0. Number of signatures: 1.")
272+
273+
-- TODO: It's hard to test for invalid schemes, because it's baked into
274+
-- chainwebversion.
275+
276+
-- TODO: It's hard to test for an invalid WebAuthn signer prefix, because it's
277+
-- baked into chainweb version.
278+
279+
cmdInvalidUserSig <- liftIO $ do
280+
bareCmd <- buildTextCmd "bare-command" v
281+
$ set cbSigners [mkEd25519Signer' sender00 []]
282+
$ set cbTTL defaultMaxTTL
283+
$ set cbCreationTime iot
284+
$ set cbChainId cid
285+
$ set cbRPC (mkExec "(+ 1 2)" (mkKeySetData "sender00" [sender00]))
286+
$ defaultCmd
287+
pure $ bareCmd
288+
{ _cmdSigs = [ED25519Sig "fakeSig"]
289+
}
290+
sendExpect [cmdInvalidUserSig] (== "Validation of command at position 0 failed: Command failed validation: The signature at position 0 is invalid: failed to parse ed25519 signature: invalid bytestring size.")
291+
292+
cmdGood <- liftIO $ buildTextCmd "good-command" v
293+
$ set cbSigners [mkEd25519Signer' sender00 []]
294+
$ set cbTTL defaultMaxTTL
295+
$ set cbCreationTime iot
296+
$ set cbChainId cid
297+
$ set cbRPC (mkExec "(+ 1 2)" (mkKeySetData "sender00" [sender00]))
298+
$ defaultCmd
299+
-- Test that [badCmd, goodCmd] fails on badCmd, and the batch is rejected.
300+
-- We just re-use a previously built bad cmd.
301+
sendExpect [cmdInvalidUserSig, cmdGood] (== "Validation of command at position 0 failed: Command failed validation: The signature at position 0 is invalid: failed to parse ed25519 signature: invalid bytestring size.")
302+
-- Test that [goodCmd, badCmd] fails on badCmd, and the batch is rejected.
303+
-- Order matters, and the error message also indicates the position of the
304+
-- failing tx.
305+
-- We just re-use a previously built bad cmd.
306+
sendExpect [cmdGood, cmdInvalidUserSig] (== "Validation of command at position 1 failed: Command failed validation: The signature at position 0 is invalid: failed to parse ed25519 signature: invalid bytestring size.")
307+
196308
-- | Check that txlogs don't problematically access history
197309
-- post-compaction.
198310
--

0 commit comments

Comments
 (0)