git.delta.rocks / unique-network / refs/commits / f8304b7ec440

difftreelog

test(xcm) make barrier unit tests cleaner

Daniel Shiposha2022-09-05parent: #5a619a9.patch.diff
in: master

15 files changed

modifiedruntime/common/mod.rsdiffbeforeafterboth
--- a/runtime/common/mod.rs
+++ b/runtime/common/mod.rs
@@ -25,7 +25,7 @@
 pub mod weights;
 
 #[cfg(test)]
-mod tests;
+pub mod tests;
 
 use sp_core::H160;
 use frame_support::traits::{Currency, OnUnbalanced, Imbalance};
modifiedruntime/common/tests/mod.rsdiffbeforeafterboth
--- a/runtime/common/tests/mod.rs
+++ b/runtime/common/tests/mod.rs
@@ -14,4 +14,35 @@
 // You should have received a copy of the GNU General Public License
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
-mod xcm;
+use sp_runtime::BuildStorage;
+use sp_core::{Public, Pair};
+use sp_std::vec;
+use up_common::types::AuraId;
+use crate::{GenesisConfig, ParachainInfoConfig, AuraConfig};
+
+pub mod xcm;
+
+fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
+	TPublic::Pair::from_string(&format!("//{}", seed), None)
+		.expect("static values are valid; qed")
+		.public()
+}
+
+fn new_test_ext(para_id: u32) -> sp_io::TestExternalities {
+    let cfg = GenesisConfig {
+        aura: AuraConfig {
+            authorities: vec![
+                get_from_seed::<AuraId>("Alice"),
+                get_from_seed::<AuraId>("Bob"),
+            ],
+        },
+        parachain_info: ParachainInfoConfig {
+            parachain_id: para_id.into(),
+        },
+        ..GenesisConfig::default()
+    };
+
+	cfg.build_storage()
+        .unwrap()
+		.into()
+}
modifiedruntime/common/tests/xcm.rsdiffbeforeafterboth
before · runtime/common/tests/xcm.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use xcm_executor::traits::ShouldExecute;18use xcm::latest::prelude::*;19use logtest::Logger;20use crate::{21    Call,22    xcm_config::Barrier,23};2425fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) {26    for record in logger {27        if record.target() == "xcm::barrier"28        && record.args() == expected_msg {29            return;30        }31    }3233    panic!("the expected XCM barrier log `{}` is not found", expected_msg);34}3536#[test]37fn xcm_barrier_denies_transact() {38    // We have a `AllowTopLevelPaidExecutionFrom` barrier,39    // so an XCM program should start from one of the following commands: 40    // * `WithdrawAsset`41    // * `ReceiveTeleportedAsset`42    // * `ReserveAssetDeposited`43    // * `ClaimAsset` 44    // We use the `WithdrawAsset` in this test4546    let location = MultiLocation {47        parents: 0,48        interior: Junctions::Here,49    };5051    // let id = AssetId::Concrete(location.clone());52    // let fun = Fungibility::Fungible(42);53    // let multiasset = MultiAsset {54    //     id,55    //     fun,56    // };57    // let withdraw_inst = WithdrawAsset(multiasset.into());5859    // We will never decode this "call",60    // so it is irrelevant what we are passing to the `transact` cmd.61    let fake_encoded_call = vec![0u8];6263    let transact_inst = Transact {64        origin_type: OriginKind::Superuser,65        require_weight_at_most: 0,66        call: fake_encoded_call.into(),67    };6869    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);7071    let mut logger = Logger::start();7273    let max_weight = 100_000;74    let mut weight_credit = 0;7576    let result = Barrier::should_execute(77        &location,78        &mut xcm_program,79        max_weight,80        &mut weight_credit81    );8283    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");8485    catch_xcm_barrier_log(&mut logger, "transact XCM rejected");86}
after · runtime/common/tests/xcm.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use xcm_executor::traits::ShouldExecute;18use xcm::latest::prelude::*;19use logtest::Logger;20use crate::Call;21use super::new_test_ext;2223fn catch_xcm_barrier_log(24    logger: &mut Logger,25    expected_msg: &str26) -> Result<(), String> {27    for record in logger {28        if record.target() == "xcm::barrier"29        && record.args() == expected_msg {30            return Ok(());31        }32    }3334    Err(format!("the expected XCM barrier log `{}` is not found", expected_msg))35}3637/// WARNING: Uses log capturing38/// See https://docs.rs/logtest/latest/logtest/index.html#constraints39pub fn barrier_denies_transact<B: ShouldExecute>(logger: &mut Logger) {40    let location = MultiLocation {41        parents: 0,42        interior: Junctions::Here,43    };4445    // We will never decode this "call",46    // so it is irrelevant what we are passing to the `transact` cmd.47    let fake_encoded_call = vec![0u8];4849    let transact_inst = Transact {50        origin_type: OriginKind::Superuser,51        require_weight_at_most: 0,52        call: fake_encoded_call.into(),53    };5455    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);5657    let max_weight = 100_000;58    let mut weight_credit = 100_000_000;5960    let result = B::should_execute(61        &location,62        &mut xcm_program,63        max_weight,64        &mut weight_credit65    );6667    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");6869    catch_xcm_barrier_log(logger, "transact XCM rejected").unwrap();70}7172fn xcm_execute<B: ShouldExecute>(73    self_para_id: u32,74    location: &MultiLocation,75    xcm: &mut Xcm<Call>76) -> Result<(), ()> {77    new_test_ext(self_para_id).execute_with(|| {78        let max_weight = 100_000;79        let mut weight_credit = 100_000_000;8081        B::should_execute(82            &location,83            xcm,84            max_weight,85            &mut weight_credit86        )87    })88}8990fn make_multiassets(location: &MultiLocation) -> MultiAssets {91    let id = AssetId::Concrete(location.clone());92    let fun = Fungibility::Fungible(42);93    let multiasset = MultiAsset {94        id,95        fun,96    };9798    multiasset.into()99}100101fn make_transfer_reserve_asset(location: &MultiLocation) -> Xcm<Call> {102    let assets = make_multiassets(location);103    let inst = TransferReserveAsset {104        assets,105        dest: location.clone(),106        xcm: Xcm(vec![]),107    };108109    Xcm::<Call>(vec![inst])110}111112fn make_deposit_reserve_asset(location: &MultiLocation) -> Xcm<Call> {113    let assets = make_multiassets(location);114    let inst = DepositReserveAsset {115        assets: assets.into(),116        max_assets: 42,117        dest: location.clone(),118        xcm: Xcm(vec![]),119    };120121    Xcm::<Call>(vec![inst])122}123124fn expect_transfer_location_denied<B: ShouldExecute>(125    logger: &mut Logger,126    self_para_id: u32,127    location: &MultiLocation,128    xcm: &mut Xcm<Call>129) -> Result<(), String> {130    let result = xcm_execute::<B>(self_para_id, location, xcm);131132    if result.is_ok() {133        return Err("the barrier should deny the unknown location".into());134    }135136    catch_xcm_barrier_log(logger, "Unexpected deposit or transfer location")137}138139/// WARNING: Uses log capturing140/// See https://docs.rs/logtest/latest/logtest/index.html#constraints141pub fn barrier_denies_transfer_from_unknown_location<B>(142    logger: &mut Logger,143    self_para_id: u32,144) -> Result<(), String>145where146    B: ShouldExecute147{148    const UNKNOWN_PARACHAIN_ID: u32 = 4057;149150    let unknown_location = MultiLocation {151        parents: 1,152        interior: X1(Parachain(UNKNOWN_PARACHAIN_ID)),153    };154155    let mut transfer_reserve_asset = make_transfer_reserve_asset(&unknown_location);156    let mut deposit_reserve_asset = make_deposit_reserve_asset(&unknown_location);157158    expect_transfer_location_denied::<B>(159        logger,160        self_para_id,161        &unknown_location,162        &mut transfer_reserve_asset163    )?;164165    expect_transfer_location_denied::<B>(166        logger,167        self_para_id,168        &unknown_location,169        &mut deposit_reserve_asset170    )?;171172    Ok(())173}
modifiedruntime/opal/src/lib.rsdiffbeforeafterboth
--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -37,6 +37,9 @@
 
 pub mod xcm_config;
 
