| Elliott Hughes | 6b586e7 | 2021-04-15 13:39:08 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 2 | # Unit tests for genseccomp.py | 
|  | 3 |  | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 4 | import textwrap | 
|  | 5 | import unittest | 
|  | 6 |  | 
|  | 7 | import genseccomp | 
|  | 8 |  | 
|  | 9 | class TestGenseccomp(unittest.TestCase): | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 10 | def test_convert_NRs_to_ranges(self): | 
|  | 11 | ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)]) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 12 | self.assertEqual(len(ranges), 1) | 
|  | 13 | self.assertEqual(ranges[0].begin, 1) | 
|  | 14 | self.assertEqual(ranges[0].end, 3) | 
|  | 15 | self.assertEqual(set(ranges[0].names), {"a", "b"}) | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)]) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 18 | self.assertEqual(len(ranges), 2) | 
|  | 19 | self.assertEqual(ranges[0].begin, 1) | 
|  | 20 | self.assertEqual(ranges[0].end, 2) | 
|  | 21 | self.assertEqual(set(ranges[0].names), {"a"}) | 
|  | 22 | self.assertEqual(ranges[1].begin, 3) | 
|  | 23 | self.assertEqual(ranges[1].end, 4) | 
|  | 24 | self.assertEqual(set(ranges[1].names), {"b"}) | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | def test_convert_to_intermediate_bpf(self): | 
|  | 27 | ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)]) | 
|  | 28 | bpf = genseccomp.convert_to_intermediate_bpf(ranges) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 29 | self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, {fail}, {allow}), //a|b']) | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)]) | 
|  | 32 | bpf = genseccomp.convert_to_intermediate_bpf(ranges) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 33 | self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),', | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 34 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, {fail}, {allow}), //a', | 
|  | 35 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, {fail}, {allow}), //b']) | 
|  | 36 |  | 
|  | 37 | def test_convert_ranges_to_bpf(self): | 
|  | 38 | ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)]) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 39 | bpf = genseccomp.convert_ranges_to_bpf(ranges, priority_syscalls=[]) | 
|  | 40 | self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 2),', | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 41 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0), //a|b', | 
|  | 42 | 'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),']) | 
|  | 43 |  | 
|  | 44 | ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)]) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 45 | bpf = genseccomp.convert_ranges_to_bpf(ranges, priority_syscalls=[]) | 
|  | 46 | self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 1, 0, 4),', | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 47 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),', | 
|  | 48 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 2, 2, 1), //a', | 
|  | 49 | 'BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 4, 1, 0), //b', | 
|  | 50 | 'BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),']) | 
|  | 51 |  | 
|  | 52 | def test_convert_bpf_to_output(self): | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 53 | output = genseccomp.convert_bpf_to_output(["line1", "line2"], | 
|  | 54 | "arm", | 
|  | 55 | name_modifier="") | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 56 | expected_output = textwrap.dedent("""\ | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 57 | // File autogenerated by genseccomp.py - edit at your peril!! | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | #include <linux/filter.h> | 
|  | 60 | #include <errno.h> | 
|  | 61 |  | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 62 | #include "seccomp/seccomp_bpfs.h" | 
| Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 63 | const sock_filter arm_filter[] = { | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 64 | line1 | 
|  | 65 | line2 | 
|  | 66 | }; | 
|  | 67 |  | 
|  | 68 | const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter); | 
|  | 69 | """) | 
| Elliott Hughes | bc6999f | 2021-02-03 13:13:57 -0800 | [diff] [blame] | 70 | self.assertEqual(output, expected_output) | 
| Paul Lawrence | 7ea4090 | 2017-02-14 13:32:23 -0800 | [diff] [blame] | 71 |  | 
|  | 72 |  | 
|  | 73 | if __name__ == '__main__': | 
|  | 74 | unittest.main() |