Skip to content

Commit

Permalink
ExecutorItf: validation setup
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Boldyrev <miboldyrev@gmail.com>
  • Loading branch information
MBoldyrev committed Aug 15, 2019
1 parent bb5e8b8 commit 22ce9a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
39 changes: 24 additions & 15 deletions test/framework/executor_itf/executor_itf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ CreateResult ExecutorItf::create(ExecutorItfTarget target) {
new ExecutorItf(std::move(target.command_executor),
std::move(target.query_executor),
log_manager));
return executor_itf->createAdmin().match(
[&executor_itf](const auto &) -> CreateResult {
return std::move(executor_itf);
},
[](const auto &error) -> CreateResult { return error.error.toString(); });
return executor_itf->prepareState() |
[&executor_itf] { return std::move(executor_itf); };
}

CommandResult ExecutorItf::executeCommandAsAccount(
const shared_model::interface::Command &cmd,
const std::string &account_id) const {
return cmd_executor_->execute(cmd, account_id, true);
const std::string &account_id,
bool do_validation) const {
return cmd_executor_->execute(cmd, account_id, do_validation);
}

Result<void, TxExecutionError> ExecutorItf::executeTransaction(
Expand All @@ -98,7 +96,7 @@ const std::unique_ptr<shared_model::interface::MockQueryFactory>
CommandResult ExecutorItf::createRoleWithPerms(
const std::string &role_id,
const shared_model::interface::RolePermissionSet &role_permissions) const {
return executeCommand(
return executeMaintenanceCommand(
*getMockCommandFactory()->constructCreateRole(role_id, role_permissions));
}

Expand All @@ -114,7 +112,7 @@ CommandResult ExecutorItf::createUserWithPerms(
CommandResult ExecutorItf::createDomain(const std::string &name) const {
const std::string default_role = getDefaultRole(name);
createRoleWithPerms(default_role, {});
return executeCommand(
return executeMaintenanceCommand(
*getMockCommandFactory()->constructCreateDomain(name, default_role));
}

Expand All @@ -124,7 +122,7 @@ CommandResult ExecutorItf::grantAllToAdmin(
getDefaultRole(kAdminName, kDomain);
shared_model::interface::GrantablePermissionSet all_grantable_perms;
CommandResult grant_perm_result =
executeCommand(*getMockCommandFactory()->constructAppendRole(
executeMaintenanceCommand(*getMockCommandFactory()->constructAppendRole(
account_id, admin_role_name));
all_grantable_perms.setAll();
all_grantable_perms.iterate(
Expand All @@ -133,11 +131,12 @@ CommandResult ExecutorItf::grantAllToAdmin(
return this->executeCommandAsAccount(
*this->getMockCommandFactory()->constructGrantPermission(kAdminId,
perm),
account_id);
account_id,
false);
};
});
return grant_perm_result | [&, this] {
return this->executeCommand(
return this->executeMaintenanceCommand(
*this->getMockCommandFactory()->constructDetachRole(account_id,
admin_role_name));
};
Expand All @@ -153,16 +152,26 @@ CommandResult ExecutorItf::createUserWithPermsInternal(
const std::string account_id = account_name + "@" + domain;
const std::string account_role_name = getDefaultRole(account_name, domain);

return executeCommand(*getMockCommandFactory()->constructCreateAccount(
account_name, domain, pubkey))
return executeMaintenanceCommand(
*getMockCommandFactory()->constructCreateAccount(
account_name, domain, pubkey))
| [&, this] { return createRoleWithPerms(account_role_name, role_perms); }
| [&, this] {
return this->executeCommand(
return this->executeMaintenanceCommand(
*this->getMockCommandFactory()->constructAppendRole(
account_id, account_role_name));
};
}

Result<void, std::string> ExecutorItf::prepareState() const {
auto create_admin_result = createAdmin();
if (auto e = resultToOptionalError(create_admin_result)) {
return makeError(std::string{"Could not create admin account: "}
+ e.value().toString());
}
return {};
}

CommandResult ExecutorItf::createAdmin() const {
shared_model::interface::RolePermissionSet all_role_perms;
all_role_perms.setAll();
Expand Down
24 changes: 16 additions & 8 deletions test/framework/executor_itf/executor_itf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,48 +61,53 @@ namespace iroha {
* Execute a command as account.
* @param cmd The command to execute.
* @param account_id The issuer account id.
* @param do_validation Initializes the same parameter of command
* executor.
* @return Result of command execution.
*/
iroha::ametsuchi::CommandResult executeCommandAsAccount(
const shared_model::interface::Command &cmd,
const std::string &account_id) const;
const std::string &account_id,
bool do_validation) const;

/**
* Execute a command as account.
* @tparam SpecificCommand The type of executed specific command.
* @param cmd The command to execute.
* @param account_id The issuer account id.
* @param do_validation Whether to perform permissions check.
* @return Result of command execution.
*/
template <typename SpecificCommand,
typename = std::enable_if_t<detail::isSpecificCommand<
typename SpecificCommand::ModelType>>>
iroha::ametsuchi::CommandResult executeCommandAsAccount(
const SpecificCommand &specific_cmd,
const std::string &account_id) const {
const std::string &account_id,
bool do_validation) const {
shared_model::interface::Command::CommandVariantType variant{
specific_cmd};
shared_model::interface::MockCommand cmd;
EXPECT_CALL(cmd, get()).WillRepeatedly(::testing::ReturnRef(variant));
return executeCommandAsAccount(cmd, account_id);
return executeCommandAsAccount(cmd, account_id, do_validation);
}

/**
* Execute a command as admin.
* Execute a command as admin without validation.
* @tparam SpecificCommand The type of executed specific command.
* @param cmd The command to execute.
* @return Result of command execution.
*/
template <typename T>
auto executeCommand(const T &cmd) const
-> decltype(executeCommandAsAccount(cmd, std::string{})) {
return executeCommandAsAccount(cmd, common_constants::kAdminId);
auto executeMaintenanceCommand(const T &cmd) const
-> decltype(executeCommandAsAccount(cmd, std::string{}, false)) {
return executeCommandAsAccount(cmd, common_constants::kAdminId, false);
}

/**
* Execute a transaction.
* @param cmd The transaction to execute.
* @param do_validation Whether to enable permissions validation.
* @param do_validation Whether to perform permissions check.
* @return Error in case of failure.
*/
iroha::expected::Result<void, iroha::ametsuchi::TxExecutionError>
Expand Down Expand Up @@ -268,6 +273,9 @@ namespace iroha {
query_executor,
logger::LoggerManagerTreePtr log_manager);

/// Prepare WSV (as part of initialization).
iroha::expected::Result<void, std::string> prepareState() const;

/// Create admin account with all permissions.
iroha::ametsuchi::CommandResult createAdmin() const;

Expand Down

0 comments on commit 22ce9a8

Please sign in to comment.