| 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2018 The ChromiumOS Authors |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | . "$(dirname "$0")/lib/sudo_stub.sh" |
| 8 | . "$(dirname "$0")/lib/sign_android_lib.sh" |
| 9 | |
| 10 | # Expected APK signatures depending on the type of APK and the type of build. |
| 11 | declare -A platform_sha=( |
| 12 | ['cheets']='AA:04:E0:5F:82:9C:7E:D1:B9:F8:FC:99:6C:5A:54:43:83:D9:F5:BC' |
| 13 | ['aosp']='27:19:6E:38:6B:87:5E:76:AD:F7:00:E7:EA:84:E4:C6:EE:E3:3D:FA' |
| 14 | ) |
| 15 | declare -A media_sha=( |
| 16 | ['cheets']='D4:C4:2D:E0:B9:1B:15:72:FA:7D:A7:21:E0:A6:09:94:B4:4C:B5:AE' |
| 17 | ['aosp']='B7:9D:F4:A8:2E:90:B5:7E:A7:65:25:AB:70:37:AB:23:8A:42:F5:D3' |
| 18 | ) |
| 19 | declare -A shared_sha=( |
| 20 | ['cheets']='38:B6:2C:E1:75:98:E3:E1:1C:CC:F6:6B:83:BB:97:0E:2D:40:6C:AE' |
| 21 | ['aosp']='5B:36:8C:FF:2D:A2:68:69:96:BC:95:EA:C1:90:EA:A4:F5:63:0F:E5' |
| 22 | ) |
| 23 | declare -A release_sha=( |
| 24 | ['cheets']='EC:63:36:20:23:B7:CB:66:18:70:D3:39:3C:A9:AE:7E:EF:A9:32:42' |
| 25 | ['aosp']='61:ED:37:7E:85:D3:86:A8:DF:EE:6B:86:4B:D8:5B:0B:FA:A5:AF:81' |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | test_android_choose_key_invalid_keyset() { |
| 30 | local keyname |
| 31 | local keyset |
| 32 | local keysets=("invalid_keyset" " " "") |
| 33 | |
| 34 | for keyset in "${keysets[@]}"; do |
| 35 | echo "TEST: Detection of invalid keyset '${keyset}'." |
| 36 | if keyname=$(android_choose_key "ignored_sha1" "${keyset}"); then |
| 37 | : $(( NUM_TEST_FAILURES += 1 )) |
| 38 | echo "ERROR: Failed to detect invalid keyset '${keyset}'." |
| 39 | else |
| 40 | echo "PASS: Detected invalid keyset '${keyset}'." |
| 41 | fi |
| 42 | done |
| 43 | } |
| 44 | |
| 45 | android_choose_key_test_helper() { |
| 46 | local sha1="$1" |
| 47 | local keyset="$2" |
| 48 | local expected_keyname="$3" |
| 49 | local keyname="invalid_key" |
| 50 | |
| 51 | echo "TEST: Detect '${expected_keyname}' key name for '${keyset}' keyset." |
| 52 | keyname=$(android_choose_key "${sha1}" "${keyset}") |
| 53 | if [[ "${keyname}" != "${expected_keyname}" ]]; then |
| 54 | : $(( NUM_TEST_FAILURES += 1 )) |
| 55 | echo "ERROR: Incorrect key name '${keyname}' returned." |
| 56 | else |
| 57 | echo "PASS: Correct key name '${keyname}' returned." |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | test_android_choose_key() { |
| 62 | local keyset |
| 63 | local expected_keyname |
| 64 | |
| 65 | local keysets=("cheets" "aosp") |
| 66 | for keyset in "${keysets[@]}"; do |
| 67 | expected_keyname="platform" |
| 68 | android_choose_key_test_helper "${platform_sha[${keyset}]}" "${keyset}" \ |
| 69 | "${expected_keyname}" |
| 70 | expected_keyname="media" |
| 71 | android_choose_key_test_helper "${media_sha[${keyset}]}" "${keyset}" \ |
| 72 | "${expected_keyname}" |
| 73 | expected_keyname="shared" |
| 74 | android_choose_key_test_helper "${shared_sha[${keyset}]}" "${keyset}" \ |
| 75 | "${expected_keyname}" |
| 76 | expected_keyname="releasekey" |
| 77 | android_choose_key_test_helper "${release_sha[${keyset}]}" "${keyset}" \ |
| 78 | "${expected_keyname}" |
| 79 | done |
| 80 | } |
| 81 | |
| 82 | build_flavor_test_helper() { |
| 83 | local prop_file="${BUILD}/build.prop" |
| 84 | local prop_content="$1" |
| 85 | local expected_flavor_prop="$2" |
| 86 | local flavor_prop="" |
| 87 | |
| 88 | echo "${prop_content}" > "${prop_file}" |
| 89 | flavor_prop=$(android_get_build_flavor_prop "${prop_file}") |
| 90 | if [[ "${flavor_prop}" != "${expected_flavor_prop}" ]]; then |
| 91 | : $(( NUM_TEST_FAILURES += 1 )) |
| 92 | echo "ERROR: Incorrect build flavor '${flavor_prop}' returned." |
| 93 | else |
| 94 | echo "PASS: Correct key name '${flavor_prop}' returned." |
| 95 | fi |
| 96 | rm "${prop_file}" |
| 97 | } |
| 98 | |
| 99 | test_android_get_build_flavor_prop() { |
| 100 | local prop_file="${BUILD}/build.prop" |
| 101 | local prop_content="" |
| 102 | local flavor_prop="" |
| 103 | |
| 104 | echo "TEST: Extract ro.build.flavor property." |
| 105 | prop_content="ro.random.prop=foo |
| 106 | other.prop=bar |
| 107 | x=foobar |
| 108 | ro.build.flavor=cheets_x86-user |
| 109 | another.prop=barfoo" |
| 110 | build_flavor_test_helper "${prop_content}" "cheets_x86-user" |
| 111 | |
| 112 | echo "TEST: Extract single ro.build.flavor property." |
| 113 | prop_content="ro.build.flavor=cheets_x86-user" |
| 114 | build_flavor_test_helper "${prop_content}" "cheets_x86-user" |
| 115 | |
| 116 | echo "TEST: Avoid commented out ro.build.flavor property." |
| 117 | prop_content="ro.random.prop=foo |
| 118 | other.prop=bar |
| 119 | x=foobar |
| 120 | #ro.build.flavor=commented_out |
| 121 | ro.build.flavor=cheets_x86-user |
| 122 | another.prop=barfoo" |
| 123 | build_flavor_test_helper "${prop_content}" "cheets_x86-user" |
| 124 | |
| 125 | # Missing ro.build.flavor property. |
| 126 | echo "TEST: Detect missing ro.build.flavor property." |
| 127 | echo "ro.random.prop=foo" > "${prop_file}" |
| 128 | if flavor_prop=$(android_get_build_flavor_prop "${prop_file}"); then |
| 129 | : $(( NUM_TEST_FAILURES += 1 )) |
| 130 | echo "ERROR: Failed to detect missing ro.build.flavor property." |
| 131 | else |
| 132 | echo "PASS: Detected missing ro.build.flavor property." |
| 133 | fi |
| 134 | rm "${prop_file}" |
| 135 | } |
| 136 | |
| 137 | choose_signing_keyset_test_helper() { |
| 138 | local flavor_prop="$1" |
| 139 | local expected_keyset="$2" |
| 140 | local keyset="" |
| 141 | |
| 142 | keyset=$(android_choose_signing_keyset "${flavor_prop}") |
| 143 | if [[ "${keyset}" != "${expected_keyset}" ]]; then |
| 144 | : $(( NUM_TEST_FAILURES += 1 )) |
| 145 | echo "ERROR: Incorrect keyset '${keyset}' returned instead of \ |
| 146 | '${expected_keyset}'." |
| 147 | else |
| 148 | echo "PASS: Correct keyset '${keyset}' returned." |
| 149 | fi |
| 150 | } |
| 151 | |
| 152 | choose_signing_keyset_test_invalid_flavors() { |
| 153 | local flavor="$1" |
| 154 | |
| 155 | echo "TEST: Detect invalid build flavor '${flavor}'." |
| 156 | if android_choose_signing_keyset "${flavor}"; then |
| 157 | : $(( NUM_TEST_FAILURES += 1 )) |
| 158 | echo "ERROR: Failed to detect invalid build flavor '${flavor}'." |
| 159 | else |
| 160 | echo "PASS: Detected invalid build flavor '${flavor}'." |
| 161 | fi |
| 162 | } |
| 163 | |
| 164 | test_android_choose_signing_keyset() { |
| 165 | echo "TEST: Keyset for aosp_cheets build." |
| 166 | choose_signing_keyset_test_helper "aosp_cheets_x86-userdebug" "aosp" |
| 167 | echo "TEST: Keyset for sdk_google_cheets build." |
| 168 | choose_signing_keyset_test_helper "sdk_google_cheets_x86-userdebug" "cheets" |
| 169 | echo "TEST: Keyset for sdk_cheets build." |
| 170 | choose_signing_keyset_test_helper "sdk_cheets_x86-userdebug" "cheets" |
| 171 | echo "TEST: Keyset for cheets_x86 build." |
| 172 | choose_signing_keyset_test_helper "cheets_x86-user" "cheets" |
| 173 | echo "TEST: Keyset for cheets_arm build." |
| 174 | choose_signing_keyset_test_helper "cheets_arm-user" "cheets" |
| 175 | echo "TEST: Keyset for cheets_x86_64 build." |
| 176 | choose_signing_keyset_test_helper "cheets_x86_64-user" "cheets" |
| 177 | echo "TEST: Keyset for userdebug build." |
| 178 | choose_signing_keyset_test_helper "cheets_x86-userdebug" "cheets" |
| 179 | echo "TEST: Keyset for bertha_x86 build." |
| 180 | choose_signing_keyset_test_helper "bertha_x86-user" "cheets" |
| 181 | echo "TEST: Keyset for bertha_arm build." |
| 182 | choose_signing_keyset_test_helper "bertha_arm-user" "cheets" |
| 183 | echo "TEST: Keyset for bertha_x86_64 build." |
| 184 | choose_signing_keyset_test_helper "bertha_x86_64-user" "cheets" |
| 185 | |
| 186 | choose_signing_keyset_test_invalid_flavors "aosp" |
| 187 | choose_signing_keyset_test_invalid_flavors "cheets" |
| 188 | choose_signing_keyset_test_invalid_flavors "bertha" |
| 189 | choose_signing_keyset_test_invalid_flavors "" |
| 190 | choose_signing_keyset_test_invalid_flavors " " |
| 191 | } |
| 192 | |
| 193 | main() { |
| 194 | if [[ $# -ne 0 ]]; then |
| 195 | echo "FAIL: unexpected arguments '$@'." |
| 196 | return 1 |
| 197 | fi |
| 198 | |
| 199 | BUILD=$(mktemp -d) |
| 200 | echo "Setting temporary build directory as '${BUILD}'." |
| 201 | |
| 202 | test_android_choose_key_invalid_keyset |
| 203 | test_android_choose_key |
| 204 | test_android_get_build_flavor_prop |
| 205 | test_android_choose_signing_keyset |
| 206 | |
| 207 | echo "Deleting temporary build directory '${BUILD}'." |
| 208 | rmdir "${BUILD}" |
| 209 | |
| 210 | if [[ ${NUM_TEST_FAILURES} -gt 0 ]]; then |
| 211 | echo "FAIL: found ${NUM_TEST_FAILURES} failed :(." |
| 212 | return 1 |
| 213 | fi |
| 214 | echo "PASS: all tests passed :)." |
| 215 | return 0 |
| 216 | } |
| 217 | |
| 218 | # Global incremented by each test when they fail. |
| 219 | NUM_TEST_FAILURES=0 |
| 220 | main "$@" |