summaryrefslogtreecommitdiff
path: root/tests/test_envtoolkit_instance_static.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_envtoolkit_instance_static.py')
-rw-r--r--tests/test_envtoolkit_instance_static.py43
1 files changed, 29 insertions, 14 deletions
diff --git a/tests/test_envtoolkit_instance_static.py b/tests/test_envtoolkit_instance_static.py
index d7ac857..73a5f36 100644
--- a/tests/test_envtoolkit_instance_static.py
+++ b/tests/test_envtoolkit_instance_static.py
@@ -1,5 +1,5 @@
1# etoolkit 1# etoolkit
2# Copyright (C) 2021 Simeon Simeonov 2# Copyright (C) 2021-2022 Simeon Simeonov
3 3
4# This program is free software: you can redistribute it and/or modify 4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by 5# it under the terms of the GNU General Public License as published by
@@ -24,14 +24,14 @@ import etoolkit
24@unittest.mock.patch('getpass.getpass') 24@unittest.mock.patch('getpass.getpass')
25def test_confirm_password_prompt(getpass, password_hash): 25def test_confirm_password_prompt(getpass, password_hash):
26 """Tests the static EtoolkitInstance.confirm_password_prompt method""" 26 """Tests the static EtoolkitInstance.confirm_password_prompt method"""
27 getpass.return_value = 'the very secret passwd' 27 getpass.return_value = 'The very secret passwd'
28 assert ( 28 assert (
29 etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash) 29 etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash)
30 == 'the very secret passwd' 30 == 'The very secret passwd'
31 ) 31 )
32 assert ( 32 assert (
33 etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False) 33 etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False)
34 == 'the very secret passwd' 34 == 'The very secret passwd'
35 ) 35 )
36 36
37 37
@@ -39,10 +39,10 @@ def test_decrypt():
39 """Tests the static EtoolkitInstance.decrypt method""" 39 """Tests the static EtoolkitInstance.decrypt method"""
40 assert ( 40 assert (
41 etoolkit.EtoolkitInstance.decrypt( 41 etoolkit.EtoolkitInstance.decrypt(
42 'the very secret passwd', 42 'The very secret passwd',
43 ( 43 (
44 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9' 44 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ4ZoAkurNgMYx'
45 'o+ABvsxogIXpJim16Gz5SVtV8=' 45 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY='
46 ), 46 ),
47 ) 47 )
48 == 'secret1' 48 == 'secret1'
@@ -51,11 +51,11 @@ def test_decrypt():
51 # now test with modified edata 51 # now test with modified edata
52 with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: 52 with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info:
53 edata = ( 53 edata = (
54 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9' 54 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ5ZoAkurNgMYx'
55 'o+ABvsxogIXpJim17Gz5SVtV8=' 55 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY='
56 ) 56 )
57 etoolkit.EtoolkitInstance.decrypt( 57 etoolkit.EtoolkitInstance.decrypt(
58 'the very secret passwd', edata 58 'The very secret passwd', edata
59 ) == 'secret1' 59 ) == 'secret1'
60 assert exc_info.type is etoolkit.EtoolkitInstanceError 60 assert exc_info.type is etoolkit.EtoolkitInstanceError
61 assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' 61 assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}'
@@ -70,17 +70,32 @@ def test_encrypt():
70 assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar') 70 assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar')
71 71
72 72
73@unittest.mock.patch('os.urandom')
74def test_encrypt_staticly(urandom, non_random_bytes_32):
75 """Tests the EtoolkitInstance.encrypt method always with the same salt"""
76 urandom.return_value = non_random_bytes_32
77 edata = etoolkit.EtoolkitInstance.encrypt('The very secret passwd', 'bar')
78 assert edata == (
79 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80'
80 'o=$HjPFNv6xC5hbMrFc0L5lSkWdfQ=='
81 )
82 assert edata == etoolkit.EtoolkitInstance.encrypt(
83 'The very secret passwd',
84 'bar',
85 )
86
87
73def test_get_new_password_hash(): 88def test_get_new_password_hash():
74 """Tests the static EtoolkitInstance.get_new_password_hash method""" 89 """Tests the static EtoolkitInstance.get_new_password_hash method"""
75 new_hash = etoolkit.EtoolkitInstance.get_new_password_hash( 90 new_hash = etoolkit.EtoolkitInstance.get_new_password_hash(
76 'the very secret passwd' 91 'The very secret passwd'
77 ) 92 )
78 # all pbkdf2 params are the same / hardcoded for the time being 93 # all pbkdf2 params are the same / hardcoded for the time being
79 assert new_hash.startswith('pbkdf2_sha256$100000$') 94 assert new_hash.startswith('pbkdf2_sha256$100000$')
80 assert len(new_hash) == 110 95 assert len(new_hash) == 110
81 # the hash should always be different because of random salting 96 # the hash should always be different because of random salting
82 assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( 97 assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash(
83 'the very secret passwd' 98 'The very secret passwd'
84 ) 99 )
85 100
86 101
@@ -95,8 +110,8 @@ def test_parse_value():
95def test_password_matches(password_hash): 110def test_password_matches(password_hash):
96 """Tests the static EtoolkitInstance.password_matches method""" 111 """Tests the static EtoolkitInstance.password_matches method"""
97 assert etoolkit.EtoolkitInstance.password_matches( 112 assert etoolkit.EtoolkitInstance.password_matches(
98 'the very secret passwd', password_hash 113 'The very secret passwd', password_hash
99 ) 114 )
100 assert not etoolkit.EtoolkitInstance.password_matches( 115 assert not etoolkit.EtoolkitInstance.password_matches(
101 'the very secret passwdo', password_hash 116 'The very secret passwdo', password_hash
102 ) 117 )