github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_duplicate_handler/tests.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 import unittest 16 17 from sawtooth_validator.protobuf.network_pb2 import GossipMessage 18 from sawtooth_validator.protobuf.block_pb2 import Block 19 from sawtooth_validator.protobuf.batch_pb2 import Batch 20 from sawtooth_validator.networking.dispatch import HandlerStatus 21 from sawtooth_validator.gossip.gossip_handlers import \ 22 GossipMessageDuplicateHandler 23 24 from test_duplicate_handler.mock import MockCompleter 25 from test_duplicate_handler.mock import MockChainController 26 from test_duplicate_handler.mock import MockPublisher 27 28 29 class TestDuplicateHandler(unittest.TestCase): 30 def setUp(self): 31 self.completer = MockCompleter() 32 self.chain = MockChainController() 33 self.publisher = MockPublisher() 34 self.handler = GossipMessageDuplicateHandler( 35 self.completer, self.chain.has_block, self.publisher.has_batch) 36 37 def test_no_block(self): 38 """ 39 Test that if the block does not exist yet in the completer or the 40 chain controller, the gossip message is passed. 41 """ 42 block = Block(header_signature="Block1") 43 handler_status = self.handler.handle( 44 "connection_id", (block, GossipMessage.BLOCK, 2)) 45 self.assertEqual(handler_status.status, HandlerStatus.PASS) 46 47 def test_no_batch(self): 48 """ 49 Test that if the block does not exist yet in the completer or the 50 chain controller, the gossip message is passed. 51 """ 52 batch = Batch(header_signature="Batch1") 53 handler_status = self.handler.handle( 54 "connection_id", (batch, GossipMessage.BATCH, 2)) 55 self.assertEqual(handler_status.status, HandlerStatus.PASS) 56 57 def test_completer_has_block(self): 58 """ 59 Test that if the block does not exist yet in the completer or the 60 chain controller, the gossip message is passed. 61 """ 62 block = Block(header_signature="Block1") 63 self.completer.add_block("Block1") 64 handler_status = self.handler.handle( 65 "connection_id", (block, GossipMessage.BLOCK, 2)) 66 self.assertEqual(handler_status.status, HandlerStatus.DROP) 67 68 def test_completer_has_batch(self): 69 """ 70 Test that if the block does not exist yet in the completer or the 71 chain controller, the gossip message is passed. 72 """ 73 batch = Batch(header_signature="Batch1") 74 self.completer.add_batch("Batch1") 75 handler_status = self.handler.handle( 76 "connection_id", (batch, GossipMessage.BATCH, 2)) 77 self.assertEqual(handler_status.status, HandlerStatus.DROP) 78 79 def test_chain_has_block(self): 80 """ 81 Test that if the block does not exist yet in the completer or the 82 chain controller, the gossip message is passed. 83 """ 84 block = Block(header_signature="Block1") 85 self.chain.add_block("Block1") 86 handler_status = self.handler.handle( 87 "connection_id", (block, GossipMessage.BLOCK, 2)) 88 self.assertEqual(handler_status.status, HandlerStatus.DROP) 89 90 def test_publisher_has_block(self): 91 """ 92 Test that if the block does not exist yet in the completer or the 93 chain controller, the gossip message is passed. 94 """ 95 batch = Batch(header_signature="Batch1") 96 self.publisher.add_batch("Batch1") 97 handler_status = self.handler.handle( 98 "connection_id", (batch, GossipMessage.BATCH, 2)) 99 self.assertEqual(handler_status.status, HandlerStatus.DROP)