From 7a0667dce1c44b0f4b19a6edaf39f93e54121d5f Mon Sep 17 00:00:00 2001 From: Paul Annesley Date: Fri, 28 Sep 2012 14:59:39 +1000 Subject: [PATCH 1/2] Test that BCrypt errors can be rescued as StandardError. --- spec/bcrypt/error_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/bcrypt/error_spec.rb diff --git a/spec/bcrypt/error_spec.rb b/spec/bcrypt/error_spec.rb new file mode 100644 index 0000000..82bb683 --- /dev/null +++ b/spec/bcrypt/error_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) + +describe "Errors" do + + shared_examples "StandardError" do + it "is is rescued as a StandardError" do + described_class.should < StandardError + end + end + + describe BCrypt::Errors::InvalidCost do + it_behaves_like "StandardError" + end + + describe BCrypt::Errors::InvalidHash do + it_behaves_like "StandardError" + end + + describe BCrypt::Errors::InvalidSalt do + it_behaves_like "StandardError" + end + + describe BCrypt::Errors::InvalidSecret do + it_behaves_like "StandardError" + end + +end From 007857af56030fbe4c85f43162679572662a396d Mon Sep 17 00:00:00 2001 From: Paul Annesley Date: Fri, 28 Sep 2012 15:07:14 +1000 Subject: [PATCH 2/2] BCrypt::Errors::* descend from new BCrypt::Error. This makes error handling far easier, and does not impact backwards compatibility. This is now possible: begin # some BCrypt call rescue BCrypt::Error # handle error end Instead of: begin # some BCrypt call rescue BCrypt::Errors::InvalidSalt, BCrypt::Errors::InvalidHash, BCrypt::Errors::InvalidCost, BCrypt::Errors::InvalidSecret # handle error end RSpec output: Errors BCrypt::Error can be rescued as a StandardError BCrypt::Errors::InvalidCost can be rescued as a BCrypt::Error BCrypt::Errors::InvalidHash can be rescued as a BCrypt::Error BCrypt::Errors::InvalidSalt can be rescued as a BCrypt::Error BCrypt::Errors::InvalidSecret can be rescued as a BCrypt::Error --- lib/bcrypt.rb | 10 ++++++---- spec/bcrypt/error_spec.rb | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/bcrypt.rb b/lib/bcrypt.rb index b07678c..12f50a5 100644 --- a/lib/bcrypt.rb +++ b/lib/bcrypt.rb @@ -11,11 +11,13 @@ # A Ruby library implementing OpenBSD's bcrypt()/crypt_blowfish algorithm for # hashing passwords. module BCrypt + + class Error < StandardError; end module Errors - class InvalidSalt < StandardError; end # The salt parameter provided to bcrypt() is invalid. - class InvalidHash < StandardError; end # The hash parameter provided to bcrypt() is invalid. - class InvalidCost < StandardError; end # The cost parameter provided to bcrypt() is invalid. - class InvalidSecret < StandardError; end # The secret parameter provided to bcrypt() is invalid. + class InvalidSalt < BCrypt::Error; end # The salt parameter provided to bcrypt() is invalid. + class InvalidHash < BCrypt::Error; end # The hash parameter provided to bcrypt() is invalid. + class InvalidCost < BCrypt::Error; end # The cost parameter provided to bcrypt() is invalid. + class InvalidSecret < BCrypt::Error; end # The secret parameter provided to bcrypt() is invalid. end # A Ruby wrapper for the bcrypt() C extension calls and the Java calls. diff --git a/spec/bcrypt/error_spec.rb b/spec/bcrypt/error_spec.rb index 82bb683..d36d6bc 100644 --- a/spec/bcrypt/error_spec.rb +++ b/spec/bcrypt/error_spec.rb @@ -2,26 +2,36 @@ describe "Errors" do - shared_examples "StandardError" do - it "is is rescued as a StandardError" do + shared_examples "descends from StandardError" do + it "can be rescued as a StandardError" do described_class.should < StandardError end end + shared_examples "descends from BCrypt::Error" do + it "can be rescued as a BCrypt::Error" do + described_class.should < BCrypt::Error + end + end + + describe BCrypt::Error do + include_examples "descends from StandardError" + end + describe BCrypt::Errors::InvalidCost do - it_behaves_like "StandardError" + include_examples "descends from BCrypt::Error" end describe BCrypt::Errors::InvalidHash do - it_behaves_like "StandardError" + include_examples "descends from BCrypt::Error" end describe BCrypt::Errors::InvalidSalt do - it_behaves_like "StandardError" + include_examples "descends from BCrypt::Error" end describe BCrypt::Errors::InvalidSecret do - it_behaves_like "StandardError" + include_examples "descends from BCrypt::Error" end end