Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 1 | from optparse import OptionParser |
| 2 | from optparse import Option, OptionValueError |
| 3 | import os |
| 4 | import policy |
| 5 | import re |
| 6 | import sys |
| 7 | |
| 8 | ############################################################# |
| 9 | # Tests |
| 10 | ############################################################# |
| 11 | def TestDataTypeViolations(pol): |
| 12 | return pol.AssertPathTypesHaveAttr(["/data/"], [], "data_file_type") |
| 13 | |
| 14 | def TestSysfsTypeViolations(pol): |
| 15 | return pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/", |
| 16 | "/sys/kernel/tracing"], "sysfs_type") |
| 17 | |
| 18 | def 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 Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame^] | 22 | |
| 23 | def TestVendorTypeViolations(pol): |
| 24 | return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type") |
| 25 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 26 | ### |
| 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 | # |
| 31 | class 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 | |
| 43 | Tests = ["TestDataTypeViolators"] |
| 44 | |
| 45 | if __name__ == '__main__': |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 46 | usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so " |
| 47 | usage += "-f nonplat_file_contexts -f " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 48 | 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 Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 60 | sys.exit("Must specify path to libsepolwrap library\n" + parser.usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 61 | 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 Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 82 | if options.test is None or "TestDataTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 83 | results += TestDataTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 84 | if options.test is None or "TestSysfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 85 | results += TestSysfsTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 86 | if options.test is None or "TestDebugfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 87 | results += TestDebugfsTypeViolations(pol) |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame^] | 88 | if options.test is None or "TestVendorTypeViolations" in options.test: |
| 89 | results += TestVendorTypeViolations(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 90 | |
| 91 | if len(results) > 0: |
| 92 | sys.exit(results) |