summaryrefslogtreecommitdiff
path: root/tests/test_envtoolkit_instance_static.py
blob: 5fb9451ffb74805ba27ef0afd58a6a01fa2f09aa (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# etoolkit
# Copyright (C) 2021-2024 Simeon Simeonov

# 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 etoolkit.EtoolkitInstance static methods"""

import unittest.mock

import pytest

import etoolkit


@unittest.mock.patch('getpass.getpass')
def test_confirm_password_prompt(getpass, password_hash):
    """Tests the static EtoolkitInstance.confirm_password_prompt method"""
    getpass.return_value = 'The very secret passwd'
    assert (
        etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash)
        == 'The very secret passwd'
    )
    assert (
        etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False)
        == 'The very secret passwd'
    )


def test_decrypt_v1():
    """Tests the static EtoolkitInstance.decrypt method"""
    assert (
        etoolkit.EtoolkitInstance.decrypt(
            'The very secret passwd',
            (
                'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ4ZoAkurNgMYx'
                'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY='
            ),
        )
        == 'secret1'
    )

    # now test with modified edata
    edata = (
        'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ5ZoAkurNgMYx'
        'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY='
    )
    with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info:
        etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata)
    assert exc_info.type is etoolkit.EtoolkitInstanceError
    assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}'


def test_decrypt_v2_no_padding():
    """Tests the static EtoolkitInstance.decrypt method for v2 - no padding"""
    assert (
        etoolkit.EtoolkitInstance.decrypt(
            'The very secret passwd',
            (
                'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$'
                'SQc0ox6Emf2m5rrumsiptpIZEujdpXXSR/'
                '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8='
            ),
        )
        == 'Nobody expects the Spanish inquisition'
    )

    # now test with modified edata
    edata = (
        'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$'
        'SQc0ox6Emf2m4rrumsiptpIZEujdpXXSR/'
        '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8='
    )
    with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info:
        etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata)
    assert exc_info.type is etoolkit.EtoolkitInstanceError
    assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}'


def test_decrypt_v2_with_padding():
    """Tests the static EtoolkitInstance.decrypt method for v2 with padding"""
    assert (
        etoolkit.EtoolkitInstance.decrypt(
            'The very secret passwd',
            (
                'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$'
                'rhRona4wP9nhnXjcHqwkjFDsiVVVjYanAs'
                'N4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w='
            ),
        )
        == 'secret1'
    )

    # now test with modified edata
    edata = (
        'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$'
        'rhRona4wP8nhnXjcHqwkjFDsiVVVjYanAsN4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w='
    )
    with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info:
        etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata)
    assert exc_info.type is etoolkit.EtoolkitInstanceError
    assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}'


def test_encrypt_no_padding():
    """Tests the static EtoolkitInstance.encrypt method with a long string"""
    edata = etoolkit.EtoolkitInstance.encrypt(
        'foo', 'Nobody expects the Spanish inquisition'
    )
    assert edata.startswith('enc-val$2$')
    assert len(edata) == 131
    # the edata should always be different because of random salting
    assert edata != etoolkit.EtoolkitInstance.encrypt(
        'foo', 'Nobody expects the Spanish inquisition'
    )


def test_encrypt_with_padding():
    """Tests the static EtoolkitInstance.encrypt method with a short string"""
    edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar')
    assert edata.startswith('enc-val$2$')
    assert len(edata) == 123
    # the edata should always be different because of random salting
    assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar')


@unittest.mock.patch('os.urandom')
def test_encrypt_staticly_no_padding(urandom, non_random_bytes_32):
    """Tests the EtoolkitInstance.encrypt method always with the same salt"""
    urandom.return_value = non_random_bytes_32
    edata = etoolkit.EtoolkitInstance.encrypt(
        'The very secret passwd', 'Nobody expects the Spanish inquisition'
    )
    assert edata == (
        'enc-val$2$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80o=$'
        'UX/5YeRsh5/2vZ2J1UOS+BJti73Kbp6C1pJmC'
        'o8hFSujpe35X/XpzAiYv4BV1LNwnSYECsotsgs='
    )
    assert len(edata) == 131
    assert edata == etoolkit.EtoolkitInstance.encrypt(
        'The very secret passwd', 'Nobody expects the Spanish inquisition'
    )


@unittest.mock.patch('os.urandom')
def test_encrypt_staticly_with_padding(urandom, non_random_bytes_61):
    """Tests the EtoolkitInstance.encrypt method always with the same salt"""
    urandom.return_value = non_random_bytes_61
    edata = etoolkit.EtoolkitInstance.encrypt('The very secret passwd', 'bar')
    assert edata == (
        'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviqrLTBxM=$'
        '+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/ESvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4='
    )
    assert edata == etoolkit.EtoolkitInstance.encrypt(
        'The very secret passwd', 'bar'
    )


def test_get_new_password_hash():
    """Tests the static EtoolkitInstance.get_new_password_hash method"""
    new_hash = etoolkit.EtoolkitInstance.get_new_password_hash(
        'The very secret passwd'
    )
    # all pbkdf2 params are the same / hardcoded for the time being
    assert new_hash.startswith('pbkdf2_sha256$500000$')
    assert len(new_hash) == 110
    # the hash should always be different because of random salting
    assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash(
        'The very secret passwd'
    )


def test_parse_value():
    """Tests the static EtoolkitInstance.parse_value method"""
    assert (
        etoolkit.EtoolkitInstance.parse_value('t%bs%t', {'%b': 'e', '%t': 't'})
        == 'test'
    )


def test_password_matches(password_hash):
    """Tests the static EtoolkitInstance.password_matches method"""
    assert etoolkit.EtoolkitInstance.password_matches(
        'The very secret passwd', password_hash
    )
    assert not etoolkit.EtoolkitInstance.password_matches(
        'The very secret passwdo', password_hash
    )