diff options
| author | Simeon Simeonov | 2024-04-30 11:39:20 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2024-04-30 11:39:20 +0200 |
| commit | c31fa58c85e866b3a5ab04882c7aff655f0b5477 (patch) | |
| tree | 84532e1b6ce24c07267d4a8d8e8f810ba657500c /tests/test_envtoolkit_instance_static.py | |
| parent | 8ca7a0d9666ede2d9d270e2a469e4b8dda492e69 (diff) | |
Implement etoolkit encryption protocol v2
Diffstat (limited to 'tests/test_envtoolkit_instance_static.py')
| -rw-r--r-- | tests/test_envtoolkit_instance_static.py | 114 |
1 files changed, 97 insertions, 17 deletions
diff --git a/tests/test_envtoolkit_instance_static.py b/tests/test_envtoolkit_instance_static.py index 73a5f36..5fb9451 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-2022 Simeon Simeonov | 2 | # Copyright (C) 2021-2024 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 |
| @@ -14,6 +14,7 @@ | |||
| 14 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | """Tests for etoolkit.EtoolkitInstance static methods""" | 16 | """Tests for etoolkit.EtoolkitInstance static methods""" |
| 17 | |||
| 17 | import unittest.mock | 18 | import unittest.mock |
| 18 | 19 | ||
| 19 | import pytest | 20 | import pytest |
| @@ -35,7 +36,7 @@ def test_confirm_password_prompt(getpass, password_hash): | |||
| 35 | ) | 36 | ) |
| 36 | 37 | ||
| 37 | 38 | ||
| 38 | def test_decrypt(): | 39 | def test_decrypt_v1(): |
| 39 | """Tests the static EtoolkitInstance.decrypt method""" | 40 | """Tests the static EtoolkitInstance.decrypt method""" |
| 40 | assert ( | 41 | assert ( |
| 41 | etoolkit.EtoolkitInstance.decrypt( | 42 | etoolkit.EtoolkitInstance.decrypt( |
| @@ -49,39 +50,118 @@ def test_decrypt(): | |||
| 49 | ) | 50 | ) |
| 50 | 51 | ||
| 51 | # now test with modified edata | 52 | # now test with modified edata |
| 53 | edata = ( | ||
| 54 | 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ5ZoAkurNgMYx' | ||
| 55 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' | ||
| 56 | ) | ||
| 52 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | 57 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: |
| 53 | edata = ( | 58 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) |
| 54 | 'enc-val$1$/cXpEMoZrTlb9yokGhw8tLTSUkqnqJ5ZoAkurNgMYx' | 59 | assert exc_info.type is etoolkit.EtoolkitInstanceError |
| 55 | 'w=$1VdkSMcZnLRwLiu1M8VlYcbelwmiVNY=' | 60 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' |
| 61 | |||
| 62 | |||
| 63 | def test_decrypt_v2_no_padding(): | ||
| 64 | """Tests the static EtoolkitInstance.decrypt method for v2 - no padding""" | ||
| 65 | assert ( | ||
| 66 | etoolkit.EtoolkitInstance.decrypt( | ||
| 67 | 'The very secret passwd', | ||
| 68 | ( | ||
| 69 | 'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$' | ||
| 70 | 'SQc0ox6Emf2m5rrumsiptpIZEujdpXXSR/' | ||
| 71 | '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8=' | ||
| 72 | ), | ||
| 56 | ) | 73 | ) |
| 74 | == 'Nobody expects the Spanish inquisition' | ||
| 75 | ) | ||
| 76 | |||
| 77 | # now test with modified edata | ||
| 78 | edata = ( | ||
| 79 | 'enc-val$2$Wer5lECGyeZhhYS58N18WVx5Zzy+rrC+BPlq3Dw89wQ=$' | ||
| 80 | 'SQc0ox6Emf2m4rrumsiptpIZEujdpXXSR/' | ||
| 81 | '1VcfEZeBz4+KDSagr9ID+bkc4R2yFdxHnhig1eqQ8=' | ||
| 82 | ) | ||
| 83 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | ||
| 84 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) | ||
| 85 | assert exc_info.type is etoolkit.EtoolkitInstanceError | ||
| 86 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | ||
| 87 | |||
| 88 | |||
| 89 | def test_decrypt_v2_with_padding(): | ||
| 90 | """Tests the static EtoolkitInstance.decrypt method for v2 with padding""" | ||
| 91 | assert ( | ||
| 57 | etoolkit.EtoolkitInstance.decrypt( | 92 | etoolkit.EtoolkitInstance.decrypt( |
| 58 | 'The very secret passwd', edata | 93 | 'The very secret passwd', |
| 59 | ) == 'secret1' | 94 | ( |
| 95 | 'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$' | ||
| 96 | 'rhRona4wP9nhnXjcHqwkjFDsiVVVjYanAs' | ||
| 97 | 'N4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w=' | ||
| 98 | ), | ||
| 99 | ) | ||
| 100 | == 'secret1' | ||
| 101 | ) | ||
| 102 | |||
| 103 | # now test with modified edata | ||
| 104 | edata = ( | ||
| 105 | 'enc-val$2$//kzyUbDEWNoPC5dyukhB8de8+IVaLR2ngx2HwkfOuM=$' | ||
| 106 | 'rhRona4wP8nhnXjcHqwkjFDsiVVVjYanAsN4kknNkgC0ix4RtJQHYDeTzw1rrR1vb2w=' | ||
| 107 | ) | ||
| 108 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | ||
| 109 | etoolkit.EtoolkitInstance.decrypt('The very secret passwd', edata) | ||
| 60 | assert exc_info.type is etoolkit.EtoolkitInstanceError | 110 | assert exc_info.type is etoolkit.EtoolkitInstanceError |
| 61 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | 111 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' |
| 62 | 112 | ||
| 63 | 113 | ||
| 64 | def test_encrypt(): | 114 | def test_encrypt_no_padding(): |
| 65 | """Tests the static EtoolkitInstance.encrypt method""" | 115 | """Tests the static EtoolkitInstance.encrypt method with a long string""" |
| 116 | edata = etoolkit.EtoolkitInstance.encrypt( | ||
| 117 | 'foo', 'Nobody expects the Spanish inquisition' | ||
| 118 | ) | ||
| 119 | assert edata.startswith('enc-val$2$') | ||
| 120 | assert len(edata) == 131 | ||
| 121 | # the edata should always be different because of random salting | ||
| 122 | assert edata != etoolkit.EtoolkitInstance.encrypt( | ||
| 123 | 'foo', 'Nobody expects the Spanish inquisition' | ||
| 124 | ) | ||
| 125 | |||
| 126 | |||
| 127 | def test_encrypt_with_padding(): | ||
| 128 | """Tests the static EtoolkitInstance.encrypt method with a short string""" | ||
| 66 | edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | 129 | edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar') |
| 67 | assert edata.startswith('enc-val$') | 130 | assert edata.startswith('enc-val$2$') |
| 68 | assert len(edata) == 83 | 131 | assert len(edata) == 123 |
| 69 | # the edata should always be different because of random salting | 132 | # the edata should always be different because of random salting |
| 70 | assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | 133 | assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar') |
| 71 | 134 | ||
| 72 | 135 | ||
| 73 | @unittest.mock.patch('os.urandom') | 136 | @unittest.mock.patch('os.urandom') |
| 74 | def test_encrypt_staticly(urandom, non_random_bytes_32): | 137 | def test_encrypt_staticly_no_padding(urandom, non_random_bytes_32): |
| 75 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" | 138 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" |
| 76 | urandom.return_value = non_random_bytes_32 | 139 | urandom.return_value = non_random_bytes_32 |
| 140 | edata = etoolkit.EtoolkitInstance.encrypt( | ||
| 141 | 'The very secret passwd', 'Nobody expects the Spanish inquisition' | ||
| 142 | ) | ||
| 143 | assert edata == ( | ||
| 144 | 'enc-val$2$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80o=$' | ||
| 145 | 'UX/5YeRsh5/2vZ2J1UOS+BJti73Kbp6C1pJmC' | ||
| 146 | 'o8hFSujpe35X/XpzAiYv4BV1LNwnSYECsotsgs=' | ||
| 147 | ) | ||
| 148 | assert len(edata) == 131 | ||
| 149 | assert edata == etoolkit.EtoolkitInstance.encrypt( | ||
| 150 | 'The very secret passwd', 'Nobody expects the Spanish inquisition' | ||
| 151 | ) | ||
| 152 | |||
| 153 | |||
| 154 | @unittest.mock.patch('os.urandom') | ||
| 155 | def test_encrypt_staticly_with_padding(urandom, non_random_bytes_61): | ||
| 156 | """Tests the EtoolkitInstance.encrypt method always with the same salt""" | ||
| 157 | urandom.return_value = non_random_bytes_61 | ||
| 77 | edata = etoolkit.EtoolkitInstance.encrypt('The very secret passwd', 'bar') | 158 | edata = etoolkit.EtoolkitInstance.encrypt('The very secret passwd', 'bar') |
| 78 | assert edata == ( | 159 | assert edata == ( |
| 79 | 'enc-val$1$uYpZM1VfAGq0CDZL2duITs076CQj+hIFEgx+F4mn80' | 160 | 'enc-val$2$RCSZqq9pWrRDoCVYVHopyu1LzaJGfv8roVviqrLTBxM=$' |
| 80 | 'o=$HjPFNv6xC5hbMrFc0L5lSkWdfQ==' | 161 | '+Yo6Ya2MAVcBLTQHuATkyFc+dzYsL/ESvA6ofOUDsiKZvIff35cUHAmoNxVuGG+MXv4=' |
| 81 | ) | 162 | ) |
| 82 | assert edata == etoolkit.EtoolkitInstance.encrypt( | 163 | assert edata == etoolkit.EtoolkitInstance.encrypt( |
| 83 | 'The very secret passwd', | 164 | 'The very secret passwd', 'bar' |
| 84 | 'bar', | ||
| 85 | ) | 165 | ) |
| 86 | 166 | ||
| 87 | 167 | ||
| @@ -91,7 +171,7 @@ def test_get_new_password_hash(): | |||
| 91 | 'The very secret passwd' | 171 | 'The very secret passwd' |
| 92 | ) | 172 | ) |
| 93 | # all pbkdf2 params are the same / hardcoded for the time being | 173 | # all pbkdf2 params are the same / hardcoded for the time being |
| 94 | assert new_hash.startswith('pbkdf2_sha256$100000$') | 174 | assert new_hash.startswith('pbkdf2_sha256$500000$') |
| 95 | assert len(new_hash) == 110 | 175 | assert len(new_hash) == 110 |
| 96 | # the hash should always be different because of random salting | 176 | # the hash should always be different because of random salting |
| 97 | assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( | 177 | assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( |
