Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] #2053: Add tests to 'private_blockchain' #2332

Conversation

pesterev
Copy link
Contributor

@pesterev pesterev commented Jun 7, 2022

Signed-off-by: Vladimir Pesterev pesterev@pm.me

Description of the Change

I added tests for permissions related to account and some asset queries and also for permissions related to register. But the OnlyAccountsDomain and OnlyAccountsData permissions need to be tested on many other queries.(will be in the next PRs) The tests are very different and it is very difficult to write a macro that would generate that tests.

  • register module
  • tests permissions with all account related and some asset related queries
  • tests permissions with other queries (resolved: will be in the next PRs)
  • weird impl IsAllowed for ProhibitRegisterDomains

Issue

Partially resolves #2053

Benefits

Improved coverage

Possible Drawbacks

None

Alternate Designs [optional]

Using macros would reduce the number of tests, but the tests are too different.

Also we can just write tests only for queries with non-trivial logic.

@github-actions github-actions bot added the iroha2-dev The re-implementation of a BFT hyperledger in RUST label Jun 7, 2022
@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from 5cca2c0 to 8d8e9bc Compare June 7, 2022 18:10
@pesterev pesterev changed the title [test] #2053: Add tests to 'private_blockchain' [feature] #2053: Add tests to 'private_blockchain' Jun 7, 2022
@codecov
Copy link

codecov bot commented Jun 7, 2022

Codecov Report

Merging #2332 (07e62bd) into iroha2-dev (6973f71) will increase coverage by 1.52%.
The diff coverage is 69.35%.

@@              Coverage Diff               @@
##           iroha2-dev    #2332      +/-   ##
==============================================
+ Coverage       65.50%   67.02%   +1.52%     
==============================================
  Files             133      140       +7     
  Lines           24697    26282    +1585     
==============================================
+ Hits            16177    17616    +1439     
- Misses           8520     8666     +146     
Impacted Files Coverage Δ
cli/derive/src/lib.rs 74.72% <ø> (ø)
cli/src/torii/mod.rs 28.88% <ø> (ø)
config/src/lib.rs 15.38% <0.00%> (+1.09%) ⬆️
core/src/lib.rs 100.00% <ø> (ø)
core/src/smartcontracts/isi/account.rs 12.30% <ø> (ø)
core/src/smartcontracts/isi/block.rs 100.00% <ø> (ø)
core/src/smartcontracts/isi/domain.rs 39.82% <ø> (ø)
core/src/smartcontracts/isi/triggers.rs 0.00% <0.00%> (ø)
core/src/smartcontracts/isi/tx.rs 25.00% <ø> (ø)
core/src/smartcontracts/isi/world.rs 9.52% <0.00%> (+0.43%) ⬆️
... and 95 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67f8fc4...07e62bd. Read the comment docs.

@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from 8d8e9bc to 5d2d14a Compare June 7, 2022 20:37

use super::*;

type TestEnv = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter here, but this should be a struct. You can bind directly to names using Rust's pattern matching syntax.

@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch 3 times, most recently from f762fc5 to 636c667 Compare June 10, 2022 14:47
@pesterev pesterev marked this pull request as ready for review June 10, 2022 15:15

{
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.into();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a negative test-case (with is_ok()).

Copy link
Contributor Author

@pesterev pesterev Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't allow to find any accounts with OnlyAccountsDomain. That's why there are no is_ok() cases here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to use another query to check is_ok() variant?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, it tests queries, not validators

}