+#[cfg(test)]
+mod tests;
+
 pub use runtime_common::*;
 
 pub const RUNTIME_NAME: &str = "opal";
addedruntime/opal/src/tests/logcapture.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/opal/src/tests/logcapture.rs
@@ -0,0 +1,25 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use super::xcm::opal_xcm_tests;
+
+#[test]
+fn opal_log_capture_tests() {
+    let mut logger = Logger::start();
+    
+    opal_xcm_tests(&mut logger);
+}
addedruntime/opal/src/tests/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/opal/src/tests/mod.rs
@@ -0,0 +1,18 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+mod xcm;
+mod logcapture;
addedruntime/opal/src/tests/xcm.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/opal/src/tests/xcm.rs
@@ -0,0 +1,32 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use crate::{
+    runtime_common::tests::xcm::*,
+    xcm_config::Barrier,
+};
+
+const OPAL_PARA_ID: u32 = 2095; // Same as Quartz
+
+pub fn opal_xcm_tests(logger: &mut Logger) {
+    barrier_denies_transact::<Barrier>(logger);
+
+    barrier_denies_transfer_from_unknown_location::<Barrier>(
+        logger,
+        OPAL_PARA_ID,
+    ).expect_err("opal runtime allows any location");
+}
modifiedruntime/quartz/src/lib.rsdiffbeforeafterboth
--- a/runtime/quartz/src/lib.rs
+++ b/runtime/quartz/src/lib.rs
@@ -37,6 +37,9 @@
 
 pub mod xcm_config;
 
