aboutsummaryrefslogtreecommitdiff
path: root/rules/apex/apex_key_test.bzl
blob: fdd31a1b65fe4ba257a96beb9b79c55aefb533c7 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (C) 2022 The Android Open Source Project
#
# 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.

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load(":apex_key.bzl", "ApexKeyInfo", "apex_key")

def _apex_key_test(ctx):
    env = analysistest.begin(ctx)
    target_under_test = analysistest.target_under_test(env)
    asserts.equals(
        env,
        ctx.attr.expected_private_key_short_path,
        target_under_test[ApexKeyInfo].private_key.short_path,
    )
    asserts.equals(
        env,
        ctx.attr.expected_public_key_short_path,
        target_under_test[ApexKeyInfo].public_key.short_path,
    )
    return analysistest.end(env)

apex_key_test = analysistest.make(
    _apex_key_test,
    attrs = {
        "expected_private_key_short_path": attr.string(mandatory = True),
        "expected_public_key_short_path": attr.string(mandatory = True),
    },
)

apex_key_with_default_app_cert_test = analysistest.make(
    _apex_key_test,
    attrs = {
        "expected_private_key_short_path": attr.string(mandatory = True),
        "expected_public_key_short_path": attr.string(mandatory = True),
    },
    config_settings = {
        # This product sets DefaultAppCertificate to build/bazel/rules/apex/testdata/devkey,
        # so we expect the apex_key to look for key_name in build/bazel/rules/apex/testdata.
        "//command_line_option:platforms": "@//build/bazel/tests/products:aosp_arm64_for_testing_with_overrides_and_app_cert",
    },
)

def _test_apex_key_file_targets_with_key_name_attribute():
    name = "apex_key_file_targets_with_key_name_attribute"
    test_name = name + "_test"
    private_key = name + ".pem"
    public_key = name + ".avbpubkey"

    apex_key(
        name = name,
        private_key_name = private_key,
        public_key_name = public_key,
    )

    apex_key_test(
        name = test_name,
        target_under_test = name,
        expected_private_key_short_path = native.package_name() + "/" + private_key,
        expected_public_key_short_path = native.package_name() + "/" + public_key,
    )

    return test_name

def _test_apex_key_file_targets_with_key_name_attribute_with_default_app_cert():
    name = "apex_key_file_targets_with_key_attribute_with_default_app_cert"
    test_name = name + "_test"
    private_key = "devkey.pem"
    public_key = "devkey.avbpubkey"

    apex_key(
        name = name,
        private_key_name = private_key,
        public_key_name = public_key,
    )

    apex_key_with_default_app_cert_test(
        name = test_name,
        target_under_test = name,
        expected_private_key_short_path = "build/bazel/rules/apex/testdata/" + private_key,
        expected_public_key_short_path = "build/bazel/rules/apex/testdata/" + public_key,
    )

    return test_name

def _test_apex_key_file_targets_with_key_attribute():
    name = "apex_key_file_targets_with_key_attribute"
    test_name = name + "_test"
    private_key = name + ".pem"
    public_key = name + ".avbpubkey"

    apex_key(
        name = name,
        # Referring to file targets with plain strings work as well, as bazel
        # will parse these labels as file targets in the same package.
        private_key = private_key,
        public_key = public_key,
    )

    apex_key_test(
        name = test_name,
        target_under_test = name,
        expected_private_key_short_path = native.package_name() + "/" + private_key,
        expected_public_key_short_path = native.package_name() + "/" + public_key,
    )

    return test_name

def _test_apex_key_generated_keys():
    name = "apex_key_generated_keys"
    test_name = name + "_test"
    private_key = name + ".pem"
    public_key = name + ".avbpubkey"

    native.genrule(
        name = private_key,
        outs = ["priv/" + name + ".generated"],
        cmd = "noop",
        tags = ["manual"],
    )

    native.genrule(
        name = public_key,
        outs = ["pub/" + name + ".generated"],
        cmd = "noop",
        tags = ["manual"],
    )

    apex_key(
        name = name,
        private_key = private_key,
        public_key = public_key,
    )

    apex_key_test(
        name = test_name,
        target_under_test = name,
        expected_private_key_short_path = native.package_name() + "/priv/" + name + ".generated",
        expected_public_key_short_path = native.package_name() + "/pub/" + name + ".generated",
    )

    return test_name

def apex_key_test_suite(name):
    native.test_suite(
        name = name,
        tests = [
            _test_apex_key_file_targets_with_key_name_attribute(),
            _test_apex_key_file_targets_with_key_name_attribute_with_default_app_cert(),
            _test_apex_key_file_targets_with_key_attribute(),
            _test_apex_key_generated_keys(),
        ],
    )