require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'dionysus/security/password_salt' describe PasswordSalt do it "should not allow placements other than before, after, and split" do salt = PasswordSalt.new('ABCDEFG') [:before, :after, :split].each do |p| salt.placement = p salt.placement.should == p salt.placement = p.to_s salt.placement.should == p end lambda { salt.placement = :wowsers }.should raise_error(ArgumentError, "Invalid salt placement: wowsers") end it "should should return the salt string with to_s" do salt = PasswordSalt.new('ABCDEFG') salt.to_s.should == 'ABCDEFG' salt = PasswordSalt.new('ABCDEFG', :after) salt.to_s.should == 'ABCDEFG' salt = PasswordSalt.new() salt.to_s.should == salt.string end it "should compare equivalance on both string and placement" do salt1 = PasswordSalt.new('ABCDEFG') salt2 = PasswordSalt.new('ABCDEFG') salt1.eql?(salt2).should be_true salt2.eql?(salt1).should be_true salt1 = PasswordSalt.new('ABCDEFG', :before) salt2 = PasswordSalt.new('ABCDEFG', :before) salt1.eql?(salt2).should be_true salt2.eql?(salt1).should be_true salt1 = PasswordSalt.new('ABCDEFG', :before) salt2 = PasswordSalt.new('ABCDEFG', :split) salt1.eql?(salt2).should_not be_true salt2.eql?(salt1).should_not be_true end describe "initializer" do it "should take the first string arg as the literal salt" do salt = PasswordSalt.new('ABCDEFG') salt.string.should == 'ABCDEFG' end it "should take the the second symbold arg as the placement" do salt = PasswordSalt.new('ABCDEFG', :before) salt.placement.should == :before end it "should generate a new salt of 8 characters and :after placement with no arguments" do s = PasswordSalt.new s.placement.should == :after s.string.length.should == 8 end it "should take a length option" do s = PasswordSalt.new(:length => 20) s.placement.should == :after s.string.length.should == 20 end end describe "salt generation" do it "should generate 8 characters by default" do salt = PasswordSalt.generate salt.length.should == 8 end it "should generate n characters when told to" do (0..20).each do |n| PasswordSalt.generate(n).length.should == n end end it "should raise an error with negative length" do lambda { PasswordSalt.generate(-1) }.should raise_error(ArgumentError, "Invalid length: -1") end it "should generate ascii characters only by default" do 100.times do salt = PasswordSalt.generate(100) salt.should match(/\A[A-Za-z0-9\+\/]{100}\Z/) end end it "should generate binary characters" do 100.times do salt = PasswordSalt.generate(100, :binary) salt.should_not match(/\A[A-Za-z0-9\+\/]{100}\Z/) end end end describe "after placement" do before(:each) do @password = 'foobar' @salt = "ABCDEFG" end it "should put the salt after the password by default" do salt = PasswordSalt.new(@salt) salt.salt_password(@password).should == @password+@salt end it "should put the salt after the password" do salt = PasswordSalt.new(@salt, :after) salt.salt_password(@password).should == @password+@salt end end describe "before placement" do before(:each) do @password = 'foobar' @salt = "ABCDEFG" end it "should put the salt before the password" do salt = PasswordSalt.new(@salt, :before) salt.salt_password(@password).should == @salt+@password salt = PasswordSalt.new(@salt) salt.placement = :before salt.salt_password(@password).should == @salt+@password end end describe "split placement with even number of characters" do before(:each) do @password = 'foobar' @salt = "ABCDEFGH" end it "should put half the salt before and half the salt after the password" do salt = PasswordSalt.new(@salt, :split) salt.salt_password(@password).should == 'ABCD'+@password+'EFGH' salt = PasswordSalt.new(@salt) salt.placement = :split salt.salt_password(@password).should == 'ABCD'+@password+'EFGH' end end describe "split placement with odd number of characters" do before(:each) do @password = 'foobar' @salt = "ABCDEFG" end it "should put half the salt before and half the salt after the password" do salt = PasswordSalt.new(@salt, :split) salt.salt_password(@password).should == 'ABC'+@password+'DEFG' salt = PasswordSalt.new(@salt) salt.placement = :split salt.salt_password(@password).should == 'ABC'+@password+'DEFG' end end end