xref: /aosp_15_r20/external/tink/testing/cross_language/util/test_keys/_test_keys_container_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"""Tests for _test_keys_container."""
15
16from absl.testing import absltest
17
18from tink.proto import tink_pb2
19from util import test_keys
20
21
22class TestKeysContainerTest(absltest.TestCase):
23
24  def test_insert_and_retrieve(self):
25    container = test_keys.TestKeysContainer()
26    container.add_key(
27        template=r"""
28          type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
29          # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
30          # }
31          value: ""
32          output_prefix_type: RAW""",
33        key=r"""
34          key_data {
35            type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
36            # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
37            #   version: 0
38            #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
39            # }
40            value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
41            key_material_type: SYMMETRIC
42          }
43          status: ENABLED
44          key_id: 1349954765
45          output_prefix_type: RAW""")
46    template = tink_pb2.KeyTemplate(
47        type_url='type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key',
48        output_prefix_type=tink_pb2.RAW)
49
50    key = container.get_key(template)
51    self.assertEqual(key.status, tink_pb2.ENABLED)
52    self.assertEqual(key.key_id, 1349954765)
53    self.assertEqual(key.output_prefix_type, tink_pb2.RAW)
54    self.assertEqual(key.key_data.key_material_type, tink_pb2.KeyData.SYMMETRIC)
55    self.assertEqual(
56        key.key_data.value,
57        b'\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611'
58    )
59
60  def test_element_not_present_throws(self):
61    container = test_keys.TestKeysContainer()
62    template = tink_pb2.KeyTemplate(
63        type_url='type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key',
64        output_prefix_type=tink_pb2.RAW)
65    with self.assertRaises(KeyError):
66      container.get_key(template)
67
68  def test_wrong_format_throws(self):
69    valid_template = r"""
70      type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
71      # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
72      # }
73      value: ""
74      output_prefix_type: RAW"""
75    valid_key = r"""
76      key_data {
77        type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
78        # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
79        #   version: 0
80        #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
81        # }
82        value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
83        key_material_type: SYMMETRIC
84      }
85      status: ENABLED
86      key_id: 1349954765
87      output_prefix_type: RAW"""
88    container = test_keys.TestKeysContainer()
89    with self.assertRaises(AssertionError):
90      container.add_key('# Comment\n' + valid_template, valid_key)
91    with self.assertRaises(AssertionError):
92      container.add_key(valid_template, '# Comment\n' + valid_key)
93
94    # To check that the above constants are valid, we insert them
95    container.add_key(valid_template, valid_key)
96
97  def test_multiple_keys_works(self):
98    container = test_keys.TestKeysContainer()
99    container.add_key(
100        template=r"""
101          type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
102          # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
103          # }
104          value: ""
105          output_prefix_type: RAW""",
106        key=r"""
107          key_data {
108            type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
109            # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
110            #   version: 0
111            #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
112            # }
113            value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
114            key_material_type: SYMMETRIC
115          }
116          status: ENABLED
117          key_id: 1349954765
118          output_prefix_type: RAW""")
119    container.add_key(
120        template=r"""
121          type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
122          # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
123          # }
124          value: ""
125          output_prefix_type: TINK""",
126        key=r"""
127          key_data {
128            type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
129            # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
130            #   version: 0
131            #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
132            # }
133            value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
134            key_material_type: SYMMETRIC
135          }
136          status: ENABLED
137          key_id: 1349954765
138          output_prefix_type: TINK""")
139    template = tink_pb2.KeyTemplate(
140        type_url='type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key',
141        output_prefix_type=tink_pb2.RAW)
142    self.assertEqual(
143        container.get_key(template).output_prefix_type, tink_pb2.RAW)
144    template.output_prefix_type = tink_pb2.TINK
145    self.assertEqual(
146        container.get_key(template).output_prefix_type, tink_pb2.TINK)
147
148  def test_insert_same_template_twice_fails(self):
149    container = test_keys.TestKeysContainer()
150    container.add_key(
151        template=r"""
152          type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
153          # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
154          # }
155          value: ""
156          output_prefix_type: RAW""",
157        key=r"""
158          key_data {
159            type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
160            # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
161            #   version: 0
162            #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
163            # }
164            value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
165            key_material_type: SYMMETRIC
166          }
167          status: ENABLED
168          key_id: 1349954765
169          output_prefix_type: RAW""")
170    with self.assertRaises(ValueError):
171      container.add_key(
172          template=r"""
173            type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
174            # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305KeyFormat] {
175            # }
176            value: ""
177            output_prefix_type: RAW""",
178          key=r"""
179            key_data {
180              type_url: "type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key"
181              # value: [type.googleapis.com/google.crypto.tink.ChaCha20Poly1305Key] {
182              #   version: 0
183              #   key_value: "\372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
184              # }
185              value: "\022 \372\022\371\335\313\301\314\253\r\364\376\341o\242\375\000p\317,t\326\373U\332\267\342\212\210\2160\3611"
186              key_material_type: SYMMETRIC
187            }
188            status: ENABLED
189            key_id: 1349954764
190            output_prefix_type: TINK""")
191
192
193if __name__ == '__main__':
194  absltest.main()
195