blob: b833c2709703f97f7de31ceeb5239f4f4f58e417 [file] [log] [blame]
Elliott Hughes6b586e72021-04-15 13:39:08 -07001#!/usr/bin/env python3
Paul Lawrence7ea40902017-02-14 13:32:23 -08002# Unit tests for genseccomp.py
3
Paul Lawrence7ea40902017-02-14 13:32:23 -08004import textwrap
5import unittest
6
7import genseccomp
8
9class TestGenseccomp(unittest.TestCase):
Paul Lawrence7ea40902017-02-14 13:32:23 -080010 def test_convert_NRs_to_ranges(self):
11 ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
Elliott Hughesbc6999f2021-02-03 13:13:57 -080012 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 Lawrence7ea40902017-02-14 13:32:23 -080016
17 ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
Elliott Hughesbc6999f2021-02-03 13:13:57 -080018 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 Lawrence7ea40902017-02-14 13:32:23 -080025
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 Hughesbc6999f2021-02-03 13:13:57 -080029 self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, {fail}, {allow}), //a|b'])
Paul Lawrence7ea40902017-02-14 13:32:23 -080030
31 ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
32 bpf = genseccomp.convert_to_intermediate_bpf(ranges)
Elliott Hughesbc6999f2021-02-03 13:13:57 -080033 self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
Paul Lawrence7ea40902017-02-14 13:32:23 -080034 '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 Hughesbc6999f2021-02-03 13:13:57 -080039 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 Lawrence7ea40902017-02-14 13:32:23 -080041 '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 Hughesbc6999f2021-02-03 13:13:57 -080045 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 Lawrence7ea40902017-02-14 13:32:23 -080047 '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 Hughesbc6999f2021-02-03 13:13:57 -080053 output = genseccomp.convert_bpf_to_output(["line1", "line2"],
54 "arm",
55 name_modifier="")
Paul Lawrence7ea40902017-02-14 13:32:23 -080056 expected_output = textwrap.dedent("""\
Elliott Hughesbc6999f2021-02-03 13:13:57 -080057 // File autogenerated by genseccomp.py - edit at your peril!!
Paul Lawrence7ea40902017-02-14 13:32:23 -080058
59 #include <linux/filter.h>
60 #include <errno.h>
61
Elliott Hughesbc6999f2021-02-03 13:13:57 -080062 #include "seccomp/seccomp_bpfs.h"
Paul Lawrencedfe84342017-02-16 09:24:39 -080063 const sock_filter arm_filter[] = {
Paul Lawrence7ea40902017-02-14 13:32:23 -080064 line1
65 line2
66 };
67
68 const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
69 """)
Elliott Hughesbc6999f2021-02-03 13:13:57 -080070 self.assertEqual(output, expected_output)
Paul Lawrence7ea40902017-02-14 13:32:23 -080071
72
73if __name__ == '__main__':
74 unittest.main()