diff options
author | juerg <juerg@google.com> | 2020-10-21 03:20:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2020-10-21 03:20:52 -0700 |
commit | e16a9f40a7f6d20d523ede3970ed37ba992d4b12 (patch) | |
tree | 1442deb77474dac64e9614bf13fb6d08966926db | |
parent | a942b2eaa2515c380833a42231d22ae17dfa17cd (diff) | |
download | tink-e16a9f40a7f6d20d523ede3970ed37ba992d4b12.tar.gz |
Rename get_tink_src_path to tink_root_path, and make it a bit more general.
PiperOrigin-RevId: 338231809
-rw-r--r-- | python/tink/integration/awskms/_aws_kms_aead_test.py | 4 | ||||
-rw-r--r-- | python/tink/integration/awskms/_aws_kms_client_test.py | 2 | ||||
-rw-r--r-- | python/tink/integration/gcpkms/_gcp_kms_aead_test.py | 2 | ||||
-rw-r--r-- | python/tink/integration/gcpkms/_gcp_kms_client_test.py | 2 | ||||
-rw-r--r-- | python/tink/testing/BUILD.bazel | 3 | ||||
-rw-r--r-- | python/tink/testing/helper.py | 28 | ||||
-rw-r--r-- | python/tink/testing/helper_test.py | 8 |
7 files changed, 33 insertions, 16 deletions
diff --git a/python/tink/integration/awskms/_aws_kms_aead_test.py b/python/tink/integration/awskms/_aws_kms_aead_test.py index 9223aeb55..9197c73b6 100644 --- a/python/tink/integration/awskms/_aws_kms_aead_test.py +++ b/python/tink/integration/awskms/_aws_kms_aead_test.py @@ -24,9 +24,9 @@ from tink import core from tink.integration import awskms from tink.testing import helper -CREDENTIAL_PATH = os.path.join(helper.get_tink_src_path(), +CREDENTIAL_PATH = os.path.join(helper.tink_root_path(), 'testdata/aws_credentials_cc.txt') -BAD_CREDENTIALS_PATH = os.path.join(helper.get_tink_src_path(), +BAD_CREDENTIALS_PATH = os.path.join(helper.tink_root_path(), 'testdata/bad_aws_credentials_cc.txt') KEY_URI = 'aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f' BAD_KEY_URI = 'gcp-kms://projects/tink-test-infrastructure/locations/global/keyRings/unit-and-integration-testing/cryptoKeys/aead-key' diff --git a/python/tink/integration/awskms/_aws_kms_client_test.py b/python/tink/integration/awskms/_aws_kms_client_test.py index 0f7c15060..5c133d2e6 100644 --- a/python/tink/integration/awskms/_aws_kms_client_test.py +++ b/python/tink/integration/awskms/_aws_kms_client_test.py @@ -24,7 +24,7 @@ from tink import core from tink.integration import awskms from tink.testing import helper -CREDENTIAL_PATH = os.path.join(helper.get_tink_src_path(), +CREDENTIAL_PATH = os.path.join(helper.tink_root_path(), 'testdata/aws_credentials_cc.txt') KEY_URI = 'aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f' BAD_KEY_URI = 'gcp-kms://projects/tink-test-infrastructure/locations/global/keyRings/unit-and-integration-testing/cryptoKeys/aead-key' diff --git a/python/tink/integration/gcpkms/_gcp_kms_aead_test.py b/python/tink/integration/gcpkms/_gcp_kms_aead_test.py index 5d0b52983..1ba2c0130 100644 --- a/python/tink/integration/gcpkms/_gcp_kms_aead_test.py +++ b/python/tink/integration/gcpkms/_gcp_kms_aead_test.py @@ -25,7 +25,7 @@ from tink import core from tink.integration import gcpkms from tink.testing import helper -CREDENTIAL_PATH = os.path.join(helper.get_tink_src_path(), +CREDENTIAL_PATH = os.path.join(helper.tink_root_path(), 'testdata/credential.json') KEY_URI = 'gcp-kms://projects/tink-test-infrastructure/locations/global/keyRings/unit-and-integration-testing/cryptoKeys/aead-key' LOCAL_KEY_URI = 'gcp-kms://projects/tink-test-infrastructure/locations/europe-west1/keyRings/unit-and-integration-test/cryptoKeys/aead-key' diff --git a/python/tink/integration/gcpkms/_gcp_kms_client_test.py b/python/tink/integration/gcpkms/_gcp_kms_client_test.py index 41bc93278..df356cc69 100644 --- a/python/tink/integration/gcpkms/_gcp_kms_client_test.py +++ b/python/tink/integration/gcpkms/_gcp_kms_client_test.py @@ -25,7 +25,7 @@ from tink.integration import gcpkms from tink.testing import helper -CREDENTIAL_PATH = os.path.join(helper.get_tink_src_path(), +CREDENTIAL_PATH = os.path.join(helper.tink_root_path(), 'testdata/credential.json') diff --git a/python/tink/testing/BUILD.bazel b/python/tink/testing/BUILD.bazel index 6b1a3923a..21adc990b 100644 --- a/python/tink/testing/BUILD.bazel +++ b/python/tink/testing/BUILD.bazel @@ -25,6 +25,9 @@ py_test( name = "helper_test", srcs = ["helper_test.py"], srcs_version = "PY3", + data = [ + "@tink_base//testdata:credentials", + ], deps = [ ":helper", requirement("absl-py"), diff --git a/python/tink/testing/helper.py b/python/tink/testing/helper.py index f86395537..459058ea3 100644 --- a/python/tink/testing/helper.py +++ b/python/tink/testing/helper.py @@ -32,22 +32,28 @@ from tink import prf from tink import signature as pk_signature -def get_tink_src_path(): +def tink_root_path(): """Returns the path to the Tink root directory used for the test enviroment. - The path must be set in the TINK_SRC_PATH enviroment variable. If Bazel - is used the path is derived from the Bazel enviroment variables. + The path can be set in the TINK_SRC_PATH enviroment variable. If Bazel + is used the path is derived from the Bazel enviroment variables. If that + does not work, it generates the root path relative to the __file__ path. """ + root_paths = [] if 'TINK_SRC_PATH' in os.environ: - return os.environ['TINK_SRC_PATH'] - elif 'TEST_SRCDIR' in os.environ: + root_paths.append(os.environ['TINK_SRC_PATH']) + if 'TEST_SRCDIR' in os.environ: # Bazel enviroment - return os.path.join(os.environ['TEST_SRCDIR'], 'tink_base') - else: - raise ValueError( - 'Could not find path to Tink root directory. Make sure that ' - 'TINK_SRC_PATH is set.' - ) + root_paths.append(os.path.join(os.environ['TEST_SRCDIR'], 'tink_base')) + # try to calculate the path relative to the __file__ path. + root_paths.append(os.path.dirname(os.path.dirname(os.path.dirname( + os.path.dirname(os.path.abspath(__file__)))))) + for root_path in root_paths: + # return the first root path that exists. + if os.path.exists(root_path): + return root_path + raise ValueError('Could not find path to Tink root directory. Make sure that ' + 'TINK_SRC_PATH is set.') def fake_key(value: bytes = b'fakevalue', diff --git a/python/tink/testing/helper_test.py b/python/tink/testing/helper_test.py index 774e5e7f9..94f72c6c4 100644 --- a/python/tink/testing/helper_test.py +++ b/python/tink/testing/helper_test.py @@ -18,6 +18,8 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +import os + from absl.testing import absltest from tink import core from tink.testing import helper @@ -25,6 +27,12 @@ from tink.testing import helper class HelperTest(absltest.TestCase): + def test_tink_root_path(self): + path = os.path.join(helper.tink_root_path(), 'testdata/credential.json') + with open(path, mode='rt') as f: + credential_json = f.read() + self.assertNotEmpty(credential_json) + def test_fake_mac_success(self): mac = helper.FakeMac('Name') mac_value = mac.compute_mac(b'data') |