spec/bcrypt/engine_spec.rb in bcrypt-ruby-2.0.5 vs spec/bcrypt/engine_spec.rb in bcrypt-ruby-2.1.0

- old
+ new

@@ -1,12 +1,12 @@ -require File.join(File.dirname(__FILE__), "..", "spec_helper") +require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) context "The BCrypt engine" do specify "should calculate the optimal cost factor to fit in a specific time" do first = BCrypt::Engine.calibrate(100) - second = BCrypt::Engine.calibrate(300) - second.should >(first + 1) + second = BCrypt::Engine.calibrate(400) + second.should > first end end context "Generating BCrypt salts" do @@ -25,13 +25,27 @@ specify "should raise a InvalidCostError if the cost parameter isn't greater than 0" do lambda { BCrypt::Engine.generate_salt(-1) }.should raise_error(BCrypt::Errors::InvalidCost) end end +context "Autodetecting of salt cost" do + + specify "should work" do + BCrypt::Engine.autodetect_cost("$2a$08$hRx2IVeHNsTSYYtUWn61Ou").should == 8 + BCrypt::Engine.autodetect_cost("$2a$05$XKd1bMnLgUnc87qvbAaCUu").should == 5 + BCrypt::Engine.autodetect_cost("$2a$13$Lni.CZ6z5A7344POTFBBV.").should == 13 + end + +end + context "Generating BCrypt hashes" do - setup do + class MyInvalidSecret + undef to_s + end + + before :each do @salt = BCrypt::Engine.generate_salt(4) @password = "woo" end specify "should produce a string" do @@ -41,11 +55,16 @@ specify "should raise an InvalidSalt error if the salt is invalid" do lambda { BCrypt::Engine.hash_secret(@password, 'nino') }.should raise_error(BCrypt::Errors::InvalidSalt) end specify "should raise an InvalidSecret error if the secret is invalid" do + lambda { BCrypt::Engine.hash_secret(MyInvalidSecret.new, @salt) }.should raise_error(BCrypt::Errors::InvalidSecret) lambda { BCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret) lambda { BCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error(BCrypt::Errors::InvalidSecret) + end + + specify "should call #to_s on the secret and use the return value as the actual secret data" do + BCrypt::Engine.hash_secret(false, @salt).should == BCrypt::Engine.hash_secret("false", @salt) end specify "should be interoperable with other implementations" do # test vectors from the OpenWall implementation <http://www.openwall.com/crypt/> test_vectors = [