Skip to content

Commit

Permalink
Merge pull request #820 from tlsfuzzer/less-printing
Browse files Browse the repository at this point in the history
Less printing in test execution
  • Loading branch information
tomato42 committed Jun 2, 2023
2 parents 32444fa + 3b20961 commit 8cb1c0e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
11 changes: 9 additions & 2 deletions tests/test_tlsfuzzer_expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
from tlslite.utils.cryptomath import secureHash


if sys.version_info < (3, 0):
BUILTIN_PRINT = "__builtin__.print"
else:
BUILTIN_PRINT = "builtins.print"

srv_raw_key = str(
"-----BEGIN RSA PRIVATE KEY-----\n"\
"MIICXQIBAAKBgQDRCQR5qRLJX8sy1N4BF1G1fml1vNW5S6o4h3PeWDtg7JEn+jIt\n"\
Expand Down Expand Up @@ -3287,7 +3292,8 @@ def test_process_with_ECDHE_RSA(self):

exp.process(state, msg)

def test_process_with_ECDHE_RSA_bad_signature(self):
@mock.patch(BUILTIN_PRINT)
def test_process_with_ECDHE_RSA_bad_signature(self, mock_print):
exp = ExpectServerKeyExchange()

state = ConnectionState()
Expand Down Expand Up @@ -3322,10 +3328,11 @@ def test_process_with_ECDHE_RSA_bad_signature(self):
msg = srv_key_exchange.makeServerKeyExchange('sha256')
msg.signature[-1] ^= 1

print("Error printed below is expected", file=sys.stderr)
with self.assertRaises(TLSDecryptionFailed):
exp.process(state, msg)

self.assertIn("Bad signature", mock_print.call_args[0][0])

def test_process_with_default_signature_algorithm(self):
exp = ExpectServerKeyExchange()

Expand Down
1 change: 0 additions & 1 deletion tests/test_tlsfuzzer_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,6 @@ def test_generate_in_tls13_with_pha(self):

ret = fg.generate(state)

print(repr(ret.verify_data))
self.assertEqual(ret.verify_data,
bytearray(b'q\xf1l\x05\x94\xb8"\xb2L7\xce\xd5\xb3\x00\xa6\r\x17*'
b'\xcc\xe7\xdc\xa6\xf0c\xd7\x90I\x11}\xbfq:'))
Expand Down
51 changes: 45 additions & 6 deletions tests/test_tlsfuzzer_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import unittest.mock as mock
from unittest.mock import call

import sys
from tlsfuzzer.runner import ConnectionState, Runner, guess_response
from tlsfuzzer.expect import ExpectClose, ExpectNoMessage
from tlsfuzzer.messages import ClientHelloGenerator
Expand All @@ -22,6 +23,13 @@
from tlslite.errors import TLSAbruptCloseError
import socket


if sys.version_info < (3, 0):
BUILTIN_PRINT = "__builtin__.print"
else:
BUILTIN_PRINT = "builtins.print"


class TestConnectionState(unittest.TestCase):
def test___init__(self):
state = ConnectionState()
Expand Down Expand Up @@ -106,7 +114,8 @@ def test___init__(self):

self.assertIsNotNone(runner.state)

def test_run_with_unknown_type(self):
@mock.patch(BUILTIN_PRINT)
def test_run_with_unknown_type(self, mock_print):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=False)
node.is_expect = mock.Mock(return_value=False)
Expand All @@ -118,6 +127,10 @@ def test_run_with_unknown_type(self):
with self.assertRaises(AssertionError):
runner.run()

mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])

def test_run_with_command_node(self):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=True)
Expand Down Expand Up @@ -206,7 +219,8 @@ def test_run_with_expect_and_closed_socket(self):

runner.run()

def test_run_with_expect_and_unexpected_closed_socket(self):
@mock.patch(BUILTIN_PRINT)
def test_run_with_expect_and_unexpected_closed_socket(self, mock_print):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=False)
node.is_expect = mock.Mock(return_value=True)
Expand All @@ -223,7 +237,12 @@ def test_run_with_expect_and_unexpected_closed_socket(self):

self.assertIn("Unexpected closure from peer", str(e.exception))

def test_run_with_expect_and_read_timeout(self):
mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])

@mock.patch(BUILTIN_PRINT)
def test_run_with_expect_and_read_timeout(self, mock_print):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=False)
node.is_expect = mock.Mock(return_value=True)
Expand All @@ -240,6 +259,10 @@ def test_run_with_expect_and_read_timeout(self):

