summaryrefslogtreecommitdiff
path: root/tests/integration/keymanager/test_refresher.py
blob: 24ad4b054448f9fa03c9166cb25c730e8c648b99 (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
# -*- coding: utf-8 -*-
# test_refresher.py
# Copyright (C) 2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Tests for refreshing the key directory.
"""
from datetime import datetime

from mock import Mock, patch
from twisted.internet import defer
from twisted.logger import Logger


from leap.bitmask.keymanager import openpgp
from leap.bitmask.keymanager.keys import OpenPGPKey
from leap.bitmask.keymanager.refresher import RandomRefreshPublicKey, \
    MIN_RANDOM_INTERVAL_RANGE, DEBUG_START_REFRESH, DEBUG_STOP_REFRESH,\
    ERROR_UNEQUAL_FINGERPRINTS
from leap.bitmask.keymanager.testing import KeyManagerWithSoledadTestCase

from common import KEY_FINGERPRINT

ANOTHER_FP = 'ANOTHERFINGERPRINT'


class RandomRefreshPublicKeyTestCase(KeyManagerWithSoledadTestCase):

    @defer.inlineCallbacks
    def test_get_random_address(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        rf = RandomRefreshPublicKey(pgp, self._key_manager())
        key = OpenPGPKey(address='user@leap.se')
        key_another = OpenPGPKey(address='zara@leap.se')

        pgp.get_all_keys = Mock(return_value=defer.succeed([key, key_another]))

        random_key = yield rf._get_random_key()
        self.assertTrue(random_key.address == key.address or
                        random_key.address == key_another.address)

    @defer.inlineCallbacks
    def test_do_not_throw_error_for_empty_key_dict(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        rf = RandomRefreshPublicKey(pgp, self._key_manager())

        pgp.get_all_keys = Mock(return_value=defer.succeed([]))
        random_address = yield rf._get_random_key()
        self.assertTrue(random_address is None)

    @defer.inlineCallbacks
    def test_log_error_if_fetch_by_fingerprint_returns_wrong_key(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        km = self._key_manager()

        with patch.object(Logger, 'error') as mock_logger_error:
            rf = RandomRefreshPublicKey(pgp, km)
            rf._get_random_key = \
                Mock(return_value=defer.succeed(OpenPGPKey(
                    fingerprint=KEY_FINGERPRINT)))

            km._nicknym.fetch_key_with_fingerprint = \
                Mock(return_value=defer.succeed(OpenPGPKey(
                    fingerprint=ANOTHER_FP)))

            yield rf.maybe_refresh_key()

            mock_logger_error.assert_called_with(ERROR_UNEQUAL_FINGERPRINTS %
                                                 (KEY_FINGERPRINT, ANOTHER_FP))

    @defer.inlineCallbacks
    def test_put_new_key_in_local_storage(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        km = self._key_manager()

        rf = RandomRefreshPublicKey(pgp, km)
        rf._get_random_key = Mock(return_value=defer.succeed(
            OpenPGPKey(fingerprint=KEY_FINGERPRINT)))

        km._nicknym.fetch_key_with_fingerprint = Mock(
            return_value=defer.succeed(OpenPGPKey(fingerprint=ANOTHER_FP)))

        yield rf.maybe_refresh_key()

    @defer.inlineCallbacks
    def test_key_expired_will_be_deactivatet(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        km = self._key_manager()
        rf = RandomRefreshPublicKey(pgp, km)
        key = OpenPGPKey(address='zara@leap.se', expiry_date=datetime.now())
        self.assertTrue(key.address is 'zara@leap.se')
        pgp.unactivate_key = Mock(return_value=defer.succeed(None))

        yield rf._maybe_unactivate_key(key)

        self.assertTrue(key.address is None)
        self.assertFalse(key.is_active())

    def test_start_refreshing(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)

        with patch.object(Logger, 'debug') as mock_logger_start:
            rf = RandomRefreshPublicKey(pgp, self._key_manager())
            rf.start()
            mock_logger_start.assert_called_with(DEBUG_START_REFRESH)
            rf.stop()
            mock_logger_start.assert_called_with(DEBUG_STOP_REFRESH)

    def test_random_interval_is_set_properly(self):
        pgp = openpgp.OpenPGPScheme(
            self._soledad, gpgbinary=self.gpg_binary_path)
        rf = RandomRefreshPublicKey(pgp, self._key_manager())
        self.assertTrue(rf._loop.interval >= MIN_RANDOM_INTERVAL_RANGE)