blob: 6f6914759880ad82c3f68c51340aec83478b677c [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
Tri Vo4c80c2c2018-03-29 03:42:47 +000014def TestProcTypeViolations(pol):
15 return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
16
Dan Cashman91d398d2017-09-26 12:58:29 -070017def TestSysfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070018 ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
19 ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070020 "/sys/kernel/tracing"], "sysfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070021 return ret
Dan Cashman91d398d2017-09-26 12:58:29 -070022
23def TestDebugfsTypeViolations(pol):
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070024 ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
25 ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type")
26 ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
Dan Cashman91d398d2017-09-26 12:58:29 -070027 "/sys/kernel/tracing"], [], "debugfs_type")
Jeff Vander Stoep1b828442018-03-21 17:27:20 -070028 return ret
Tri Vo30c3c2a2018-01-10 11:04:06 -080029
30def TestVendorTypeViolations(pol):
31 return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type")
32
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080033def TestCoreDataTypeViolations(pol):
Jeff Vander Stoep370a52f2018-02-08 09:54:59 -080034 return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
35 "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -080036
Dan Cashman91d398d2017-09-26 12:58:29 -070037###
38# extend OptionParser to allow the same option flag to be used multiple times.
39# This is used to allow multiple file_contexts files and tests to be
40# specified.
41#
42class MultipleOption(Option):
43 ACTIONS = Option.ACTIONS + ("extend",)
44 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
45 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
46 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
47
48 def take_action(self, action, dest, opt, value, values, parser):
49 if action == "extend":
50 values.ensure_value(dest, []).append(value)
51 else:
52 Option.take_action(self, action, dest, opt, value, values, parser)
53
Tri Vo4c80c2c2018-03-29 03:42:47 +000054Tests = [
55 "TestDataTypeViolators",
56 "TestProcTypeViolations",
57 "TestSysfsTypeViolations",
58 "TestDebugfsTypeViolations",
59 "TestVendorTypeViolations",
60 "TestCoreDataTypeViolations",
61]
Dan Cashman91d398d2017-09-26 12:58:29 -070062
63if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070064 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
Bowgo Tsaiafbcf212018-02-05 17:34:52 +080065 usage += "-f vendor_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -070066 usage +="plat_file_contexts -p policy [--test test] [--help]"
67 parser = OptionParser(option_class=MultipleOption, usage=usage)
68 parser.add_option("-f", "--file_contexts", dest="file_contexts",
69 metavar="FILE", action="extend", type="string")
70 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
71 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
72 parser.add_option("-t", "--test", dest="test", action="extend",
73 help="Test options include "+str(Tests))
74
75 (options, args) = parser.parse_args()
76
77 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070078 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -070079 if not os.path.exists(options.libpath):
80 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
81 + parser.usage)
82
83 if not options.policy:
84 sys.exit("Must specify monolithic policy file\n" + parser.usage)
85 if not os.path.exists(options.policy):
86 sys.exit("Error: policy file " + options.policy + " does not exist\n"
87 + parser.usage)
88
89 if not options.file_contexts:
90 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
91 for f in options.file_contexts:
92 if not os.path.exists(f):
93 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
94 parser.usage)
95
96 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
97
98 results = ""
99 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700100 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700101 results += TestDataTypeViolations(pol)
Tri Vo4c80c2c2018-03-29 03:42:47 +0000102 if options.test is None or "TestProcTypeViolations" in options.test:
103 results += TestProcTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700104 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700105 results += TestSysfsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -0700106 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -0700107 results += TestDebugfsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -0800108 if options.test is None or "TestVendorTypeViolations" in options.test:
109 results += TestVendorTypeViolations(pol)
Jeff Vander Stoepccf965e2018-01-24 07:01:13 -0800110 if options.test is None or "TestCoreDataTypeViolations" in options.test:
111 results += TestCoreDataTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -0700112
113 if len(results) > 0:
114 sys.exit(results)