pluginaweek / encrypted_strings

Provides dead-simple string encryption/decryption syntax

This URL has Read+Write access

encrypted_strings / test / sha_cipher_test.rb
100644 83 lines (65 sloc) 2.288 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require File.dirname(__FILE__) + '/test_helper'
 
class ShaCipherByDefaulTest < Test::Unit::TestCase
  def setup
    @sha_cipher = EncryptedStrings::ShaCipher.new
  end
  
  def test_should_use_default_salt
    assert_equal 'salt', @sha_cipher.salt
  end
  
  def test_should_encrypt_using_default_salt
    assert_equal 'f438229716cab43569496f3a3630b3727524b81b', @sha_cipher.encrypt('test')
  end
end
 
class ShaCipherWithCustomDefaultsTest < Test::Unit::TestCase
  def setup
    @original_default_salt = EncryptedStrings::ShaCipher.default_salt
    EncryptedStrings::ShaCipher.default_salt = 'custom_salt'
    @sha_cipher = EncryptedStrings::ShaCipher.new
  end
  
  def test_should_use_custom_default_salt
    assert_equal 'custom_salt', @sha_cipher.salt
  end
  
  def test_should_encrypt_using_custom_default_salt
    assert_equal '280f3c516070b09aa3eb755378509c725a9c6561', @sha_cipher.encrypt('test')
  end
  
  def teardown
    EncryptedStrings::ShaCipher.default_salt = @original_default_salt
  end
end
 
class ShaCipherWithInvalidOptionsTest < Test::Unit::TestCase
  def test_should_throw_an_exception
    assert_raise(ArgumentError) {EncryptedStrings::ShaCipher.new(:invalid => true)}
  end
end
 
class ShaCipherTest < Test::Unit::TestCase
  def setup
    @sha_cipher = EncryptedStrings::ShaCipher.new
  end
  
  def test_should_not_be_able_to_decrypt
    assert !EncryptedStrings::ShaCipher.new.can_decrypt?
  end
  
  def test_should_raise_exception_if_trying_to_decrypt
    assert_raises(NotImplementedError) {EncryptedStrings::ShaCipher.new.decrypt('test')}
  end
end
 
class ShaCipherWithCustomOptionsTest < Test::Unit::TestCase
  def setup
    @sha_cipher = EncryptedStrings::ShaCipher.new(:salt => 'different salt')
  end
  
  def test_should_use_custom_salt
    assert_equal 'different salt', @sha_cipher.salt
  end
  
  def test_should_encrypt_using_custom_salt
    assert_equal '18e3256d71529db8fa65b2eef24a69ddad7070f3', @sha_cipher.encrypt('test')
  end
end
 
class ShaCipherWithNonStringSaltTest < Test::Unit::TestCase
  require 'time'
  
  def setup
    @time = Time.parse('Tue Jan 01 00:00:00 UTC 2008')
    @sha_cipher = EncryptedStrings::ShaCipher.new(:salt => @time)
  end
  
  def test_should_stringify_salt
    assert_equal @time.to_s, @sha_cipher.salt
  end
end