• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright 2022 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16####################################################################################
17
18# This script runs the Java hello world test application with Maven.
19#
20# This is to test Maven snapshots that, if local, should be pre-installed
21# locally.
22
23usage() {
24  echo "Usage: $0 [-lh] <path/to/example/helloworld/pom.xml>"
25  echo "  -l: Local. Test a local tink-java artifact (default: false)."
26  echo "  -h: Help. Print this usage information."
27  exit 1
28}
29
30# Load the test library.
31source "kokoro/testutils/test_utils.sh"
32
33LOCAL="false"
34EXAMPLE_APPLICATION_POM=
35
36process_args() {
37  while getopts "lh" opt; do
38    case "${opt}" in
39      l) LOCAL="true" ;;
40      h) usage ;;
41      *) usage ;;
42    esac
43  done
44  shift $((OPTIND - 1))
45  readonly LOCAL
46  EXAMPLE_APPLICATION_POM="$1"
47  readonly EXAMPLE_APPLICATION_POM
48}
49
50test_MavenSnapshotTest_RunJavaTestApplication() {
51  local mvn_flags=()
52  if [[ "${LOCAL}" == "true" ]]; then
53    # Use snapshots present in the local repository.
54    mvn_flags+=( --no-snapshot-updates )
55  fi
56  readonly mvn_flags
57  mvn "${mvn_flags[@]}" package -f "${EXAMPLE_APPLICATION_POM}"
58  ASSERT_CMD_SUCCEEDED
59
60  local -r plaintext="${TEST_CASE_TMPDIR}/plaintext.bin"
61  local -r encrypted="${TEST_CASE_TMPDIR}/encrypted.bin"
62  local -r decrypted="${TEST_CASE_TMPDIR}/decrypted.bin"
63  local -r keyset="${TEST_CASE_TMPDIR}/keyset.cfg"
64
65  dd if=/dev/urandom of="${plaintext}" bs=1K count=1
66  mvn exec:java "${mvn_flags[@]}" -f "${EXAMPLE_APPLICATION_POM}" \
67    -Dexec.args="encrypt --keyset ${keyset} --in ${plaintext} --out \
68${encrypted}"
69  ASSERT_CMD_SUCCEEDED
70  mvn exec:java "${mvn_flags[@]}" -f $EXAMPLE_APPLICATION_POM \
71    -Dexec.args="decrypt --keyset ${keyset} --in ${encrypted} --out \
72${decrypted}"
73  ASSERT_CMD_SUCCEEDED
74  ASSERT_FILE_EQUALS "${plaintext}" "${decrypted}"
75}
76
77main() {
78  process_args "$@"
79  run_all_tests
80}
81
82main "$@"
83