xref: /aosp_15_r20/external/tink/python/examples/walkthrough/obtain_and_use_a_primitive_test.py (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 obtain_and_use_a_primitive."""
15from absl.testing import absltest
16
17from tink import aead
18
19import load_cleartext_keyset
20import obtain_and_use_a_primitive
21
22_KMS_AEAD_KEY = r"""{
23  "key": [
24    {
25      "keyData": {
26        "keyMaterialType": "SYMMETRIC",
27        "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey",
28        "value": "GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg=="
29      },
30      "keyId": 294406504,
31      "outputPrefixType": "TINK",
32      "status": "ENABLED"
33    }
34  ],
35  "primaryKeyId": 294406504
36}"""
37
38
39class ObtainAndUseAPrimitiveTest(absltest.TestCase):
40
41  def setUp(self):
42    super().setUp()
43    aead.register()
44
45  def test_obtain_and_use_a_primitive_encrypt_decrypt(self):
46    keyset_handle = load_cleartext_keyset.LoadKeyset(_KMS_AEAD_KEY)
47
48    # Encrypt/decrypt.
49    plaintext = b'Some plaintext'
50    associated_data = b'Some associated data'
51    ciphertext = obtain_and_use_a_primitive.AeadEncrypt(keyset_handle,
52                                                        plaintext,
53                                                        associated_data)
54    self.assertEqual(
55        obtain_and_use_a_primitive.AeadDecrypt(keyset_handle, ciphertext,
56                                               associated_data), plaintext)
57
58
59if __name__ == '__main__':
60  absltest.main()
61