This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Proto Validator is added Signed-off-by: Akvinikym <anarant12@gmail.com> * Review issues Signed-off-by: Akvinikym <anarant12@gmail.com> * Review issues 2 Signed-off-by: Akvinikym <anarant12@gmail.com>
- Loading branch information
Showing
12 changed files
with
140 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
shared_model/validators/protobuf/proto_block_validator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "validators/protobuf/proto_block_validator.hpp" | ||
|
||
namespace shared_model { | ||
namespace validation { | ||
Answer ProtoBlockValidator::validate( | ||
const iroha::protocol::Block &block) const { | ||
Answer answer; | ||
std::string tx_reason_name = "Protobuf Block"; | ||
ReasonsGroupType reason{tx_reason_name, GroupedReasons()}; | ||
|
||
// make sure version one_of field of the Block is set | ||
if (block.block_version_case() | ||
== iroha::protocol::Block::BLOCK_VERSION_NOT_SET) { | ||
reason.second.emplace_back("Block version is not set"); | ||
answer.addReason(std::move(reason)); | ||
return answer; | ||
} | ||
|
||
return answer; | ||
} | ||
} // namespace validation | ||
} // namespace shared_model |
22 changes: 22 additions & 0 deletions
22
shared_model/validators/protobuf/proto_block_validator.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_PROTO_BLOCK_VALIDATOR_HPP | ||
#define IROHA_PROTO_BLOCK_VALIDATOR_HPP | ||
|
||
#include "block.pb.h" | ||
#include "validators/abstract_validator.hpp" | ||
|
||
namespace shared_model { | ||
namespace validation { | ||
class ProtoBlockValidator | ||
: public AbstractValidator<iroha::protocol::Block> { | ||
public: | ||
Answer validate(const iroha::protocol::Block &block) const override; | ||
}; | ||
} // namespace validation | ||
} // namespace shared_model | ||
|
||
#endif // IROHA_PROTO_BLOCK_VALIDATOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/module/shared_model/validators/protobuf/proto_block_validator_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "validators/protobuf/proto_block_validator.hpp" | ||
#include <gmock/gmock-matchers.h> | ||
#include "block.pb.h" | ||
#include "module/shared_model/validators/validators_fixture.hpp" | ||
|
||
using testing::HasSubstr; | ||
|
||
class ProtoBlockValidatorTest : public ValidatorsTest { | ||
public: | ||
shared_model::validation::ProtoBlockValidator validator; | ||
}; | ||
|
||
/** | ||
* @given protocol block object with unset version field | ||
* @when validating this object | ||
* @then corresponding error is returned | ||
*/ | ||
TEST_F(ProtoBlockValidatorTest, UnsetVersion) { | ||
iroha::protocol::Block invalid_block; | ||
|
||
auto answer = validator.validate(invalid_block); | ||
ASSERT_TRUE(answer.hasErrors()); | ||
ASSERT_THAT(answer.reason(), HasSubstr("Block version is not set")); | ||
} | ||
|
||
/** | ||
* @given valid protocol block object | ||
* @when validating this object | ||
* @then validation is successful | ||
*/ | ||
TEST_F(ProtoBlockValidatorTest, ValidBlock) { | ||
iroha::protocol::Block valid_block; | ||
|
||
iroha::protocol::Block_v1 versioned_block; | ||
*valid_block.mutable_block_v1() = versioned_block; | ||
|
||
auto answer = validator.validate(valid_block); | ||
ASSERT_FALSE(answer.hasErrors()); | ||
} |