Skip to content

Commit

Permalink
Move cross-language envelope tests to its own test script and re-enab…
Browse files Browse the repository at this point in the history
…le AWS tests.

PiperOrigin-RevId: 292344789
  • Loading branch information
thaidn authored and copybara-github committed Jan 30, 2020
1 parent e04363c commit 57ab1a8
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 125 deletions.
25 changes: 20 additions & 5 deletions tools/testing/cross_language/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ sh_test(
],
data = [
":test_lib",
"@tink_base//:tink_version",
"//testing:version_cli_java",
"//testing/cc:version_cli_cc",
"@tink_base//:tink_version",
],
)

Expand All @@ -47,14 +47,29 @@ sh_test(
"//testing:aead_cli_java",
"//testing/cc:aead_cli_cc",
"//testing/go:aead_cli_go",
"//testing/go:generate_envelope_keyset",
"//tinkey",
"@tink_base//testdata:credentials",
],
)

sh_test(
name = "aead_envelope_test",
size = "medium",
srcs = [
"aead_envelope_test.sh",
],
args = select({
"//remote_build_execution:rbe": ["--no_external_tests"],
"//conditions:default": [],
"//remote_build_execution:rbe": ["--no_external_tests"],
"//conditions:default": [],
}),
data = [
":test_lib",
"//testing:aead_cli_java",
"//testing/go:aead_cli_go",
"//testing/go:generate_envelope_keyset",
"//tinkey",
"@tink_base//testdata:credentials",
],
)

sh_test(
Expand Down Expand Up @@ -114,8 +129,8 @@ sh_test(
"//testing:hybrid_encrypt_cli_java",
"//testing/cc:hybrid_decrypt_cli_cc",
"//testing/cc:hybrid_encrypt_cli_cc",
"//testing/go:hybrid_encrypt_cli_go",
"//testing/go:hybrid_decrypt_cli_go",
"//testing/go:hybrid_encrypt_cli_go",
"//tinkey",
],
)
Expand Down
127 changes: 127 additions & 0 deletions tools/testing/cross_language/aead_envelope_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################


ROOT_DIR="$TEST_SRCDIR/tools"
GO_AEAD_CLI="$ROOT_DIR/testing/go/aead_cli_go"
JAVA_AEAD_CLI="$ROOT_DIR/testing/aead_cli_java"
TEST_UTIL="$ROOT_DIR/testing/cross_language/test_util.sh"

# TODO(b/136245485): Update this script to use go/gbash.
# Envelope encryption tests require external resources cannot run on neither
# Kokoro/RBE nor TAP/Forge. They can run only on Kokoro non-RBE.
# If the --no_external_tests flag is specified, no tests are run, this script
# simply exits.
if [ "$1" == "--no_external_tests" ]; then
exit 0
fi

source $TEST_UTIL || exit 1

#############################################################################
### Helpers for AWS AEAD-tests.

# Envelope encryption tests using AWS KMS AEAD-implementations.
aead_aws_test() {
local test_name="aead-aws-test"
local encrypt_clis=$1
local decrypt_clis=$2
local key_templates=$3
echo "############ starting test $test_name for the following templates:"
echo $key_templates
for key_template in ${key_templates[*]}
do
echo "## TEST for key template $key_template"
for encrypt_cli in ${encrypt_clis[*]}
do
local encrypt_cli_name=$(basename $encrypt_cli)
echo "## ENCRYPTING using $encrypt_cli_name"
local test_instance="${test_name}_${key_template}"
generate_aws_keyset "${test_instance}_ENCRYPT_${encrypt_cli_name}" \
$key_template
generate_plaintext $test_instance 30000

local encrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_encrypted.bin"
local associated_data_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_aad.bin"
echo "some associated data for $test_instance using $encrypt_cli_name" \
"for encryption" > $associated_data_file

$encrypt_cli $aws_keyset_file "encrypt" $plaintext_file\
$associated_data_file $encrypted_file|| exit 1
assert_files_different $plaintext_file $encrypted_file
for decrypt_cli in ${decrypt_clis[*]}
do
local decrypt_cli_name=$(basename "$decrypt_cli")
local decrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_DECRYPT_${decrypt_cli_name}_decrypted.bin"
echo "## DECRYPTING using $decrypt_cli_name"
$decrypt_cli $aws_keyset_file "decrypt" $encrypted_file\
$associated_data_file $decrypted_file || exit 1
assert_files_equal $plaintext_file $decrypted_file
done
done
done
}

#############################################################################
### Helpers for GCP AEAD-tests.

