opennel-pymanager/tests/test_certificate.py

189 lines
8.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# create certificate (use for test)
# Copyright (C) 2017 AleaJactaEst
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import unittest
import tempfile
try:
import pymanager.certificate as cert
except ImportError:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pymanager.certificate as cert
class TestCertificate(unittest.TestCase):
def setUp(self):
self.openssl = '/usr/bin/openssl'
self.size_root = 4096
self.size_appli = 4096
self.size_child = 2048
self.passroot = 'BadPasswordRoot'
self.passappli = 'BadPasswordApplication'
self.country_name = 'FR'
self.state_or_province_name = 'France'
self.locality_name = 'Paris'
self.organization_name = 'OpenNeL'
self.common_name = 'OpenNeL'
def testInitialize(self):
logfile = tempfile.NamedTemporaryFile(suffix=".log")
loglevel = 'DEBUG'
show_log_console = True
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
#ele = pymanager.certificate.Certificate()
cert.root(logfile,
loglevel,
show_log_console,
workdir_cert_root,
workdir_cert_appli,
self.openssl,
self.size_root,
self.size_appli,
self.size_child,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name)
self.assertTrue(True)
def testMain(self):
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
args=['--workdir-cert-root', workdir_cert_root, '--workdir-cert-appli', workdir_cert_appli]
cert.main(args)
self.assertTrue(True)
def testErrorLogLevel(self):
logfile = tempfile.NamedTemporaryFile(suffix=".log")
loglevel = 'BADVALUE'
show_log_console = False
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
with self.assertRaises(ValueError):
cert.root(logfile,
loglevel,
show_log_console,
workdir_cert_root,
workdir_cert_appli,
self.openssl,
self.size_root,
self.size_appli,
self.size_child,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name)
def testErrorCreateNoneDirectory(self):
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
certificate = cert.Certificate(self.openssl,
workdir_cert_root,
workdir_cert_appli,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name,
self.size_root,
self.size_appli,
self.size_child
)
with self.assertRaises(TypeError):
certificate.directory_create(None)
def testErrorCreateBadDirectory(self):
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
certificate = cert.Certificate(self.openssl,
workdir_cert_root,
workdir_cert_appli,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name,
self.size_root,
self.size_appli,
self.size_child
)
with self.assertRaises(FileNotFoundError):
certificate.directory_create("")
def testErrorSendCommandOpenssl(self):
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
certificate = cert.Certificate("false",
workdir_cert_root,
workdir_cert_appli,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name,
self.size_root,
self.size_appli,
self.size_child
)
with self.assertRaises(RuntimeError):
certificate.send_command_openssl("")
def testError2SendCommandOpenssl(self):
workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-')
workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-')
certificate = cert.Certificate(workdir_cert_root,
workdir_cert_root,
workdir_cert_appli,
self.passroot,
self.passappli,
self.country_name,
self.state_or_province_name,
self.locality_name,
self.organization_name,
self.common_name,
self.size_root,
self.size_appli,
self.size_child
)
with self.assertRaises(PermissionError):
certificate.send_command_openssl("")
if __name__ == '__main__':
unittest.main()