blob: 275debb0ba519085d3f3d0c50460e0b210efda27 [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
Dan Cashman91d398d2017-09-26 12:58:29 -070026###
27# extend OptionParser to allow the same option flag to be used multiple times.
28# This is used to allow multiple file_contexts files and tests to be
29# specified.
30#
31class MultipleOption(Option):
32 ACTIONS = Option.ACTIONS + ("extend",)
33 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
34 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
35 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
36
37 def take_action(self, action, dest, opt, value, values, parser):
38 if action == "extend":
39 values.ensure_value(dest, []).append(value)
40 else:
41 Option.take_action(self, action, dest, opt, value, values, parser)
42
43Tests = ["TestDataTypeViolators"]
44
45if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070046 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
47 usage += "-f nonplat_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -070048 usage +="plat_file_contexts -p policy [--test test] [--help]"
49 parser = OptionParser(option_class=MultipleOption, usage=usage)
50 parser.add_option("-f", "--file_contexts", dest="file_contexts",
51 metavar="FILE", action="extend", type="string")
52 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
53 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
54 parser.add_option("-t", "--test", dest="test", action="extend",
55 help="Test options include "+str(Tests))
56
57 (options, args) = parser.parse_args()
58
59 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070060 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -070061 if not os.path.exists(options.libpath):
62 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
63 + parser.usage)
64
65 if not options.policy:
66 sys.exit("Must specify monolithic policy file\n" + parser.usage)
67 if not os.path.exists(options.policy):
68 sys.exit("Error: policy file " + options.policy + " does not exist\n"
69 + parser.usage)
70
71 if not options.file_contexts:
72 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
73 for f in options.file_contexts:
74 if not os.path.exists(f):
75 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
76 parser.usage)
77
78 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
79
80 results = ""
81 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070082 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070083 results += TestDataTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070084 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070085 results += TestSysfsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070086 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070087 results += TestDebugfsTypeViolations(pol)
Tri Vo30c3c2a2018-01-10 11:04:06 -080088 if options.test is None or "TestVendorTypeViolations" in options.test:
89 results += TestVendorTypeViolations(pol)
Dan Cashman91d398d2017-09-26 12:58:29 -070090
91 if len(results) > 0:
92 sys.exit(results)