1from 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 TestSystemTypeViolations(pol):
15    return pol.AssertPathTypesHaveAttr(["/system/"], [], "system_file_type")
16
17def TestProcTypeViolations(pol):
18    return pol.AssertGenfsFilesystemTypesHaveAttr("proc", "proc_type")
19
20def TestSysfsTypeViolations(pol):
21    ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type")
22    ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/",
23                                    "/sys/kernel/tracing"], "sysfs_type")
24    return ret
25
26def TestDebugfsTypeViolations(pol):
27    ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type")
28    ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type")
29    ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/",
30                                    "/sys/kernel/tracing"], [], "debugfs_type")
31    return ret
32
33def TestVendorTypeViolations(pol):
34    return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type")
35
36def TestCoreDataTypeViolations(pol):
37    return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor",
38            "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type")
39
40###
41# extend OptionParser to allow the same option flag to be used multiple times.
42# This is used to allow multiple file_contexts files and tests to be
43# specified.
44#
45class MultipleOption(Option):
46    ACTIONS = Option.ACTIONS + ("extend",)
47    STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
48    TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
49    ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
50
51    def take_action(self, action, dest, opt, value, values, parser):
52        if action == "extend":
53            values.ensure_value(dest, []).append(value)
54        else:
55            Option.take_action(self, action, dest, opt, value, values, parser)
56
57Tests = [
58    "TestDataTypeViolators",
59    "TestProcTypeViolations",
60    "TestSysfsTypeViolations",
61    "TestSystemTypeViolators",
62    "TestDebugfsTypeViolations",
63    "TestVendorTypeViolations",
64    "TestCoreDataTypeViolations",
65]
66
67if __name__ == '__main__':
68    usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so "
69    usage += "-f vendor_file_contexts -f "
70    usage +="plat_file_contexts -p policy [--test test] [--help]"
71    parser = OptionParser(option_class=MultipleOption, usage=usage)
72    parser.add_option("-f", "--file_contexts", dest="file_contexts",
73            metavar="FILE", action="extend", type="string")
74    parser.add_option("-p", "--policy", dest="policy", metavar="FILE")
75    parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE")
76    parser.add_option("-t", "--test", dest="test", action="extend",
77            help="Test options include "+str(Tests))
78
79    (options, args) = parser.parse_args()
80
81    if not options.libpath:
82        sys.exit("Must specify path to libsepolwrap library\n" + parser.usage)
83    if not os.path.exists(options.libpath):
84        sys.exit("Error: library-path " + options.libpath + " does not exist\n"
85                + parser.usage)
86
87    if not options.policy:
88        sys.exit("Must specify monolithic policy file\n" + parser.usage)
89    if not os.path.exists(options.policy):
90        sys.exit("Error: policy file " + options.policy + " does not exist\n"
91                + parser.usage)
92
93    if not options.file_contexts:
94        sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage)
95    for f in options.file_contexts:
96        if not os.path.exists(f):
97            sys.exit("Error: File_contexts file " + f + " does not exist\n" +
98                    parser.usage)
99
100    pol = policy.Policy(options.policy, options.file_contexts, options.libpath)
101
102    results = ""
103    # If an individual test is not specified, run all tests.
104    if options.test is None or "TestDataTypeViolations" in options.test:
105        results += TestDataTypeViolations(pol)
106    if options.test is None or "TestProcTypeViolations" in options.test:
107        results += TestProcTypeViolations(pol)
108    if options.test is None or "TestSysfsTypeViolations" in options.test:
109        results += TestSysfsTypeViolations(pol)
110    if options.test is None or "TestSystemTypeViolations" in options.test:
111        results += TestSystemTypeViolations(pol)
112    if options.test is None or "TestDebugfsTypeViolations" in options.test:
113        results += TestDebugfsTypeViolations(pol)
114    if options.test is None or "TestVendorTypeViolations" in options.test:
115        results += TestVendorTypeViolations(pol)
116    if options.test is None or "TestCoreDataTypeViolations" in options.test:
117        results += TestCoreDataTypeViolations(pol)
118
119    if len(results) > 0:
120        sys.exit(results)
121