ruby-openid 1.1.4 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ruby-openid might be problematic. Click here for more details.

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