1# Copyright 2019 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 15"""Tests for tink.python.tink.testing.helper.""" 16 17import os 18 19from absl.testing import absltest 20from tink import core 21from tink.testing import helper 22 23 24class HelperTest(absltest.TestCase): 25 26 def test_tink_py_testdata_path(self): 27 path = os.path.join(helper.tink_py_testdata_path(), 'gcp/credential.json') 28 with open(path, mode='rt') as f: 29 credential_json = f.read() 30 self.assertNotEmpty(credential_json) 31 32 def test_fake_mac_success(self): 33 mac = helper.FakeMac('Name') 34 mac_value = mac.compute_mac(b'data') 35 mac.verify_mac(mac_value, b'data') 36 37 def test_fake_mac_fail_wrong_data(self): 38 mac = helper.FakeMac('Name') 39 mac_value = mac.compute_mac(b'data') 40 with self.assertRaises(core.TinkError): 41 mac.verify_mac(mac_value, b'wrong data') 42 43 def test_fake_mac_fail_wrong_primitive(self): 44 mac = helper.FakeMac('Name') 45 mac_value = mac.compute_mac(b'data') 46 wrong_mac = helper.FakeMac('Wrong Name') 47 with self.assertRaises(core.TinkError): 48 wrong_mac.verify_mac(mac_value, b'data') 49 50 def test_fake_aead_success(self): 51 aead = helper.FakeAead('Name') 52 ciphertext = aead.encrypt(b'plaintext', b'associated_data') 53 self.assertEqual( 54 aead.decrypt(ciphertext, b'associated_data'), 55 b'plaintext') 56 57 def test_fake_aead_fail_wrong_cipertext(self): 58 aead = helper.FakeAead('Name') 59 with self.assertRaises(core.TinkError): 60 aead.decrypt(b'wrong ciphertext', b'associated_data') 61 62 def test_fake_aead_fail_wrong_associated_data(self): 63 aead = helper.FakeAead('Name') 64 ciphertext = aead.encrypt(b'plaintext', b'associated_data') 65 with self.assertRaises(core.TinkError): 66 aead.decrypt(ciphertext, b'wrong_associated_data') 67 68 def test_fake_aead_fail_wrong_primitive(self): 69 aead = helper.FakeAead('Name') 70 ciphertext = aead.encrypt(b'plaintext', b'associated_data') 71 wrong_aead = helper.FakeAead('Wrong Name') 72 with self.assertRaises(core.TinkError): 73 wrong_aead.decrypt(ciphertext, b'associated_data') 74 75 def test_fake_deterministic_aead_success(self): 76 daead = helper.FakeDeterministicAead('Name') 77 ciphertext = daead.encrypt_deterministically(b'plaintext', 78 b'associated_data') 79 self.assertEqual( 80 daead.decrypt_deterministically(ciphertext, b'associated_data'), 81 b'plaintext') 82 83 def test_fake_deterministic_aead_fail_wrong_cipertext(self): 84 daead = helper.FakeDeterministicAead('Name') 85 with self.assertRaises(core.TinkError): 86 daead.decrypt_deterministically(b'wrong ciphertext', b'associated_data') 87 88 def test_fake_deterministic_aead_fail_wrong_associated_data(self): 89 daead = helper.FakeDeterministicAead('Name') 90 ciphertext = daead.encrypt_deterministically(b'plaintext', 91 b'associated_data') 92 with self.assertRaises(core.TinkError): 93 daead.decrypt_deterministically(ciphertext, b'wrong_associated_data') 94 95 def test_fake_deterministic_aead_fail_wrong_primitive(self): 96 daead = helper.FakeDeterministicAead('Name') 97 ciphertext = daead.encrypt_deterministically(b'plaintext', 98 b'associated_data') 99 wrong_daead = helper.FakeDeterministicAead('Wrong Name') 100 with self.assertRaises(core.TinkError): 101 wrong_daead.decrypt_deterministically(ciphertext, b'associated_data') 102 103 def test_fake_hybrid_success(self): 104 enc = helper.FakeHybridEncrypt('Name') 105 dec = helper.FakeHybridDecrypt('Name') 106 ciphertext = enc.encrypt(b'plaintext', b'context_info') 107 self.assertEqual( 108 dec.decrypt(ciphertext, b'context_info'), 109 b'plaintext') 110 111 def test_fake_hybrid_fail_wrong_context(self): 112 enc = helper.FakeHybridEncrypt('Name') 113 dec = helper.FakeHybridDecrypt('Name') 114 ciphertext = enc.encrypt(b'plaintext', b'context_info') 115 with self.assertRaises(core.TinkError): 116 dec.decrypt(ciphertext, b'other_context_info') 117 118 def test_fake_hybrid_fail_wrong_dec(self): 119 enc = helper.FakeHybridEncrypt('Name') 120 dec = helper.FakeHybridDecrypt('Wrong Name') 121 ciphertext = enc.encrypt(b'plaintext', b'context_info') 122 with self.assertRaises(core.TinkError): 123 dec.decrypt(ciphertext, b'context_info') 124 125 def test_fake_hybrid_fail_wrong_ciphertext(self): 126 dec = helper.FakeHybridDecrypt('Name') 127 with self.assertRaises(core.TinkError): 128 dec.decrypt(b'wrong ciphertext', b'context_info') 129 130 def test_fake_prf_set_success(self): 131 prf_set = helper.FakePrfSet('Name') 132 output_primary = prf_set.primary().compute(b'input', output_length=31) 133 self.assertLen(output_primary, 31) 134 prfs = prf_set.all() 135 self.assertLen(prfs, 1) 136 output = prfs[0].compute(b'input', output_length=31) 137 self.assertLen(output, 31) 138 139 140if __name__ == '__main__': 141 absltest.main() 142