diff --git a/families/block_info/tests/test_tp_block_info.yaml b/families/block_info/tests/test_tp_block_info.yaml index 4074c109ec..de5b3c98c6 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 -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_block_info_config.py b/families/block_info/tests/test_tp_block_info_config.py new file mode 100644 index 0000000000..54461dbaa6 --- /dev/null +++ b/families/block_info/tests/test_tp_block_info_config.py @@ -0,0 +1,77 @@ +# 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 +import os + +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 + +class TestBlockInfoConfig(unittest.TestCase): + def test_load_default_block_info_config(self): + """ Tests the default return value of config file """ + 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" + 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" + 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 """ + 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): + block_config_default = load_default_block_info_config() + self.assertEqual(block_config_default.to_toml_string(), + ['connect = "tcp://localhost:4004"']) + + def test_repr(self): + 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" + 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) 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..bd15fae0e2 --- /dev/null +++ b/families/block_info/tests/test_tp_handler.py @@ -0,0 +1,69 @@ +# 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 unittest + +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.common import DEFAULT_SYNC_TOLERANCE + +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) + 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) + 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) + 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) + self.assertTrue(True) + + def test_validate_hex_ve(self): + string_hex = validate_hex("test", 128) + self.assertEqual(string_hex, False)