Ejemplo n.º 1
0
    def _run_test(self, param, expected):
        obj = NetworkRule.parse(self.rule)
        check_obj = NetworkRule.parse(param)

        self.assertTrue(NetworkRule.match(param))

        self.assertEqual(obj.is_equal(check_obj), expected[0], 'Mismatch in is_equal, expected %s' % expected[0])
        self.assertEqual(obj.is_equal(check_obj, True), expected[1], 'Mismatch in is_equal/strict, expected %s' % expected[1])

        self.assertEqual(obj.is_covered(check_obj), expected[2], 'Mismatch in is_covered, expected %s' % expected[2])
        self.assertEqual(obj.is_covered(check_obj, True, True), expected[3], 'Mismatch in is_covered/exact, expected %s' % expected[3])
Ejemplo n.º 2
0
    def test_invalid_is_equal(self):
        obj = NetworkRule.parse('network inet,')

        testobj = BaseRule()  # different type

        with self.assertRaises(AppArmorBug):
            obj.is_equal(testobj)
Ejemplo n.º 3
0
    def test_ruleset_2(self):
        ruleset = NetworkRuleset()
        rules = [
            'network inet6 raw,',
            'allow network inet,',
            'deny network udp, # example comment',
        ]

        expected_raw = [
            '  network inet6 raw,',
            '  allow network inet,',
            '  deny network udp, # example comment',
            '',
        ]

        expected_clean = [
            '  deny network udp, # example comment',
            '',
            '  allow network inet,',
            '  network inet6 raw,',
            '',
        ]

        for rule in rules:
            ruleset.add(NetworkRule.parse(rule))

        self.assertEqual(expected_raw, ruleset.get_raw(1))
        self.assertEqual(expected_clean, ruleset.get_clean(1))
Ejemplo n.º 4
0
    def test_ruleset_2(self):
        ruleset = NetworkRuleset()
        rules = [
            'network inet6 raw,',
            'allow network inet,',
            'deny network udp, # example comment',
        ]

        expected_raw = [
            '  network inet6 raw,',
            '  allow network inet,',
            '  deny network udp, # example comment',
            '',
        ]

        expected_clean = [
            '  deny network udp, # example comment',
            '',
            '  allow network inet,',
            '  network inet6 raw,',
            '',
        ]

        for rule in rules:
            ruleset.add(NetworkRule.parse(rule))

        self.assertEqual(expected_raw, ruleset.get_raw(1))
        self.assertEqual(expected_clean, ruleset.get_clean(1))
Ejemplo n.º 5
0
    def test_network_ruleset_repr(self):
        obj = NetworkRuleset()
        obj.add(NetworkRule('inet', 'stream'))
        obj.add(NetworkRule.parse(' allow  network  inet  stream, # foo'))

        expected = '<NetworkRuleset>\n  network inet stream,\n  allow  network  inet  stream, # foo\n</NetworkRuleset>'
        self.assertEqual(str(obj), expected)
Ejemplo n.º 6
0
    def test_invalid_is_equal(self):
        obj = NetworkRule.parse('network inet,')

        testobj = BaseRule()  # different type

        with self.assertRaises(AppArmorBug):
            obj.is_equal(testobj)
Ejemplo n.º 7
0
    def _check_invalid_rawrule(self, rawrule):
        obj = None
        self.assertFalse(NetworkRule.match(rawrule))
        with self.assertRaises(AppArmorException):
            obj = NetworkRule(NetworkRule.parse(rawrule))

        self.assertIsNone(obj, 'NetworkRule handed back an object unexpectedly')
Ejemplo n.º 8
0
    def test_borked_obj_is_covered_1(self):
        obj = NetworkRule.parse('network inet,')

        testobj = NetworkRule('inet', 'stream')
        testobj.domain = ''

        with self.assertRaises(AppArmorBug):
            obj.is_covered(testobj)
Ejemplo n.º 9
0
    def _run_test(self, param, expected):
        obj = NetworkRule.parse(self.rule)
        check_obj = NetworkRule.parse(param)

        self.assertTrue(NetworkRule.match(param))

        self.assertEqual(obj.is_equal(check_obj), expected[0],
                         'Mismatch in is_equal, expected %s' % expected[0])
        self.assertEqual(
            obj.is_equal(check_obj, True), expected[1],
            'Mismatch in is_equal/strict, expected %s' % expected[1])

        self.assertEqual(obj.is_covered(check_obj), expected[2],
                         'Mismatch in is_covered, expected %s' % expected[2])
        self.assertEqual(
            obj.is_covered(check_obj, True, True), expected[3],
            'Mismatch in is_covered/exact, expected %s' % expected[3])
