Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions families/block_info/tests/test_tp_block_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
77 changes: 77 additions & 0 deletions families/block_info/tests/test_tp_block_info_config.py
Original file line number Diff line number Diff line change
@@ -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)
69 changes: 69 additions & 0 deletions families/block_info/tests/test_tp_handler.py
Original file line number Diff line number Diff line change
@@ -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)