entp-ruby-openid 2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. data/CHANGELOG +215 -0
  2. data/INSTALL +47 -0
  3. data/LICENSE +210 -0
  4. data/NOTICE +2 -0
  5. data/README +85 -0
  6. data/UPGRADE +127 -0
  7. data/admin/runtests.rb +45 -0
  8. data/examples/README +32 -0
  9. data/examples/active_record_openid_store/README +58 -0
  10. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  11. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  12. data/examples/active_record_openid_store/init.rb +8 -0
  13. data/examples/active_record_openid_store/lib/association.rb +10 -0
  14. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  15. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  16. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  17. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  18. data/examples/discover +49 -0
  19. data/examples/rails_openid/README +153 -0
  20. data/examples/rails_openid/Rakefile +10 -0
  21. data/examples/rails_openid/app/controllers/application.rb +4 -0
  22. data/examples/rails_openid/app/controllers/consumer_controller.rb +125 -0
  23. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  24. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  25. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  26. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  27. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  28. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  29. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  30. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  31. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  32. data/examples/rails_openid/config/boot.rb +19 -0
  33. data/examples/rails_openid/config/database.yml +74 -0
  34. data/examples/rails_openid/config/environment.rb +54 -0
  35. data/examples/rails_openid/config/environments/development.rb +19 -0
  36. data/examples/rails_openid/config/environments/production.rb +19 -0
  37. data/examples/rails_openid/config/environments/test.rb +19 -0
  38. data/examples/rails_openid/config/routes.rb +24 -0
  39. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  40. data/examples/rails_openid/public/404.html +8 -0
  41. data/examples/rails_openid/public/500.html +8 -0
  42. data/examples/rails_openid/public/dispatch.cgi +12 -0
  43. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  44. data/examples/rails_openid/public/dispatch.rb +12 -0
  45. data/examples/rails_openid/public/favicon.ico +0 -0
  46. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  47. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  48. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  49. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  50. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  51. data/examples/rails_openid/public/robots.txt +1 -0
  52. data/examples/rails_openid/script/about +3 -0
  53. data/examples/rails_openid/script/breakpointer +3 -0
  54. data/examples/rails_openid/script/console +3 -0
  55. data/examples/rails_openid/script/destroy +3 -0
  56. data/examples/rails_openid/script/generate +3 -0
  57. data/examples/rails_openid/script/performance/benchmarker +3 -0
  58. data/examples/rails_openid/script/performance/profiler +3 -0
  59. data/examples/rails_openid/script/plugin +3 -0
  60. data/examples/rails_openid/script/process/reaper +3 -0
  61. data/examples/rails_openid/script/process/spawner +3 -0
  62. data/examples/rails_openid/script/process/spinner +3 -0
  63. data/examples/rails_openid/script/runner +3 -0
  64. data/examples/rails_openid/script/server +3 -0
  65. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  66. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  67. data/examples/rails_openid/test/test_helper.rb +28 -0
  68. data/lib/hmac/hmac.rb +112 -0
  69. data/lib/hmac/sha1.rb +11 -0
  70. data/lib/hmac/sha2.rb +25 -0
  71. data/lib/openid.rb +22 -0
  72. data/lib/openid/association.rb +249 -0
  73. data/lib/openid/consumer.rb +395 -0
  74. data/lib/openid/consumer/associationmanager.rb +344 -0
  75. data/lib/openid/consumer/checkid_request.rb +186 -0
  76. data/lib/openid/consumer/discovery.rb +497 -0
  77. data/lib/openid/consumer/discovery_manager.rb +123 -0
  78. data/lib/openid/consumer/html_parse.rb +134 -0
  79. data/lib/openid/consumer/idres.rb +523 -0
  80. data/lib/openid/consumer/responses.rb +150 -0
  81. data/lib/openid/cryptutil.rb +115 -0
  82. data/lib/openid/dh.rb +89 -0
  83. data/lib/openid/extension.rb +39 -0
  84. data/lib/openid/extensions/ax.rb +539 -0
  85. data/lib/openid/extensions/oauth.rb +91 -0
  86. data/lib/openid/extensions/pape.rb +179 -0
  87. data/lib/openid/extensions/sreg.rb +277 -0
  88. data/lib/openid/extras.rb +11 -0
  89. data/lib/openid/fetchers.rb +258 -0
  90. data/lib/openid/kvform.rb +136 -0
  91. data/lib/openid/kvpost.rb +58 -0
  92. data/lib/openid/message.rb +553 -0
  93. data/lib/openid/protocolerror.rb +12 -0
  94. data/lib/openid/server.rb +1544 -0
  95. data/lib/openid/store.rb +10 -0
  96. data/lib/openid/store/filesystem.rb +272 -0
  97. data/lib/openid/store/interface.rb +75 -0
  98. data/lib/openid/store/memcache.rb +109 -0
  99. data/lib/openid/store/memory.rb +84 -0
  100. data/lib/openid/store/nonce.rb +68 -0
  101. data/lib/openid/trustroot.rb +349 -0
  102. data/lib/openid/urinorm.rb +75 -0
  103. data/lib/openid/util.rb +119 -0
  104. data/lib/openid/version.rb +3 -0
  105. data/lib/openid/yadis.rb +15 -0
  106. data/lib/openid/yadis/accept.rb +148 -0
  107. data/lib/openid/yadis/constants.rb +21 -0
  108. data/lib/openid/yadis/discovery.rb +153 -0
  109. data/lib/openid/yadis/filters.rb +205 -0
  110. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  111. data/lib/openid/yadis/parsehtml.rb +45 -0
  112. data/lib/openid/yadis/services.rb +42 -0
  113. data/lib/openid/yadis/xrds.rb +155 -0
  114. data/lib/openid/yadis/xri.rb +90 -0
  115. data/lib/openid/yadis/xrires.rb +91 -0
  116. data/test/data/test_discover/openid_utf8.html +11 -0
  117. data/test/support/test_data_mixin.rb +127 -0
  118. data/test/support/test_util.rb +53 -0
  119. data/test/support/yadis_data.rb +131 -0
  120. data/test/support/yadis_data/accept.txt +124 -0
  121. data/test/support/yadis_data/dh.txt +29 -0
  122. data/test/support/yadis_data/example-xrds.xml +14 -0
  123. data/test/support/yadis_data/linkparse.txt +587 -0
  124. data/test/support/yadis_data/n2b64 +650 -0
  125. data/test/support/yadis_data/test1-discover.txt +137 -0
  126. data/test/support/yadis_data/test1-parsehtml.txt +152 -0
  127. data/test/support/yadis_data/test_discover/malformed_meta_tag.html +19 -0
  128. data/test/support/yadis_data/test_discover/openid.html +11 -0
  129. data/test/support/yadis_data/test_discover/openid2.html +11 -0
  130. data/test/support/yadis_data/test_discover/openid2_xrds.xml +12 -0
  131. data/test/support/yadis_data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  132. data/test/support/yadis_data/test_discover/openid_1_and_2.html +11 -0
  133. data/test/support/yadis_data/test_discover/openid_1_and_2_xrds.xml +16 -0
  134. data/test/support/yadis_data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  135. data/test/support/yadis_data/test_discover/openid_and_yadis.html +12 -0
  136. data/test/support/yadis_data/test_discover/openid_no_delegate.html +10 -0
  137. data/test/support/yadis_data/test_discover/openid_utf8.html +11 -0
  138. data/test/support/yadis_data/test_discover/yadis_0entries.xml +12 -0
  139. data/test/support/yadis_data/test_discover/yadis_2_bad_local_id.xml +15 -0
  140. data/test/support/yadis_data/test_discover/yadis_2entries_delegate.xml +22 -0
  141. data/test/support/yadis_data/test_discover/yadis_2entries_idp.xml +21 -0
  142. data/test/support/yadis_data/test_discover/yadis_another_delegate.xml +14 -0
  143. data/test/support/yadis_data/test_discover/yadis_idp.xml +12 -0
  144. data/test/support/yadis_data/test_discover/yadis_idp_delegate.xml +13 -0
  145. data/test/support/yadis_data/test_discover/yadis_no_delegate.xml +11 -0
  146. data/test/support/yadis_data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  147. data/test/support/yadis_data/test_xrds/README +12 -0
  148. data/test/support/yadis_data/test_xrds/delegated-20060809-r1.xrds +34 -0
  149. data/test/support/yadis_data/test_xrds/delegated-20060809-r2.xrds +34 -0
  150. data/test/support/yadis_data/test_xrds/delegated-20060809.xrds +34 -0
  151. data/test/support/yadis_data/test_xrds/no-xrd.xml +7 -0
  152. data/test/support/yadis_data/test_xrds/not-xrds.xml +2 -0
  153. data/test/support/yadis_data/test_xrds/prefixsometimes.xrds +34 -0
  154. data/test/support/yadis_data/test_xrds/ref.xrds +109 -0
  155. data/test/support/yadis_data/test_xrds/sometimesprefix.xrds +34 -0
  156. data/test/support/yadis_data/test_xrds/spoof1.xrds +25 -0
  157. data/test/support/yadis_data/test_xrds/spoof2.xrds +25 -0
  158. data/test/support/yadis_data/test_xrds/spoof3.xrds +37 -0
  159. data/test/support/yadis_data/test_xrds/status222.xrds +9 -0
  160. data/test/support/yadis_data/test_xrds/subsegments.xrds +58 -0
  161. data/test/support/yadis_data/test_xrds/valid-populated-xrds.xml +39 -0
  162. data/test/support/yadis_data/trustroot.txt +153 -0
  163. data/test/support/yadis_data/urinorm.txt +79 -0
  164. data/test/test_accept.rb +170 -0
  165. data/test/test_association.rb +268 -0
  166. data/test/test_associationmanager.rb +918 -0
  167. data/test/test_ax.rb +690 -0
  168. data/test/test_checkid_request.rb +293 -0
  169. data/test/test_consumer.rb +260 -0
  170. data/test/test_cryptutil.rb +119 -0
  171. data/test/test_dh.rb +85 -0
  172. data/test/test_discover.rb +848 -0
  173. data/test/test_discovery_manager.rb +259 -0
  174. data/test/test_extension.rb +46 -0
  175. data/test/test_extras.rb +35 -0
  176. data/test/test_fetchers.rb +554 -0
  177. data/test/test_filters.rb +269 -0
  178. data/test/test_helper.rb +4 -0
  179. data/test/test_idres.rb +961 -0
  180. data/test/test_kvform.rb +164 -0
  181. data/test/test_kvpost.rb +64 -0
  182. data/test/test_linkparse.rb +100 -0
  183. data/test/test_message.rb +1115 -0
  184. data/test/test_nonce.rb +89 -0
  185. data/test/test_oauth.rb +176 -0
  186. data/test/test_openid_yadis.rb +177 -0
  187. data/test/test_pape.rb +248 -0
  188. data/test/test_parsehtml.rb +79 -0
  189. data/test/test_responses.rb +63 -0
  190. data/test/test_server.rb +2455 -0
  191. data/test/test_sreg.rb +479 -0
  192. data/test/test_stores.rb +292 -0
  193. data/test/test_trustroot.rb +111 -0
  194. data/test/test_urinorm.rb +34 -0
  195. data/test/test_util.rb +145 -0
  196. data/test/test_xrds.rb +167 -0
  197. data/test/test_xri.rb +48 -0
  198. data/test/test_xrires.rb +67 -0
  199. data/test/test_yadis_discovery.rb +218 -0
  200. metadata +268 -0