Ejemplo n.º 10
0
    def _run_test(self, rawrule, expected):
        self.assertTrue(NetworkRule.match(rawrule))
        obj = NetworkRule.parse(rawrule)
        clean = obj.get_clean()
        raw = obj.get_raw()

        self.assertEqual(expected.strip(), clean, 'unexpected clean rule')
        self.assertEqual(rawrule.strip(), raw, 'unexpected raw rule')
Ejemplo n.º 11
0
    def _check_invalid_rawrule(self, rawrule):
        obj = None
        self.assertFalse(NetworkRule.match(rawrule))
        with self.assertRaises(AppArmorException):
            obj = NetworkRule(NetworkRule.parse(rawrule))

        self.assertIsNone(obj,
                          'NetworkRule handed back an object unexpectedly')
Ejemplo n.º 12
0
    def test_borked_obj_is_covered_2(self):
        obj = NetworkRule.parse('network inet,')

        testobj = NetworkRule('inet', 'stream')
        testobj.type_or_protocol = ''

        with self.assertRaises(AppArmorBug):
            obj.is_covered(testobj)
Ejemplo n.º 13
0
    def test_borked_obj_is_covered_1(self):
        obj = NetworkRule.parse('network inet,')

        testobj = NetworkRule('inet', 'stream')
        testobj.domain = ''

        with self.assertRaises(AppArmorBug):
            obj.is_covered(testobj)
Ejemplo n.º 14
0
    def test_borked_obj_is_covered_2(self):
        obj = NetworkRule.parse('network inet,')

        testobj = NetworkRule('inet', 'stream')
        testobj.type_or_protocol = ''

        with self.assertRaises(AppArmorBug):
            obj.is_covered(testobj)
Ejemplo n.º 15
0
    def _run_test(self, rawrule, expected):
        self.assertTrue(NetworkRule.match(rawrule))
        obj = NetworkRule.parse(rawrule)
        clean = obj.get_clean()
        raw = obj.get_raw()

        self.assertEqual(expected.strip(), clean, 'unexpected clean rule')
        self.assertEqual(rawrule.strip(), raw, 'unexpected raw rule')
Ejemplo n.º 16
0
class NetworkRuleReprTest(AATest):
    tests = [
        (NetworkRule('inet', 'stream'), '<NetworkRule> network inet stream,'),
        (NetworkRule.parse(' allow  network  inet  stream, # foo'),
         '<NetworkRule> allow  network  inet  stream, # foo'),
    ]

    def _run_test(self, params, expected):
        self.assertEqual(str(params), expected)
Ejemplo n.º 17
0
    def test_ruleset_1(self):
        ruleset = NetworkRuleset()
        rules = [
            'network tcp,',
            'network inet,',
        ]

        expected_raw = [
            'network tcp,',
            'network inet,',
            '',
        ]

        expected_clean = [
            'network inet,',
            'network tcp,',
            '',
        ]

        for rule in rules:
            ruleset.add(NetworkRule.parse(rule))

        self.assertEqual(expected_raw, ruleset.get_raw())
        self.assertEqual(expected_clean, ruleset.get_clean())
Ejemplo n.º 18
0
    def test_ruleset_1(self):
        ruleset = NetworkRuleset()
        rules = [
            'network tcp,',
            'network inet,',
        ]

        expected_raw = [
            'network tcp,',
            'network inet,',
            '',
        ]

        expected_clean = [
            'network inet,',
            'network tcp,',
            '',
        ]

        for rule in rules:
            ruleset.add(NetworkRule.parse(rule))

        self.assertEqual(expected_raw, ruleset.get_raw())
        self.assertEqual(expected_clean, ruleset.get_clean())
Ejemplo n.º 19
0
 def _run_test(self, rawrule, expected):
     self.assertTrue(NetworkRule.match(
         rawrule))  # the above invalid rules still match the main regex!
     with self.assertRaises(expected):
         NetworkRule.parse(rawrule)
Ejemplo n.º 20
0
 def _run_test(self, rawrule, expected):
     self.assertTrue(NetworkRule.match(rawrule))
     obj = NetworkRule.parse(rawrule)
     self.assertEqual(rawrule.strip(), obj.raw_rule)
     self._compare_obj(obj, expected)
Ejemplo n.º 21
0
 def _run_test(self, rawrule, expected):
     self.assertTrue(NetworkRule.match(rawrule))
     obj = NetworkRule.parse(rawrule)
     self.assertEqual(rawrule.strip(), obj.raw_rule)
     self._compare_obj(obj, expected)
Ejemplo n.º 22
0
 def _run_test(self, rawrule, expected):
     self.assertTrue(NetworkRule.match(rawrule))  # the above invalid rules still match the main regex!
     with self.assertRaises(expected):
         NetworkRule.parse(rawrule)