Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Ref #123: Add test macros, testing, and fixing
Browse files Browse the repository at this point in the history
Add macros for Link_Authority and Unlink_Authority, write some initial
tests of these operations, and fix a bug (unlinkauth didn't require any
authority)
  • Loading branch information
nathanielhourt committed Aug 8, 2017
1 parent 806508a commit 35c4740
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libraries/native_contract/eos_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ void apply_eos_linkauth(apply_context& context) {
void apply_eos_unlinkauth(apply_context& context) {
auto& db = context.mutable_db;
auto unlink = context.msg.as<types::unlinkauth>();

context.require_authorization(unlink.account);

auto linkKey = boost::make_tuple(unlink.account, unlink.code, unlink.type);
auto link = db.find<permission_link_object, by_message_type>(linkKey);
EOS_ASSERT(link != nullptr, message_precondition_exception, "Attempting to unlink authority, but no link found");
Expand Down
29 changes: 29 additions & 0 deletions tests/common/macro_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
BOOST_TEST_CHECKPOINT("Deleted " << #account << "'s " << authname << " authority."); \
}

#define LINKAUTH5(chain, account, authname, codeacct, messagetype) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"linkauth", types::linkauth{#account, #codeacct, messagetype, authname}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Link " << #codeacct << "::" << messagetype << " to " << #account \
<< "'s " << authname << " authority."); \
}
#define LINKAUTH4(chain, account, authname, codeacct) LINKAUTH5(chain, account, authname, codeacct, "")

#define UNLINKAUTH4(chain, account, codeacct, messagetype) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = {#account}; \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#account,"active"}}, \
"unlinkauth", types::unlinkauth{#account, #codeacct, messagetype}); \
trx.expiration = chain.head_block_time() + 100; \
trx.set_reference_block(chain.head_block_id()); \
chain.push_transaction(trx, chain_controller::skip_transaction_signatures); \
BOOST_TEST_CHECKPOINT("Unlink " << #codeacct << "::" << messagetype << " from " << #account); \
}
#define LINKAUTH3(chain, account, codeacct) LINKAUTH5(chain, account, codeacct, "")

#define XFER5(chain, sender, recipient, Amount, memo) \
{ \
eos::chain::SignedTransaction trx; \
Expand Down
22 changes: 22 additions & 0 deletions tests/common/testing_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@
* @endcode
*/
#define Delete_Authority(...) BOOST_PP_OVERLOAD(DELAUTH, __VA_ARGS__)(__VA_ARGS__)
/**
* @brief Shorthand way to link named authority with a contract/message type
*
* @code{.cpp}
* // Link alice's "money" authority with eos::transfer
* Link_Authority(chain, alice, "money", eos, "transfer");
* // Set alice's "native" authority as default for eos contract
* Link_Authority(chain, alice, "money", eos);
* @endcode
*/
#define Link_Authority(...) BOOST_PP_OVERLOAD(LINKAUTH, __VA_ARGS__)(__VA_ARGS__)
/**
* @brief Shorthand way to unlink named authority from a contract/message type
*
* @code{.cpp}
* // Unlink alice's authority for eos::transfer
* Unlink_Authority(chain, alice, eos, "transfer");
* // Unset alice's default authority for eos contract
* Unlink_Authority(chain, alice, eos);
* @endcode
*/
#define Unlink_Authority(...) BOOST_PP_OVERLOAD(UNLINKAUTH, __VA_ARGS__)(__VA_ARGS__)

/**
* @brief Shorthand way to transfer funds
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/native_contract_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <eos/chain/chain_controller.hpp>
#include <eos/chain/exceptions.hpp>
#include <eos/chain/permission_object.hpp>
#include <eos/chain/permission_link_object.hpp>
#include <eos/chain/key_value_object.hpp>

#include <eos/native_contract/producer_objects.hpp>
Expand Down Expand Up @@ -463,4 +464,32 @@ BOOST_FIXTURE_TEST_CASE(auth_tests, testing_fixture) {
}
} FC_LOG_AND_RETHROW() }

BOOST_FIXTURE_TEST_CASE(auth_links, testing_fixture) { try {
Make_Blockchain(chain);
Make_Account(chain, alice);
chain.produce_blocks();

Make_Key(spending);
Make_Key(scud);

Set_Authority(chain, alice, "spending", "active", Key_Authority(spending_public_key));
Set_Authority(chain, alice, "scud", "spending", Key_Authority(scud_public_key));
Link_Authority(chain, alice, "spending", eos, "transfer");

{
auto obj = chain_db.find<permission_link_object, by_message_type>(boost::make_tuple("alice", "eos", "transfer"));
BOOST_CHECK_NE(obj, nullptr);
BOOST_CHECK_EQUAL(obj->account, "alice");
BOOST_CHECK_EQUAL(obj->code, "eos");
BOOST_CHECK_EQUAL(obj->message_type, "transfer");
}

Unlink_Authority(chain, alice, eos, "transfer");

{
auto obj = chain_db.find<permission_link_object, by_message_type>(boost::make_tuple("alice", "eos", "transfer"));
BOOST_CHECK_EQUAL(obj, nullptr);
}
} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 35c4740

Please sign in to comment.