@@ -0,0 +1,292 @@
1
+ require "test_helper"
2
+ require 'openid/association'
3
+
4
+ module OpenID
5
+ module Store
6
+ module StoreTestCase
7
+ @@allowed_handle = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
8
+ @@allowed_nonce = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
9
+
10
+ def _gen_nonce
11
+ OpenID::CryptUtil.random_string(8, @@allowed_nonce)
12
+ end
13
+
14
+ def _gen_handle(n)
15
+ OpenID::CryptUtil.random_string(n, @@allowed_handle)
16
+ end
17
+
18
+ def _gen_secret(n, chars=nil)
19
+ OpenID::CryptUtil.random_string(n, chars)
20
+ end
21
+
22
+ def _gen_assoc(issued, lifetime=600)
23
+ secret = _gen_secret(20)
24
+ handle = _gen_handle(128)
25
+ OpenID::Association.new(handle, secret, Time.now + issued, lifetime,
26
+ 'HMAC-SHA1')
27
+ end
28
+
29
+ def _check_retrieve(url, handle=nil, expected=nil)
30
+ ret_assoc = @store.get_association(url, handle)
31
+
32
+ if expected.nil?
33
+ assert_nil(ret_assoc)
34
+ else
35
+ assert_equal(expected, ret_assoc)
36
+ assert_equal(expected.handle, ret_assoc.handle)
37
+ assert_equal(expected.secret, ret_assoc.secret)
38
+ end
39
+ end
40
+
41
+ def _check_remove(url, handle, expected)
42
+ present = @store.remove_association(url, handle)
43
+ assert_equal(expected, present)
44
+ end
45
+
46
+ def test_store
47
+ assoc = _gen_assoc(issued=0)
48
+
49
+ # Make sure that a missing association returns no result
50
+ _check_retrieve(server_url)
51
+
52
+ # Check that after storage, getting returns the same result
53
+ @store.store_association(server_url, assoc)
54
+ _check_retrieve(server_url, nil, assoc)
55
+
56
+ # more than once
57
+ _check_retrieve(server_url, nil, assoc)
58
+
59
+ # Storing more than once has no ill effect
60
+ @store.store_association(server_url, assoc)
61
+ _check_retrieve(server_url, nil, assoc)
62
+
63
+ # Removing an association that does not exist returns not present
64
+ _check_remove(server_url, assoc.handle + 'x', false)
65
+
66
+ # Removing an association that does not exist returns not present
67
+ _check_remove(server_url + 'x', assoc.handle, false)
68
+
69
+ # Removing an association that is present returns present
70
+ _check_remove(server_url, assoc.handle, true)
71
+
72
+ # but not present on subsequent calls
73
+ _check_remove(server_url, assoc.handle, false)
74
+
75
+ # Put assoc back in the store
76
+ @store.store_association(server_url, assoc)
77
+
78
+ # More recent and expires after assoc
79
+ assoc2 = _gen_assoc(issued=1)
80
+ @store.store_association(server_url, assoc2)
81
+
82
+ # After storing an association with a different handle, but the
83
+ # same server_url, the handle with the later expiration is returned.
84
+ _check_retrieve(server_url, nil, assoc2)
85
+
86
+ # We can still retrieve the older association
87
+ _check_retrieve(server_url, assoc.handle, assoc)
88
+
89
+ # Plus we can retrieve the association with the later expiration
90
+ # explicitly
91
+ _check_retrieve(server_url, assoc2.handle, assoc2)
92
+
93
+ # More recent, and expires earlier than assoc2 or assoc. Make sure
94
+ # that we're picking the one with the latest issued date and not
95
+ # taking into account the expiration.
96
+ assoc3 = _gen_assoc(issued=2, lifetime=100)
97
+ @store.store_association(server_url, assoc3)
98
+
99
+ _check_retrieve(server_url, nil, assoc3)
100
+ _check_retrieve(server_url, assoc.handle, assoc)
101
+ _check_retrieve(server_url, assoc2.handle, assoc2)
102
+ _check_retrieve(server_url, assoc3.handle, assoc3)
103
+
104
+ _check_remove(server_url, assoc2.handle, true)
105
+
106
+ _check_retrieve(server_url, nil, assoc3)
107
+ _check_retrieve(server_url, assoc.handle, assoc)
108
+ _check_retrieve(server_url, assoc2.handle, nil)
109
+ _check_retrieve(server_url, assoc3.handle, assoc3)
110
+
111
+ _check_remove(server_url, assoc2.handle, false)
112
+ _check_remove(server_url, assoc3.handle, true)
113
+
114
+ ret_assoc = @store.get_association(server_url, nil)
115
+ unexpected = [assoc2.handle, assoc3.handle]
116
+ assert(ret_assoc.nil? || !unexpected.member?(ret_assoc.handle),
117
+ ret_assoc.to_s)
118
+
119
+ _check_retrieve(server_url, assoc.handle, assoc)
120
+ _check_retrieve(server_url, assoc2.handle, nil)
121
+ _check_retrieve(server_url, assoc3.handle, nil)
122
+
123
+ _check_remove(server_url, assoc2.handle, false)
124
+ _check_remove(server_url, assoc.handle, true)
125
+ _check_remove(server_url, assoc3.handle, false)
126
+
127
+ _check_retrieve(server_url, nil, nil)
128
+ _check_retrieve(server_url, assoc.handle, nil)
129
+ _check_retrieve(server_url, assoc2.handle, nil)
130
+ _check_retrieve(server_url, assoc3.handle, nil)
131
+
132
+ _check_remove(server_url, assoc2.handle, false)
133
+ _check_remove(server_url, assoc.handle, false)
134
+ _check_remove(server_url, assoc3.handle, false)
135
+ end
136
+
137
+ def test_assoc_cleanup
138
+ assocValid1 = _gen_assoc(-3600, 7200)
139
+ assocValid2 = _gen_assoc(-5)
140
+ assocExpired1 = _gen_assoc(-7200, 3600)
141
+ assocExpired2 = _gen_assoc(-7200, 3600)
142
+
143
+ @store.cleanup_associations
144
+ @store.store_association(server_url + '1', assocValid1)
145
+ @store.store_association(server_url + '1', assocExpired1)
146
+ @store.store_association(server_url + '2', assocExpired2)
147
+ @store.store_association(server_url + '3', assocValid2)
148
+
149
+ cleaned = @store.cleanup_associations()
150
+ assert_equal(2, cleaned, "cleaned up associations")
151
+ end
152
+
153
+ def _check_use_nonce(nonce, expected, server_url, msg='')
154
+ stamp, salt = Nonce::split_nonce(nonce)
155
+ actual = @store.use_nonce(server_url, stamp, salt)
156
+ assert_equal(expected, actual, msg)
157
+ end
158
+
159
+ def server_url
160
+ "http://www.myopenid.com/openid"
161
+ end
162
+
163
+ def test_nonce
164
+ [server_url, ''].each{|url|
165
+ nonce1 = Nonce::mk_nonce
166
+
167
+ _check_use_nonce(nonce1, true, url, "#{url}: nonce allowed by default")
168
+ _check_use_nonce(nonce1, false, url, "#{url}: nonce not allowed twice")
169
+ _check_use_nonce(nonce1, false, url, "#{url}: nonce not allowed third time")
170
+
171
+ # old nonces shouldn't pass
172
+ old_nonce = Nonce::mk_nonce(3600)
173
+ _check_use_nonce(old_nonce, false, url, "Old nonce #{old_nonce.inspect} passed")
174
+
175
+ }
176
+ end
177
+
178
+ def test_nonce_cleanup
179
+ now = Time.now.to_i
180
+ old_nonce1 = Nonce::mk_nonce(now - 20000)
181
+ old_nonce2 = Nonce::mk_nonce(now - 10000)
182
+ recent_nonce = Nonce::mk_nonce(now - 600)
183
+
184
+ orig_skew = Nonce.skew
185
+ Nonce.skew = 0
186
+ count = @store.cleanup_nonces
187
+ Nonce.skew = 1000000
188
+ ts, salt = Nonce::split_nonce(old_nonce1)
189
+ assert(@store.use_nonce(server_url, ts, salt), "oldnonce1")
190
+ ts, salt = Nonce::split_nonce(old_nonce2)
191
+ assert(@store.use_nonce(server_url, ts, salt), "oldnonce2")
192
+ ts, salt = Nonce::split_nonce(recent_nonce)
193
+ assert(@store.use_nonce(server_url, ts, salt), "recent_nonce")
194
+
195
+ Nonce.skew = 1000
196
+ cleaned = @store.cleanup_nonces
197
+ assert_equal(2, cleaned, "Cleaned #{cleaned} nonces")
198
+
199
+ Nonce.skew = 100000
200
+ ts, salt = Nonce::split_nonce(old_nonce1)
201
+ assert(@store.use_nonce(server_url, ts, salt), "oldnonce1 after cleanup")
202
+ ts, salt = Nonce::split_nonce(old_nonce2)
203
+ assert(@store.use_nonce(server_url, ts, salt), "oldnonce2 after cleanup")
204
+ ts, salt = Nonce::split_nonce(recent_nonce)
205
+ assert(!@store.use_nonce(server_url, ts, salt), "recent_nonce after cleanup")
206
+
207
+ Nonce.skew = orig_skew
208
+
209
+ end
210
+ end
211
+
212
+ class FileStoreTestCase < Test::Unit::TestCase
213
+ include StoreTestCase
214
+
215
+ def setup
216
+ raise "filestoretest directory exists" if File.exists?('filestoretest')
217
+ @store = Filesystem.new('filestoretest')
218
+ end
219
+
220
+ def teardown
221
+ Kernel.system('rm -r filestoretest')
222
+ end
223
+ end
224
+
225
+ class MemoryStoreTestCase < Test::Unit::TestCase
226
+ include StoreTestCase
227
+
228
+ def setup
229
+ @store = Memory.new
230
+ end
231
+ end
232
+
233
+ begin
234
+ ::TESTING_MEMCACHE
235
+ rescue NameError
236
+ else
237
+ class MemcacheStoreTestCase < Test::Unit::TestCase
238
+ include StoreTestCase
239
+ def setup
240
+ store_uniq = OpenID::CryptUtil.random_string(6, "0123456789")
241
+ store_namespace = "openid-store-#{store_uniq}:"
242
+ @store = Memcache.new(::TESTING_MEMCACHE, store_namespace)
243
+ end
244
+
245
+ def test_nonce_cleanup
246
+ end
247
+
248
+ def test_assoc_cleanup
249
+ end
250
+ end
251
+ end
252
+
253
+ class AbstractStoreTestCase < Test::Unit::TestCase
254
+ def test_abstract_class
255
+ # the abstract made concrete
256
+ abc = Interface.new()
257
+ server_url = "http://server.com/"
258
+ association = OpenID::Association.new("foo", "bar", Time.now, Time.now + 10, "dummy")
259
+
260
+ assert_raise(NotImplementedError) {
261
+ abc.store_association(server_url, association)
262
+ }
263
+
264
+ assert_raise(NotImplementedError) {
265
+ abc.get_association(server_url)
266
+ }
267
+
268
+ assert_raise(NotImplementedError) {
269
+ abc.remove_association(server_url, association.handle)
270
+ }
271
+
272
+ assert_raise(NotImplementedError) {
273
+ abc.use_nonce(server_url, Time.now.to_i, "foo")
274
+ }
275
+
276
+ assert_raise(NotImplementedError) {
277
+ abc.cleanup_nonces()
278
+ }
279
+
280
+ assert_raise(NotImplementedError) {
281
+ abc.cleanup_associations()
282
+ }
283
+
284
+ assert_raise(NotImplementedError) {
285
+ abc.cleanup()
286
+ }
287
+
288
+ end
289
+
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,111 @@
1
+ require "test_helper"
2
+ require 'openid/trustroot'
3
+
4
+ class TrustRootTest < Test::Unit::TestCase
5
+ include OpenID::TestDataMixin
6
+
7
+ def _test_sanity(case_, sanity, desc)
8
+ tr = OpenID::TrustRoot::TrustRoot.parse(case_)
9
+ if sanity == 'sane'
10
+ assert(! tr.nil?)
11
+ assert(tr.sane?)
12
+ assert(OpenID::TrustRoot::TrustRoot.check_sanity(case_))
13
+ elsif sanity == 'insane'
14
+ assert(!tr.sane?)
15
+ assert(!OpenID::TrustRoot::TrustRoot.check_sanity(case_))
16
+ else
17
+ assert(tr.nil?, case_)
18
+ end
19
+ end
20
+
21
+ def _test_match(trust_root, url, expected_match)
22
+ tr = OpenID::TrustRoot::TrustRoot.parse(trust_root)
23
+ actual_match = tr.validate_url(url)
24
+ if expected_match
25
+ assert(actual_match)
26
+ assert(OpenID::TrustRoot::TrustRoot.check_url(trust_root, url))
27
+ else
28
+ assert(!actual_match)
29
+ assert(!OpenID::TrustRoot::TrustRoot.check_url(trust_root, url))
30
+ end
31
+ end
32
+
33
+ def test_trustroots
34
+ data = read_data_file('trustroot.txt', false)
35
+
36
+ parts = data.split('=' * 40 + "\n").collect { |i| i.strip() }
37
+ assert(parts[0] == '')
38
+ _, ph, pdat, mh, mdat = parts
39
+
40
+ getTests(['bad', 'insane', 'sane'], ph, pdat).each { |tc|
41
+ sanity, desc, case_ = tc
42
+ _test_sanity(case_, sanity, desc)
43
+ }
44
+
45
+ getTests([true, false], mh, mdat).each { |tc|
46
+ match, desc, case_ = tc
47
+ trust_root, url = case_.split()
48
+ _test_match(trust_root, url, match)
49
+ }
50
+ end
51
+
52
+ def getTests(grps, head, dat)
53
+ tests = []
54
+ top = head.strip()
55
+ gdat = dat.split('-' * 40 + "\n").collect { |i| i.strip() }
56
+ assert(gdat[0] == '')
57
+ assert(gdat.length == (grps.length * 2 + 1), "length match failed: gdat #{gdat}, grps #{grps}")
58
+ i = 1
59
+ grps.each { |x|
60
+ n, desc = gdat[i].split(': ')
61
+ cases = gdat[i + 1].split("\n")
62
+ assert(cases.length == n.to_i, "Number of cases differs from header count")
63
+ cases.each { |case_|
64
+ tests += [[x, top + ' - ' + desc, case_]]
65
+ }
66
+ i += 2
67
+ }
68
+
69
+ return tests
70
+ end
71
+
72
+ def test_return_to_matches
73
+ data = [
74
+ [[], nil, false],
75
+ [[], "", false],
76
+ [[], "http://bogus/return_to", false],
77
+ [["http://bogus/"], nil, false],
78
+ [["://broken/"], nil, false],
79
+ [["://broken/"], "http://broken/", false],
80
+ [["http://*.broken/"], "http://foo.broken/", false],
81
+ [["http://x.broken/"], "http://foo.broken/", false],
82
+ [["http://first/", "http://second/path/"], "http://second/?query=x", false],
83
+
84
+ [["http://broken/"], "http://broken/", true],
85
+ [["http://first/", "http://second/"], "http://second/?query=x", true],
86
+ ]
87
+
88
+ data.each { |case_|
89
+ allowed_return_urls, return_to, expected_result = case_
90
+ actual_result = OpenID::TrustRoot::return_to_matches(allowed_return_urls,
91
+ return_to)
92
+ assert(expected_result == actual_result)
93
+ }
94
+ end
95
+
96
+ def test_build_discovery_url
97
+ data = [
98
+ ["http://foo.com/path", "http://foo.com/path"],
99
+ ["http://foo.com/path?foo=bar", "http://foo.com/path?foo=bar"],
100
+ ["http://*.bogus.com/path", "http://www.bogus.com/path"],
101
+ ["http://*.bogus.com:122/path", "http://www.bogus.com:122/path"],
102
+ ]
103
+
104
+ data.each { |case_|
105
+ trust_root, expected_disco_url = case_
106
+ tr = OpenID::TrustRoot::TrustRoot.parse(trust_root)
107
+ actual_disco_url = tr.build_discovery_url()
108
+ assert(actual_disco_url == expected_disco_url)
109
+ }
110
+ end
111
+ end
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ require "openid/urinorm"
4
+
5
+ class URINormTestCase < Test::Unit::TestCase
6
+ include OpenID::TestDataMixin
7
+
8
+ def test_normalize
9
+ lines = read_data_file('urinorm.txt')
10
+
11
+ while lines.length > 0
12
+
13
+ case_name = lines.shift.strip
14
+ actual = lines.shift.strip
15
+ expected = lines.shift.strip
16
+ _newline = lines.shift
17
+
18
+ if expected == 'fail'
19
+ begin
20
+ OpenID::URINorm.urinorm(actual)
21
+ rescue URI::InvalidURIError
22
+ assert true
23
+ else
24
+ raise 'Should have gotten URI error'
25
+ end
26
+ else
27
+ normalized = OpenID::URINorm.urinorm(actual)
28
+ assert_equal(expected, normalized, case_name)
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+
@@ -0,0 +1,145 @@
1
+ # coding: ASCII-8BIT
2
+ require "test_helper"
3
+
4
+ require "openid/util"
5
+
6
+ module OpenID
7
+ class UtilTestCase < Test::Unit::TestCase
8
+
9
+ def test_base64
10
+ cases = [
11
+ "",
12
+ "\000",
13
+ "\001",
14
+ "\000" * 100,
15
+ (0...256).collect{ |i| i.chr }.join('')
16
+ ]
17
+
18
+ cases.each do |c|
19
+ encoded = Util.to_base64(c)
20
+ decoded = Util.from_base64(encoded)
21
+ assert(c == decoded)
22
+ end
23
+
24
+ end
25
+
26
+ def test_base64_valid
27
+ [["foos", "~\212,"],
28
+ ["++++", "\373\357\276"],
29
+ ["/+==", "\377"],
30
+ ["", ""],
31
+ ["FOOSBALL", "\024\343\222\004\002\313"],
32
+ ["FoosBL==", "\026\212,\004"],
33
+ ["Foos\nBall", "\026\212,\005\251e"],
34
+ ["Foo\r\ns\nBall", "\026\212,\005\251e"]
35
+ ].each do | input, expected |
36
+ assert_equal(expected, Util.from_base64(input))
37
+ end
38
+ end
39
+
40
+ def test_base64_invalid
41
+ ['!',
42
+ 'Foos!',
43
+ 'Balls',
44
+ 'B===',
45
+ 'Foos Ball',
46
+ '=foo',
47
+ ].each do |invalid_input|
48
+ assert_raises(ArgumentError) do
49
+ Util.from_base64(invalid_input)
50
+ end
51
+ end
52
+ end
53
+
54
+ def test_append_args()
55
+ simple = 'http://www.example.com/'
56
+
57
+ cases = [
58
+ ['empty list',
59
+ [simple, []],
60
+ simple],
61
+
62
+ ['empty dict',
63
+ [simple, {}],
64
+ simple],
65
+
66
+ ['one list',
67
+ [simple, [['a', 'b']]],
68
+ simple + '?a=b'],
69
+
70
+ ['one dict',
71
+ [simple, {'a' => 'b'}],
72
+ simple + '?a=b'],
73
+
74
+ ['two list (same)',
75
+ [simple, [['a', 'b'], ['a', 'c']]],
76
+ simple + '?a=b&a=c'],
77
+
78
+ ['two list',
79
+ [simple, [['a', 'b'], ['b', 'c']]],
80
+ simple + '?a=b&b=c'],
81
+
82
+ ['two list (order)',
83
+ [simple, [['b', 'c'], ['a', 'b']]],
84
+ simple + '?b=c&a=b'],
85
+
86
+ ['two dict [order]',
87
+ [simple, {'b' => 'c', 'a' => 'b'}],
88
+ simple + '?a=b&b=c'],
89
+
90
+ ['args exist [empty]',
91
+ [simple + '?stuff=bother', []],
92
+ simple + '?stuff=bother'],
93
+
94
+ ['escape',
95
+ [simple, [['=', '=']]],
96
+ simple + '?%3D=%3D'],
97
+
98
+ ['escape [URL]',
99
+ [simple, [['this_url', simple]]],
100
+ simple + '?this_url=http%3A%2F%2Fwww.example.com%2F'],
101
+
102
+ ['use dots',
103
+ [simple, [['openid.stuff', 'bother']]],
104
+ simple + '?openid.stuff=bother'],
105
+
106
+ ['args exist',
107
+ [simple + '?stuff=bother', [['ack', 'ack']]],
108
+ simple + '?stuff=bother&ack=ack'],
109
+
110
+ ['args exist',
111
+ [simple + '?stuff=bother', [['ack', 'ack']]],
112
+ simple + '?stuff=bother&ack=ack'],
113
+
114
+ ['args exist [dict]',
115
+ [simple + '?stuff=bother', {'ack' => 'ack'}],
116
+ simple + '?stuff=bother&ack=ack'],
117
+
118
+ ['args exist [dict 2]',
119
+ [simple + '?stuff=bother', {'ack' => 'ack', 'zebra' => 'lion'}],
120
+ simple + '?stuff=bother&ack=ack&zebra=lion'],
121
+
122
+ ['three args [dict]',
123
+ [simple, {'stuff' => 'bother', 'ack' => 'ack', 'zebra' => 'lion'}],
124
+ simple + '?ack=ack&stuff=bother&zebra=lion'],
125
+
126
+ ['three args [list]',
127
+ [simple, [['stuff', 'bother'], ['ack', 'ack'], ['zebra', 'lion']]],
128
+ simple + '?stuff=bother&ack=ack&zebra=lion'],
129
+ ]
130
+
131
+ cases.each { |name, args, expected|
132
+ url, pairs = args
133
+ actual = Util.append_args(url, pairs)
134
+ msg = "[#{name}] Expected: #{expected}, actual: #{actual}"
135
+ assert_equal(expected, actual, msg)
136
+ }
137
+
138
+ end
139
+
140
+ def test_parse_query
141
+ assert_equal({'foo'=>'bar'}, Util.parse_query('foo=bar'))
142
+ end
143
+
144
+ end
145
+ end