From f43333735988484ba8541278af3e7e4d7f380a23 Mon Sep 17 00:00:00 2001 From: sandeep hs Date: Fri, 13 Apr 2018 18:52:20 +0530 Subject: [PATCH 1/5] block_info : test case for handler funtions --- .../block_info/tests/test_tp_block_info.yaml | 3 +- families/block_info/tests/test_tp_handler.py | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 families/block_info/tests/test_tp_handler.py diff --git a/families/block_info/tests/test_tp_block_info.yaml b/families/block_info/tests/test_tp_block_info.yaml index 4074c109ec..8793b3db40 100644 --- a/families/block_info/tests/test_tp_block_info.yaml +++ b/families/block_info/tests/test_tp_block_info.yaml @@ -32,11 +32,10 @@ services: - $SAWTOOTH_CORE:/project/sawtooth-core expose: - 4004 - command: nose2-3 + command: nose2-3 --coverage-report term-missing -c /project/sawtooth-core/families/block_info/nose2.cfg -v -s /project/sawtooth-core/families/block_info/tests - test_tp_block_info stop_signal: SIGKILL environment: TEST_BIND: "tcp://eth0:4004" diff --git a/families/block_info/tests/test_tp_handler.py b/families/block_info/tests/test_tp_handler.py new file mode 100644 index 0000000000..9358775cad --- /dev/null +++ b/families/block_info/tests/test_tp_handler.py @@ -0,0 +1,69 @@ +import time +import zmq +import unittest + + + + +from sawtooth_processor_test.message_factory import MessageFactory +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoTxn +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfo +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoConfig +from sawtooth_block_info.processor.handler import BlockInfoTransactionHandler, validate_hex, validate_timestamp +from sawtooth_block_info.common import FAMILY_NAME +from sawtooth_block_info.common import FAMILY_VERSION +from sawtooth_block_info.common import NAMESPACE +from sawtooth_block_info.common import CONFIG_ADDRESS +from sawtooth_block_info.common import DEFAULT_SYNC_TOLERANCE +from sawtooth_block_info.common import DEFAULT_TARGET_COUNT +from sawtooth_block_info.common import create_block_address +from sawtooth_sdk.processor.exceptions import InvalidTransaction + + +def create_block_info(block_num, + signer_public_key="2" * 66, + header_signature="1" * 128, + timestamp=None, + previous_block_id="0" * 128): + if timestamp is None: + timestamp = int(time.time()) + + return BlockInfo( + block_num=block_num, + signer_public_key=signer_public_key, + header_signature=header_signature, + timestamp=timestamp, + previous_block_id=previous_block_id) + +class TestHandler(unittest.TestCase): + + def test_validate_hex_previd(self): + """ Tests previous block id is in valid hex """ + block_info = create_block_info(block_num=1) + vp=validate_hex(block_info.previous_block_id, 128) + self.assertEqual(vp, True) + + def test_validate_hex_sign_public_key(self): + """ Tests signer public key is in valid hex """ + block_info = create_block_info(block_num=1) + vp=validate_hex(block_info.signer_public_key, 66) + self.assertEqual(vp, True) + + def test_validate_hex_header_sign_key(self): + """ Tests header signature is in valid hex """ + block_info = create_block_info(block_num=1) + vp=validate_hex(block_info.header_signature, 128) + self.assertEqual(vp, True) + + def test_validate_timestamp(self): + """ Tests the timestamp is greater than zero """ + block_info = create_block_info(block_num=1) + now = time.time() + if (block_info.timestamp - now) < DEFAULT_SYNC_TOLERANCE: + validate_timestamp(block_info.timestamp,DEFAULT_SYNC_TOLERANCE) + self.assertTrue(True) + + def test_validate_hex_ve(self): + vp=validate_hex("test", 128) + self.assertEqual(vp, False) + \ No newline at end of file From 56b0a330a76c453410e1c16dd7534a7f77bc4e60 Mon Sep 17 00:00:00 2001 From: sandeep hs Date: Fri, 13 Apr 2018 18:54:34 +0530 Subject: [PATCH 2/5] block_info : test case for block info config funtions --- .../tests/test_tp_block_info_config.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 families/block_info/tests/test_tp_block_info_config.py diff --git a/families/block_info/tests/test_tp_block_info_config.py b/families/block_info/tests/test_tp_block_info_config.py new file mode 100644 index 0000000000..654f00abf3 --- /dev/null +++ b/families/block_info/tests/test_tp_block_info_config.py @@ -0,0 +1,75 @@ +import time +import zmq +import unittest + + + +from sawtooth_processor_test.message_factory import MessageFactory +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoTxn +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfo +from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoConfig +from sawtooth_sdk.processor.exceptions import LocalConfigurationError +from sawtooth_block_info.processor.config.block_info import load_default_block_info_config, load_toml_block_info_config, merge_block_info_config +from sawtooth_block_info.common import FAMILY_NAME +from sawtooth_block_info.common import FAMILY_VERSION +from sawtooth_block_info.common import NAMESPACE +from sawtooth_block_info.common import CONFIG_ADDRESS +from sawtooth_block_info.common import DEFAULT_SYNC_TOLERANCE +from sawtooth_block_info.common import DEFAULT_TARGET_COUNT +from sawtooth_block_info.common import create_block_address + + + +class TestBlockInfoConfig(unittest.TestCase): + def test_load_default_block_info_config(self): + """ Tests the default return value of config file """ + bc=load_default_block_info_config() + self.assertEqual(bc.connect, "tcp://localhost:4004") + #self.assertEqual(bc.connect, "hi") + + def test_load_toml_block_info_config_ne(self): + """ Tests toml load file if it is not present """ + filename="test.toml" + cf=load_toml_block_info_config(filename) + self.assertEqual(cf.connect, None) + + def test_load_toml_block_info_config(self): + """ Tests value inside toml load file """ + filename="test_block_info.toml" + with open(filename, 'w') as fd: + fd.write('connect = "tcp://blarg:1234"') + config = load_toml_block_info_config(filename) + self.assertEqual(config.connect, "tcp://blarg:1234") + + def test_merge_config(self): + """ Tests the merge of all toml config files """ + L=[] + bc1=load_default_block_info_config() + bc2=load_default_block_info_config() + bc3=load_default_block_info_config() + L.append(bc1) + L.append(bc2) + L.append(bc3) + mc=merge_block_info_config(L) + self.assertEqual(mc.connect, "tcp://localhost:4004") + + def test_to_toml(self): + tt=load_default_block_info_config() + self.assertEqual(tt.to_toml_string(), ['connect = "tcp://localhost:4004"']) + + def test_repr(self): + repr=load_default_block_info_config() + self.assertEqual(repr.__repr__(), "BlockInfoConfig(connect='tcp://localhost:4004')") + + def test_load_toml_block_info_config_invalidkeys(self): + filename="a.toml" + with open(filename, 'w') as fd: + fd.write('ty = "tcp://test:4004"') + with self.assertRaises(LocalConfigurationError): + config = load_toml_block_info_config(filename) + + + + + + From cc61a363db1b2edb1a437ffbe7c4f77157afba8f Mon Sep 17 00:00:00 2001 From: sandeep hs Date: Fri, 13 Apr 2018 20:58:52 +0530 Subject: [PATCH 3/5] block_info : test case for handler and config file --- .../tests/test_tp_block_info_config.py | 103 +++++++++--------- families/block_info/tests/test_tp_handler.py | 61 ++++++----- 2 files changed, 80 insertions(+), 84 deletions(-) diff --git a/families/block_info/tests/test_tp_block_info_config.py b/families/block_info/tests/test_tp_block_info_config.py index 654f00abf3..84b17a90cb 100644 --- a/families/block_info/tests/test_tp_block_info_config.py +++ b/families/block_info/tests/test_tp_block_info_config.py @@ -1,75 +1,70 @@ -import time -import zmq -import unittest - +# Copyright 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ------------------------------------------------------------------------------ +import unittest -from sawtooth_processor_test.message_factory import MessageFactory -from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoTxn -from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfo -from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoConfig +from sawtooth_block_info.processor.config.block_info \ +import load_default_block_info_config,\ + load_toml_block_info_config, merge_block_info_config from sawtooth_sdk.processor.exceptions import LocalConfigurationError -from sawtooth_block_info.processor.config.block_info import load_default_block_info_config, load_toml_block_info_config, merge_block_info_config -from sawtooth_block_info.common import FAMILY_NAME -from sawtooth_block_info.common import FAMILY_VERSION -from sawtooth_block_info.common import NAMESPACE -from sawtooth_block_info.common import CONFIG_ADDRESS -from sawtooth_block_info.common import DEFAULT_SYNC_TOLERANCE -from sawtooth_block_info.common import DEFAULT_TARGET_COUNT -from sawtooth_block_info.common import create_block_address - - class TestBlockInfoConfig(unittest.TestCase): def test_load_default_block_info_config(self): """ Tests the default return value of config file """ - bc=load_default_block_info_config() - self.assertEqual(bc.connect, "tcp://localhost:4004") - #self.assertEqual(bc.connect, "hi") - + block_config_default = load_default_block_info_config() + self.assertEqual(block_config_default.connect, "tcp://localhost:4004") + def test_load_toml_block_info_config_ne(self): """ Tests toml load file if it is not present """ - filename="test.toml" - cf=load_toml_block_info_config(filename) - self.assertEqual(cf.connect, None) - + filename = "test.toml" + block_config_default = load_toml_block_info_config(filename) + self.assertEqual(block_config_default.connect, None) + def test_load_toml_block_info_config(self): """ Tests value inside toml load file """ - filename="test_block_info.toml" + filename = "test_block_info.toml" with open(filename, 'w') as fd: fd.write('connect = "tcp://blarg:1234"') - config = load_toml_block_info_config(filename) - self.assertEqual(config.connect, "tcp://blarg:1234") + block_config_filename = load_toml_block_info_config(filename) + self.assertEqual(block_config_filename.connect, "tcp://blarg:1234") def test_merge_config(self): - """ Tests the merge of all toml config files """ - L=[] - bc1=load_default_block_info_config() - bc2=load_default_block_info_config() - bc3=load_default_block_info_config() - L.append(bc1) - L.append(bc2) - L.append(bc3) - mc=merge_block_info_config(L) - self.assertEqual(mc.connect, "tcp://localhost:4004") - + """ Tests the merge of all toml config files """ + l = [] + block_config_default_1 = load_default_block_info_config() + block_config_default_2 = load_default_block_info_config() + block_config_default_3 = load_default_block_info_config() + l.append(block_config_default_1) + l.append(block_config_default_2) + l.append(block_config_default_3) + mc = merge_block_info_config(l) + self.assertEqual(mc.connect, "tcp://localhost:4004") + def test_to_toml(self): - tt=load_default_block_info_config() - self.assertEqual(tt.to_toml_string(), ['connect = "tcp://localhost:4004"']) - + block_config_default = load_default_block_info_config() + self.assertEqual(block_config_default.to_toml_string(), + ['connect = "tcp://localhost:4004"']) + def test_repr(self): - repr=load_default_block_info_config() - self.assertEqual(repr.__repr__(), "BlockInfoConfig(connect='tcp://localhost:4004')") - + block_config_default = load_default_block_info_config() + self.assertEqual(block_config_default.__repr__(), + "BlockInfoConfig(connect='tcp://localhost:4004')") + def test_load_toml_block_info_config_invalidkeys(self): - filename="a.toml" + filename = "a.toml" with open(filename, 'w') as fd: fd.write('ty = "tcp://test:4004"') with self.assertRaises(LocalConfigurationError): - config = load_toml_block_info_config(filename) - - - - - - + load_toml_block_info_config(filename) diff --git a/families/block_info/tests/test_tp_handler.py b/families/block_info/tests/test_tp_handler.py index 9358775cad..b87db0c221 100644 --- a/families/block_info/tests/test_tp_handler.py +++ b/families/block_info/tests/test_tp_handler.py @@ -1,24 +1,25 @@ +# Copyright 2018 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ------------------------------------------------------------------------------ + import time -import zmq import unittest - - - -from sawtooth_processor_test.message_factory import MessageFactory -from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoTxn +from sawtooth_block_info.processor.handler \ +import validate_hex, validate_timestamp from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfo -from sawtooth_block_info.protobuf.block_info_pb2 import BlockInfoConfig -from sawtooth_block_info.processor.handler import BlockInfoTransactionHandler, validate_hex, validate_timestamp -from sawtooth_block_info.common import FAMILY_NAME -from sawtooth_block_info.common import FAMILY_VERSION -from sawtooth_block_info.common import NAMESPACE -from sawtooth_block_info.common import CONFIG_ADDRESS from sawtooth_block_info.common import DEFAULT_SYNC_TOLERANCE -from sawtooth_block_info.common import DEFAULT_TARGET_COUNT -from sawtooth_block_info.common import create_block_address -from sawtooth_sdk.processor.exceptions import InvalidTransaction - def create_block_info(block_num, signer_public_key="2" * 66, @@ -36,34 +37,34 @@ def create_block_info(block_num, previous_block_id=previous_block_id) class TestHandler(unittest.TestCase): - + def test_validate_hex_previd(self): """ Tests previous block id is in valid hex """ block_info = create_block_info(block_num=1) - vp=validate_hex(block_info.previous_block_id, 128) - self.assertEqual(vp, True) - + previd_hex = validate_hex(block_info.previous_block_id, 128) + self.assertEqual(previd_hex, True) + def test_validate_hex_sign_public_key(self): """ Tests signer public key is in valid hex """ block_info = create_block_info(block_num=1) - vp=validate_hex(block_info.signer_public_key, 66) - self.assertEqual(vp, True) - + public_key_hex = validate_hex(block_info.signer_public_key, 66) + self.assertEqual(public_key_hex, True) + def test_validate_hex_header_sign_key(self): """ Tests header signature is in valid hex """ block_info = create_block_info(block_num=1) - vp=validate_hex(block_info.header_signature, 128) - self.assertEqual(vp, True) - + header_signature_hex = validate_hex(block_info.header_signature, 128) + self.assertEqual(header_signature_hex, True) + def test_validate_timestamp(self): """ Tests the timestamp is greater than zero """ block_info = create_block_info(block_num=1) now = time.time() if (block_info.timestamp - now) < DEFAULT_SYNC_TOLERANCE: - validate_timestamp(block_info.timestamp,DEFAULT_SYNC_TOLERANCE) + validate_timestamp(block_info.timestamp, DEFAULT_SYNC_TOLERANCE) self.assertTrue(True) - + def test_validate_hex_ve(self): - vp=validate_hex("test", 128) - self.assertEqual(vp, False) + string_hex = validate_hex("test", 128) + self.assertEqual(string_hex, False) \ No newline at end of file From 190b01758378199d314853596a6c7e9032b2b985 Mon Sep 17 00:00:00 2001 From: sandeep hs Date: Fri, 13 Apr 2018 23:32:59 +0530 Subject: [PATCH 4/5] Block Info : Test case to handler and block info config files --- families/block_info/tests/test_tp_block_info.yaml | 2 +- families/block_info/tests/test_tp_handler.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/families/block_info/tests/test_tp_block_info.yaml b/families/block_info/tests/test_tp_block_info.yaml index 8793b3db40..de5b3c98c6 100644 --- a/families/block_info/tests/test_tp_block_info.yaml +++ b/families/block_info/tests/test_tp_block_info.yaml @@ -32,7 +32,7 @@ services: - $SAWTOOTH_CORE:/project/sawtooth-core expose: - 4004 - command: nose2-3 --coverage-report term-missing + command: nose2-3 -c /project/sawtooth-core/families/block_info/nose2.cfg -v -s /project/sawtooth-core/families/block_info/tests diff --git a/families/block_info/tests/test_tp_handler.py b/families/block_info/tests/test_tp_handler.py index b87db0c221..bd15fae0e2 100644 --- a/families/block_info/tests/test_tp_handler.py +++ b/families/block_info/tests/test_tp_handler.py @@ -67,4 +67,3 @@ def test_validate_timestamp(self): def test_validate_hex_ve(self): string_hex = validate_hex("test", 128) self.assertEqual(string_hex, False) - \ No newline at end of file From beef08fc526e39220fc6bd7ed7e4b872026d9693 Mon Sep 17 00:00:00 2001 From: sandeep hs Date: Tue, 17 Apr 2018 23:38:54 +0530 Subject: [PATCH 5/5] Test Cases for Handler and Config file of Block Info Module --- .../tests/test_tp_block_info_config.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/families/block_info/tests/test_tp_block_info_config.py b/families/block_info/tests/test_tp_block_info_config.py index 84b17a90cb..54461dbaa6 100644 --- a/families/block_info/tests/test_tp_block_info_config.py +++ b/families/block_info/tests/test_tp_block_info_config.py @@ -14,6 +14,7 @@ # ------------------------------------------------------------------------------ import unittest +import os from sawtooth_block_info.processor.config.block_info \ import load_default_block_info_config,\ @@ -35,10 +36,13 @@ def test_load_toml_block_info_config_ne(self): def test_load_toml_block_info_config(self): """ Tests value inside toml load file """ filename = "test_block_info.toml" - with open(filename, 'w') as fd: - fd.write('connect = "tcp://blarg:1234"') - block_config_filename = load_toml_block_info_config(filename) - self.assertEqual(block_config_filename.connect, "tcp://blarg:1234") + try: + with open(filename, 'w') as fd: + fd.write('connect = "tcp://blarg:1234"') + block_config_filename = load_toml_block_info_config(filename) + self.assertEqual(block_config_filename.connect, "tcp://blarg:1234") + finally: + os.remove(filename) def test_merge_config(self): """ Tests the merge of all toml config files """ @@ -64,7 +68,10 @@ def test_repr(self): def test_load_toml_block_info_config_invalidkeys(self): filename = "a.toml" - with open(filename, 'w') as fd: - fd.write('ty = "tcp://test:4004"') - with self.assertRaises(LocalConfigurationError): - load_toml_block_info_config(filename) + try: + with open(filename, 'w') as fd: + fd.write('ty = "tcp://test:4004"') + with self.assertRaises(LocalConfigurationError): + load_toml_block_info_config(filename) + finally: + os.remove(filename)