1# Copyright 2022 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"""Test for load_cleartext_keyset.""" 15from absl.testing import absltest 16 17import tink 18 19from tink import aead 20 21import load_cleartext_keyset 22 23_AES_GCM_KEYSET = r"""{ 24 "key": [{ 25 "keyData": { 26 "keyMaterialType": 27 "SYMMETRIC", 28 "typeUrl": 29 "type.googleapis.com/google.crypto.tink.AesGcmKey", 30 "value": 31 "GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg==" 32 }, 33 "keyId": 294406504, 34 "outputPrefixType": "TINK", 35 "status": "ENABLED" 36 }], 37 "primaryKeyId": 294406504 38 }""" 39 40 41class LoadCleartextKeysetTest(absltest.TestCase): 42 43 def test_load_cleartext_keyset_fails_if_keyset_is_invalid(self): 44 with self.assertRaises(tink.TinkError): 45 load_cleartext_keyset.LoadKeyset('Invlid keyset') 46 47 def test_load_cleartext_keyset_produces_a_valid_keyset(self): 48 aead.register() 49 keyset_handle = load_cleartext_keyset.LoadKeyset(_AES_GCM_KEYSET) 50 # Make sure that we can use this primitive. 51 aead_primitive = keyset_handle.primitive(aead.Aead) 52 plaintext = b'Some plaintext' 53 associated_data = b'Some associated data' 54 ciphertext = aead_primitive.encrypt(plaintext, associated_data) 55 self.assertEqual( 56 aead_primitive.decrypt(ciphertext, associated_data), plaintext) 57 58 59if __name__ == '__main__': 60 absltest.main() 61