def test_spamc_headers_report(arg): with mock.patch.object(SpamC, 'perform') as mock_perform: msg = 'xxxx' spamc_tcp = SpamC(host='127.0.0.1', port=10060) spamc_tcp.tell(msg, 'report') headers = {'Message-class': 'spam', 'Set': 'local, remote'} mock_perform.assert_called_once_with('TELL', msg, headers)
def test_spamc_tcp_exp3(self): with mock.patch.object(SpamC, 'get_connection') as mock_conn: mock_conn.return_value._s = None mock_conn.return_value.send.side_effect = socket.error('xxxx') spamc_tcp = SpamC(host='127.0.0.1', port=10060) with self.assertRaises(SpamCError): spamc_tcp.ping()
def test_spamc_tcp_exp1(self): with mock.patch.object(SpamC, 'get_connection') as mock_conn: mock_conn.return_value.socket.return_value.makefile\ .return_value.read.return_value = '' spamc_tcp = SpamC(host='127.0.0.1', port=10060) with self.assertRaises(SpamCResponseError): spamc_tcp.ping() mock_conn.return_value.socket.return_value.makefile\ .return_value.read.assert_called_once_with()
def test_spamc_headers_report(arg): with mock.patch.object(SpamC, 'perform') as mock_perform: msg = 'xxxx' spamc_tcp = SpamC( host='127.0.0.1', port=10060) spamc_tcp.tell(msg, 'report') headers = {'Message-class': 'spam', 'Set': 'local, remote'} mock_perform.assert_called_once_with('TELL', msg, headers)
def test_spamc_tcp_exp3(self): with mock.patch.object(SpamC, 'get_connection') as mock_conn: mock_conn.return_value._s = None mock_conn.return_value.send.side_effect = socket.error('xxxx') spamc_tcp = SpamC( host='127.0.0.1', port=10060) with self.assertRaises(SpamCError): spamc_tcp.ping()
def test_spamc_tcp_exp1(self): with mock.patch.object(SpamC, 'get_connection') as mock_conn: mock_conn.return_value.socket.return_value.makefile\ .return_value.read.return_value = '' spamc_tcp = SpamC( host='127.0.0.1', port=10060) with self.assertRaises(SpamCResponseError): spamc_tcp.ping() mock_conn.return_value.socket.return_value.makefile\ .return_value.read.assert_called_once_with()
def setUpClass(cls): cls.using_sa = True cls.tcp_server = return_tcp(10060) t1 = threading.Thread(target=cls.tcp_server.serve_forever) t1.setDaemon(True) t1.start() cls.using_sa = False cls.spamc_tcp = SpamC(host='127.0.0.1', port=10060)
def setUpClass(cls): cls.using_sa = True if os.environ.get('SPAMD_SOCK', None) is None: cls.unix_server = return_unix() t1 = threading.Thread(target=cls.unix_server.serve_forever) t1.setDaemon(True) t1.start() cls.using_sa = False cls.spamc_unix = SpamC( socket_file=os.environ.get('SPAMD_SOCK', 'spamd.sock'))
def setUpClass(cls): gzip = False cls.using_sa = True if os.environ.get('SPAMD_SOCK', None) is None: cls.unix_server = return_unix() t1 = threading.Thread(target=cls.unix_server.serve_forever) t1.setDaemon(True) t1.start() cls.using_sa = False if os.environ.get('SPAMD_COMPRESS', None) and \ os.environ.get('CI', False): gzip = True cls.spamc_unix = SpamC(socket_file=os.environ.get( 'SPAMD_SOCK', 'spamd.sock'), gzip=gzip) path = os.path.dirname(os.path.dirname(__file__)) cls.filename = os.path.join(path, 'examples', 'sample-spam.txt')
def setUpClass(cls): gzip = False cls.using_sa = True if os.environ.get('SPAMD_HOST', None) is None: cls.tcp_server = return_tcp(10050) t1 = threading.Thread(target=cls.tcp_server.serve_forever) t1.setDaemon(True) t1.start() cls.using_sa = False if os.environ.get('SPAMD_COMPRESS', None) and \ os.environ.get('CI', False) is False: gzip = True cls.spamc_tcp = SpamC( host=os.environ.get('SPAMD_HOST', '127.0.0.1'), port=int(os.environ.get('SPAMD_PORT', 10050)), gzip=gzip, compress_level=int(os.environ.get('SPAMD_COMPRESS_LEVEL', 6))) path = os.path.dirname(os.path.dirname(__file__)) cls.filename = os.path.join(path, 'examples', 'sample-spam.txt')
def test_spamc_unix_no_conn(self): spamc_unix = SpamC(socket_file='no-exist-spamd.sock') self.assertRaises(SpamCError, spamc_unix.ping)
def test_spamc_tcp_no_conn(self): spamc_tcp = SpamC(host='127.0.0.1', port=10001) self.assertRaises(SpamCError, spamc_tcp.ping)
def run(): res = SpamC(host=self.host, port=self.port, socket_file=self.socket_file, user='******') func(res, self.filename)
def runit(): """run things""" parser = OptionParser() parser.add_option('-s', '--server', help='The spamassassin spamd server to connect to', dest='server', type='str', default='standalone.home.topdog-software.com') parser.add_option('-p', '--port', help='The spamassassin spamd server port to connect to', dest='port', type='int', default=783) parser.add_option('-u', '--unix-socket', help='The spamassassin spamd unix socket to connect to', dest='socket_path', type='str') parser.add_option('-t', '--tls', help='Use TLS', dest='tls', action='store_true', default=False) parser.add_option('-z', '--use-zlib-compression', help='Use Zlib compression', dest='gzip', action='store_true', default=False) parser.add_option('-l', '--zlib-compression-level', help='Zlib compression level', dest='compress_level', type='choice', choices=[str(val) for val in range(0, 10)], default=6) parser.add_option('-a', '--user', help=('''Username of the user on whose behalf''' '''this scan is being performed'''), dest='user', type='str', default='exim') options, _ = parser.parse_args() sslopts = {} if options.tls: sslopts = dict(ssl_version=PROTOCOL_TLSv1) if options.socket_path and os.path.exists(options.socket_path): options.server = None client = SpamC( options.server, port=options.port, socket_file=options.socket_path, user=options.user, gzip=options.gzip, compress_level=int(options.compress_level), is_ssl=options.tls, **sslopts) pprint.pprint(client.ping()) path = os.path.dirname(__file__) for test in FILES: filename = os.path.join(path, test['name']) print "File => %s" % filename fileobj = open(filename) print "=" * 10, "client.check()" pprint.pprint(client.check(fileobj)) print "=" * 10, "client.symbols()" pprint.pprint(client.symbols(fileobj)) print "=" * 10, "client.report()" pprint.pprint(client.report(fileobj)) print "=" * 10, "client.report_ifspam()" pprint.pprint(client.report_ifspam(fileobj)) print "=" * 10, "client.process()" pprint.pprint(client.process(fileobj)) print "=" * 10, "client.headers()" pprint.pprint(client.headers(fileobj)) print "=" * 10, "client.learn()" pprint.pprint(client.learn(fileobj, test['type'])) print "=" * 10, "client.tell()" pprint.pprint(client.tell(fileobj, 'forget')) print "=" * 10, "client.revoke()" pprint.pprint(client.revoke(fileobj))