def test_parse_cert(self, mock_popen): mock_popen.return_value = any_popen(b"any-cert") cert = CertParser.parse_cert("any-file-path") assert_popen_called_once_with(mock_popen, "keytool -printcert -file any-file-path") self.assertEqual("any-cert", cert)
def test_parse_fails_when_keytool_fails(self, mock_file_parser, mock_popen): mock_file_parser.return_value = any_file_parser(file=any_file()) mock_popen.return_value = any_popen(b"keytool error") with self.assertRaises(CertParsingError): self.sut.parse("any-file-path", "any-file-name") assert_popen_called_once_with(mock_popen, "keytool -printcert -file any-file-path")
def test_parse_strings(self, mock_popen): mock_popen.return_value = any_popen( b"1-any-string\n2-any-other-string\n0-yet-another-string") strings = DexParser.parse_strings("any-file-path") assert_popen_called_once_with(mock_popen, "strings any-file-path") # NOTE: the strings are returned alphabetically ordered self.assertEqual([ "0-yet-another-string", "1-any-string", "2-any-other-string", ], strings)
def test_parse(self, mock_file_parser, mock_popen, mock_uri_signature, mock_shell_signature): file = any_file() mock_parser_instance = any_file_parser(file=file) mock_file_parser.return_value = mock_parser_instance mock_popen.return_value = any_popen( b"any-string\nany-url\nany-command") mock_uri_signature.return_value = self.any_signature( matches=[(None, False), ("any-url", True), (None, False)]) mock_shell_signature.return_value = self.any_signature( matches=[(None, False), (None, False), ("any-command", True)]) dex = DexParser().parse("any-file-path", "any-file-name") assert_file_parser_called_once_with(mock_parser_instance, filepath="any-file-path", filename="any-file-name") assert_popen_called_once_with(mock_popen, "strings any-file-path") assert_file_equal(self, expected=file, actual=dex) self.assertEqual(["any-command", "any-string", "any-url"], dex.get_strings()) self.assertEqual(["any-url"], dex.get_urls()) self.assertEqual(["any-command"], dex.get_shell_commands()) self.assertEqual([], dex.get_custom_signatures())
def test_init(self, mock_file_parser, mock_get_localzone, mock_popen): file = any_file() mock_parser_instance = any_file_parser(file=file) mock_file_parser.return_value = mock_parser_instance mock_popen.return_value = any_popen( response=b"Owner: CN=OwnerName, OU=OwnerUnit, O=OwnerOrganization, L=OwnerCity, ST=OwnerState, C=OwnerCountry\n" \ b"Issuer: CN=IssuerName, OU=IssuerUnit, O=IssuerOrganization, L=IssuerCity, ST=IssuerState, C=IssuerCountry\n" \ b"Serial number: 558e7595\n" \ b"Valid from: Sat Jun 27 12:06:13 CEST 2015 until: Tue Feb 26 11:06:13 CET 2515\n" \ b"Certificate fingerprints:\n" \ b"\t MD5: 90:22:EF:0C:DB:C3:78:87:7B:C3:A3:6C:5A:68:E6:45\n" \ b"\t SHA1: 5A:C0:6C:32:63:7F:5D:BE:CA:F9:38:38:4C:FA:FF:ED:20:52:43:B6\n" \ b"\t SHA256: E5:15:CC:BC:5E:BF:B2:9D:A6:13:03:63:CF:19:33:FA:CE:AF:DC:ED:5D:2F:F5:98:7C:CE:37:13:64:4A:CF:77\n" \ b"Signature algorithm name: SHA1withRSA\n" \ b"Subject Public Key Algorithm: 1024-bit RSA key\n" \ b"Version: 3" ) mock_get_localzone.return_value.localize.side_effect = ValueError() cert = self.sut.parse("any-file-path", "any-file-name") assert_file_parser_called_once_with(mock_parser_instance, filepath="any-file-path", filename="any-file-name") assert_popen_called_once_with(mock_popen, "keytool -printcert -file any-file-path") assert_file_equal(self, expected=file, actual=cert) self.assertEqual("558e7595", cert.get_serial_number()) self.assertEqual( CertValidity(valid_from="Sat Jun 27 12:06:13 CEST 2015", valid_to="Tue Feb 26 11:06:13 CET 2515"), cert.get_validity() ) self.assertEqual( CertFingerprint( md5="90:22:EF:0C:DB:C3:78:87:7B:C3:A3:6C:5A:68:E6:45", sha1="5A:C0:6C:32:63:7F:5D:BE:CA:F9:38:38:4C:FA:FF:ED:20:52:43:B6", sha256="E5:15:CC:BC:5E:BF:B2:9D:A6:13:03:63:CF:19:33:FA:CE:AF:DC:ED:5D:2F:F5:98:7C:CE:37:13:64:4A:CF:77", signature="SHA1withRSA", version="3" ), cert.get_fingerprint() ) self.assertEqual( CertParticipant( name="OwnerName", email="", unit="OwnerUnit", organization="OwnerOrganization", city="OwnerCity", state="OwnerState", country="OwnerCountry", domain="" ), cert.get_owner() ) self.assertEqual( CertParticipant( name="IssuerName", email="", unit="IssuerUnit", organization="IssuerOrganization", city="IssuerCity", state="IssuerState", country="IssuerCountry", domain="" ), cert.get_issuer() )