/vendor/gems/ruby-openid-1.1.4/test/storetestcase.rb

https://github.com/ekcell/openmind · Ruby · 172 lines · 104 code · 43 blank · 25 comment · 2 complexity · 5b5b68cf99cb1231aafca5b2a4aab053 MD5 · raw file

  1. require 'openid/util'
  2. require 'openid/association'
  3. module StoreTestCase
  4. @@allowed_handle = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  5. @@allowed_nonce = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6. def _gen_nonce
  7. OpenID::Util.random_string(8, @@allowed_nonce)
  8. end
  9. def _gen_handle(n)
  10. OpenID::Util.random_string(n, @@allowed_handle)
  11. end
  12. def _gen_secret(n, chars=nil)
  13. OpenID::Util.random_string(n, chars)
  14. end
  15. def _gen_assoc(issued, lifetime=600)
  16. secret = _gen_secret(20)
  17. handle = _gen_handle(128)
  18. OpenID::Association.new(handle, secret, Time.now.to_i + issued, lifetime,
  19. 'HMAC-SHA1')
  20. end
  21. def _check_retrieve(url, handle=nil, expected=nil)
  22. ret_assoc = @store.get_association(url, handle)
  23. if expected.nil? or @store.dumb?
  24. assert_nil(ret_assoc)
  25. else
  26. assert_equal(ret_assoc, expected)
  27. assert_equal(ret_assoc.handle, expected.handle)
  28. assert_equal(ret_assoc.secret, expected.secret)
  29. end
  30. end
  31. def _check_remove(url, handle, expected)
  32. present = @store.remove_association(url, handle)
  33. expected_present = ((not @store.dumb?) and expected)
  34. assert ((not expected_present and not present) or \
  35. (expected_present and present))
  36. end
  37. def test_store
  38. server_url = "http://www.myopenid.com/openid"
  39. assoc = _gen_assoc(issued=0)
  40. # Make sure that a missing association returns no result
  41. _check_retrieve(server_url)
  42. # Check that after storage, getting returns the same result
  43. @store.store_association(server_url, assoc)
  44. _check_retrieve(server_url, nil, assoc)
  45. # more than once
  46. _check_retrieve(server_url, nil, assoc)
  47. # Storing more than once has no ill effect
  48. @store.store_association(server_url, assoc)
  49. _check_retrieve(server_url, nil, assoc)
  50. # Removing an association that does not exist returns not present
  51. _check_remove(server_url, assoc.handle + 'x', false)
  52. # Removing an association that does not exist returns not present
  53. _check_remove(server_url + 'x', assoc.handle, false)
  54. # Removing an association that is present returns present
  55. _check_remove(server_url, assoc.handle, true)
  56. # but not present on subsequent calls
  57. _check_remove(server_url, assoc.handle, false)
  58. # Put assoc back in the store
  59. @store.store_association(server_url, assoc)
  60. # More recent and expires after assoc
  61. assoc2 = _gen_assoc(issued=1)
  62. @store.store_association(server_url, assoc2)
  63. # After storing an association with a different handle, but the
  64. # same server_url, the handle with the later expiration is returned.
  65. _check_retrieve(server_url, nil, assoc2)
  66. # We can still retrieve the older association
  67. _check_retrieve(server_url, assoc.handle, assoc)
  68. # Plus we can retrieve the association with the later expiration
  69. # explicitly
  70. _check_retrieve(server_url, assoc2.handle, assoc2)
  71. # More recent, and expires earlier than assoc2 or assoc. Make sure
  72. # that we're picking the one with the latest issued date and not
  73. # taking into account the expiration.
  74. assoc3 = _gen_assoc(issued=2, lifetime=100)
  75. @store.store_association(server_url, assoc3)
  76. _check_retrieve(server_url, nil, assoc3)
  77. _check_retrieve(server_url, assoc.handle, assoc)
  78. _check_retrieve(server_url, assoc2.handle, assoc2)
  79. _check_retrieve(server_url, assoc3.handle, assoc3)
  80. _check_remove(server_url, assoc2.handle, true)
  81. _check_retrieve(server_url, nil, assoc3)
  82. _check_retrieve(server_url, assoc.handle, assoc)
  83. _check_retrieve(server_url, assoc2.handle, nil)
  84. _check_retrieve(server_url, assoc3.handle, assoc3)
  85. _check_remove(server_url, assoc2.handle, false)
  86. _check_remove(server_url, assoc3.handle, true)
  87. _check_retrieve(server_url, nil, assoc)
  88. _check_retrieve(server_url, assoc.handle, assoc)
  89. _check_retrieve(server_url, assoc2.handle, nil)
  90. _check_retrieve(server_url, assoc3.handle, nil)
  91. _check_remove(server_url, assoc2.handle, false)
  92. _check_remove(server_url, assoc.handle, true)
  93. _check_remove(server_url, assoc3.handle, false)
  94. _check_retrieve(server_url, nil, nil)
  95. _check_retrieve(server_url, assoc.handle, nil)
  96. _check_retrieve(server_url, assoc2.handle, nil)
  97. _check_retrieve(server_url, assoc3.handle, nil)
  98. _check_remove(server_url, assoc2.handle, false)
  99. _check_remove(server_url, assoc.handle, false)
  100. _check_remove(server_url, assoc3.handle, false)
  101. end
  102. def test_nonce
  103. nonce1 = _gen_nonce
  104. assert_not_nil(nonce1)
  105. # a nonce is present by default
  106. present = @store.use_nonce(nonce1)
  107. assert_equal(present, false)
  108. # Storing once causes use_nonce to return true the first, and only
  109. # the first, time it is called after the store.
  110. @store.store_nonce(nonce1)
  111. present = @store.use_nonce(nonce1)
  112. assert present
  113. present = @store.use_nonce(nonce1)
  114. assert_equal(present, false)
  115. # Storing twice has the same effect as storing once.
  116. @store.store_nonce(nonce1)
  117. @store.store_nonce(nonce1)
  118. present = @store.use_nonce(nonce1)
  119. assert present
  120. present = @store.use_nonce(nonce1)
  121. assert_equal(present, false)
  122. ### Auth key stuff
  123. # there is no key to start with, so generate a new key and return it
  124. key = @store.get_auth_key
  125. # the second time we should return the same key as before
  126. key2 = @store.get_auth_key
  127. assert key == key2
  128. end
  129. end