certbot/letsencrypt-apache/letsencrypt_apache/tests/dvsni_test.py

138 lines
4.6 KiB
Python
Raw Normal View History

2015-05-10 06:47:58 -04:00
"""Test for letsencrypt_apache.dvsni."""
2015-01-09 08:30:15 -05:00
import unittest
import shutil
import mock
from letsencrypt.plugins import common_test
2015-01-09 08:30:15 -05:00
2015-07-19 05:22:10 -04:00
from letsencrypt_apache import obj
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.tests import util
2015-01-09 08:30:15 -05:00
2015-01-24 08:12:45 -05:00
class DvsniPerformTest(util.ApacheTest):
2015-01-17 05:29:29 -05:00
"""Test the ApacheDVSNI challenge."""
2015-01-09 08:30:15 -05:00
2015-08-05 18:39:31 -04:00
auth_key = common_test.DvsniTest.auth_key
achalls = common_test.DvsniTest.achalls
2015-07-19 19:48:27 -04:00
def setUp(self): # pylint: disable=arguments-differ
2015-01-24 08:12:45 -05:00
super(DvsniPerformTest, self).setUp()
2015-01-09 08:30:15 -05:00
2015-07-19 05:22:10 -04:00
config = util.get_apache_configurator(
self.config_path, self.config_dir, self.work_dir)
config.config.dvsni_port = 443
2015-01-09 08:30:15 -05:00
2015-05-10 06:47:58 -04:00
from letsencrypt_apache import dvsni
2015-01-09 08:30:15 -05:00
self.sni = dvsni.ApacheDvsni(config)
def tearDown(self):
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.config_dir)
shutil.rmtree(self.work_dir)
2015-01-10 01:25:36 -05:00
def test_perform0(self):
resp = self.sni.perform()
2015-03-25 13:46:22 -04:00
self.assertEqual(len(resp), 0)
2015-01-10 01:25:36 -05:00
2015-07-30 02:40:07 -04:00
@mock.patch("letsencrypt.le_util.exe_exists")
@mock.patch("letsencrypt.le_util.run_script")
def test_perform1(self, _, mock_exists):
mock_register = mock.Mock()
self.sni.configurator.reverter.register_undo_command = mock_register
2015-07-30 02:40:07 -04:00
mock_exists.return_value = True
self.sni.configurator.parser.update_runtime_variables = mock.Mock()
2015-07-19 05:22:10 -04:00
2015-02-13 17:37:45 -05:00
achall = self.achalls[0]
self.sni.add_chall(achall)
2015-08-05 18:39:31 -04:00
response = self.achalls[0].gen_response(self.auth_key)
mock_setup_cert = mock.MagicMock(return_value=response)
2015-01-27 01:25:08 -05:00
# pylint: disable=protected-access
self.sni._setup_challenge_cert = mock_setup_cert
responses = self.sni.perform()
# Make sure that register_undo_command was called into temp directory.
self.assertEqual(True, mock_register.call_args[0][0])
2015-02-13 17:37:45 -05:00
mock_setup_cert.assert_called_once_with(achall)
2015-01-27 01:25:08 -05:00
# Check to make sure challenge config path is included in apache config.
2015-01-09 08:30:15 -05:00
self.assertEqual(
len(self.sni.configurator.parser.find_dir(
2015-07-19 05:22:10 -04:00
"Include", self.sni.challenge_conf)), 1)
2015-01-10 01:25:36 -05:00
self.assertEqual(len(responses), 1)
2015-08-05 18:39:31 -04:00
self.assertEqual(responses[0], response)
2015-01-10 01:25:36 -05:00
2015-01-27 01:25:08 -05:00
def test_perform2(self):
2015-07-19 05:22:10 -04:00
# Avoid load module
self.sni.configurator.parser.modules.add("ssl_module")
2015-08-05 18:39:31 -04:00
acme_responses = []
2015-02-13 17:37:45 -05:00
for achall in self.achalls:
self.sni.add_chall(achall)
2015-08-05 18:39:31 -04:00
acme_responses.append(achall.gen_response(self.auth_key))
2015-01-10 01:25:36 -05:00
2015-08-05 18:39:31 -04:00
mock_setup_cert = mock.MagicMock(side_effect=acme_responses)
2015-01-27 01:25:08 -05:00
# pylint: disable=protected-access
self.sni._setup_challenge_cert = mock_setup_cert
2015-01-10 01:25:36 -05:00
2015-08-05 18:39:31 -04:00
sni_responses = self.sni.perform()
2015-01-10 01:25:36 -05:00
2015-01-27 01:25:08 -05:00
self.assertEqual(mock_setup_cert.call_count, 2)
2015-01-10 01:25:36 -05:00
2015-01-27 01:25:08 -05:00
# Make sure calls made to mocked function were correct
self.assertEqual(
2015-02-13 17:37:45 -05:00
mock_setup_cert.call_args_list[0], mock.call(self.achalls[0]))
2015-01-27 01:25:08 -05:00
self.assertEqual(
2015-02-13 17:37:45 -05:00
mock_setup_cert.call_args_list[1], mock.call(self.achalls[1]))
2015-01-10 01:25:36 -05:00
self.assertEqual(
len(self.sni.configurator.parser.find_dir(
2015-01-10 01:25:36 -05:00
"Include", self.sni.challenge_conf)),
1)
2015-08-05 18:39:31 -04:00
self.assertEqual(len(sni_responses), 2)
2015-01-27 17:53:28 -05:00
for i in xrange(2):
2015-08-05 18:39:31 -04:00
self.assertEqual(sni_responses[i], acme_responses[i])
2015-01-09 08:30:15 -05:00
def test_mod_config(self):
2015-08-05 18:39:31 -04:00
z_domains = []
2015-02-13 17:37:45 -05:00
for achall in self.achalls:
self.sni.add_chall(achall)
2015-08-05 18:39:31 -04:00
z_domain = achall.gen_response(self.auth_key).z_domain
z_domains.append(set([z_domain]))
2015-07-19 05:22:10 -04:00
self.sni._mod_config() # pylint: disable=protected-access
self.sni.configurator.save()
2015-01-09 08:30:15 -05:00
self.sni.configurator.parser.find_dir(
"Include", self.sni.challenge_conf)
vh_match = self.sni.configurator.aug.match(
2015-01-09 08:30:15 -05:00
"/files" + self.sni.challenge_conf + "//VirtualHost")
vhs = []
for match in vh_match:
# pylint: disable=protected-access
vhs.append(self.sni.configurator._create_vhost(match))
2015-01-09 08:30:15 -05:00
self.assertEqual(len(vhs), 2)
for vhost in vhs:
2015-07-19 05:22:10 -04:00
self.assertEqual(vhost.addrs, set([obj.Addr.fromstring("*:443")]))
2015-07-22 05:05:01 -04:00
names = vhost.get_names()
2015-08-05 18:39:31 -04:00
self.assertTrue(names in z_domains)
2015-01-24 08:12:45 -05:00
2015-07-24 20:05:25 -04:00
def test_get_dvsni_addrs_default(self):
self.sni.configurator.choose_vhost = mock.Mock(
return_value=obj.VirtualHost(
"path", "aug_path", set([obj.Addr.fromstring("_default_:443")]),
False, False)
)
self.assertEqual(
set([obj.Addr.fromstring("*:443")]),
self.sni.get_dvsni_addrs(self.achalls[0]))
2015-01-24 08:12:45 -05:00
if __name__ == "__main__":
unittest.main() # pragma: no cover