# Envelope encryption tests using GCP KMS AEAD-implementations.
aead_gcp_test() {
local test_name="aead-gcp-test"
local encrypt_clis=$1
local decrypt_clis=$2
local key_templates=$3
echo "############ starting test $test_name for the following templates:"
echo $key_templates
for key_template in ${key_templates[*]}
do
echo "## TEST for key template $key_template"
for encrypt_cli in ${encrypt_clis[*]}
do
local encrypt_cli_name=$(basename $encrypt_cli)
echo "## ENCRYPTING using $encrypt_cli_name"
local test_instance="${test_name}_${key_template}"
generate_gcp_keyset "${test_instance}_ENCRYPT_${encrypt_cli_name}" \
$key_template
generate_plaintext $test_instance 30000

local encrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_encrypted.bin"
local associated_data_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_aad.bin"
echo "some associated data for $test_instance using $encrypt_cli_name" \
"for encryption" > $associated_data_file
$encrypt_cli $gcp_keyset_file "encrypt" $plaintext_file\
$associated_data_file $encrypted_file || exit 1
assert_files_different $plaintext_file $encrypted_file

for decrypt_cli in ${decrypt_clis[*]}
do
local decrypt_cli_name=$(basename "$decrypt_cli")
local decrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_DECRYPT_${decrypt_cli_name}_decrypted.bin"
echo "## DECRYPTING using $decrypt_cli_name"
$decrypt_cli $gcp_keyset_file "decrypt" $encrypted_file\
$associated_data_file $decrypted_file || exit 1
assert_files_equal $plaintext_file $decrypted_file
done
done
done
}

#############################################################################
##### Run the actual tests.

KEY_TEMPLATES=(AES128_GCM AES128_CTR_HMAC_SHA256)
ENCRYPT_CLIS=($GO_AEAD_CLI $JAVA_AEAD_CLI)
DECRYPT_CLIS=($GO_AEAD_CLI $JAVA_AEAD_CLI)
aead_gcp_test "${ENCRYPT_CLIS[*]}" "${DECRYPT_CLIS[*]}" "${KEY_TEMPLATES[*]}"
aead_aws_test "${ENCRYPT_CLIS[*]}" "${DECRYPT_CLIS[*]}" "${KEY_TEMPLATES[*]}"
112 changes: 0 additions & 112 deletions tools/testing/cross_language/aead_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ PY3_AEAD_CLI="" # placeholder, please ignore
GO_AEAD_CLI="$ROOT_DIR/testing/go/aead_cli_go"
JAVA_AEAD_CLI="$ROOT_DIR/testing/aead_cli_java"
TEST_UTIL="$ROOT_DIR/testing/cross_language/test_util.sh"
RUN_EXTERNAL_TESTS=true

# TODO(b/136245485): Update this script to use go/gbash.
# Tests that require external resources cannot run on RBE. If the
# --no_external_tests flag is specified disable these test cases.
if [ "$1" == "--no_external_tests" ]; then
RUN_EXTERNAL_TESTS=false
fi

source $TEST_UTIL || exit 1

Expand Down Expand Up @@ -76,100 +70,6 @@ aead_basic_test() {
done
}

#############################################################################
### Helpers for AWS AEAD-tests.

# Envelope encryption tests using AWS KMS AEAD-implementations.
aead_aws_test() {
local test_name="aead-aws-test"
local encrypt_clis=$1
local decrypt_clis=$2
local key_templates=$3
# lint placeholder header, please ignore
# ignore-placeholder1
# lint placeholder footer, please ignore
echo "############ starting test $test_name for the following templates:"
echo $key_templates
for key_template in ${key_templates[*]}
do
echo "## TEST for key template $key_template"
for encrypt_cli in ${encrypt_clis[*]}
do
local encrypt_cli_name=$(basename $encrypt_cli)
echo "## ENCRYPTING using $encrypt_cli_name"
local test_instance="${test_name}_${key_template}"
generate_aws_keyset "${test_instance}_ENCRYPT_${encrypt_cli_name}" \
$key_template
generate_plaintext $test_instance 30000

local encrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_encrypted.bin"
local associated_data_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_aad.bin"
echo "some associated data for $test_instance using $encrypt_cli_name" \
"for encryption" > $associated_data_file

$encrypt_cli $aws_keyset_file "encrypt" $plaintext_file\
$associated_data_file $encrypted_file|| exit 1
assert_files_different $plaintext_file $encrypted_file
for decrypt_cli in ${decrypt_clis[*]}
do
local decrypt_cli_name=$(basename "$decrypt_cli")
local decrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_DECRYPT_${decrypt_cli_name}_decrypted.bin"
echo "## DECRYPTING using $decrypt_cli_name"
$decrypt_cli $aws_keyset_file "decrypt" $encrypted_file\
$associated_data_file $decrypted_file || exit 1
assert_files_equal $plaintext_file $decrypted_file
done
done
done
}

#############################################################################
### Helpers for GCP AEAD-tests.

