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): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame^] | 15 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("sysfs", "sysfs_type") |
| 16 | ret += pol.AssertPathTypesHaveAttr(["/sys/"], ["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 17 | "/sys/kernel/tracing"], "sysfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame^] | 18 | return ret |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 19 | |
| 20 | def TestDebugfsTypeViolations(pol): |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame^] | 21 | ret = pol.AssertGenfsFilesystemTypesHaveAttr("debugfs", "debugfs_type") |
| 22 | ret += pol.AssertGenfsFilesystemTypesHaveAttr("tracefs", "debugfs_type") |
| 23 | ret += pol.AssertPathTypesHaveAttr(["/sys/kernel/debug/", |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 24 | "/sys/kernel/tracing"], [], "debugfs_type") |
Jeff Vander Stoep | 1b82844 | 2018-03-21 17:27:20 -0700 | [diff] [blame^] | 25 | return ret |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 26 | |
| 27 | def TestVendorTypeViolations(pol): |
| 28 | return pol.AssertPathTypesHaveAttr(["/vendor/"], [], "vendor_file_type") |
| 29 | |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 30 | def TestCoreDataTypeViolations(pol): |
Jeff Vander Stoep | 370a52f | 2018-02-08 09:54:59 -0800 | [diff] [blame] | 31 | return pol.AssertPathTypesHaveAttr(["/data/"], ["/data/vendor", |
| 32 | "/data/vendor_ce", "/data/vendor_de"], "core_data_file_type") |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 33 | |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 34 | ### |
| 35 | # extend OptionParser to allow the same option flag to be used multiple times. |
| 36 | # This is used to allow multiple file_contexts files and tests to be |
| 37 | # specified. |
| 38 | # |
| 39 | class MultipleOption(Option): |
| 40 | ACTIONS = Option.ACTIONS + ("extend",) |
| 41 | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
| 42 | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
| 43 | ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) |
| 44 | |
| 45 | def take_action(self, action, dest, opt, value, values, parser): |
| 46 | if action == "extend": |
| 47 | values.ensure_value(dest, []).append(value) |
| 48 | else: |
| 49 | Option.take_action(self, action, dest, opt, value, values, parser) |
| 50 | |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 51 | Tests = ["TestDataTypeViolators", "TestSysfsTypeViolations", |
| 52 | "TestDebugfsTypeViolations", "TestVendorTypeViolations", |
| 53 | "TestCoreDataTypeViolations"] |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 54 | |
| 55 | if __name__ == '__main__': |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 56 | usage = "sepolicy_tests -l $(ANDROID_HOST_OUT)/lib64/libsepolwrap.so " |
Bowgo Tsai | afbcf21 | 2018-02-05 17:34:52 +0800 | [diff] [blame] | 57 | usage += "-f vendor_file_contexts -f " |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 58 | usage +="plat_file_contexts -p policy [--test test] [--help]" |
| 59 | parser = OptionParser(option_class=MultipleOption, usage=usage) |
| 60 | parser.add_option("-f", "--file_contexts", dest="file_contexts", |
| 61 | metavar="FILE", action="extend", type="string") |
| 62 | parser.add_option("-p", "--policy", dest="policy", metavar="FILE") |
| 63 | parser.add_option("-l", "--library-path", dest="libpath", metavar="FILE") |
| 64 | parser.add_option("-t", "--test", dest="test", action="extend", |
| 65 | help="Test options include "+str(Tests)) |
| 66 | |
| 67 | (options, args) = parser.parse_args() |
| 68 | |
| 69 | if not options.libpath: |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 70 | sys.exit("Must specify path to libsepolwrap library\n" + parser.usage) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 71 | if not os.path.exists(options.libpath): |
| 72 | sys.exit("Error: library-path " + options.libpath + " does not exist\n" |
| 73 | + parser.usage) |
| 74 | |
| 75 | if not options.policy: |
| 76 | sys.exit("Must specify monolithic policy file\n" + parser.usage) |
| 77 | if not os.path.exists(options.policy): |
| 78 | sys.exit("Error: policy file " + options.policy + " does not exist\n" |
| 79 | + parser.usage) |
| 80 | |
| 81 | if not options.file_contexts: |
| 82 | sys.exit("Error: Must specify file_contexts file(s)\n" + parser.usage) |
| 83 | for f in options.file_contexts: |
| 84 | if not os.path.exists(f): |
| 85 | sys.exit("Error: File_contexts file " + f + " does not exist\n" + |
| 86 | parser.usage) |
| 87 | |
| 88 | pol = policy.Policy(options.policy, options.file_contexts, options.libpath) |
| 89 | |
| 90 | results = "" |
| 91 | # If an individual test is not specified, run all tests. |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 92 | if options.test is None or "TestDataTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 93 | results += TestDataTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 94 | if options.test is None or "TestSysfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 95 | results += TestSysfsTypeViolations(pol) |
Jeff Vander Stoep | 3ca843a | 2017-10-04 09:42:29 -0700 | [diff] [blame] | 96 | if options.test is None or "TestDebugfsTypeViolations" in options.test: |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 97 | results += TestDebugfsTypeViolations(pol) |
Tri Vo | 30c3c2a | 2018-01-10 11:04:06 -0800 | [diff] [blame] | 98 | if options.test is None or "TestVendorTypeViolations" in options.test: |
| 99 | results += TestVendorTypeViolations(pol) |
Jeff Vander Stoep | ccf965e | 2018-01-24 07:01:13 -0800 | [diff] [blame] | 100 | if options.test is None or "TestCoreDataTypeViolations" in options.test: |
| 101 | results += TestCoreDataTypeViolations(pol) |
Dan Cashman | 91d398d | 2017-09-26 12:58:29 -0700 | [diff] [blame] | 102 | |
| 103 | if len(results) > 0: |
| 104 | sys.exit(results) |