LCOV - code coverage report
Current view: top level - shared_model/interfaces/impl - permissions.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 35 46 76.1 %
Date: 2019-03-07 14:46:43 Functions: 30 47 63.8 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "interfaces/permissions.hpp"
       7             : 
       8             : using namespace shared_model::interface;
       9             : 
      10             : namespace shared_model {
      11             :   namespace interface {
      12             :     namespace permissions {
      13             : 
      14             :       Role permissionFor(Grantable g) {
      15          37 :         switch (g) {
      16             :           case Grantable::kAddMySignatory:
      17          12 :             return Role::kAddMySignatory;
      18             :           case Grantable::kRemoveMySignatory:
      19           6 :             return Role::kRemoveMySignatory;
      20             :           case Grantable::kSetMyQuorum:
      21          10 :             return Role::kSetMyQuorum;
      22             :           case Grantable::kSetMyAccountDetail:
      23           5 :             return Role::kSetMyAccountDetail;
      24             :           case Grantable::kTransferMyAssets:
      25           4 :             return Role::kTransferMyAssets;
      26             :           default:;
      27           0 :         }
      28           0 :         return Role::COUNT;
      29          37 :       }
      30             : 
      31             :       bool isValid(Role perm) noexcept {
      32       28760 :         auto p = static_cast<size_t>(perm);
      33       28760 :         return p < static_cast<size_t>(Role::COUNT);
      34             :       }
      35             : 
      36             :       bool isValid(Grantable perm) noexcept {
      37         519 :         auto p = static_cast<size_t>(perm);
      38         519 :         return p < static_cast<size_t>(Grantable::COUNT);
      39             :       }
      40             :     }  // namespace permissions
      41             :   }    // namespace interface
      42             : }  // namespace shared_model
      43             : 
      44             : template <typename Perm>
      45             : constexpr auto bit(Perm p) {
      46      931775 :   return static_cast<size_t>(p);
      47             : }
      48             : 
      49             : template <typename Perm>
      50             : PermissionSet<Perm>::PermissionSet() : Parent() {}
      51             : 
      52             : template <typename Perm>
      53             : PermissionSet<Perm>::PermissionSet(std::initializer_list<Perm> list) {
      54      103541 :   for (auto l : list) {
      55       51866 :     set(l);
      56             :   }
      57       51676 : }
      58             : 
      59             : template <typename Perm>
      60             : PermissionSet<Perm>::PermissionSet(const std::string &bitstring) : Parent(bitstring) {}
      61             : 
      62             : template <typename Perm>
      63             : std::string PermissionSet<Perm>::toBitstring() const {
      64       52816 :   return Parent::to_string();
      65             : }
      66             : 
      67             : template <typename Perm>
      68             : size_t PermissionSet<Perm>::size() {
      69      324369 :   return bit(Perm::COUNT);
      70             : }
      71             : 
      72             : template <typename Perm>
      73             : PermissionSet<Perm> &PermissionSet<Perm>::reset() {
      74           0 :   Parent::reset();
      75           0 :   return *this;
      76             : }
      77             : 
      78             : template <typename Perm>
      79             : PermissionSet<Perm> &PermissionSet<Perm>::set() {
      80          49 :   Parent::set();
      81          49 :   return *this;
      82             : }
      83             : 
      84             : template <typename Perm>
      85             : PermissionSet<Perm> &PermissionSet<Perm>::set(Perm p) {
      86      355010 :   Parent::set(bit(p), true);
      87      355010 :   return *this;
      88             : }
      89             : 
      90             : template <typename Perm>
      91             : PermissionSet<Perm> &PermissionSet<Perm>::unset(Perm p) {
      92          12 :   Parent::set(bit(p), false);
      93          12 :   return *this;
      94             : }
      95             : 
      96             : template <typename Perm>
      97             : bool PermissionSet<Perm>::test(Perm p) const {
      98      253517 :   return PermissionSet<Perm>::Parent::test(bit(p));
      99       51676 : }
     100             : 
     101             : template <typename Perm>
     102             : bool PermissionSet<Perm>::none() const {
     103           1 :   return Parent::none();
     104             : }
     105             : 
     106             : template <typename Perm>
     107             : bool PermissionSet<Perm>::isSubsetOf(const PermissionSet<Perm> &r) const {
     108           4 :   return (*this & r) == *this;
     109             : }
     110             : 
     111             : template <typename Perm>
     112             : bool PermissionSet<Perm>::operator==(const PermissionSet<Perm> &r) const {
     113           2 :   return Parent::operator==(r);
     114             : }
     115             : 
     116             : template <typename Perm>
     117             : bool PermissionSet<Perm>::operator!=(const PermissionSet<Perm> &r) const {
     118           0 :   return Parent::operator!=(r);
     119             : }
     120             : 
     121             : template <typename Perm>
     122             : PermissionSet<Perm> &PermissionSet<Perm>::operator&=(
     123             :     const PermissionSet<Perm> &r) {
     124           0 :   Parent::operator&=(r);
     125           0 :   return *this;
     126             : }
     127             : 
     128             : template <typename Perm>
     129             : PermissionSet<Perm> &PermissionSet<Perm>::operator|=(
     130             :     const PermissionSet<Perm> &r) {
     131           0 :   Parent::operator|=(r);
     132           0 :   return *this;
     133             : }
     134             : 
     135             : template <typename Perm>
     136             : PermissionSet<Perm> &PermissionSet<Perm>::operator^=(
     137             :     const PermissionSet<Perm> &r) {
     138           0 :   Parent::operator^=(r);
     139           0 :   return *this;
     140             : }
     141             : 
     142             : template <typename Perm>
     143             : void PermissionSet<Perm>::iterate(std::function<void(Perm)> f) const {
     144      207109 :   for (size_t i = 0; i < size(); ++i) {
     145      202689 :     auto perm = static_cast<Perm>(i);
     146      202689 :     if (test(perm)) {
     147       28802 :       f(perm);
     148       28802 :     }
     149      202591 :   }
     150        4507 : }
     151             : 
     152             : template class shared_model::interface::PermissionSet<permissions::Role>;
     153             : template class shared_model::interface::PermissionSet<permissions::Grantable>;

Generated by: LCOV version 1.13