aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/cross_language/test_util.sh
blob: e466760c4d14e25d1fd9dcb9d8e51e1404b83d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# Copyright 2018 Google LLC
#
# 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.
################################################################################


REPO_DIR="${TEST_SRCDIR}"
TOOLS_DIR="${REPO_DIR}/tools"
TINKEY_CLI="${TOOLS_DIR}/tinkey/tinkey"
#############################################################################
##### Helper functions.

# Generates private and public keys according to $key_template,
# which should be supported by Tinkey.
# If $output_prefix is specified, the generated keyset will use it
# instead of default value "TINK".
# Stores the keys in files $priv_key_file and $pub_key_file, respectively.
generate_asymmetric_keys() {
  local key_name="$1"
  local key_template="$2"
  local output_prefix="$3"
  if [ "$output_prefix" == "" ]; then
    output_prefix="TINK"
  fi

  local json_priv_key_file="$TEST_TMPDIR/${key_name}_private_key.json"
  priv_key_file="$TEST_TMPDIR/${key_name}_private_key.bin"
  pub_key_file="$TEST_TMPDIR/${key_name}_public_key.bin"
  echo "--- Using template $key_template to generate keysets"\
      "to files $priv_key_file and $pub_key_file ..."

  $TINKEY_CLI create-keyset --key-template  $key_template --out-format JSON\
    | sed -e "s/\"TINK\"/\"$output_prefix\"/" > $json_priv_key_file  || exit 1
  $TINKEY_CLI convert-keyset --in-format JSON --in $json_priv_key_file\
    --out-format BINARY --out $priv_key_file  || exit 1
  $TINKEY_CLI create-public-keyset --in-format BINARY --in $priv_key_file\
      --out-format BINARY --out $pub_key_file  || exit 1
  echo "Done generating keysets."
}

# Generates a symmetric key according to $key_template,
# which should be supported by Tinkey.
# Stores the key in file $symmetric_key_file.
generate_symmetric_key() {
  local key_name="$1"
  local key_template="$2"
  local output_format="$3"
  if [ "$output_format" == "" ]; then
    output_format="BINARY"
  fi

  symmetric_key_file="$TEST_TMPDIR/${key_name}_symmetric_key.bin"
  echo "--- Using template $key_template to generate keyset"\
      "to file $symmetric_key_file ..."

  $TINKEY_CLI create-keyset --key-template $key_template\
      --out-format $output_format --out $symmetric_key_file  || exit 1
  echo "Done generating a symmetric keyset."
}

# Generates some example plaintext data, and stores it in $plaintext_file.
generate_plaintext() {
  local plaintext_name="$1"

  plaintext_file="$TEST_TMPDIR/${plaintext_name}_plaintext.bin"
  echo "This is some plaintext message to be encrypted and/or signed" \
       " named $plaintext_name just like that." > $plaintext_file
}

# Checks that two files are equal.
assert_files_equal() {
  local expected_file="$1"
  local given_file="$2"
  echo "*** Checking that 2 files are equal:"
  echo "    file #1: $expected_file"
  echo "    file #2: $given_file"
  diff $expected_file $given_file
  if [ $? -ne 0 ]; then
    echo "--- Failure: the files are different."
    exit 1
  fi
  echo "+++ Success: the files are equal."
}

# Checks that the given file has the expected content.
assert_file_equals() {
  local expected_content="$1"
  local given_file="$2"
  echo "*** Checking that given file: $given_file"
  echo "    has content equal to \"$expected_content\""
  local file_content=`cat $given_file`
  if [ "$expected_content" != "$file_content" ]; then
    echo "--- Failure. expected content: \"$expected_content\","\
        " actual content: \"$file_content\""
    exit 1
  fi
  echo "+++ Success: the file has expected content: \"$expected_content\"."
}

# Checks that two files are different.
assert_files_different() {
  local expected_file="$1"
  local given_file="$2"
  echo "*** Checking that 2 files are different:"
  echo "    file #1: $expected_file"
  echo "    file #2: $given_file"
  diff -q $expected_file $given_file
  if [ $? -eq 0 ]; then
    echo "--- Failure: the files are equal."
    exit 1
  fi
  echo "+++ Success: the files are different."
}

# Checks that a given file contains specified substrings.
assert_file_contains() {
  local file_to_test="$1"
  echo "*** Checking that file $file_to_test contains substrings:"
  cat $file_to_test
  # Shift the first argument and iterate through the remaining ones.
  shift
  for s do
  echo "... checking for string [$s]"
  if grep -q "$s" "$file_to_test"; then
    echo "   found"
  else
    echo "--- Failure: file does not contain string [$s]"
    exit 1
  fi
  done
  echo "+++ Success: file contains all expected substrings."
}