def test_primary_returns_primary(self): primitive_set = core.new_primitive_set(mac.Mac) key = helper.fake_key(key_id=1) fake_mac = helper.FakeMac('FakeMac') entry = primitive_set.add_primitive(fake_mac, key) primitive_set.set_primary(entry) entry = primitive_set.primary() self.assertEqual(fake_mac, entry.primitive) self.assertEqual(tink_pb2.ENABLED, entry.status) self.assertEqual(crypto_format.output_prefix(key), entry.identifier)
def test_primitive_from_identifier_returns_entry(self): primitive_set = core.new_primitive_set(mac.Mac) key = helper.fake_key(key_id=1) fake_mac = helper.FakeMac('FakeMac') primitive_set.add_primitive(fake_mac, key) ident = crypto_format.output_prefix(key) entries = primitive_set.primitive_from_identifier(ident) self.assertLen(entries, 1) entry = entries[0] self.assertEqual(fake_mac, entry.primitive) self.assertEqual(tink_pb2.ENABLED, entry.status) self.assertEqual(ident, entry.identifier)
def test_same_key_id_but_different_prefix_type(self): primitive_set = core.new_primitive_set(mac.Mac) key1 = helper.fake_key(key_id=1, output_prefix_type=tink_pb2.TINK) fake_mac1 = helper.FakeMac('FakeMac1') primitive_set.add_primitive(fake_mac1, key1) key2 = helper.fake_key(key_id=1, output_prefix_type=tink_pb2.LEGACY) fake_mac2 = helper.FakeMac('FakeMac2') primitive_set.add_primitive(fake_mac2, key2) entries1 = primitive_set.primitive(key1) self.assertLen(entries1, 1) self.assertEqual(fake_mac1, entries1[0].primitive) self.assertEqual(tink_pb2.ENABLED, entries1[0].status) self.assertEqual(crypto_format.output_prefix(key1), entries1[0].identifier) entries2 = primitive_set.primitive(key2) self.assertLen(entries2, 1) self.assertEqual(fake_mac2, entries2[0].primitive) self.assertEqual(tink_pb2.ENABLED, entries2[0].status) self.assertEqual(crypto_format.output_prefix(key2), entries2[0].identifier)
def add_primitive(self, primitive: P, key: tink_pb2.Keyset.Key) -> Entry: """Adds a new primitive and key entry to the set, and returns the entry.""" if not isinstance(primitive, self._primitive_class): raise tink_error.TinkError( 'The primitive is not an instance of {}'.format( self._primitive_class)) identifier = crypto_format.output_prefix(key) entry = Entry(primitive, identifier, key.status, key.output_prefix_type) entries = self._primitives.setdefault(identifier, []) entries.append(entry) return entry
def test_same_key_id_and_prefix_type(self): primitive_set = core.new_primitive_set(mac.Mac) key1 = helper.fake_key(key_id=1, status=tink_pb2.ENABLED) fake_mac1 = helper.FakeMac('FakeMac1') primitive_set.add_primitive(fake_mac1, key1) key2 = helper.fake_key(key_id=1, status=tink_pb2.DISABLED) fake_mac2 = helper.FakeMac('FakeMac2') primitive_set.add_primitive(fake_mac2, key2) expected_ident = crypto_format.output_prefix(key1) entries = primitive_set.primitive(key1) self.assertLen(entries, 2) self.assertEqual(fake_mac1, entries[0].primitive) self.assertEqual(fake_mac2, entries[1].primitive) self.assertEqual(tink_pb2.ENABLED, entries[0].status) self.assertEqual(tink_pb2.DISABLED, entries[1].status) self.assertEqual(expected_ident, entries[0].identifier) self.assertEqual(expected_ident, entries[1].identifier) self.assertLen(primitive_set.primitive(key2), 2)
def primitive(self, key: tink_pb2.Keyset.Key) -> List[P]: """Returns a copy of the list of primitives for a given identifier.""" return self.primitive_from_identifier(crypto_format.output_prefix(key))