def _create_AS(isd, as_id, is_core=False): as_ = AS(isd=isd, as_id=as_id, as_id_int=as_ids.parse(as_id), is_core=is_core) as_.save() return as_
def _create_AS(isd, as_id, is_core=False): # bypass ASManager.create to avoid initializing keys as_ = AS(isd=isd, as_id=as_id, as_id_int=as_ids.parse(as_id), is_core=is_core) as_.save() return as_
class GenerateKeyTests(TestCase): def setUp(self): self.isd = ISD.objects.create(isd_id=1, label='Test') # bypass ASManager.create to avoid initializing keys as_id = 'ff00:0:110' self.AS = AS(isd=self.isd, as_id=as_id, as_id_int=as_ids.parse(as_id)) self.AS.save() def test_generate_decrypt_key(self): k = Key.objects.create(AS=self.AS, usage=Key.DECRYPT) self.assertIsNotNone(k) self.assertEqual(k.AS_id, self.AS.pk) self.assertEqual(k.version, 1) self.assertEqual(k.usage, Key.DECRYPT) self.assertEqual(k.not_after - k.not_before, DEFAULT_EXPIRATION_AS_KEYS) def test_generate_sign_key(self): k = Key.objects.create(AS=self.AS, usage=Key.SIGNING) self.assertIsNotNone(k) self.assertEqual(k.AS_id, self.AS.pk) self.assertEqual(k.version, 1) self.assertEqual(k.usage, Key.SIGNING) self.assertEqual(k.not_after - k.not_before, DEFAULT_EXPIRATION_AS_KEYS) def test_key_timestamp_format(self): d = datetime(2006, 1, 2, 15, 4, 5) formatted = Key._format_timestamp(d) self.assertEqual(formatted, "2006-01-02 15:04:05+0000") # Ignore microseconds d = datetime(2006, 1, 2, 15, 4, 5, 12345) formatted = Key._format_timestamp(d) self.assertEqual(formatted, "2006-01-02 15:04:05+0000") # Should also work on whatever utcnow returns d = datetime.utcnow() formatted = Key._format_timestamp(d) self.assertIsNotNone( re.match(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\+0000", formatted)) def test_delete_as(self): ks = Key.objects.create(AS=self.AS, usage=Key.SIGNING) kd = Key.objects.create(AS=self.AS, usage=Key.DECRYPT) ko = Key.objects.create(AS=self.AS, usage=Key.TRC_VOTING_OFFLINE) self.AS.delete() self.assertFalse(Key.objects.filter( pk=ks.pk).exists()) # Delete should cascade here, ... self.assertFalse( Key.objects.filter(pk=kd.pk).exists()) # ... and here too. self.assertTrue(Key.objects.filter( pk=ko.pk).exists()) # This one should still exist!