Skip to content

Commit

Permalink
[tst] unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
grindsa committed May 6, 2021
1 parent a837967 commit 52a28ab
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 2 deletions.
13 changes: 11 additions & 2 deletions est_proxyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ def _arg_parse():
def _config_load(debug=None, cfg_file=None):
""" load config from file """
config_dic = config_load(debug, cfg_file=cfg_file)
debug = config_dic.getboolean('DEFAULT', 'debug', fallback=False)
try:
debug = config_dic.getboolean('DEFAULT', 'debug', fallback=False)
except BaseException:
debug = False

svc_dic = {}
if 'Daemon' in config_dic:
svc_dic['Daemon'] = {}
svc_dic['Daemon']['address'] = config_dic.get('Daemon', 'address', fallback=None)
svc_dic['Daemon']['port'] = int(config_dic.get('Daemon', 'port', fallback='1443'))
try:
svc_dic['Daemon']['port'] = int(config_dic.get('Daemon', 'port', fallback='1443'))
except ValueError:
print('defaulting port to 1443')
svc_dic['Daemon']['port'] = 1443

return(debug, svc_dic)

Expand All @@ -46,6 +54,7 @@ def srv_run(logger, server_class=SecureServer, handler_class=ESTSrvHandler, addr
try:
httpd.serve_forever()
except KeyboardInterrupt:
logger.info('srv_run(): keyboard interrupt}'.format(address, port))
pass
httpd.server_close()
logger.info('stopping est_proxy on {0}:{1}'.format(address, port))
Expand Down
3 changes: 3 additions & 0 deletions examples/ca_handler/xca_ca_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ def _kue_generate(self, ku_val=None):
key_usage_list = ['digitalSignature', 'nonRepudiation', 'keyEncipherment', 'dataEncipherment', 'keyAgreement', 'keyCertSign', 'cRLSign', 'encipherOnly', 'decipherOnly']

kuval = int(ku_val)
if kuval == 0:
self.logger.debug('CAhandler._extension_list_generate(): defaulting ku_val to 23')
kuval = 23
kubin = '{0:b}'.format(kuval)[::-1]
ku_list = []
for idx, ele in enumerate(kubin):
Expand Down
114 changes: 114 additions & 0 deletions test/test_est_proxyd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" unittests for helper """
# pylint: disable=C0302, C0415, E0401, R0902, R0904, R0913, R0914, R0915, W0212
import unittest
import configparser
import sys
from unittest.mock import patch, Mock

sys.path.insert(0, '.')
sys.path.insert(1, '..')

class HelperTestCases(unittest.TestCase):
""" test class for helper """
def setUp(self):
""" setup """
import logging
logging.basicConfig(level=logging.CRITICAL)
self.logger = logging.getLogger('test_est')
from est_proxyd import _arg_parse, _config_load, srv_run
self._arg_parse = _arg_parse
self._config_load = _config_load
self.srv_run = srv_run

def tearDown(self):
""" teardown test environment """
# Clean up run after every test method.

def test_001_allways_ok(self):
""" a test that never failes """
self.assertEqual('foo', 'foo')

@patch('os.path.isfile')
def test_002__arg_parse(self, mock_file):
""" a test that never failes """
sys.argv = ['foo', '-c', 'foo.cfg']
mock_file.return_value = True
self._arg_parse()

@patch('os.path.isfile')
def test_003__arg_parse(self, mock_file):
""" a test that never failes """
sys.argv = ['foo', '-c', 'foo.cfg']
mock_file.return_value = False
self._arg_parse()

@patch('est_proxyd.config_load')
def test_002_config_load(self, mock_load_cfg):
""" test _config_load """
mock_load_cfg.return_value = 'foo'
self.assertEqual((False, {}), self._config_load())

@patch('est_proxyd.config_load')
def test_003_config_load(self, mock_load_cfg):
""" test _config_load """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'foo': 'bar'}
mock_load_cfg.return_value = parser
self.assertEqual((False, {}), self._config_load())

@patch('est_proxyd.config_load')
def test_004_config_load(self, mock_load_cfg):
""" test _config_load debug false """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'debug': False}
mock_load_cfg.return_value = parser
self.assertEqual((False, {}), self._config_load())

