blob: 3edf1f257766893d0f85494a1a72d75d426455eb [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")
22###
23# extend OptionParser to allow the same option flag to be used multiple times.
24# This is used to allow multiple file_contexts files and tests to be
25# specified.
26#
27class MultipleOption(Option):
28 ACTIONS = Option.ACTIONS + ("extend",)
29 STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
30 TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
31 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
32
33 def take_action(self, action, dest, opt, value, values, parser):
34 if action == "extend":
35 values.ensure_value(dest, []).append(value)
36 else:
37 Option.take_action(self, action, dest, opt, value, values, parser)
38
39Tests = ["TestDataTypeViolators"]
40
41if __name__ == '__main__':
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070042 usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
43 usage += "-f nonplat_file_contexts -f "
Dan Cashman91d398d2017-09-26 12:58:29 -070044 usage +="plat_file_contexts -p policy [--test test] [--help]"
45 parser = OptionParser(option_class=MultipleOption, usage=usage)
46 parser.add_option("-f", "--file_contexts", dest="file_contexts",
47 metavar="FILE", action="extend", type="string")
48 parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
49 parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
50 parser.add_option("-t", "--test", dest="test", action="extend",
51 help="Test options include "+str(Tests))
52
53 (options, args) = parser.parse_args()
54
55 if not options.libpath:
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070056 sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
Dan Cashman91d398d2017-09-26 12:58:29 -070057 if not os.path.exists(options.libpath):
58 sys.exit("Error: library-path " + options.libpath + " does not exist\n"
59 + parser.usage)
60
61 if not options.policy:
62 sys.exit("Must specify monolithic policy file\n" + parser.usage)
63 if not os.path.exists(options.policy):
64 sys.exit("Error: policy file " + options.policy + " does not exist\n"
65 + parser.usage)
66
67 if not options.file_contexts:
68 sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
69 for f in options.file_contexts:
70 if not os.path.exists(f):
71 sys.exit("Error: File_contexts file " + f + " does not exist\n" +
72 parser.usage)
73
74 pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
75
76 results = ""
77 # If an individual test is not specified, run all tests.
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070078 if options.test is None or "TestDataTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070079 results += TestDataTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070080 if options.test is None or "TestSysfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070081 results += TestSysfsTypeViolations(pol)
Jeff Vander Stoep3ca843a2017-10-04 09:42:29 -070082 if options.test is None or "TestDebugfsTypeViolations" in options.test:
Dan Cashman91d398d2017-09-26 12:58:29 -070083 results += TestDebugfsTypeViolations(pol)
84
85 if len(results) > 0:
86 sys.exit(results)