示例#1
0
 def test_set_policies(self):
     invalid_policy = {'mode': 'none'}
     with self.assertRaises(util.ConfigError):
         policy.Config().policies = {'invalid': invalid_policy}
     conf = policy.Config()
     conf.policies = {'valid': {}}
     self.assertEqual(
         conf.get_policy_for('valid').min_tls_version, 'TLSv1.2')
示例#2
0
 def test_set_author(self):
     with self.assertRaises(util.ConfigError):
         policy.Config().load_from_dict({'author': 0})
     with self.assertRaises(util.ConfigError):
         policy.Config().author = 0
     conf = policy.Config()
     conf.author = "me"
     self.assertEqual(conf.author, "me")
示例#3
0
def update(remote_url=constants.POLICY_REMOTE_URL,
           filename=constants.POLICY_LOCAL_FILE):
    """ Fetches and updates local copy of the policy file with the remote file,
    if local copy is outdated. """
    data = _get_remote_data(remote_url)
    remote_config = policy.Config()
    remote_config.load_from_dict(json.loads(data))
    local_config = policy.Config(constants.POLICY_LOCAL_FILE)
    local_config.load()
    if _should_replace(local_config, remote_config):
        with open(filename, 'w+') as handle:
            handle.write(data)
示例#4
0
 def test_timestamp_and_exipres_required(self):
     with self.assertRaises(util.ConfigError):
         policy.Config().load_from_dict(
             {'expires': datetime.datetime.now()})
     with self.assertRaises(util.ConfigError):
         policy.Config().load_from_dict(
             {'timestamp': datetime.datetime.now()})
     conf = policy.Config()
     conf.load_from_dict({
         'timestamp': datetime.datetime.now(),
         'expires': datetime.datetime.now()
     })
     self.assertTrue(isinstance(conf.timestamp, datetime.datetime))
     self.assertTrue(isinstance(conf.expires, datetime.datetime))
示例#5
0
 def test_set_pins_for_policy(self):
     conf = policy.Config()
     conf.pinsets = {'valid': 'lol a pin'}
     with self.assertRaises(util.ConfigError):
         conf.policies = {'invalid': {'pin': 'invalid'}}
     conf.policies = {'valid': {'pin': 'valid'}}
     self.assertEqual(conf.get_policy_for('valid').pin, 'valid')
示例#6
0
 def test_set_aliased_policy(self):
     conf = policy.Config()
     conf.policy_aliases = {'valid': {}}
     with self.assertRaises(util.ConfigError):
         conf.policies = {'invalid': {'policy-alias': 'invalid'}}
     conf.policies = {'valid': {'policy-alias': 'valid'}}
     self.assertEqual(
         conf.get_policy_for('valid').min_tls_version, 'TLSv1.2')
示例#7
0
 def test_merge_keeps_old_settings(self):
     conf2 = policy.Config()
     conf2.author = "EFF"
     updated_timestamp = self.conf.expires - datetime.timedelta(days=1)
     conf2.timestamp = updated_timestamp
     new_conf = self.conf.merge(conf2)
     self.assertEqual(new_conf.author, "EFF")
     self.assertTrue(new_conf.timestamp is not None)
     self.assertEqual(new_conf.timestamp, updated_timestamp)
示例#8
0
 def test_no_aliasing_in_alias(self):
     conf = policy.Config()
     with self.assertRaises(util.ConfigError):
         conf.policy_aliases = {
             'valid': {},
             'valid2': {
                 'policy-alias': 'valid'
             }
         }
示例#9
0
 def test_generate(self):
     from starttls_policy import policy
     conf = policy.Config()
     conf.load_from_dict(json.loads(test_json))
     generator = configure.PostfixGenerator("./")
     result = generator._generate(conf)  # pylint: disable=protected-access
     self.assertEqual(
         result, "# .testing.example-recipient.com "
         "undefined due to testing policy\n"
         ".valid.example-recipient.com    "
         "secure match=.valid.example-recipient.com")
示例#10
0
 def test_iter_policies_aliased(self):
     conf = policy.Config()
     conf.policy_aliases = {'valid': {'tls-report': 'https://tls.report'}}
     conf.policies = {
         'valid1': {
             'policy-alias': 'valid'
         },
         'valid2': {
             'policy-alias': 'valid'
         }
     }
     for val in conf:
         self.assertEqual(conf[val].tls_report, 'https://tls.report')
示例#11
0
 def setUp(self):
     self.conf = policy.Config()
     self.conf.author = "EFF"
     self.conf.expires = datetime.datetime.now()
     self.conf.timestamp = datetime.datetime.now()
     self.sample_policy = {
         'mxs': ['eff.org', '.eff.org'],
         'mode': 'testing'
     }
     self.other_policy = {
         'mxs': ['example.com', '.example.com'],
         'mode': 'enforce'
     }
     # If uncommented, the tests fail. TODO figure out why
     self.conf.policies = {'eff.org': self.sample_policy}
示例#12
0
 def test_update_drops_old_policies(self):
     conf2 = policy.Config()
     conf2.policies = {'example.com': self.other_policy}
     new_conf = self.conf.update(conf2)
     self.assertTrue('example.com' in new_conf.policies)
     self.assertFalse('eff.org' in new_conf.policies)
示例#13
0
 def test_merge_keeps_old_policies(self):
     conf2 = policy.Config()
     conf2.policies = {'example.com': self.other_policy}
     new_conf = self.conf.merge(conf2)
     self.assertTrue('example.com' in new_conf.policies)
     self.assertTrue('eff.org' in new_conf.policies)
示例#14
0
 def test_update_drops_old_settings(self):
     conf2 = policy.Config()
     conf2.author = "EFF"
     new_conf = self.conf.update(conf2)
     self.assertEqual(new_conf.author, "EFF")
     self.assertEqual(new_conf.timestamp, None)
示例#15
0
 def test_iter_policies(self):
     conf = policy.Config()
     sample_policy = {'tls-report': 'https://tls.report'}
     conf.policies = {'valid1': sample_policy, 'valid2': sample_policy}
     for val in conf:
         self.assertEqual(conf[val].tls_report, 'https://tls.report')
示例#16
0
 def _load_config(self):
     if self._policy_config is None:
         self._policy_config = policy.Config(filename=self._policy_filename)
         self._policy_config.load()
     return self._policy_config