@patch('est_proxyd.config_load')
def test_005_config_load(self, mock_load_cfg):
""" test _config_load debug true """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'debug': True}
mock_load_cfg.return_value = parser
self.assertEqual((True, {}), self._config_load())

@patch('est_proxyd.config_load')
def test_006_config_load(self, mock_load_cfg):
""" test _config_load address and port int """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'debug': True}
parser['Daemon'] = {'address': 'address', 'port': 1234}
mock_load_cfg.return_value = parser
self.assertEqual((True, {'Daemon': {'address': 'address', 'port': 1234}}), self._config_load())

@patch('est_proxyd.config_load')
def test_007_config_load(self, mock_load_cfg):
""" test _config_load address and port string """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'debug': True}
parser['Daemon'] = {'address': 'address', 'port': '1235'}
mock_load_cfg.return_value = parser
self.assertEqual((True, {'Daemon': {'address': 'address', 'port': 1235}}), self._config_load())

@patch('est_proxyd.config_load')
def test_008_config_load(self, mock_load_cfg):
""" test _config_load address and port string """
parser = configparser.ConfigParser()
parser['DEFAULT'] = {'debug': True}
parser['Daemon'] = {'address': 'address', 'port': 'aaa'}
mock_load_cfg.return_value = parser
self.assertEqual((True, {'Daemon': {'address': 'address', 'port': 1443}}), self._config_load())

def test_009_srv_run(self):
""" test srv_run """
server_class = Mock()
handler_class = Mock()
with self.assertLogs('test_est', level='INFO') as lcm:
self.srv_run(self.logger, server_class, handler_class)
self.assertIn('INFO:test_est:starting est_proxy 0.1.0 on 127.0.0.1:8080', lcm.output)
self.assertIn('INFO:test_est:stopping est_proxy on 127.0.0.1:8080', lcm.output)

if __name__ == '__main__':
unittest.main()
41 changes: 41 additions & 0 deletions test/test_esthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,47 @@ def test_068__init__(self):
self.esthandler.__init__()
self.assertEqual('est_proxy.cfg', self.esthandler.cfg_file)

@patch('est_proxy.est_handler.ESTSrvHandler._post_process')
def test_069_do_post(self, mock_process):
""" test do get """
mock_process.return_value = ['code', 'content_type', 'content_length', 'encoding', 'content']
self.esthandler.client_address = ('127.0.0.1', 8080)
self.esthandler.path = '/'
self.esthandler.requestline = 'requestline'
self.esthandler.request_version = 'HTTP/0.9'
self.esthandler.rfile = Mock()
self.esthandler.wfile = Mock()
self.esthandler.headers = {'Content-Length': 15}
self.assertFalse(self.esthandler.do_POST())

@patch('est_proxy.est_handler.ESTSrvHandler._post_process')
def test_070_do_post(self, mock_process):
""" test do get """
mock_process.return_value = ['code', 'content_type', 'content_length', 'encoding', 'content']
self.esthandler.client_address = ('127.0.0.1', 8080)
self.esthandler.path = '/'
self.esthandler.requestline = 'requestline'
self.esthandler.request_version = 'HTTP/0.9'
self.esthandler.rfile = Mock()
self.esthandler.wfile = Mock()
self.esthandler.headers = {'Content-Length': 0}
self.assertFalse(self.esthandler.do_POST())

@patch('est_proxy.est_handler.ESTSrvHandler._post_process')
def test_071_do_post(self, mock_process):
""" test do get """
mock_process.return_value = ['code', 'content_type', 'content_length', 'encoding', 'content']
self.esthandler.client_address = ('127.0.0.1', 8080)
self.esthandler.path = '/'
self.esthandler.requestline = 'requestline'
self.esthandler.request_version = 'HTTP/0.9'
self.esthandler.rfile = Mock()
self.esthandler.rfile.readline = Mock()
self.esthandler.rfile.readline.side_effect = [b'11', b'12345678901234567', b'', b'0', b'']
self.esthandler.wfile = Mock()
self.esthandler.headers = {'Transfer-Encoding': 'chunked'}
self.assertFalse(self.esthandler.do_POST())


if __name__ == '__main__':
unittest.main()

0 comments on commit 52a28ab

Please sign in to comment.