LCOV - code coverage report
Current view: top level - irohad/main - iroha_conf_loader.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 44 44 100.0 %
Date: 2018-12-05 17:11:35 Functions: 1 1 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_CONF_LOADER_HPP
       7             : #define IROHA_CONF_LOADER_HPP
       8             : 
       9             : #include <fstream>
      10             : #include <string>
      11             : 
      12             : #include <rapidjson/document.h>
      13             : #include <rapidjson/error/en.h>
      14             : #include <rapidjson/istreamwrapper.h>
      15             : #include <rapidjson/rapidjson.h>
      16             : 
      17             : #include "main/assert_config.hpp"
      18             : 
      19             : namespace config_members {
      20             :   const char *BlockStorePath = "block_store_path";
      21             :   const char *ToriiPort = "torii_port";
      22             :   const char *InternalPort = "internal_port";
      23             :   const char *KeyPairPath = "key_pair_path";
      24             :   const char *PgOpt = "pg_opt";
      25             :   const char *MaxProposalSize = "max_proposal_size";
      26             :   const char *ProposalDelay = "proposal_delay";
      27             :   const char *VoteDelay = "vote_delay";
      28             :   const char *MstSupport = "mst_enable";
      29             : }  // namespace config_members
      30             : 
      31             : /**
      32             :  * parse and assert trusted peers json in `iroha.conf`
      33             :  * @param conf_path is a path to iroha's config
      34             :  * @return rapidjson::Document is a parsed equivalent of that file
      35             :  */
      36             : inline rapidjson::Document parse_iroha_config(const std::string &conf_path) {
      37             :   namespace ac = assert_config;
      38             :   namespace mbr = config_members;
      39          10 :   rapidjson::Document doc;
      40          10 :   std::ifstream ifs_iroha(conf_path);
      41          10 :   rapidjson::IStreamWrapper isw(ifs_iroha);
      42          10 :   const std::string kStrType = "string";
      43          10 :   const std::string kUintType = "uint";
      44          10 :   const std::string kBoolType = "bool";
      45          10 :   doc.ParseStream(isw);
      46          10 :   ac::assert_fatal(
      47          10 :       not doc.HasParseError(),
      48          10 :       "JSON parse error [" + conf_path + "]: "
      49          10 :           + std::string(rapidjson::GetParseError_En(doc.GetParseError())));
      50             : 
      51          10 :   ac::assert_fatal(doc.HasMember(mbr::BlockStorePath),
      52          10 :                    ac::no_member_error(mbr::BlockStorePath));
      53          10 :   ac::assert_fatal(doc[mbr::BlockStorePath].IsString(),
      54          10 :                    ac::type_error(mbr::BlockStorePath, kStrType));
      55             : 
      56          10 :   ac::assert_fatal(doc.HasMember(mbr::ToriiPort),
      57          10 :                    ac::no_member_error(mbr::ToriiPort));
      58          10 :   ac::assert_fatal(doc[mbr::ToriiPort].IsUint(),
      59          10 :                    ac::type_error(mbr::ToriiPort, kUintType));
      60             : 
      61          10 :   ac::assert_fatal(doc.HasMember(mbr::InternalPort),
      62          10 :                    ac::no_member_error(mbr::InternalPort));
      63          10 :   ac::assert_fatal(doc[mbr::InternalPort].IsUint(),
      64          10 :                    ac::type_error(mbr::InternalPort, kUintType));
      65             : 
      66          10 :   ac::assert_fatal(doc.HasMember(mbr::PgOpt), ac::no_member_error(mbr::PgOpt));
      67          10 :   ac::assert_fatal(doc[mbr::PgOpt].IsString(),
      68          10 :                    ac::type_error(mbr::PgOpt, kStrType));
      69             : 
      70          10 :   ac::assert_fatal(doc.HasMember(mbr::MaxProposalSize),
      71          10 :                    ac::no_member_error(mbr::MaxProposalSize));
      72          10 :   ac::assert_fatal(doc[mbr::MaxProposalSize].IsUint(),
      73          10 :                    ac::type_error(mbr::MaxProposalSize, kUintType));
      74             : 
      75          10 :   ac::assert_fatal(doc.HasMember(mbr::ProposalDelay),
      76          10 :                    ac::no_member_error(mbr::ProposalDelay));
      77          10 :   ac::assert_fatal(doc[mbr::ProposalDelay].IsUint(),
      78          10 :                    ac::type_error(mbr::ProposalDelay, kUintType));
      79             : 
      80          10 :   ac::assert_fatal(doc.HasMember(mbr::VoteDelay),
      81          10 :                    ac::no_member_error(mbr::VoteDelay));
      82          10 :   ac::assert_fatal(doc[mbr::VoteDelay].IsUint(),
      83          10 :                    ac::type_error(mbr::VoteDelay, kUintType));
      84             : 
      85          10 :   ac::assert_fatal(doc.HasMember(mbr::MstSupport),
      86          10 :                    ac::no_member_error(mbr::MstSupport));
      87          10 :   ac::assert_fatal(doc[mbr::MstSupport].IsBool(),
      88          10 :                    ac::type_error(mbr::MstSupport, kBoolType));
      89          10 :   return doc;
      90          10 : }
      91             : 
      92             : #endif  // IROHA_CONF_LOADER_HPP

Generated by: LCOV version 1.13