1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 16load(":apex_key.bzl", "ApexKeyInfo", "apex_key") 17 18def _apex_key_test(ctx): 19 env = analysistest.begin(ctx) 20 target_under_test = analysistest.target_under_test(env) 21 asserts.equals( 22 env, 23 ctx.attr.expected_private_key_short_path, 24 target_under_test[ApexKeyInfo].private_key.short_path, 25 ) 26 asserts.equals( 27 env, 28 ctx.attr.expected_public_key_short_path, 29 target_under_test[ApexKeyInfo].public_key.short_path, 30 ) 31 return analysistest.end(env) 32 33apex_key_test = analysistest.make( 34 _apex_key_test, 35 attrs = { 36 "expected_private_key_short_path": attr.string(mandatory = True), 37 "expected_public_key_short_path": attr.string(mandatory = True), 38 }, 39) 40 41apex_key_with_default_app_cert_test = analysistest.make( 42 _apex_key_test, 43 attrs = { 44 "expected_private_key_short_path": attr.string(mandatory = True), 45 "expected_public_key_short_path": attr.string(mandatory = True), 46 }, 47 config_settings = { 48 # This product sets DefaultAppCertificate to build/bazel/rules/apex/testdata/devkey, 49 # so we expect the apex_key to look for key_name in build/bazel/rules/apex/testdata. 50 "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_arm64_for_testing_with_overrides_and_app_cert", 51 }, 52) 53 54def _test_apex_key_file_targets_with_key_name_attribute(): 55 name = "apex_key_file_targets_with_key_name_attribute" 56 test_name = name + "_test" 57 private_key = name + ".pem" 58 public_key = name + ".avbpubkey" 59 60 apex_key( 61 name = name, 62 private_key_name = private_key, 63 public_key_name = public_key, 64 ) 65 66 apex_key_test( 67 name = test_name, 68 target_under_test = name, 69 expected_private_key_short_path = native.package_name() + "/" + private_key, 70 expected_public_key_short_path = native.package_name() + "/" + public_key, 71 ) 72 73 return test_name 74 75def _test_apex_key_file_targets_with_key_name_attribute_with_default_app_cert(): 76 name = "apex_key_file_targets_with_key_attribute_with_default_app_cert" 77 test_name = name + "_test" 78 private_key = "devkey.pem" 79 public_key = "devkey.avbpubkey" 80 81 apex_key( 82 name = name, 83 private_key_name = private_key, 84 public_key_name = public_key, 85 ) 86 87 apex_key_with_default_app_cert_test( 88 name = test_name, 89 target_under_test = name, 90 expected_private_key_short_path = "build/bazel/rules/apex/testdata/" + private_key, 91 expected_public_key_short_path = "build/bazel/rules/apex/testdata/" + public_key, 92 ) 93 94 return test_name 95 96def _test_apex_key_file_targets_with_key_attribute(): 97 name = "apex_key_file_targets_with_key_attribute" 98 test_name = name + "_test" 99 private_key = name + ".pem" 100 public_key = name + ".avbpubkey" 101 102 apex_key( 103 name = name, 104 # Referring to file targets with plain strings work as well, as bazel 105 # will parse these labels as file targets in the same package. 106 private_key = private_key, 107 public_key = public_key, 108 ) 109 110 apex_key_test( 111 name = test_name, 112 target_under_test = name, 113 expected_private_key_short_path = native.package_name() + "/" + private_key, 114 expected_public_key_short_path = native.package_name() + "/" + public_key, 115 ) 116 117 return test_name 118 119def _test_apex_key_generated_keys(): 120 name = "apex_key_generated_keys" 121 test_name = name + "_test" 122 private_key = name + ".pem" 123 public_key = name + ".avbpubkey" 124 125 native.genrule( 126 name = private_key, 127 outs = ["priv/" + name + ".generated"], 128 cmd = "noop", 129 tags = ["manual"], 130 ) 131 132 native.genrule( 133 name = public_key, 134 outs = ["pub/" + name + ".generated"], 135 cmd = "noop", 136 tags = ["manual"], 137 ) 138 139 apex_key( 140 name = name, 141 private_key = private_key, 142 public_key = public_key, 143 ) 144 145 apex_key_test( 146 name = test_name, 147 target_under_test = name, 148 expected_private_key_short_path = native.package_name() + "/priv/" + name + ".generated", 149 expected_public_key_short_path = native.package_name() + "/pub/" + name + ".generated", 150 ) 151 152 return test_name 153 154def apex_key_test_suite(name): 155 native.test_suite( 156 name = name, 157 tests = [ 158 _test_apex_key_file_targets_with_key_name_attribute(), 159 _test_apex_key_file_targets_with_key_name_attribute_with_default_app_cert(), 160 _test_apex_key_file_targets_with_key_attribute(), 161 _test_apex_key_generated_keys(), 162 ], 163 ) 164