lvh / caesium

Modern cryptography (libsodium/NaCl) for Clojure
Eclipse Public License 1.0
180 stars 28 forks source link

Write a generic test that tests that functions that should fail with an exception have a negative path #52

Closed lvh closed 5 years ago

lvh commented 5 years ago

Not sure how to really do this. I guess we could find e.g. decryption functions and verify functions and just feed them random garbage?

anthonygalea commented 5 years ago

Is this what you have in mind?

(deftest decryption-fns-invalid-ciphertext-test
  (let [decryption-fns (filter #(clojure.string/includes? (key %) "decrypt")
                               (ns-publics 'caesium.crypto.aead))
        k (rb/randombytes 32)
        ctext (rb/randombytes 16)
        ad (rb/randombytes 12)
        nonce (rb/randombytes 12)]
    (doseq [[fn-name decryption-fn] decryption-fns]
      (is (thrown? RuntimeException
                   (decryption-fn ctext ad nonce k))
          (str "Function `" fn-name "` should throw an exception when given an invalid ciphertext.")))))
lvh commented 5 years ago

Sure yep that looks about right :)