1# Copyright 2020 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for tink.testing.cross_language.supported_key_types.""" 15 16 17from absl.testing import absltest 18from tink import aead 19from tink import mac 20 21from tink.proto import tink_pb2 22import tink_config 23from util import test_keys 24from util import utilities 25 26 27def all_key_template_names(): 28 for _, names in utilities.KEY_TEMPLATE_NAMES.items(): 29 for name in names: 30 yield name 31 32 33def setUpModule(): 34 aead.register() 35 36 37class SupportedKeyTypesTest(absltest.TestCase): 38 39 def test_template_types_subset(self): 40 """Tests that all key types which have a template are in all_key_types().""" 41 self.assertContainsSubset( 42 set(utilities.KEY_TEMPLATE_NAMES.keys()), 43 set(tink_config.all_key_types())) 44 45 def test_all_key_templates_present(self): 46 self.assertEqual( 47 list(all_key_template_names()), 48 list(utilities.KEY_TEMPLATE.keys())) 49 50 def test_supported_lang_by_template_name_all_present(self): 51 self.assertEqual( 52 list(all_key_template_names()), 53 list(utilities.SUPPORTED_LANGUAGES_BY_TEMPLATE_NAME.keys())) 54 55 def test_supported_langauges_by_template_name(self): 56 self.assertEqual( 57 utilities.SUPPORTED_LANGUAGES_BY_TEMPLATE_NAME[ 58 'ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM'], 59 ['cc', 'java', 'go', 'python']) 60 61 def test_tinkey_template_names_for(self): 62 self.assertEqual( 63 list(utilities.tinkey_template_names_for(mac.Mac)), [ 64 'AES_CMAC', 'HMAC_SHA256_128BITTAG', 'HMAC_SHA256_256BITTAG', 65 'HMAC_SHA512_256BITTAG', 'HMAC_SHA512_512BITTAG' 66 ]) 67 68 def test_key_types_in_keyset_single_key(self): 69 aes_gcm_keyset = test_keys.new_or_stored_keyset( 70 aead.aead_key_templates.AES128_GCM) 71 self.assertEqual( 72 utilities.key_types_in_keyset(aes_gcm_keyset), ['AesGcmKey']) 73 74 def test_key_types_in_keyset_multiple_keys(self): 75 key1 = test_keys.new_or_stored_key(aead.aead_key_templates.AES128_GCM) 76 key2 = test_keys.new_or_stored_key(aead.aead_key_templates.AES256_GCM) 77 key3 = test_keys.new_or_stored_key(aead.aead_key_templates.AES128_EAX) 78 keyset = tink_pb2.Keyset(key=[key1, key2, key3], primary_key_id=key1.key_id) 79 self.assertEqual( 80 utilities.key_types_in_keyset(keyset.SerializeToString()), 81 ['AesGcmKey', 'AesGcmKey', 'AesEaxKey']) 82 83if __name__ == '__main__': 84 absltest.main() 85