You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mem_cache_store_test.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. require File.dirname(__FILE__) + '/test_helper'
  2. require File.dirname(__FILE__) + '/../lib/open_id_authentication/mem_cache_store'
  3. # Mock MemCacheStore with MemoryStore for testing
  4. class OpenIdAuthentication::MemCacheStore < OpenID::Store::Interface
  5. def initialize(*addresses)
  6. @connection = ActiveSupport::Cache::MemoryStore.new
  7. end
  8. end
  9. class MemCacheStoreTest < Test::Unit::TestCase
  10. ALLOWED_HANDLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  11. def setup
  12. @store = OpenIdAuthentication::MemCacheStore.new
  13. end
  14. def test_store
  15. server_url = "http://www.myopenid.com/openid"
  16. assoc = gen_assoc(0)
  17. # Make sure that a missing association returns no result
  18. assert_retrieve(server_url)
  19. # Check that after storage, getting returns the same result
  20. @store.store_association(server_url, assoc)
  21. assert_retrieve(server_url, nil, assoc)
  22. # more than once
  23. assert_retrieve(server_url, nil, assoc)
  24. # Storing more than once has no ill effect
  25. @store.store_association(server_url, assoc)
  26. assert_retrieve(server_url, nil, assoc)
  27. # Removing an association that does not exist returns not present
  28. assert_remove(server_url, assoc.handle + 'x', false)
  29. # Removing an association that does not exist returns not present
  30. assert_remove(server_url + 'x', assoc.handle, false)
  31. # Removing an association that is present returns present
  32. assert_remove(server_url, assoc.handle, true)
  33. # but not present on subsequent calls
  34. assert_remove(server_url, assoc.handle, false)
  35. # Put assoc back in the store
  36. @store.store_association(server_url, assoc)
  37. # More recent and expires after assoc
  38. assoc2 = gen_assoc(1)
  39. @store.store_association(server_url, assoc2)
  40. # After storing an association with a different handle, but the
  41. # same server_url, the handle with the later expiration is returned.
  42. assert_retrieve(server_url, nil, assoc2)
  43. # We can still retrieve the older association
  44. assert_retrieve(server_url, assoc.handle, assoc)
  45. # Plus we can retrieve the association with the later expiration
  46. # explicitly
  47. assert_retrieve(server_url, assoc2.handle, assoc2)
  48. # More recent, and expires earlier than assoc2 or assoc. Make sure
  49. # that we're picking the one with the latest issued date and not
  50. # taking into account the expiration.
  51. assoc3 = gen_assoc(2, 100)
  52. @store.store_association(server_url, assoc3)
  53. assert_retrieve(server_url, nil, assoc3)
  54. assert_retrieve(server_url, assoc.handle, assoc)
  55. assert_retrieve(server_url, assoc2.handle, assoc2)
  56. assert_retrieve(server_url, assoc3.handle, assoc3)
  57. assert_remove(server_url, assoc2.handle, true)
  58. assert_retrieve(server_url, nil, assoc3)
  59. assert_retrieve(server_url, assoc.handle, assoc)
  60. assert_retrieve(server_url, assoc2.handle, nil)
  61. assert_retrieve(server_url, assoc3.handle, assoc3)
  62. assert_remove(server_url, assoc2.handle, false)
  63. assert_remove(server_url, assoc3.handle, true)
  64. assert_retrieve(server_url, nil, assoc)
  65. assert_retrieve(server_url, assoc.handle, assoc)
  66. assert_retrieve(server_url, assoc2.handle, nil)
  67. assert_retrieve(server_url, assoc3.handle, nil)
  68. assert_remove(server_url, assoc2.handle, false)
  69. assert_remove(server_url, assoc.handle, true)
  70. assert_remove(server_url, assoc3.handle, false)
  71. assert_retrieve(server_url, nil, nil)
  72. assert_retrieve(server_url, assoc.handle, nil)
  73. assert_retrieve(server_url, assoc2.handle, nil)
  74. assert_retrieve(server_url, assoc3.handle, nil)
  75. assert_remove(server_url, assoc2.handle, false)
  76. assert_remove(server_url, assoc.handle, false)
  77. assert_remove(server_url, assoc3.handle, false)
  78. end
  79. def test_nonce
  80. server_url = "http://www.myopenid.com/openid"
  81. [server_url, ''].each do |url|
  82. nonce1 = OpenID::Nonce::mk_nonce
  83. assert_nonce(nonce1, true, url, "#{url}: nonce allowed by default")
  84. assert_nonce(nonce1, false, url, "#{url}: nonce not allowed twice")
  85. assert_nonce(nonce1, false, url, "#{url}: nonce not allowed third time")
  86. # old nonces shouldn't pass
  87. old_nonce = OpenID::Nonce::mk_nonce(3600)
  88. assert_nonce(old_nonce, false, url, "Old nonce #{old_nonce.inspect} passed")
  89. end
  90. end
  91. private
  92. def gen_assoc(issued, lifetime = 600)
  93. secret = OpenID::CryptUtil.random_string(20, nil)
  94. handle = OpenID::CryptUtil.random_string(128, ALLOWED_HANDLE)
  95. OpenID::Association.new(handle, secret, Time.now + issued, lifetime, 'HMAC-SHA1')
  96. end
  97. def assert_retrieve(url, handle = nil, expected = nil)
  98. assoc = @store.get_association(url, handle)
  99. if expected.nil?
  100. assert_nil(assoc)
  101. else
  102. assert_equal(expected, assoc)
  103. assert_equal(expected.handle, assoc.handle)
  104. assert_equal(expected.secret, assoc.secret)
  105. end
  106. end
  107. def assert_remove(url, handle, expected)
  108. present = @store.remove_association(url, handle)
  109. assert_equal(expected, present)
  110. end
  111. def assert_nonce(nonce, expected, server_url, msg = "")
  112. stamp, salt = OpenID::Nonce::split_nonce(nonce)
  113. actual = @store.use_nonce(server_url, stamp, salt)
  114. assert_equal(expected, actual, msg)
  115. end
  116. end