2008-11-23 09:29:11 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class MessageVerifierTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!")
|
|
|
|
@data = {:some=>"data", :now=>Time.now}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_round_tripping
|
2008-11-23 10:33:56 -05:00
|
|
|
message = @verifier.generate(@data)
|
|
|
|
assert_equal @data, @verifier.verify(message)
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tampered_data_raises
|
2008-11-23 10:33:56 -05:00
|
|
|
data, hash = @verifier.generate(@data).split("--")
|
2008-11-23 09:29:11 -05:00
|
|
|
assert_not_verified("#{data.reverse}--#{hash}")
|
|
|
|
assert_not_verified("#{data}--#{hash.reverse}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_verified(message)
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveSupport::MessageVerifier::InvalidSignature) do
|
2008-11-23 10:33:56 -05:00
|
|
|
@verifier.verify(message)
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|