diff options
Diffstat (limited to 'tests/test_envtoolkit_instance_static.py')
| -rw-r--r-- | tests/test_envtoolkit_instance_static.py | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/tests/test_envtoolkit_instance_static.py b/tests/test_envtoolkit_instance_static.py index 5fb9451..b3b24f0 100644 --- a/tests/test_envtoolkit_instance_static.py +++ b/tests/test_envtoolkit_instance_static.py | |||
| @@ -23,24 +23,26 @@ import etoolkit | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | @unittest.mock.patch('getpass.getpass') | 25 | @unittest.mock.patch('getpass.getpass') |
| 26 | def test_confirm_password_prompt(getpass, password_hash): | 26 | def test_confirm_password_prompt(getpass, password_hash, master_password): |
| 27 | """Tests the static EtoolkitInstance.confirm_password_prompt method""" | 27 | """Tests the static EtoolkitInstance.confirm_password_prompt method""" |
| 28 | getpass.return_value = 'The very secret passwd' | 28 | |
| 29 | getpass.return_value = master_password | ||
| 29 | assert ( | 30 | assert ( |
| 30 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash) | 31 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash) |
| 31 | == 'The very secret passwd' | 32 | == master_password |
| 32 | ) | 33 | ) |
| 33 | assert ( | 34 | assert ( |
| 34 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False) | 35 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False) |
| 35 | == 'The very secret passwd' | 36 | == master_password |
| 36 | ) | 37 | ) |
| 37 | 38 | ||
| 38 | 39 | ||
| 39 | def test_decrypt_v1(): | 40 | def test_decrypt_v1(master_password): |
| 40 | """Tests the static EtoolkitInstance.decrypt method""" | 41 | """Tests the static EtoolkitInstance.decrypt method""" |
| 42 | |||
| 41 | assert ( | 43 | assert ( |
| 42 | etoolkit.EtoolkitInstance.decrypt( | 44 | etoolkit.EtoolkitInstance.decrypt( |
| 43 | 'The very secret passwd', | 45 | master_password, |
| 44 | ( | 46 | ( |
| 45 | 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ4ZoAkurNgMYx' | 47 | 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ4ZoAkurNgMYx' |
| 46 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' | 48 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' |
| @@ -55,16 +57,17 @@ def test_decrypt_v1(): | |||
| 55 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' | 57 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' |
| 56 | ) | 58 | ) |
| 57 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | 59 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: |
| 58 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) | 60 | etoolkit.EtoolkitInstance.decrypt(master_password, edata) |
| 59 | assert exc_info.type is etoolkit.EtoolkitInstanceError | 61 | assert exc_info.type is etoolkit.EtoolkitInstanceError |
| 60 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | 62 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' |
| 61 | 63 | ||
| 62 | 64 | ||
| 63 | def test_decrypt_v2_no_padding(): | 65 | def test_decrypt_v2_no_padding(master_password): |
| 64 | """Tests the static EtoolkitInstance.decrypt method for v2 - no padding""" | 66 | """Tests the static EtoolkitInstance.decrypt method for v2 - no padding""" |
| 67 | |||
| 65 | assert ( | 68 | assert ( |
| 66 | etoolkit.EtoolkitInstance.decrypt( | 69 | etoolkit.EtoolkitInstance.decrypt( |
| 67 | 'The very secret passwd', | 70 | master_password, |
| 68 | ( | 71 | ( |
| 69 | 'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$' | 72 | 'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$' |
| 70 | 'SQc0ox6Emf2m5rrumsiptpIZEujdpXXSR/' | 73 | 'SQc0ox6Emf2m5rrumsiptpIZEujdpXXSR/' |
| @@ -81,16 +84,17 @@ def test_decrypt_v2_no_padding(): | |||
| 81 | '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8=' | 84 | '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8=' |
| 82 | ) | 85 | ) |
| 83 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | 86 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: |
| 84 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) | 87 | etoolkit.EtoolkitInstance.decrypt(master_password, edata) |
| 85 | assert exc_info.type is etoolkit.EtoolkitInstanceError | 88 | assert exc_info.type is etoolkit.EtoolkitInstanceError |
| 86 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | 89 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' |
| 87 | 90 | ||
| 88 | 91 | ||
| 89 | def test_decrypt_v2_with_padding(): | 92 | def test_decrypt_v2_with_padding(master_password): |
| 90 | """Tests the static EtoolkitInstance.decrypt method for v2 with padding""" | 93 | """Tests the static EtoolkitInstance.decrypt method for v2 with padding""" |
| 94 | |||
| 91 | assert ( | 95 | assert ( |
| 92 | etoolkit.EtoolkitInstance.decrypt( | 96 | etoolkit.EtoolkitInstance.decrypt( |
| 93 | 'The very secret passwd', | 97 | master_password, |
| 94 | ( | 98 | ( |
| 95 | 'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$' | 99 | 'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$' |
| 96 | 'rhRona4wP9nhnXjcHqwkjFDsiVVVjYanAs' | 100 | 'rhRona4wP9nhnXjcHqwkjFDsiVVVjYanAs' |
| @@ -106,39 +110,44 @@ def test_decrypt_v2_with_padding(): | |||
| 106 | 'rhRona4wP8nhnXjcHqwkjFDsiVVVjYanAsN4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w=' | 110 | 'rhRona4wP8nhnXjcHqwkjFDsiVVVjYanAsN4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w=' |
| 107 | ) | 111 | ) |
| 108 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | 112 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: |
| 109 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) | 113 | etoolkit.EtoolkitInstance.decrypt(master_password, edata) |
| 110 | assert exc_info.type is etoolkit.EtoolkitInstanceError | 114 | assert exc_info.type is etoolkit.EtoolkitInstanceError |
| 111 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | 115 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' |
| 112 | 116 | ||
| 113 | 117 | ||
| 114 | def test_encrypt_no_padding(): | 118 | def test_encrypt_no_padding(master_password): |
| 115 | """Tests the static EtoolkitInstance.encrypt method with a long string""" | 119 | """Tests the static EtoolkitInstance.encrypt method with a long string""" |
| 120 | |||
| 116 | edata = etoolkit.EtoolkitInstance.encrypt( | 121 | edata = etoolkit.EtoolkitInstance.encrypt( |
| 117 | 'foo', 'Nobody expects the Spanish inquisition' | 122 | master_password, 'Nobody expects the Spanish inquisition' |
| 118 | ) | 123 | ) |
| 119 | assert edata.startswith('enc-val$2$') | 124 | assert edata.startswith('enc-val$2$') |
| 120 | assert len(edata) == 131 | 125 | assert len(edata) == 131 |
| 121 | # the edata should always be different because of random salting | 126 | # the edata should always be different because of random salting |
| 122 | assert edata != etoolkit.EtoolkitInstance.encrypt( | 127 | assert edata != etoolkit.EtoolkitInstance.encrypt( |
| 123 | 'foo', 'Nobody expects the Spanish inquisition' | 128 | master_password, 'Nobody expects the Spanish inquisition' |
| 124 | ) | 129 | ) |
| 125 | 130 | ||
| 126 | 131 | ||
| 127 | def test_encrypt_with_padding(): | 132 | def test_encrypt_with_padding(master_password): |
| 128 | """Tests the static EtoolkitInstance.encrypt method with a short string""" | 133 | """Tests the static EtoolkitInstance.encrypt method with a short string""" |
| 129 | edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | 134 | |
| 135 | edata = etoolkit.EtoolkitInstance.encrypt(master_password, 'bar') | ||
| 130 | assert edata.startswith('enc-val$2$') | 136 | assert edata.startswith('enc-val$2$') |
| 131 | assert len(edata) == 123 | 137 | assert len(edata) == 123 |
| 132 | # the edata should always be different because of random salting | 138 | # the edata should always be different because of random salting |
| 133 | assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | 139 | assert edata != etoolkit.EtoolkitInstance.encrypt(master_password, 'bar') |
| 134 | 140 | ||
| 135 | 141 | ||
| 136 | @unittest.mock.patch('os.urandom') | 142 | @unittest.mock.patch('os.urandom') |
| 137 | def test_encrypt_staticly_no_padding(urandom, non_random_bytes_32): | 143 | def test_encrypt_staticly_no_padding( |
| 144 | urandom, master_password, non_random_bytes_32 | ||
| 145 | ): | ||
| 138 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" | 146 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" |
| 147 | |||
| 139 | urandom.return_value = non_random_bytes_32 | 148 | urandom.return_value = non_random_bytes_32 |
| 140 | edata = etoolkit.EtoolkitInstance.encrypt( | 149 | edata = etoolkit.EtoolkitInstance.encrypt( |
| 141 | 'The very secret passwd', 'Nobody expects the Spanish inquisition' | 150 | master_password, 'Nobody expects the Spanish inquisition' |
| 142 | ) | 151 | ) |
| 143 | assert edata == ( | 152 | assert edata == ( |
| 144 | 'enc-val$2$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80o=$' | 153 | 'enc-val$2$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80o=$' |
| @@ -147,51 +156,55 @@ def test_encrypt_staticly_no_padding(urandom, non_random_bytes_32): | |||
| 147 | ) | 156 | ) |
| 148 | assert len(edata) == 131 | 157 | assert len(edata) == 131 |
| 149 | assert edata == etoolkit.EtoolkitInstance.encrypt( | 158 | assert edata == etoolkit.EtoolkitInstance.encrypt( |
| 150 | 'The very secret passwd', 'Nobody expects the Spanish inquisition' | 159 | master_password, 'Nobody expects the Spanish inquisition' |
| 151 | ) | 160 | ) |
| 152 | 161 | ||
| 153 | 162 | ||
| 154 | @unittest.mock.patch('os.urandom') | 163 | @unittest.mock.patch('os.urandom') |
| 155 | def test_encrypt_staticly_with_padding(urandom, non_random_bytes_61): | 164 | def test_encrypt_staticly_with_padding( |
| 165 | urandom, master_password, non_random_bytes_61 | ||
| 166 | ): | ||
| 156 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" | 167 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" |
| 168 | |||
| 157 | urandom.return_value = non_random_bytes_61 | 169 | urandom.return_value = non_random_bytes_61 |
| 158 | edata = etoolkit.EtoolkitInstance.encrypt('The very secret passwd', 'bar') | 170 | edata = etoolkit.EtoolkitInstance.encrypt(master_password, 'bar') |
| 159 | assert edata == ( | 171 | assert edata == ( |
| 160 | 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviqrLTBxM=$' | 172 | 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviqrLTBxM=$' |
| 161 | '+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/ESvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4=' | 173 | '+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/ESvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4=' |
| 162 | ) | 174 | ) |
| 163 | assert edata == etoolkit.EtoolkitInstance.encrypt( | 175 | assert edata == etoolkit.EtoolkitInstance.encrypt(master_password, 'bar') |
| 164 | 'The very secret passwd', 'bar' | ||
| 165 | ) | ||
| 166 | 176 | ||
| 167 | 177 | ||
| 168 | def test_get_new_password_hash(): | 178 | def test_get_new_password_hash(master_password): |
| 169 | """Tests the static EtoolkitInstance.get_new_password_hash method""" | 179 | """Tests the static EtoolkitInstance.get_new_password_hash method""" |
| 170 | new_hash = etoolkit.EtoolkitInstance.get_new_password_hash( | 180 | |
| 171 | 'The very secret passwd' | 181 | new_hash = etoolkit.EtoolkitInstance.get_new_password_hash(master_password) |
| 172 | ) | ||
| 173 | # all pbkdf2 params are the same / hardcoded for the time being | 182 | # all pbkdf2 params are the same / hardcoded for the time being |
| 174 | assert new_hash.startswith('pbkdf2_sha256$500000$') | 183 | assert new_hash.startswith('pbkdf2_sha256$500000$') |
| 175 | assert len(new_hash) == 110 | 184 | assert len(new_hash) == 110 |
| 176 | # the hash should always be different because of random salting | 185 | # the hash should always be different because of random salting |
| 177 | assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( | 186 | assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( |
| 178 | 'The very secret passwd' | 187 | master_password |
| 179 | ) | 188 | ) |
| 180 | 189 | ||
| 181 | 190 | ||
| 182 | def test_parse_value(): | 191 | def test_parse_value(): |
| 183 | """Tests the static EtoolkitInstance.parse_value method""" | 192 | """Tests the static EtoolkitInstance.parse_value method""" |
| 193 | |||
| 184 | assert ( | 194 | assert ( |
| 185 | etoolkit.EtoolkitInstance.parse_value('t%bs%t', {'%b': 'e', '%t': 't'}) | 195 | etoolkit.EtoolkitInstance.parse_value('t%bs%t', {'%b': 'e', '%t': 't'}) |
| 186 | == 'test' | 196 | == 'test' |
| 187 | ) | 197 | ) |
| 188 | 198 | ||
| 189 | 199 | ||
| 190 | def test_password_matches(password_hash): | 200 | def test_password_matches( |
| 201 | password_hash, master_password, wrong_master_password | ||
| 202 | ): | ||
| 191 | """Tests the static EtoolkitInstance.password_matches method""" | 203 | """Tests the static EtoolkitInstance.password_matches method""" |
| 204 | |||
| 192 | assert etoolkit.EtoolkitInstance.password_matches( | 205 | assert etoolkit.EtoolkitInstance.password_matches( |
| 193 | 'The very secret passwd', password_hash | 206 | master_password, password_hash |
| 194 | ) | 207 | ) |
| 195 | assert not etoolkit.EtoolkitInstance.password_matches( | 208 | assert not etoolkit.EtoolkitInstance.password_matches( |
| 196 | 'The very secret passwdo', password_hash | 209 | wrong_master_password, password_hash |
| 197 | ) | 210 | ) |
