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
|
# etoolkit
# Copyright (C) 2021-2022 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():
"""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
with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info:
edata = (
'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ5ZoAkurNgMYx'
'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY='
)
etoolkit.EtoolkitInstance.decrypt(
'The very secret passwd', edata
) == 'secret1'
assert exc_info.type is etoolkit.EtoolkitInstanceError
assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}'
def test_encrypt():
"""Tests the static EtoolkitInstance.encrypt method"""
edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar')
assert edata.startswith('enc-val$')
assert len(edata) == 83
# 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(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', 'bar')
assert edata == (
'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80'
'o=$HjPFNv6xC5hbMrFc0L5lSkWdfQ=='
)
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$100000$')
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
)
|