# Envelope encryption tests using GCP KMS AEAD-implementations.
aead_gcp_test() {
local test_name="aead-gcp-test"
local encrypt_clis=$1
local decrypt_clis=$2
local key_templates=$3
# lint placeholder header, please ignore
# ignore-placeholder1
# lint placeholder footer, please ignore
echo "############ starting test $test_name for the following templates:"
echo $key_templates
for key_template in ${key_templates[*]}
do
echo "## TEST for key template $key_template"
for encrypt_cli in ${encrypt_clis[*]}
do
local encrypt_cli_name=$(basename $encrypt_cli)
echo "## ENCRYPTING using $encrypt_cli_name"
local test_instance="${test_name}_${key_template}"
generate_gcp_keyset "${test_instance}_ENCRYPT_${encrypt_cli_name}" \
$key_template
generate_plaintext $test_instance 30000

local encrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_encrypted.bin"
local associated_data_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_aad.bin"
echo "some associated data for $test_instance using $encrypt_cli_name" \
"for encryption" > $associated_data_file
$encrypt_cli $gcp_keyset_file "encrypt" $plaintext_file\
$associated_data_file $encrypted_file || exit 1
assert_files_different $plaintext_file $encrypted_file

for decrypt_cli in ${decrypt_clis[*]}
do
local decrypt_cli_name=$(basename "$decrypt_cli")
local decrypted_file="$TEST_TMPDIR/${test_instance}_ENCRYPT_${encrypt_cli_name}_DECRYPT_${decrypt_cli_name}_decrypted.bin"
echo "## DECRYPTING using $decrypt_cli_name"
$decrypt_cli $gcp_keyset_file "decrypt" $encrypted_file\
$associated_data_file $decrypted_file || exit 1
assert_files_equal $plaintext_file $decrypted_file
done
done
done
}

#############################################################################
##### Run the actual tests.
KEY_TEMPLATES=(AES128_GCM AES256_GCM AES128_CTR_HMAC_SHA256 AES256_CTR_HMAC_SHA256 XCHACHA20_POLY1305)
Expand All @@ -186,15 +86,3 @@ KEY_TEMPLATES=(CHACHA20_POLY1305)
ENCRYPT_CLIS=($JAVA_AEAD_CLI $GO_AEAD_CLI)
DECRYPT_CLIS=($JAVA_AEAD_CLI $GO_AEAD_CLI)
aead_basic_test "${ENCRYPT_CLIS[*]}" "${DECRYPT_CLIS[*]}" "${KEY_TEMPLATES[*]}"

if [ "$RUN_EXTERNAL_TESTS" = true ]; then
KEY_TEMPLATES=(AES128_GCM AES128_CTR_HMAC_SHA256)
ENCRYPT_CLIS=($GO_AEAD_CLI $JAVA_AEAD_CLI)
DECRYPT_CLIS=($GO_AEAD_CLI $JAVA_AEAD_CLI)
aead_gcp_test "${ENCRYPT_CLIS[*]}" "${DECRYPT_CLIS[*]}" "${KEY_TEMPLATES[*]}"

# lint placeholder header, please ignore
aead_aws_test "${ENCRYPT_CLIS[*]}" "${DECRYPT_CLIS[*]}" "${KEY_TEMPLATES[*]}"

# lint placeholder footer, please ignore
fi
12 changes: 4 additions & 8 deletions tools/testing/java/com/google/crypto/tink/testing/AeadCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import com.google.crypto.tink.KmsClient;
import com.google.crypto.tink.KmsClients;
import com.google.crypto.tink.aead.AeadConfig;
import com.google.crypto.tink.integration.gcpkms.GcpKmsClient;

import com.google.crypto.tink.integration.awskms.AwsKmsClient;
import com.google.crypto.tink.integration.gcpkms.GcpKmsClient;

/**
* A command-line utility for testing Aead-primitives. It requires 5 arguments: keyset-file: name of
Expand All @@ -46,13 +45,10 @@ public static void main(String[] args) throws Exception {
String associatedDataFile = args[3];
String outputFilename = args[4];

KmsClient gcpKmsClient = new GcpKmsClient().withCredentials(
"../tink_base/" + TestUtil.SERVICE_ACCOUNT_FILE);
KmsClient gcpKmsClient = new GcpKmsClient().withCredentials("../tink_base/" + TestUtil.SERVICE_ACCOUNT_FILE);
KmsClients.add(gcpKmsClient);
AeadConfig.register();

KmsClient awsKmsClient = new AwsKmsClient(TestUtil.AWS_CRYPTO_URI).withCredentials(
"../tink_base/" + TestUtil.AWS_CREDS);
KmsClient awsKmsClient =
new AwsKmsClient(TestUtil.AWS_CRYPTO_URI).withCredentials("../tink_base/" + TestUtil.AWS_CREDS);
KmsClients.add(awsKmsClient);
AeadConfig.register();

Expand Down

0 comments on commit 57ab1a8

Please sign in to comment.