| 1 | --- |
| 2 | tags: technical, python, properties |
| 3 | date: 2016-06-13 00:00 |
| 4 | title: Testing Configuration Parameters |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | A lot of applications end up growing a complex configuration system, |
| 9 | with a large number of different knobs and dials you can turn to change |
| 10 | behaviour. Some of these are just for performance tuning, some change |
| 11 | operational concerns, some have other functions. |
| 12 | |
| 13 | Testing these is tricky. As the number of parameters goes up, the number |
| 14 | of possible configuration goes up exponentially. Manual testing of the |
| 15 | different combinations quickly becomes completely unmanageable, not |
| 16 | to mention extremely tedious. |
| 17 | |
| 18 | Fortunately, this is somewhere where property-based testing in general |
| 19 | and Hypothesis in particular can help a lot. |
| 20 | |
| 21 | <!--more--> |
| 22 | |
| 23 | Configuration parameters almost all have one thing in common: For the |
| 24 | vast majority of things, they shouldn't change the behaviour. A |
| 25 | configuration parameter is rarely going to be a complete reskin of your |
| 26 | application. |
| 27 | |
| 28 | This means that they are relatively easy to test with property-based |
| 29 | testing. You take an existing test - either one that is already using |
| 30 | Hypothesis or a normal example based test - and you vary some |
| 31 | configuration parameters and make sure the test still passes. |
| 32 | |
| 33 | This turns out to be remarkably effective. Here's an example where I |
| 34 | used this technique and found some bugs in the [Argon2]( |
| 35 | https://github.com/P-H-C/phc-winner-argon2) password hashing library, |
| 36 | using [Hynek](https://hynek.me/)'s |
| 37 | [CFFI based bindings](https://github.com/hynek/argon2_cffi). |
| 38 | |
| 39 | The idea of password hashing is straightforward: Given a password, you |
| 40 | can create a hash against which the password can be verified without |
| 41 | ever storing the password (after all, you're not storing passwords in |
| 42 | plain text on your servers, right?). Although straightforward to |
| 43 | describe, there's a lot of difficulty in making a good implementation |
| 44 | of this. Argon2 is a fairly recent one which won [the Password Hashing |
| 45 | Competition](https://password-hashing.net/) so should be fairly good. |
| 46 | |
| 47 | We can verify that hashing works correctly fairly immediately using |
| 48 | Hypothesis: |
| 49 | |
| 50 | ```python |
| 51 | from argon2 import PasswordHasher |
| 52 | |
| 53 | from hypothesis import given, strategies as st |
| 54 | |
| 55 | |
| 56 | class TestPasswordHasherWithHypothesis: |
| 57 | @given(password=st.text()) |
| 58 | def test_a_password_verifies(self, password): |
| 59 | ph = PasswordHasher() |
| 60 | hash = ph.hash(password) |
| 61 | assert ph.verify(hash, password) |
| 62 | ``` |
| 63 | |
| 64 | This takes an arbitrary text password, hashes it and verifies it against |
| 65 | the generated hash. |
| 66 | |
| 67 | This passes. So far, so good. |
| 68 | |
| 69 | But as you probably expected from its context here, argon2 has quite |
| 70 | a lot of different parameters to it. We can expand the test to vary |
| 71 | them and see what happens: |
| 72 | |
| 73 | ```python |
| 74 | from argon2 import PasswordHasher |
| 75 | |
| 76 | from hypothesis import assume, given, strategies as st |
| 77 | |
| 78 | |
| 79 | class TestPasswordHasherWithHypothesis: |
| 80 | @given( |
| 81 | password=st.text(), |
| 82 | time_cost=st.integers(1, 10), |
| 83 | parallelism=st.integers(1, 10), |
| 84 | memory_cost=st.integers(8, 2048), |
| 85 | hash_len=st.integers(12, 1000), |
| 86 | salt_len=st.integers(8, 1000), |
| 87 | ) |
| 88 | def test_a_password_verifies( |
| 89 | self, |
| 90 | password, |
| 91 | time_cost, |
| 92 | parallelism, |
| 93 | memory_cost, |
| 94 | hash_len, |
| 95 | salt_len, |
| 96 | ): |
| 97 | assume(parallelism * 8 <= memory_cost) |
| 98 | ph = PasswordHasher( |
| 99 | time_cost=time_cost, |
| 100 | parallelism=parallelism, |
| 101 | memory_cost=memory_cost, |
| 102 | hash_len=hash_len, |
| 103 | salt_len=salt_len, |
| 104 | ) |
| 105 | hash = ph.hash(password) |
| 106 | assert ph.verify(hash, password) |
| 107 | ``` |
| 108 | |
| 109 | |
| 110 | These parameters are mostly intended to vary the difficulty of |
| 111 | calculating the hash. Honestly I'm not entirely sure what all of them |
| 112 | do. Fortunately for the purposes of writing this test, understanding is |
| 113 | optional. |
| 114 | |
| 115 | In terms of how I chose the specific strategies to get there, I just |
| 116 | picked some plausible looking parameters ranges and adjusted them until |
| 117 | I wasn't getting validation errors (I did look for documentation, I |
| 118 | promise). The assume() call comes from reading the argon2 source to try |
| 119 | to find out what the valid range of parallelism was. |
| 120 | |
| 121 | This ended up finding |
| 122 | [two bugs](https://github.com/hynek/argon2_cffi/issues/4), which I duly |
| 123 | reported to Hynek, but they actually turned out to be upstream bugs! |
| 124 | |
| 125 | In both cases, a password would no longer validate against itself: |
| 126 | |
| 127 | |
| 128 | ``` |
| 129 | Falsifying example: test_a_password_verifies( |
| 130 | password='', time_cost=1, parallelism=1, memory_cost=8, hash_len=4, |
| 131 | salt_len=8, |
| 132 | ) |
| 133 | ``` |
| 134 | |
| 135 | ``` |
| 136 | Falsifying example: test_a_password_verifies( |
| 137 | password='', time_cost=1, parallelism=1, memory_cost=8, |
| 138 | hash_len=513, salt_len=8 |
| 139 | ) |
| 140 | ``` |
| 141 | |
| 142 | (I found the second one by manually determining that the first bug |
| 143 | happened whenever salt_len < 12 and manually ruling that case out). |
| 144 | |
| 145 | One interesting thing about both of these bugs is that they're actually |
| 146 | not bugs in the Python library but are both downstream bugs. I hadn't |
| 147 | set out to do that when I wrote these tests, but it nicely validates |
| 148 | that Hypothesis is rather useful for testing C libraries as well as |
| 149 | Python, given how easy they are to bind to with CFFI. |