• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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
15from optparse import OptionParser
16import mini_parser
17import os
18import sys
19
20def do_main():
21    usage = "sepolicy_freeze_test "
22    usage += "-c current_cil -p prebuilt_cil [--help]"
23    parser = OptionParser(usage=usage)
24    parser.add_option("-c", "--current", dest="current", metavar="FILE")
25    parser.add_option("-p", "--prebuilt", dest="prebuilt", metavar="FILE")
26
27    (options, args) = parser.parse_args()
28
29    if not options.current or not options.prebuilt:
30        sys.exit("Must specify both current and prebuilt\n" + parser.usage)
31    if not os.path.exists(options.current):
32        sys.exit("Current policy " + options.current + " does not exist\n"
33                + parser.usage)
34    if not os.path.exists(options.prebuilt):
35        sys.exit("Prebuilt policy " + options.prebuilt + " does not exist\n"
36                + parser.usage)
37
38    current_policy = mini_parser.MiniCilParser(options.current)
39    prebuilt_policy = mini_parser.MiniCilParser(options.prebuilt)
40    current_policy.typeattributes = set(filter(lambda x: "base_typeattr_" not in x,
41                                               current_policy.typeattributes))
42    prebuilt_policy.typeattributes = set(filter(lambda x: "base_typeattr_" not in x,
43                                                prebuilt_policy.typeattributes))
44
45    results = ""
46    removed_types = prebuilt_policy.types - current_policy.types
47    added_types = current_policy.types - prebuilt_policy.types
48    removed_attributes = prebuilt_policy.typeattributes - current_policy.typeattributes
49    added_attributes = current_policy.typeattributes - prebuilt_policy.typeattributes
50
51    if removed_types:
52        results += "The following public types were removed:\n" + ", ".join(removed_types) + "\n"
53
54    if added_types:
55        results += "The following public types were added:\n" + ", ".join(added_types) + "\n"
56
57    if removed_attributes:
58        results += "The following public attributes were removed:\n" + ", ".join(removed_attributes) + "\n"
59
60    if added_attributes:
61        results += "The following public attributes were added:\n" + ", ".join(added_attributes) + "\n"
62
63    if results:
64        sys.exit(f'''{results}
65******************************
66You have tried to change system/sepolicy/public after vendor API freeze.
67To make these errors go away, you can guard types and attributes listed above,
68so they won't be included to the release build.
69
70See an example of how to guard them:
71    https://android-review.googlesource.com/3050544
72******************************
73''')
74
75if __name__ == '__main__':
76    do_main()
77