impl TestEnv {
fn new() -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With rust 1.62, we should be able to turn this into a const function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the comments above blocks (but keep the grouping). Instead I'd add a doc-comment above the function, so people could hover and see both the assets, the domains and the relationships.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With rust 1.62, we should be able to turn this into a const function.

It is not possible to turn fn new() into a const fn because the functions called inside fn new() are not const.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. const new for some types is going to be stabilised soon, so it should be possible to propagate those changes to here.


let wsv = WorldStateView::<World>::new(World::with(
[first_domain.clone(), second_domain.clone()],
vec![],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style point: prefer Vec::new() for empty immutable vectors. This conveys to the reader that what you made empty is meant to remain empty.


{
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.into();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of a good assertion block. You need to be able to demonstrate when it works and when it doesn't.

..
} = TestEnv::new();

let op1 = QueryBox::FindAssetById(FindAssetById::new(gold_asset_id));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd give these operations more descriprtive names. e.g. find_gold, find_bronze etc.

assert!(only_accounts_data.check(&alice_id, &op3, &wsv).is_err());
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Group the tests that check the queries into mod queries. It should end about here.

Also add mod revoke_and_grant_tests and include a few of the following tests.

}

#[test]
fn granted_allowed_register_domains_success() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd combine the test cases. I'd also give them better names: this one for example is closer to
add_register_domains_permission_allows_registering_domain. You're documenting an invariant, in this case that it A) isn't possible to register the domain without this permission, B) that it becomes possible after adding the permission, C) that it becomes impossible again when you revoke it.

All three should be checked in a single test.

@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from 636c667 to b63607f Compare June 10, 2022 16:22
appetrosyan
appetrosyan previously approved these changes Jun 13, 2022
}

impl TestEnv {
fn new() -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have time, please add doc comments, which explain who has which assets to this associated function, or to the struct (preferably both, preferably using #[doc(include...].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't think of anything better than describing relationships in the comments to the fields of TestEnv structure. Comments are prompted when you hover over the field. I wanted to use something like mermaid but rustdoc doesn't support it. Creating a separate markdown file is probably too much.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

Comment on lines 25 to 28
let register = if let Instruction::Register(register) = instruction {
register
} else {
return Ok(());
};
Err("Domain registration is prohibited.".to_owned())

if let Ok(RegistrableBox::Domain(_)) = register.object.evaluate(wsv, &Context::new()) {
Err("Domain registration is prohibited.".to_owned())
} else {
Ok(())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rewrite something like this:

if let Instruction::Register(register) = instruction {
    if let Ok(RegistrableBox::Domain(_)) = register.object.evaluate(wsv, &Context::new()) {
       return Err("Domain registration is prohibited.".to_owned());
    }
}
Ok(())

Easier to read for me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably with the merge of #2284 you'll have to add into() after to_owned()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment on lines +151 to +161
let TestEnv {
alice_id,
bob_id,
carol_id,
wsv,
..
} = TestEnv::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea


{
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.into();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to use another query to check is_ok() variant?


{
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.into();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, it tests queries, not validators

}
}

mod revoke_and_grant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to add more negative tests scenarios in this module tests.
I.e. add_register_domains_permission_allows_registering_domain_with_right_token() checks that Alice can register new domainsm, but doesn't check if anyone without permission token can do it too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch 3 times, most recently from 8b7fd59 to 14fafcf Compare June 16, 2022 19:43
@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from 14fafcf to 8a6f32d Compare June 16, 2022 19:50
@pesterev pesterev requested a review from Arjentix June 16, 2022 19:51
@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch 2 times, most recently from 1ab9bf8 to 57b6c0b Compare June 17, 2022 16:27
Copy link
Contributor

@appetrosyan appetrosyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

}

impl TestEnv {
fn new() -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

appetrosyan
appetrosyan previously approved these changes Jun 17, 2022
Arjentix
Arjentix previously approved these changes Jun 20, 2022
@pesterev pesterev enabled auto-merge (rebase) June 20, 2022 14:35
@pesterev pesterev disabled auto-merge June 20, 2022 14:36
@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from 57b6c0b to d9ef279 Compare June 20, 2022 14:36
Signed-off-by: Vladimir Pesterev <pesterev@pm.me>
@pesterev pesterev dismissed stale reviews from Arjentix and appetrosyan via 07e62bd June 20, 2022 14:53
@pesterev pesterev force-pushed the private_blockchain_permissions_tests branch from d9ef279 to 07e62bd Compare June 20, 2022 14:53
@appetrosyan appetrosyan merged commit 4412892 into hyperledger:iroha2-dev Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iroha2-dev The re-implementation of a BFT hyperledger in RUST
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants