13
13
from certbot .compat import os
14
14
from certbot .tests import util as certbot_test_util
15
15
16
- from certbot_nginx import constants
17
16
from certbot_nginx import obj
18
17
from certbot_nginx import parser
19
18
from certbot_nginx .configurator import _redirect_block_for_domain
@@ -883,12 +882,11 @@ def setUp(self):
883
882
self .config_path , self .config_dir , self .work_dir , self .logs_dir )
884
883
885
884
def _call (self ):
886
- from certbot_nginx . configurator import install_ssl_options_conf
887
- install_ssl_options_conf ( self . config . mod_ssl_conf , self .config .updated_mod_ssl_conf_digest )
885
+ self . config . install_ssl_options_conf ( self . config . mod_ssl_conf ,
886
+ self .config .updated_mod_ssl_conf_digest )
888
887
889
888
def _current_ssl_options_hash (self ):
890
- from certbot_nginx .constants import MOD_SSL_CONF_SRC
891
- return crypto_util .sha256sum (MOD_SSL_CONF_SRC )
889
+ return crypto_util .sha256sum (self .config .mod_ssl_conf_src )
892
890
893
891
def _assert_current_file (self ):
894
892
self .assertTrue (os .path .isfile (self .config .mod_ssl_conf ))
@@ -908,12 +906,32 @@ def test_current_file(self):
908
906
self ._call ()
909
907
self ._assert_current_file ()
910
908
909
+ def _mock_hash_except_ssl_conf_src (self , fake_hash ):
910
+ # Write a bad file in place so that update tests fail if no update occurs.
911
+ # We're going to pretend this file (the currently installed conf file)
912
+ # actually hashes to `fake_hash` for the update tests.
913
+ with open (self .config .mod_ssl_conf , "w" ) as f :
914
+ f .write ("bogus" )
915
+ sha256 = crypto_util .sha256sum
916
+ def _hash (filename ):
917
+ return sha256 (filename ) if filename == self .config .mod_ssl_conf_src else fake_hash
918
+ return _hash
919
+
911
920
def test_prev_file_updates_to_current (self ):
912
921
from certbot_nginx .constants import ALL_SSL_OPTIONS_HASHES
913
- with mock .patch ('certbot.crypto_util.sha256sum' ) as mock_sha256 :
914
- mock_sha256 .return_value = ALL_SSL_OPTIONS_HASHES [0 ]
922
+ with mock .patch ('certbot.crypto_util.sha256sum' ,
923
+ new = self ._mock_hash_except_ssl_conf_src (ALL_SSL_OPTIONS_HASHES [0 ])):
924
+ self ._call ()
925
+ self ._assert_current_file ()
926
+
927
+ def test_prev_file_updates_to_current_old_nginx (self ):
928
+ from certbot_nginx .constants import ALL_SSL_OPTIONS_HASHES , SSL_OPTIONS_HASHES_NEW
929
+ self .config .version = (1 , 5 , 8 )
930
+ with mock .patch ('certbot.crypto_util.sha256sum' ,
931
+ new = self ._mock_hash_except_ssl_conf_src (ALL_SSL_OPTIONS_HASHES [0 ])):
915
932
self ._call ()
916
933
self ._assert_current_file ()
934
+ self .assertTrue (self ._current_ssl_options_hash () not in SSL_OPTIONS_HASHES_NEW )
917
935
918
936
def test_manually_modified_current_file_does_not_update (self ):
919
937
with open (self .config .mod_ssl_conf , "a" ) as mod_ssl_conf :
@@ -922,7 +940,7 @@ def test_manually_modified_current_file_does_not_update(self):
922
940
self ._call ()
923
941
self .assertFalse (mock_logger .warning .called )
924
942
self .assertTrue (os .path .isfile (self .config .mod_ssl_conf ))
925
- self .assertEqual (crypto_util .sha256sum (constants . MOD_SSL_CONF_SRC ),
943
+ self .assertEqual (crypto_util .sha256sum (self . config . mod_ssl_conf_src ),
926
944
self ._current_ssl_options_hash ())
927
945
self .assertNotEqual (crypto_util .sha256sum (self .config .mod_ssl_conf ),
928
946
self ._current_ssl_options_hash ())
@@ -937,7 +955,7 @@ def test_manually_modified_past_file_warns(self):
937
955
self .assertEqual (mock_logger .warning .call_args [0 ][0 ],
938
956
"%s has been manually modified; updated file "
939
957
"saved to %s. We recommend updating %s for security purposes." )
940
- self .assertEqual (crypto_util .sha256sum (constants . MOD_SSL_CONF_SRC ),
958
+ self .assertEqual (crypto_util .sha256sum (self . config . mod_ssl_conf_src ),
941
959
self ._current_ssl_options_hash ())
942
960
# only print warning once
943
961
with mock .patch ("certbot.plugins.common.logger" ) as mock_logger :
@@ -950,6 +968,16 @@ def test_current_file_hash_in_all_hashes(self):
950
968
"Constants.ALL_SSL_OPTIONS_HASHES must be appended"
951
969
" with the sha256 hash of self.config.mod_ssl_conf when it is updated." )
952
970
971
+ def test_old_nginx_version_uses_old_config (self ):
972
+ self .config .version = (1 , 5 , 8 )
973
+ self .assertEqual (os .path .basename (self .config .mod_ssl_conf_src ),
974
+ "options-ssl-nginx-old.conf" )
975
+ self ._call ()
976
+ self ._assert_current_file ()
977
+ self .config .version = (1 , 5 , 9 )
978
+ self .assertEqual (os .path .basename (self .config .mod_ssl_conf_src ),
979
+ "options-ssl-nginx.conf" )
980
+
953
981
954
982
class DetermineDefaultServerRootTest (certbot_test_util .ConfigTestCase ):
955
983
"""Tests for certbot_nginx.configurator._determine_default_server_root."""
0 commit comments