From edb821dcd896055beacc22a883fa771e23fbda24 Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Wed, 18 Oct 2023 09:59:19 +0200 Subject: [PATCH] Add wrong method tests --- test/python/apps/exchange_test_runner.py | 43 ++++++++++++++++-- .../test_polkadot_fund_wrong_method/00000.png | Bin 0 -> 374 bytes .../test_polkadot_fund_wrong_method/00001.png | Bin 0 -> 314 bytes .../test_polkadot_fund_wrong_method/00002.png | Bin 0 -> 359 bytes .../test_polkadot_fund_wrong_method/00003.png | Bin 0 -> 398 bytes .../test_polkadot_fund_wrong_method/00004.png | Bin 0 -> 287 bytes .../test_polkadot_fund_wrong_method/00005.png | Bin 0 -> 414 bytes .../test_polkadot_fund_wrong_method/00006.png | Bin 0 -> 367 bytes test/python/test_polkadot.py | 14 ++++++ 9 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00000.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00001.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00002.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00003.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00004.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00005.png create mode 100644 test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00006.png diff --git a/test/python/apps/exchange_test_runner.py b/test/python/apps/exchange_test_runner.py index e515cbb0..88355696 100644 --- a/test/python/apps/exchange_test_runner.py +++ b/test/python/apps/exchange_test_runner.py @@ -203,16 +203,29 @@ def perform_valid_sell_from_custom(self, destination, send_amount, fees, legacy= def perform_final_tx(self, destination, send_amount, fees, memo): raise NotImplementedError - # Wrapper of the function above to handle the USB reset in the parent class instead of the currency class - def perform_coin_specific_final_tx(self, destination, send_amount, fees, memo): + # Implement this function for each tested coin + def perform_final_tx_wrong_method(self, destination, send_amount, fees, memo): + raise NotImplementedError + + # Wrapper of the functions above to handle the USB reset in the parent class instead of the currency class + def _safe_wrapper(self, wrong_method: bool, destination: str, send_amount: int, fees: int, memo: str): try: - self.perform_final_tx(destination, send_amount, fees, memo) + if wrong_method: + self.perform_final_tx_wrong_method(destination, send_amount, fees, memo) + else: + self.perform_final_tx(destination, send_amount, fees, memo) except Exception as e: raise e finally: self.exchange_navigation_helper.check_post_sign_display() handle_lib_call_start_or_stop(self.backend) + def perform_coin_specific_final_tx(self, destination, send_amount, fees, memo): + self._safe_wrapper(False, destination, send_amount, fees, memo) + + def perform_coin_specific_final_tx_wrong_method(self, destination, send_amount, fees, memo): + self._safe_wrapper(True, destination, send_amount, fees, memo) + def assert_exchange_is_started(self): # We don't care at all for the subcommand / rate version = ExchangeClient(self.backend, Rate.FIXED, SubCommand.SWAP_NG).get_version().data @@ -292,6 +305,14 @@ def perform_test_swap_wrong_amount(self, legacy): assert e.value.status == self.wrong_amount_error_code self.assert_exchange_is_started() + # Test swap with a malicious TX trying a wrong method + def perform_test_swap_wrong_method(self, legacy): + self.perform_valid_swap_from_custom(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, self.valid_destination_memo_1, legacy=legacy) + with pytest.raises(ExceptionRAPDU) as e: + self.perform_coin_specific_final_tx_wrong_method(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, self.valid_destination_memo_1) + assert e.value.status == self.wrong_method_error_code + self.assert_exchange_is_started() + ######################################################### # Generic FUND tests functions, call them in your tests # ######################################################### @@ -343,6 +364,14 @@ def perform_test_fund_wrong_amount(self, legacy): assert e.value.status == self.wrong_amount_error_code self.assert_exchange_is_started() + # Test fund with a malicious TX trying a wrong method + def perform_test_fund_wrong_method(self, legacy): + self.perform_valid_fund_from_custom(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, legacy=legacy) + with pytest.raises(ExceptionRAPDU) as e: + self.perform_coin_specific_final_tx_wrong_method(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, "") + assert e.value.status == self.wrong_method_error_code + self.assert_exchange_is_started() + ######################################################### # Generic SELL tests functions, call them in your tests # ######################################################### @@ -394,6 +423,14 @@ def perform_test_sell_wrong_amount(self, legacy): assert e.value.status == self.wrong_amount_error_code self.assert_exchange_is_started() + # Test sell with a malicious TX trying a wrong method + def perform_test_sell_wrong_method(self, legacy): + self.perform_valid_sell_from_custom(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, legacy=legacy) + with pytest.raises(ExceptionRAPDU) as e: + self.perform_coin_specific_final_tx_wrong_method(self.valid_destination_1, self.valid_send_amount_1, self.valid_fees_1, "") + assert e.value.status == self.wrong_method_error_code + self.assert_exchange_is_started() + # Automatically collect all tests functions and export their name in ready-to-be-parametrized lists _all_test_methods_prefixed = [method for method in dir(ExchangeTestRunner) if method.startswith(TEST_METHOD_PREFIX)] # Remove prefix to have nice snapshots directories diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00000.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00000.png new file mode 100644 index 0000000000000000000000000000000000000000..8d84cc70fea8013b7e8b25c0982ce142fa103d5c GIT binary patch literal 374 zcmV-+0g3*JP)K?4J2QqX3 zXn7ePqhBDwvA-|J28LIrjF%tX0z{3v6!_gF&(SYH)S*7TN;1RxW-%}TLH9_+sttDO z-T32w&rL(!1@Vr`jm5G(8dd=Ruu#)JAN?l#rFLU`oQ(r?$33AC7s0}pw!fi~(9k^# zU9aci?pXN=I7A<-f!Mw2untnw9MivqZYw}c5&)gweYhbTnv#2#t$0X2wST7_1F;LR zw?>Da2SkSfG>qWq1mej-6nW@2l+Z=0om5jdB%3o3Qx19_LNUlTqF4R_1Iaj%@^2O= z@;n%h1N>H$2;%-~Dvx1!0LtkT-;cq73rgisOElFtPbcL{d3TfNQvm<~003s>1*i4o Uje8$F8UO$Q07*qoM6N<$g5rpv%m4rY literal 0 HcmV?d00001 diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00001.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00001.png new file mode 100644 index 0000000000000000000000000000000000000000..f68ce8a93bb7604f7ba1e4cad1f4d1fadd05bb90 GIT binary patch literal 314 zcmV-A0mc4_P)_CFd2rfv5A1-oOcR zDhy+Lq<#Po<^b5Ivh!5xZqU*v$d919Jg`*=<_ySw&o+nmdkuL8nAtGWx!yBGZ(twI zV|`B%=F16YHxaNQuhhSRRgtCsZ8W5o`ZuE(M50qD62v-u_@#bgaScwt*H(_Q8mPt3 zY%9<80oYs+MhLuCMCW>C-!~ilU98z0rM#?NPSd&l!D6I%xSremssoS*{Jo&VV0iz* z0l?_u@dqI3=sVK;tql+Zup;agx%Pbc-OB$!v<)8&;YUOvgb?By7QTm#ELHe^NdN!< M07*qoM6N<$f;uCNQ~&?~ literal 0 HcmV?d00001 diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00002.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00002.png new file mode 100644 index 0000000000000000000000000000000000000000..c693eec013725376823748e38049c0a07c57dcd5 GIT binary patch literal 359 zcmV-t0hs=YP)SoQJ7FYAX98@e5|pca^;+9fMl4VgpAUnH|O_sA$}g=2FcgHGf@Tk&|$f zS!43UfbEm88d|SP>Q`2as4(*{ChZRI5F@N|@WrB);MIktj<2bwWx~t$k{`Gwui(H*0YN1F~ z{}o7VTGg46tx}7nbmD#aSSgN+uXWxibzQ>w^Xz8z#{5uk%#k6lVpXl^u_!8V{6&Nx4`9a^rrQRaC!(c-AF zi2(A{f{-U+Q7Q;d6!NL!<(Nk@RV6dLx=l>Myl(vtWR2|B?*?INw|-}Q51z!c(3A8y zhtGqkH%zp{D;hLjQK_8IF7R=Ka>3qO-tJe_y7O^07*qoM6N<$f+g>;V*mgE literal 0 HcmV?d00001 diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00004.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00004.png new file mode 100644 index 0000000000000000000000000000000000000000..cd5a1cb404aebe8d7fde6a0be264355b0013c694 GIT binary patch literal 287 zcmV+)0pR|LP)zdL63vBggUoUz_tRnvG+eTFY89{qd3Yq@R5!+~8v*E}Q@#NPsaCV2lJ9v;H6>zqEwX8M5+) lnQlBTTlZ^O1poj5J3kX)cU*ib&Kv*$002ovPDHLkV1gANc$NSF literal 0 HcmV?d00001 diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00005.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00005.png new file mode 100644 index 0000000000000000000000000000000000000000..1c9156c31edf73cb072eaebf2db3fbe1ba5c612b GIT binary patch literal 414 zcmV;P0b%}$P)j~f|{k#}B z4)Di%0(GQh%XXzDG$sKK-Q2JkKR-`QsiPFD-4d_!i{fwH;J|_aEL%ZYNu#B0FJ7G| z;=_VQWOA9;dQ4Y9)LpKCV=ri`X({49JagUwO3&5UdSK)wEJxPN%9wG}45n;lzK(aC zKtbL$y*-#9uSql`7 zENOEh^0jsFWbV&<+yj5)WEX_aERLO-`${sQhD#B5ZCio#Fr4qHlr^7X|9DL69eFa= z#kf#}$Pcl(QEE=tNl8Pt$o;~Fk*8r@j8mpwSGW8d!1&11FbzPh@^gWj=oh7f%f7G)4s^6#xJL07*qo IM6N<$g02I(Gynhq literal 0 HcmV?d00001 diff --git a/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00006.png b/test/python/snapshots/nanos/test_polkadot_fund_wrong_method/00006.png new file mode 100644 index 0000000000000000000000000000000000000000..a0aef4dec0dfc252a17bd807d57c4c6fe3c5fb07 GIT binary patch literal 367 zcmV-#0g(QQP)FKR_EmkR81A^c*O;tGrCLNUFwG*=R`Xh|jbfxn=D{Uy81^r#!;7sJlQ%fbhFv z{OMwsKORvZPW3Zr1Du7+>_jyM2K<X?HP|KLs=qP~!1To{iNp_zd7I#_3*XL6nRa zdIBqyWwO)7GG%|h2=J6At5r<>$MI>=&