+#[cfg(test)]
+mod tests;
+
 pub use runtime_common::*;
 
 pub const RUNTIME_NAME: &str = "quartz";
addedruntime/quartz/src/tests/logcapture.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/quartz/src/tests/logcapture.rs
@@ -0,0 +1,25 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use super::xcm::quartz_xcm_tests;
+
+#[test]
+fn quartz_log_capture_tests() {
+    let mut logger = Logger::start();
+    
+    quartz_xcm_tests(&mut logger);
+}
addedruntime/quartz/src/tests/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/quartz/src/tests/mod.rs
@@ -0,0 +1,18 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+mod xcm;
+mod logcapture;
addedruntime/quartz/src/tests/xcm.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/quartz/src/tests/xcm.rs
@@ -0,0 +1,32 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use crate::{
+    runtime_common::tests::xcm::*,
+    xcm_config::Barrier,
+};
+
+const QUARTZ_PARA_ID: u32 = 2095;
+
+pub fn quartz_xcm_tests(logger: &mut Logger) {
+    barrier_denies_transact::<Barrier>(logger);
+
+    barrier_denies_transfer_from_unknown_location::<Barrier>(
+        logger,
+        QUARTZ_PARA_ID,
+    ).expect("quartz runtime denies an unknown location");
+}
modifiedruntime/unique/src/lib.rsdiffbeforeafterboth
--- a/runtime/unique/src/lib.rs
+++ b/runtime/unique/src/lib.rs
@@ -37,6 +37,9 @@
 
 pub mod xcm_config;
 
+#[cfg(test)]
+mod tests;
+
 pub use runtime_common::*;
 
 pub const RUNTIME_NAME: &str = "unique";
addedruntime/unique/src/tests/logcapture.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/unique/src/tests/logcapture.rs
@@ -0,0 +1,25 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use super::xcm::unique_xcm_tests;
+
+#[test]
+fn unique_log_capture_tests() {
+    let mut logger = Logger::start();
+    
+    unique_xcm_tests(&mut logger);
+}
addedruntime/unique/src/tests/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/unique/src/tests/mod.rs
@@ -0,0 +1,18 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+mod xcm;
+mod logcapture;
addedruntime/unique/src/tests/xcm.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/unique/src/tests/xcm.rs
@@ -0,0 +1,32 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use logtest::Logger;
+use crate::{
+    runtime_common::tests::xcm::*,
+    xcm_config::Barrier,
+};
+
+const UNIQUE_PARA_ID: u32 = 2037;
+
+pub fn unique_xcm_tests(logger: &mut Logger) {
+    barrier_denies_transact::<Barrier>(logger);
+
+    barrier_denies_transfer_from_unknown_location::<Barrier>(
+        logger,
+        UNIQUE_PARA_ID,
+    ).expect("unique runtime denies an unknown location");
+}