From ca460b73482d48a41774d3fb0e01df8238a3cc7f Mon Sep 17 00:00:00 2001 From: Shreya Date: Thu, 14 Jan 2021 18:13:43 +0530 Subject: [PATCH] Changes in unit test --- .../network/test_postgresql_fingerprint.py | 92 ++++++++++++------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/monkey/infection_monkey/network/test_postgresql_fingerprint.py b/monkey/infection_monkey/network/test_postgresql_fingerprint.py index 708899690..87f7e01af 100644 --- a/monkey/infection_monkey/network/test_postgresql_fingerprint.py +++ b/monkey/infection_monkey/network/test_postgresql_fingerprint.py @@ -27,60 +27,88 @@ RESULT_STRINGS =\ 'only_selected': "Only selected hosts can make connections (SSL or non-SSL).\n" } -EXAMPLE_EXCEPTIONS =\ - [ - RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], # SSL not configured, all non-SSL allowed +EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS =\ + { + # SSL not configured, all non-SSL allowed, # SSL not configured, all non-SSL allowed + RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']: [ + RESULT_STRINGS['ssl_not_conf'], + RESULT_STRINGS['all_non_ssl'] + ], - RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found'], # SSL not configured, selected non-SSL allowed + # SSL not configured, selected non-SSL allowed + RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']: [ + RESULT_STRINGS['ssl_not_conf'], + RESULT_STRINGS['selected_non_ssl'] + ], + # all SSL allowed, all non-SSL allowed '\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], - RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]), # all SSL allowed, all non-SSL allowed + RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]): [ + RESULT_STRINGS['ssl_conf'], + RESULT_STRINGS['all_ssl'], + RESULT_STRINGS['all_non_ssl'] + ], + # all SSL allowed, selected non-SSL allowed '\n'.join([RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed'], - RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]), # all SSL allowed, selected non-SSL allowed + RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]): [ + RESULT_STRINGS['ssl_conf'], + RESULT_STRINGS['all_ssl'], + RESULT_STRINGS['selected_non_ssl'] + ], + # selected SSL allowed, all non-SSL allowed '\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'], - RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]), # selected SSL allowed, all non-SSL allowed + RELEVANT_EXCEPTION_STRINGS['pwd_auth_failed']]): [ + RESULT_STRINGS['ssl_conf'], + RESULT_STRINGS['selected_ssl'], + RESULT_STRINGS['all_non_ssl'] + ], + # selected SSL allowed, selected non-SSL allowed '\n'.join([RELEVANT_EXCEPTION_STRINGS['ssl_on_entry_not_found'], - RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]) # selected SSL allowed, selected non-SSL allowed - ] # don't change order! + RELEVANT_EXCEPTION_STRINGS['ssl_off_entry_not_found']]): [ + RESULT_STRINGS['ssl_conf'], + RESULT_STRINGS['only_selected'] + ] + } -EXPECTED_RESULTS =\ - [ - [RESULT_STRINGS['ssl_not_conf'], - RESULT_STRINGS['all_non_ssl']], # SSL not configured, all non-SSL allowed +# EXPECTED_RESULTS =\ +# [ + # [RESULT_STRINGS['ssl_not_conf'], + # RESULT_STRINGS['all_non_ssl']], # SSL not configured, all non-SSL allowed - [RESULT_STRINGS['ssl_not_conf'], - RESULT_STRINGS['selected_non_ssl']], # SSL not configured, selected non-SSL allowed + # [RESULT_STRINGS['ssl_not_conf'], + # RESULT_STRINGS['selected_non_ssl']], # SSL not configured, selected non-SSL allowed - [RESULT_STRINGS['ssl_conf'], - RESULT_STRINGS['all_ssl'], - RESULT_STRINGS['all_non_ssl']], # all SSL allowed, all non-SSL allowed + # [RESULT_STRINGS['ssl_conf'], + # RESULT_STRINGS['all_ssl'], + # RESULT_STRINGS['all_non_ssl']], # all SSL allowed, all non-SSL allowed - [RESULT_STRINGS['ssl_conf'], - RESULT_STRINGS['all_ssl'], - RESULT_STRINGS['selected_non_ssl']], # all SSL allowed, selected non-SSL allowed + # [RESULT_STRINGS['ssl_conf'], + # RESULT_STRINGS['all_ssl'], + # RESULT_STRINGS['selected_non_ssl']], # all SSL allowed, selected non-SSL allowed - [RESULT_STRINGS['ssl_conf'], - RESULT_STRINGS['selected_ssl'], - RESULT_STRINGS['all_non_ssl']], # selected SSL allowed, all non-SSL allowed + # [RESULT_STRINGS['ssl_conf'], + # RESULT_STRINGS['selected_ssl'], + # RESULT_STRINGS['all_non_ssl']], # selected SSL allowed, all non-SSL allowed - [RESULT_STRINGS['ssl_conf'], - RESULT_STRINGS['only_selected']] # selected SSL allowed, selected non-SSL allowed - ] # don't change order! + # [RESULT_STRINGS['ssl_conf'], + # RESULT_STRINGS['only_selected']] # selected SSL allowed, selected non-SSL allowed + # ] # don't change order! class TestPostgreSQLFinger(TestCase): def test_is_relevant_exception(self): assert PostgreSQLFinger().is_relevant_exception(IRRELEVANT_EXCEPTION_STRING) is False - for exception_string in EXAMPLE_EXCEPTIONS: + for exception_string in EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS: assert PostgreSQLFinger().is_relevant_exception(exception_string) is True def test_analyze_operational_error(self): host = Mock(['services']) host.services = {} - for idx in range(len(EXAMPLE_EXCEPTIONS)): - with self.subTest(msg=f"Checking result for exception: {EXAMPLE_EXCEPTIONS[idx]}"): - PostgreSQLFinger().analyze_operational_error(host, EXAMPLE_EXCEPTIONS[idx]) - assert host.services['PostgreSQL']['communication_encryption_details'] == ''.join(EXPECTED_RESULTS[idx]) + for exception_string in EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS: + with self.subTest(msg=f"Checking result for exception: {exception_string}"): + PostgreSQLFinger().analyze_operational_error(host, exception_string) + assert host.services['PostgreSQL']['communication_encryption_details'] ==\ + ''.join(EXAMPLE_EXCEPTIONS_WITH_EXPECTED_RESULTS[exception_string])