LCOV - code coverage report
Current view: top level - shared_model/validators/protobuf - proto_transaction_validator.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 35 35 100.0 %
Date: 2018-12-05 17:11:35 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_PROTO_TRANSACTION_VALIDATOR_HPP
       7             : #define IROHA_PROTO_TRANSACTION_VALIDATOR_HPP
       8             : 
       9             : #include "validators/abstract_validator.hpp"
      10             : 
      11             : #include "transaction.pb.h"
      12             : 
      13             : namespace shared_model {
      14             :   namespace validation {
      15             : 
      16             :     class ProtoTransactionValidator
      17             :         : public AbstractValidator<iroha::protocol::Transaction> {
      18             :      private:
      19             :       Answer validateProtoTx(
      20             :           const iroha::protocol::Transaction &transaction) const {
      21          35 :         Answer answer;
      22          35 :         std::string tx_reason_name = "Protobuf Transaction";
      23          35 :         ReasonsGroupType reason(tx_reason_name, GroupedReasons());
      24          66 :         for (const auto &command :
      25          35 :              transaction.payload().reduced_payload().commands()) {
      26          35 :           switch (command.command_case()) {
      27             :             case iroha::protocol::Command::COMMAND_NOT_SET: {
      28           1 :               reason.second.emplace_back("Undefined command is found");
      29           1 :               answer.addReason(std::move(reason));
      30           1 :               return answer;
      31             :             }
      32             :             case iroha::protocol::Command::kCreateRole: {
      33           2 :               const auto &cr = command.create_role();
      34           2 :               bool all_permissions_valid = std::all_of(
      35           2 :                   cr.permissions().begin(),
      36           2 :                   cr.permissions().end(),
      37             :                   [](const auto &perm) {
      38           2 :                     return iroha::protocol::RolePermission_IsValid(perm);
      39             :                   });
      40           2 :               if (not all_permissions_valid) {
      41           1 :                 reason.second.emplace_back("Invalid role permission");
      42           1 :                 answer.addReason(std::move(reason));
      43           1 :                 return answer;
      44             :               }
      45           1 :               break;
      46             :             }
      47             :             case iroha::protocol::Command::kGrantPermission: {
      48           2 :               if (not iroha::protocol::GrantablePermission_IsValid(
      49           2 :                       command.grant_permission().permission())) {
      50           1 :                 reason.second.emplace_back("Invalid grantable permission");
      51           1 :                 answer.addReason(std::move(reason));
      52           1 :                 return answer;
      53             :               }
      54           1 :               break;
      55             :             }
      56             :             case iroha::protocol::Command::kRevokePermission: {
      57           2 :               if (not iroha::protocol::GrantablePermission_IsValid(
      58           2 :                       command.revoke_permission().permission())) {
      59           1 :                 reason.second.emplace_back("Invalid grantable permission");
      60           1 :                 answer.addReason(std::move(reason));
      61           1 :                 return answer;
      62             :               }
      63           1 :               break;
      64             :             }
      65          28 :             default: { break; }
      66             :           }
      67             :         }
      68          31 :         return answer;
      69          35 :       }
      70             : 
      71             :      public:
      72             :       Answer validate(const iroha::protocol::Transaction &tx) const override {
      73          35 :         return validateProtoTx(tx);
      74             :       };
      75             :     };
      76             :   }  // namespace validation
      77             : }  // namespace shared_model
      78             : 
      79             : #endif  // IROHA_PROTO_TRANSACTION_VALIDATOR_HPP

Generated by: LCOV version 1.13