summaryrefslogtreecommitdiffstats
path: root/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestCfg/TestCfgEncryptedGenerator.py
blob: f07994d3418f21cc30b6caf0593a6c211c511207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import sys
import lxml.etree
import Bcfg2.Server.Plugins.Cfg
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator import *
from Bcfg2.Server.Plugin import PluginExecutionError

# add all parent testsuite directories to sys.path to allow (most)
# relative imports in python 2.4
path = os.path.dirname(__file__)
while path != "/":
    if os.path.basename(path).lower().startswith("test"):
        sys.path.append(path)
    if os.path.basename(path) == "testsuite":
        break
    path = os.path.dirname(path)
from common import *
from TestServer.TestPlugins.TestCfg.Test_init import TestCfgGenerator


if can_skip or HAS_CRYPTO:
    class TestCfgEncryptedGenerator(TestCfgGenerator):
        test_obj = CfgEncryptedGenerator

        @skipUnless(HAS_CRYPTO, "Encryption libraries not found, skipping")
        def setUp(self):
            pass

        @patchIf(HAS_CRYPTO,
                 "Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator.get_algorithm")
        @patchIf(HAS_CRYPTO,
                 "Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator.bruteforce_decrypt")
        @patch("Bcfg2.Server.Plugins.Cfg.CfgEncryptedGenerator.SETUP")
        def test_handle_event(self, mock_SETUP, mock_decrypt,
                              mock_get_algorithm):
            @patch("Bcfg2.Server.Plugins.Cfg.CfgGenerator.handle_event")
            def inner(mock_handle_event):
                def reset():
                    mock_decrypt.reset_mock()
                    mock_get_algorithm.reset_mock()
                    mock_handle_event.reset_mock()

                def get_event_data(obj, event):
                    obj.data = "encrypted"

                mock_handle_event.side_effect = get_event_data
                mock_decrypt.side_effect = lambda d, **kw: "plaintext"
                event = Mock()
                ceg = self.get_obj()
                ceg.handle_event(event)
                mock_handle_event.assert_called_with(ceg, event)
                mock_decrypt.assert_called_with(
                    "encrypted",
                    setup=mock_SETUP,
                    algorithm=mock_get_algorithm.return_value)
                self.assertEqual(ceg.data, "plaintext")

                reset()
                mock_decrypt.side_effect = EVPError
                self.assertRaises(PluginExecutionError,
                                  ceg.handle_event, event)
            inner()

            # to perform the tests from the parent test object, we
            # make bruteforce_decrypt just return whatever data was
            # given to it
            mock_decrypt.side_effect = lambda d, **kw: d
            TestCfgGenerator.test_handle_event(self)

        def test_get_data(self):
            ceg = self.get_obj()
            ceg.data = None
            entry = lxml.etree.Element("Path", name="/test.txt")
            metadata = Mock()

            self.assertRaises(PluginExecutionError,
                              ceg.get_data, entry, metadata)

            TestCfgGenerator.test_get_data(self)