Files: 812e12f884941325badb2553b22b5914d4b4bc91 / spec / bcrypt / password_spec.rb
2264 bytesRaw
1 | require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) |
2 | |
3 | context "Creating a hashed password" do |
4 | |
5 | before :each do |
6 | @secret = "wheedle" |
7 | @password = BCrypt::Password.create(@secret, :cost => 4) |
8 | end |
9 | |
10 | specify "should return a BCrypt::Password" do |
11 | @password.should be_an_instance_of(BCrypt::Password) |
12 | end |
13 | |
14 | specify "should return a valid bcrypt password" do |
15 | lambda { BCrypt::Password.new(@password) }.should_not raise_error |
16 | end |
17 | |
18 | specify "should behave normally if the secret is not a string" do |
19 | lambda { BCrypt::Password.create(nil) }.should_not raise_error(BCrypt::Errors::InvalidSecret) |
20 | lambda { BCrypt::Password.create({:woo => "yeah"}) }.should_not raise_error(BCrypt::Errors::InvalidSecret) |
21 | lambda { BCrypt::Password.create(false) }.should_not raise_error(BCrypt::Errors::InvalidSecret) |
22 | end |
23 | |
24 | specify "should tolerate empty string secrets" do |
25 | lambda { BCrypt::Password.create( "\n".chop ) }.should_not raise_error |
26 | lambda { BCrypt::Password.create( "" ) }.should_not raise_error |
27 | lambda { BCrypt::Password.create( String.new ) }.should_not raise_error |
28 | end |
29 | end |
30 | |
31 | context "Reading a hashed password" do |
32 | before :each do |
33 | @secret = "U*U" |
34 | @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" |
35 | end |
36 | |
37 | specify "should read the version, cost, salt, and hash" do |
38 | password = BCrypt::Password.new(@hash) |
39 | password.version.should eql("2a") |
40 | password.cost.should equal(5) |
41 | password.salt.should eql("$2a$05$CCCCCCCCCCCCCCCCCCCCC.") |
42 | password.to_s.should eql(@hash) |
43 | end |
44 | |
45 | specify "should raise an InvalidHashError when given an invalid hash" do |
46 | lambda { BCrypt::Password.new('weedle') }.should raise_error(BCrypt::Errors::InvalidHash) |
47 | end |
48 | end |
49 | |
50 | context "Comparing a hashed password with a secret" do |
51 | before :each do |
52 | @secret = "U*U" |
53 | @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" |
54 | @password = BCrypt::Password.create(@secret) |
55 | end |
56 | |
57 | specify "should compare successfully to the original secret" do |
58 | (@password == @secret).should be(true) |
59 | end |
60 | |
61 | specify "should compare unsuccessfully to anything besides original secret" do |
62 | (@password == "@secret").should be(false) |
63 | end |
64 | end |
Built with git-ssb-web