Navigation Menu

Skip to content

Commit

Permalink
Add unit test to sanity check privilege module
Browse files Browse the repository at this point in the history
Add a unit test to verify the following:

  * All privileges can be converted to a textual name and back
  * The privilege mask may hold the privilege

(Change the printout of the value for an unknown privilege in the
exception from hex to ease debugging)

Change-Id: I28eed82298fea2d4f26b93ce7a324c3f222f13a1
Reviewed-on: https://review.couchbase.org/c/kv_engine/+/177261
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Richard de Mellow <richard.demellow@couchbase.com>
  • Loading branch information
trondn committed Jul 11, 2022
1 parent 8328ef3 commit 8ffabfd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/memcached/rbac/privileges.h
Expand Up @@ -214,6 +214,12 @@ bool is_bucket_privilege(Privilege);
/// is this a privilege which should be mapped to a scope / collection
bool is_collection_privilege(Privilege);

/**
* Check to see if the privilege is a legal value or not (to be used in
* unit tests to sanity check the privilege masks)
*/
bool is_legal_privilege(Privilege);

/**
* Convert a textual string to a Privilege
*
Expand Down
40 changes: 40 additions & 0 deletions rbac/privilege_test.cc
Expand Up @@ -141,3 +141,43 @@ TEST(PrivilegeDatabaseTest, to_json) {
EXPECT_EQ(json.dump(2), db.to_json(cb::rbac::Domain::External).dump(2))
<< db.to_json(cb::rbac::Domain::External).dump(2);
}

/// Perform a sanity check on the Privilege that the following is true:
///
/// 1. It is possible to map the privilege to a textual name
/// 2. It is possible to map the textual name to the same privilege
/// 3. The privilege may be put inside the PrivilegeMask
TEST(Privilege, sanity_check) {
using namespace cb::rbac;
PrivilegeMask mask;
int highest = -1;

// We've only defined a handfull of privileges, so loop with some
// negative values and some higher so that we don't need to update
// the test every time we add a new privilege
const int lower_test_limit = -10;
const int upper_test_limit = 1000;

for (int ii = lower_test_limit; ii < upper_test_limit; ++ii) {
auto priv = Privilege(ii);
// The function is_legal_privilege use a switch on an enum class
// which would cause a compile failure if you add a new value and
// don't update the switch
if (is_legal_privilege(priv)) {
// Verify 1
auto textual = to_string(priv);
// Verify 2
EXPECT_EQ(priv, to_privilege(textual));
// Verify 3
EXPECT_LT(ii, mask.size())
<< textual << " is outside the privilege mask";
if (highest > ii) {
highest = ii;
}
}
}

EXPECT_LT(highest + 100, upper_test_limit)
<< "Please bump the upper test limit to ensure we test values "
"outside the legal range";
}
35 changes: 33 additions & 2 deletions rbac/privileges.cc
Expand Up @@ -106,7 +106,7 @@ bool is_bucket_privilege(Privilege priv) {

throw std::invalid_argument(
"is_bucket_privilege() invalid privilege provided: " +
cb::to_hex(uint8_t(priv)));
std::to_string(int(priv)));
}

bool is_collection_privilege(Privilege priv) {
Expand Down Expand Up @@ -141,7 +141,38 @@ bool is_collection_privilege(Privilege priv) {

throw std::invalid_argument(
"is_collection_privilege() invalid privilege provided: " +
cb::to_hex(uint8_t(priv)));
std::to_string(int(priv)));
}

bool is_legal_privilege(Privilege privilege) {
switch (privilege) {
case Privilege::Read:
case Privilege::Insert:
case Privilege::Delete:
case Privilege::Upsert:
case Privilege::SimpleStats:
case Privilege::Stats:
case Privilege::NodeSupervisor:
case Privilege::Administrator:
case Privilege::Audit:
case Privilege::DcpConsumer:
case Privilege::DcpProducer:
case Privilege::DcpStream:
case Privilege::MetaWrite:
case Privilege::IdleConnection:
case Privilege::SystemXattrRead:
case Privilege::SystemXattrWrite:
case Privilege::BucketThrottleManagement:
case Privilege::Unthrottled:
case Privilege::Unmetered:
case Privilege::Impersonate:
case Privilege::Settings:
case Privilege::SystemSettings:
case Privilege::RangeScan:
return true;
}

return false;
}

} // namespace cb::rbac

0 comments on commit 8ffabfd

Please sign in to comment.