summaryrefslogtreecommitdiff
path: root/tests/analysis/db/conn.py
blob: f388f60ad068affe51074720be12fc6ecd3ad5b0 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

from chrysacase import ChrysalideTestCase
from pychrysalide.analysis.db import certs
from pychrysalide.analysis.db import AdminClient
from pychrysalide.analysis.db import HubServer
import os
import shutil
import tempfile
import threading


class TestDbConnection(ChrysalideTestCase):
    """TestCase for analysis.db."""

    @classmethod
    def setUpClass(cls):

        super(TestDbConnection, cls).setUpClass()

        cls._tmp_path = tempfile.mkdtemp()

        cls._server_path = '%s/.chrysalide/servers/localhost-9999/' % cls._tmp_path
        os.makedirs(cls._server_path)

        cls._server_authorized_path = '%s/authorized/' % cls._server_path
        os.makedirs(cls._server_authorized_path)

        cls._client_path = '%s/.chrysalide/clients/' % cls._tmp_path
        os.makedirs(cls._client_path)

        cls._client_cert_path = '%s/localhost-9999/' % cls._client_path
        os.makedirs(cls._client_cert_path)

        cls.log('Using temporary directory "%s"' % cls._tmp_path)


    @classmethod
    def tearDownClass(cls):

        super(TestDbConnection, cls).tearDownClass()

        os.system('ls -laihR %s' % cls._tmp_path)

        cls.log('Delete directory "%s"' % cls._tmp_path)

        shutil.rmtree(cls._tmp_path)


    def testServerListening(self):
        """List binaries available from server."""


        from pychrysalide import core
        #core.set_verbosity(0)



        identity = {

            'C': 'FR',
            'CN': 'Test authority'

        }

        ret = certs.build_keys_and_ca(self._server_path, 'ca', 3650 * 24 * 60 * 60, identity)
        self.assertTrue(ret)

        identity = {

            'C': 'FR',
            'CN': 'Test server'

        }

        ret = certs.build_keys_and_request(self._server_path, 'server', identity);
        self.assertTrue(ret)


        ret = certs.sign_cert('%s/server-csr.pem' % self._server_path, '%s/ca-cert.pem' % self._server_path, \
                              '%s/ca-key.pem' % self._server_path, '%s/server-cert.pem' % self._server_path, \
                              3650 * 24 * 60 * 60)
        self.assertTrue(ret)

        identity = {

            'C': 'FR',
            'CN': 'Test admin'

        }

        ret = certs.build_keys_and_request(self._client_path, 'client', identity);
        self.assertTrue(ret)

        ret = certs.sign_cert('%s/client-csr.pem' % self._client_path, '%s/ca-cert.pem' % self._server_path, \
                              '%s/ca-key.pem' % self._server_path, '%s/client-cert.pem' % self._client_cert_path, \
                              3650 * 24 * 60 * 60)
        self.assertTrue(ret)

        shutil.copy('%s/ca-cert.pem' % self._server_path,
                    '%s/ca-cert.pem' % self._client_cert_path)

        shutil.copy('%s/client-cert.pem' % self._client_cert_path,
                    '%s/client-cert.pem' % self._server_authorized_path)


        os.environ['XDG_CONFIG_HOME'] = self._tmp_path
        os.environ['HOME'] = self._tmp_path

        server = HubServer('localhost', '9999')

        #print(server)

        ret = server.start()

        #print(ret)




        admin = AdminClient()

        ret = admin.start('localhost', '9999')
        self.assertTrue(ret)

        def _on_existing_binaries_updated(adm, evt):
            evt.set()

        event = threading.Event()

        admin.connect('existing-binaries-updated', _on_existing_binaries_updated, event)

        ret = admin.request_existing_binaries()
        self.assertTrue(ret)

        event.wait()

        self.assertEqual(len(admin.existing_binaries), 0)