self.assertIn("Timeout when waiting", str(e.exception))

mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])

def test_run_with_expect_and_no_message(self):
node = ExpectNoMessage()

Expand All @@ -250,7 +273,8 @@ def test_run_with_expect_and_no_message(self):

runner.run()

def test_run_with_expect_no_message_and_message_received(self):
@mock.patch(BUILTIN_PRINT)
def test_run_with_expect_no_message_and_message_received(self, mock_print):
node = ExpectNoMessage()

runner = Runner(node)
Expand All @@ -262,7 +286,13 @@ def test_run_with_expect_no_message_and_message_received(self):
with self.assertRaises(AssertionError):
runner.run()

def test_run_with_expect_node_and_unexpected_message(self):
mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])
self.assertIn("ExpectNoMessage", mock_print.call_args[0][0])

@mock.patch(BUILTIN_PRINT)
def test_run_with_expect_node_and_unexpected_message(self, mock_print):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=False)
node.is_expect = mock.Mock(return_value=True)
Expand All @@ -282,7 +312,12 @@ def test_run_with_expect_node_and_unexpected_message(self):

runner.state.msg_sock.sock.close.assert_called_once_with()

def test_run_with_generate_and_unexpected_closed_socket(self):
mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])

@mock.patch(BUILTIN_PRINT)
def test_run_with_generate_and_unexpected_closed_socket(self, mock_print):
node = mock.MagicMock()
node.is_command = mock.Mock(return_value=False)
node.is_expect = mock.Mock(return_value=False)
Expand All @@ -297,6 +332,10 @@ def test_run_with_generate_and_unexpected_closed_socket(self):
with self.assertRaises(AssertionError):
runner.run()

mock_print.assert_called_once()
self.assertIn("Error encountered while processing node",
mock_print.call_args[0][0])

def test_run_with_generate_and_expected_closed_socket(self):
node = ClientHelloGenerator()
node.next_sibling = ExpectClose()
Expand Down
26 changes: 23 additions & 3 deletions tests/test_tlsfuzzer_timing_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def is_33():
return (3, 3) == version_info[:2]


if version_info < (3, 0):
BUILTIN_PRINT = "__builtin__.print"
else:
BUILTIN_PRINT = "builtins.print"


class TestRunner(unittest.TestCase):
def setUp(self):
with mock.patch('tlsfuzzer.timing_runner.os.mkdir'):
Expand Down Expand Up @@ -107,7 +113,10 @@ def test_generate_log_run_only(self):

def test_create_dir(self):
with mock.patch('tlsfuzzer.timing_runner.os.mkdir') as mock_mkdir:
TimingRunner("test", [], "/outdir", "localhost", 4433, "lo")
with mock.patch(
'tlsfuzzer.timing_runner.TimingRunner.check_tcpdump',
return_value=True):
TimingRunner("test", [], "/outdir", "localhost", 4433, "lo")
mock_mkdir.assert_called_once()

def test_create_dir_with_duplicate(self):
Expand Down Expand Up @@ -137,7 +146,8 @@ def test_check_analysis_availability(self):

self.assertEqual(TimingRunner.check_analysis_availability(), analysis_present)

def test_extract(self):
@mock.patch(BUILTIN_PRINT)
def test_extract(self, mock_print):
check_extract = mock.Mock()
check_extract.return_value = False

Expand All @@ -148,7 +158,13 @@ def test_extract(self):
with mock.patch("__main__.__builtins__.__import__"):
self.assertTrue(self.runner.extract())

def test_analyse(self):
mock_print.assert_called_once()
self.assertIn(
"Extraction is not available. Install required packages to enable",
mock_print.call_args[0][0])

@mock.patch(BUILTIN_PRINT)
def test_analyse(self, mock_print):
check_analysis = mock.Mock()
check_analysis.return_value = False

Expand All @@ -159,6 +175,10 @@ def test_analyse(self):
with mock.patch("__main__.__builtins__.__import__"):
self.assertNotEqual(self.runner.analyse(), 2)

self.assertIn(
"Analysis is not available. Install required packages to enable.",
mock_print.call_args[0][0])

def test_run(self):
self.runner.tests = {"A": None, "B": None, "C": None}
self.runner.tcpdump_running = True
Expand Down

0 comments on commit 8cb1c0e

Please sign in to comment.