blob: ca95f8a1892d6efba6191ae6ffd028f120a22410 [file] [log] [blame]
Dan Cashman91d398d2017-09-26 12:58:29 -07001from optparse import OptionParser
2from optparse import Option, OptionValueError
3import os
4import policy
5import re
6import sys
7
8#############################################################
9# Tests
10#############################################################
11def TestDataTypeViolations(pol):
12 return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type")
13
14def TestSysfsTypeViolations(pol):
15 return pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
16 "/sys/kernel/tracing"], "sysfs_type")
17
18def TestDebugfsTypeViolations(pol):
19 # TODO: this should apply to genfs_context entries as well
20 return pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
21 "/sys/kernel/tracing"], [], "debugfs_type")
Tri Vo30c3c2a2018-01-10 11:04:06 -080022
23def TestVendorTypeViolations(pol):
24 return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type")
25
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080026def TestCoreDataTypeViolations(pol):
27 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor/",
28 "/data/vendor_ce/", "/data/vendor_de/"], "core_data_file_type")
29
Dan Cashman91d398d2017-09-26 12:58:29 -070030###
31# extend OptionParser to allow the same option flag to be used multiple times.
32# This is used to allow multiple file_contexts files and tests to be
33# specified.
34#
35class MultipleOption(Option):
36 ACTIONS = Option.ACTIONS + ("extend",)
37 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
38 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
39 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
40
41 def take_action(self, action, dest, opt, value, values, parser):
42 if action == "extend":
43 values.ensure_value(dest, []).append(value)
44 else:
45 Option.take_action(self, action, dest, opt, value, values, parser)
46
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080047Tests = ["TestDataTypeViolators", "TestSysfsTypeViolations",
48 "TestDebugfsTypeViolations", "TestVendorTypeViolations",
49 "TestCoreDataTypeViolations"]
Dan Cashman91d398d2017-09-26 12:58:29 -070050
51if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070052 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
Bowgo Tsaiafbcf212018-02-05 17:34:52 +080053 usage += "-f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -070054 usage +="plat_file_contexts -p policy [--test test] [--help]"
55 parser = OptionParser(option_class=MultipleOption, usage=usage)
56 parser.add_option("-f", "--file_contexts", dest="file_contexts",
57 metavar="FILE", action="extend", type="string")
58 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
59 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
60 parser.add_option("-t", "--test", dest="test", action="extend",
61 help="Test options include "+str(Tests))
62
63 (options, args) = parser.parse_args()
64
65 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070066 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -070067 if not os.path.exists(options.libpath):
68 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
69 + parser.usage)
70
71 if not options.policy:
72 sys.exit("Must specify monolithic policy file\n" + parser.usage)
73 if not os.path.exists(options.policy):
74 sys.exit("Error: policy file " + options.policy + " does not exist\n"
75 + parser.usage)
76
77 if not options.file_contexts:
78 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
79 for f in options.file_contexts:
80 if not os.path.exists(f):
81 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
82 parser.usage)
83
84 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
85
86 results = ""
87 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070088 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070089 results += TestDataTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070090 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070091 results += TestSysfsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070092 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070093 results += TestDebugfsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -080094 if options.test is None or "TestVendorTypeViolations" in options.test:
95 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080096 if options.test is None or "TestCoreDataTypeViolations" in options.test:
97 results += TestCoreDataTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -070098
99 if len(results) > 0:
100 sys.exit(results)