Skip to content

Commit

Permalink
host config tests + rename file
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 1, 2024
1 parent 9c965f2 commit 7418a64
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 9 deletions.
16 changes: 8 additions & 8 deletions host/src/host_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ pub trait HostConfigModule:

#[only_owner]
#[endpoint(registerClient)]
fn register_client(&self, client_type: &ClientType<Self::Api>, client: &ManagedAddress) {
require!(self.is_valid_client_type(client_type), "Invalid client ID");
fn register_client(&self, client_type: ClientType<Self::Api>, client: ManagedAddress) {
require!(self.is_valid_client_type(&client_type), "Invalid client ID");

let mapper = self.client_registry(client_type);
let mapper = self.client_registry(&client_type);
require!(mapper.is_empty(), "Client already exists");
self.require_valid_address(client);
self.require_valid_address(&client);

mapper.set(client);
}

#[only_owner]
#[endpoint(bindPort)]
fn bind_port(&self, port_id: &PortId<Self::Api>, module: &ManagedAddress) {
require!(self.is_valid_port_id(port_id), "Invalid Port ID");
self.require_valid_address(module);
fn bind_port(&self, port_id: PortId<Self::Api>, module: ManagedAddress) {
require!(self.is_valid_port_id(&port_id), "Invalid Port ID");
self.require_valid_address(&module);

self.claim_port_capability(port_id, module);
self.claim_port_capability(&port_id, &module);
}
}
2 changes: 1 addition & 1 deletion host/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ClientInfo<M: ManagedTypeApi> {
pub client_impl: ManagedAddress<M>,
}

#[derive(TypeAbi, TopEncode, TopDecode, Default)]
#[derive(TypeAbi, TopEncode, TopDecode, Default, PartialEq, Debug)]
pub struct HostInfo {
pub next_client_seq: Sequence,
pub next_connection_seq: Sequence,
Expand Down
280 changes: 280 additions & 0 deletions host/tests/host_config_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
use std::{cell::RefCell, rc::Rc};

use host::{
host_config::HostConfigModule,
storage::{HostInfo, StorageModule},
};
use host_setup::HostSetup;
use multiversx_sc_scenario::{
imports::BlockchainStateWrapper, managed_address, managed_buffer, rust_biguint, DebugApi,
};

pub mod host_setup;

fn get_host_setup<HostObjBuilder: 'static + Copy + Fn() -> host::ContractObj<DebugApi>>(
host_builder: HostObjBuilder,
) -> HostSetup<HostObjBuilder> {
let mut b_mock = BlockchainStateWrapper::new();
let owner = b_mock.create_user_account(&rust_biguint!(0));
HostSetup::new(Rc::new(RefCell::new(b_mock)), &owner, host_builder)
}

#[test]
fn set_expected_time_per_block_new_info_test() {
let host_setup = get_host_setup(host::contract_obj);
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.set_expected_time_per_block(6);

let host_info = sc.host_info().get();
assert_eq!(
host_info,
HostInfo {
expected_time_per_block: 6,
..Default::default()
}
);
},
)
.assert_ok();
}

#[test]
fn set_expected_time_per_block_update_info_test() {
let host_setup = get_host_setup(host::contract_obj);
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.host_info().set(HostInfo {
next_channel_seq: 1,
next_client_seq: 2,
next_connection_seq: 3,
expected_time_per_block: 42,
});
sc.set_expected_time_per_block(6);

let host_info = sc.host_info().get();
assert_eq!(
host_info,
HostInfo {
next_channel_seq: 1,
next_client_seq: 2,
next_connection_seq: 3,
expected_time_per_block: 6,
}
);
},
)
.assert_ok();
}

#[test]
fn register_new_client_ok_test() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_client = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.register_client(
managed_buffer!(b"my-cool-client"),
managed_address!(&my_cool_client),
);

assert_eq!(
sc.client_registry(&managed_buffer!(b"my-cool-client"))
.get(),
managed_address!(&my_cool_client),
);
},
)
.assert_ok();
}

#[test]
fn try_register_already_existing_client_test() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_client = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.register_client(
managed_buffer!(b"my-cool-client"),
managed_address!(&my_cool_client),
);

assert_eq!(
sc.client_registry(&managed_buffer!(b"my-cool-client"))
.get(),
managed_address!(&my_cool_client),
);
},
)
.assert_ok();

host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.register_client(
managed_buffer!(b"my-cool-client"),
managed_address!(&my_cool_client),
);
},
)
.assert_user_error("Client already exists");
}

#[test]
fn try_register_invalid_client_id_test() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_client = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.register_client(
managed_buffer!(b"EVIL-CLIENT"),
managed_address!(&my_cool_client),
);
},
)
.assert_user_error("Invalid client ID");
}

#[test]
fn bind_port_ok_test() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_module = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.bind_port(
managed_buffer!(b"my-cool-module"),
managed_address!(&my_cool_module),
);

assert_eq!(
sc.port_capabilities(&managed_buffer!(b"my-cool-module"))
.get(),
managed_address!(&my_cool_module),
);
},
)
.assert_ok();
}

#[test]
fn try_bind_same_port_twice_test() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_module = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.bind_port(
managed_buffer!(b"my-cool-module"),
managed_address!(&my_cool_module),
);

assert_eq!(
sc.port_capabilities(&managed_buffer!(b"my-cool-module"))
.get(),
managed_address!(&my_cool_module),
);
},
)
.assert_ok();

host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.bind_port(
managed_buffer!(b"my-cool-module"),
managed_address!(&my_cool_module),
);
},
)
.assert_user_error("Port already claimed");
}

#[test]
fn try_bind_invalid_port_id() {
let host_setup = get_host_setup(host::contract_obj);
let my_cool_module = host_setup
.b_mock
.borrow_mut()
.create_user_account(&rust_biguint!(0));
host_setup
.b_mock
.borrow_mut()
.execute_tx(
&host_setup.host_owner,
&host_setup.host_wrapper,
&rust_biguint!(0),
|sc| {
sc.bind_port(
managed_buffer!(b"I'm @wesome!"),
managed_address!(&my_cool_module),
);
},
)
.assert_user_error("Invalid Port ID");
}
File renamed without changes.

0 comments on commit 7418a64

Please sign in to comment.