• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Unit tests for genseccomp.py
3
4import textwrap
5import unittest
6
7import genseccomp
8
9class TestGenseccomp(unittest.TestCase):
10  def test_convert_NRs_to_ranges(self):
11    ranges = genseccomp.convert_NRs_to_ranges([("b", 2), ("a", 1)])
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"})
16
17    ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
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"})
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)
29    self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, {fail}, {allow}), //a|b'])
30
31    ranges = genseccomp.convert_NRs_to_ranges([("b", 3), ("a", 1)])
32    bpf = genseccomp.convert_to_intermediate_bpf(ranges)
33    self.assertEqual(bpf, ['BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 3, 1, 0),',
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)])
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),',
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)])
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),',
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):
53    output = genseccomp.convert_bpf_to_output(["line1", "line2"],
54                                              "arm",
55                                              name_modifier="")
56    expected_output = textwrap.dedent("""\
57    // File autogenerated by genseccomp.py - edit at your peril!!
58
59    #include <linux/filter.h>
60    #include <errno.h>
61
62    #include "seccomp/seccomp_bpfs.h"
63    const sock_filter arm_filter[] = {
64    line1
65    line2
66    };
67
68    const size_t arm_filter_size = sizeof(arm_filter) / sizeof(struct sock_filter);
69    """)
70    self.assertEqual(output, expected_output)
71
72
73if __name__ == '__main__':
74  unittest.main()
75