Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for custom error handling. #22

Merged
merged 3 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/clj_pgp/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[byte-streams :as bytes]
[clojure.java.io :as io]
[clojure.string :as str]
[clj-pgp.tags :as tags])
[clj-pgp.tags :as tags]
[clj-pgp.error :as error])
(:import
(java.io
ByteArrayOutputStream
Expand Down Expand Up @@ -254,13 +255,25 @@

;; ## PGP Object Decoding

(defn read-next-object
"A thin wrapper on reading the next object from a PGPObjectFactory."
[^PGPObjectFactory factory]
(.nextObject factory))
brycecovert marked this conversation as resolved.
Show resolved Hide resolved

(defn ^:no-doc read-objects
"Lazily decodes a sequence of PGP objects from an input stream."
[^InputStream input]
(let [factory (PGPObjectFactory. input (BcKeyFingerprintCalculator.))]
(->>
(repeatedly #(.nextObject factory))
(take-while some?))))
(->> (range)
(map (fn next-object [n]
(try
(read-next-object factory)
(catch Exception e
(error/*handler* ::read-object-error
brycecovert marked this conversation as resolved.
Show resolved Hide resolved
(.getMessage e)
(assoc (ex-data e) ::stream input ::nth n)
e)))))
(take-while some?))))


(defn decode
Expand Down
11 changes: 11 additions & 0 deletions src/clj_pgp/error.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns clj-pgp.error)

(defn default-error-handler
[error-type message data cause]
(throw (ex-info message (assoc data :pgp/error error-type) cause)))

(def ^:dynamic *handler*
"Dynamic error handler"
default-error-handler)

(derive :clj-pgp.core/read-object-error ::decrypt-error)
14 changes: 12 additions & 2 deletions test/clj_pgp/test/encryption.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
[gen-ec-keyspec
gen-rsa-keyspec
spec->keypair
memospec->keypair]])
memospec->keypair]]
[clj-pgp.error :as error])
(:import
java.io.ByteArrayOutputStream
java.security.SecureRandom))
Expand Down Expand Up @@ -97,7 +98,16 @@
"Decrypting with a keypair-retrieval function returns the data.")
(is (thrown? IllegalArgumentException
(pgp-msg/decrypt ciphertext "passphrase"))
"Decrypting without a matching key throws an exception"))))
"Decrypting without a matching key throws an exception")
(testing "should allow overriding error behavior with custom behavior"
(let [error-occured? (atom false)
error-handler (fn [type message data cause]
(reset! error-occured? true)
nil)]
(with-redefs [pgp/read-next-object (fn [_] (throw (Exception. "Simulating PGP nextObject error")))]
(binding [error/*handler* error-handler]
(pgp-msg/decrypt ciphertext (constantly keypair))
(is @error-occured? "A PGP error was simulated but not passed to the error handler."))))))))


(deftest encryption-scenarios
Expand Down