PageRenderTime 84ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/openssl/test_x509name.rb

http://github.com/ruby/ruby
Ruby | 469 lines | 404 code | 42 blank | 23 comment | 5 complexity | 4a082102ddfe01a645cd9b0d307cdf1d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # coding: ASCII-8BIT
  2. # frozen_string_literal: true
  3. require_relative 'utils'
  4. if defined?(OpenSSL)
  5. class OpenSSL::TestX509Name < OpenSSL::TestCase
  6. def setup
  7. super
  8. @obj_type_tmpl = Hash.new(OpenSSL::ASN1::PRINTABLESTRING)
  9. @obj_type_tmpl.update(OpenSSL::X509::Name::OBJECT_TYPE_TEMPLATE)
  10. end
  11. def test_s_new
  12. dn = [ ["C", "JP"], ["O", "example"], ["CN", "www.example.jp"] ]
  13. name = OpenSSL::X509::Name.new(dn)
  14. ary = name.to_a
  15. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  16. assert_equal("C", ary[0][0])
  17. assert_equal("O", ary[1][0])
  18. assert_equal("CN", ary[2][0])
  19. assert_equal("JP", ary[0][1])
  20. assert_equal("example", ary[1][1])
  21. assert_equal("www.example.jp", ary[2][1])
  22. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  23. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
  24. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  25. dn = [
  26. ["countryName", "JP"],
  27. ["organizationName", "example"],
  28. ["commonName", "www.example.jp"]
  29. ]
  30. name = OpenSSL::X509::Name.new(dn)
  31. ary = name.to_a
  32. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  33. assert_equal("C", ary[0][0])
  34. assert_equal("O", ary[1][0])
  35. assert_equal("CN", ary[2][0])
  36. assert_equal("JP", ary[0][1])
  37. assert_equal("example", ary[1][1])
  38. assert_equal("www.example.jp", ary[2][1])
  39. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  40. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
  41. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  42. name = OpenSSL::X509::Name.new(dn, @obj_type_tmpl)
  43. ary = name.to_a
  44. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  45. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  46. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
  47. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  48. dn = [
  49. ["countryName", "JP", OpenSSL::ASN1::PRINTABLESTRING],
  50. ["organizationName", "example", OpenSSL::ASN1::PRINTABLESTRING],
  51. ["commonName", "www.example.jp", OpenSSL::ASN1::PRINTABLESTRING]
  52. ]
  53. name = OpenSSL::X509::Name.new(dn)
  54. ary = name.to_a
  55. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  56. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  57. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
  58. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  59. dn = [
  60. ["DC", "org"],
  61. ["DC", "ruby-lang"],
  62. ["CN", "GOTOU Yuuzou"],
  63. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  64. ["serialNumber", "123"],
  65. ]
  66. name = OpenSSL::X509::Name.new(dn)
  67. ary = name.to_a
  68. assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
  69. assert_equal("DC", ary[0][0])
  70. assert_equal("DC", ary[1][0])
  71. assert_equal("CN", ary[2][0])
  72. assert_equal("emailAddress", ary[3][0])
  73. assert_equal("serialNumber", ary[4][0])
  74. assert_equal("org", ary[0][1])
  75. assert_equal("ruby-lang", ary[1][1])
  76. assert_equal("GOTOU Yuuzou", ary[2][1])
  77. assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
  78. assert_equal("123", ary[4][1])
  79. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  80. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  81. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  82. assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
  83. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
  84. name_from_der = OpenSSL::X509::Name.new(name.to_der)
  85. assert_equal(name_from_der.to_s, name.to_s)
  86. assert_equal(name_from_der.to_a, name.to_a)
  87. assert_equal(name_from_der.to_der, name.to_der)
  88. end
  89. def test_unrecognized_oid
  90. dn = [ ["1.2.3.4.5.6.7.8.9.7.5.3.1", "Unknown OID 1"],
  91. ["1.1.2.3.5.8.13.21.34", "Unknown OID 2"],
  92. ["C", "US"],
  93. ["postalCode", "60602"],
  94. ["ST", "Illinois"],
  95. ["L", "Chicago"],
  96. #["street", "123 Fake St"],
  97. ["O", "Some Company LLC"],
  98. ["CN", "mydomain.com"] ]
  99. name = OpenSSL::X509::Name.new(dn)
  100. ary = name.to_a
  101. #assert_equal("/1.2.3.4.5.6.7.8.9.7.5.3.1=Unknown OID 1/1.1.2.3.5.8.13.21.34=Unknown OID 2/C=US/postalCode=60602/ST=Illinois/L=Chicago/street=123 Fake St/O=Some Company LLC/CN=mydomain.com", name.to_s)
  102. assert_equal("/1.2.3.4.5.6.7.8.9.7.5.3.1=Unknown OID 1/1.1.2.3.5.8.13.21.34=Unknown OID 2/C=US/postalCode=60602/ST=Illinois/L=Chicago/O=Some Company LLC/CN=mydomain.com", name.to_s)
  103. assert_equal("1.2.3.4.5.6.7.8.9.7.5.3.1", ary[0][0])
  104. assert_equal("1.1.2.3.5.8.13.21.34", ary[1][0])
  105. assert_equal("C", ary[2][0])
  106. assert_equal("postalCode", ary[3][0])
  107. assert_equal("ST", ary[4][0])
  108. assert_equal("L", ary[5][0])
  109. #assert_equal("street", ary[6][0])
  110. assert_equal("O", ary[6][0])
  111. assert_equal("CN", ary[7][0])
  112. assert_equal("Unknown OID 1", ary[0][1])
  113. assert_equal("Unknown OID 2", ary[1][1])
  114. assert_equal("US", ary[2][1])
  115. assert_equal("60602", ary[3][1])
  116. assert_equal("Illinois", ary[4][1])
  117. assert_equal("Chicago", ary[5][1])
  118. #assert_equal("123 Fake St", ary[6][1])
  119. assert_equal("Some Company LLC", ary[6][1])
  120. assert_equal("mydomain.com", ary[7][1])
  121. end
  122. def test_unrecognized_oid_parse_encode_equality
  123. dn = [ ["1.2.3.4.5.6.7.8.9.7.5.3.2", "Unknown OID1"],
  124. ["1.1.2.3.5.8.13.21.35", "Unknown OID2"],
  125. ["C", "US"],
  126. ["postalCode", "60602"],
  127. ["ST", "Illinois"],
  128. ["L", "Chicago"],
  129. #["street", "123 Fake St"],
  130. ["O", "Some Company LLC"],
  131. ["CN", "mydomain.com"] ]
  132. name1 = OpenSSL::X509::Name.new(dn)
  133. name2 = OpenSSL::X509::Name.parse(name1.to_s)
  134. assert_equal(name1.to_s, name2.to_s)
  135. assert_equal(name1.to_a, name2.to_a)
  136. end
  137. def test_s_parse
  138. dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org/1.2.3.4.5.6=A=BCD"
  139. name = OpenSSL::X509::Name.parse(dn)
  140. assert_equal(dn, name.to_s)
  141. ary = name.to_a
  142. assert_equal [
  143. ["DC", "org", OpenSSL::ASN1::IA5STRING],
  144. ["DC", "ruby-lang", OpenSSL::ASN1::IA5STRING],
  145. ["CN", "www.ruby-lang.org", OpenSSL::ASN1::UTF8STRING],
  146. ["1.2.3.4.5.6", "A=BCD", OpenSSL::ASN1::UTF8STRING],
  147. ], ary
  148. dn2 = "DC=org, DC=ruby-lang, CN=www.ruby-lang.org, 1.2.3.4.5.6=A=BCD"
  149. name = OpenSSL::X509::Name.parse(dn2)
  150. assert_equal(dn, name.to_s)
  151. assert_equal ary, name.to_a
  152. name = OpenSSL::X509::Name.parse(dn2, @obj_type_tmpl)
  153. ary = name.to_a
  154. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  155. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  156. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  157. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[3][2])
  158. end
  159. def test_s_parse_rfc2253
  160. scanner = OpenSSL::X509::Name::RFC2253DN.method(:scan)
  161. assert_equal([["C", "JP"]], scanner.call("C=JP"))
  162. assert_equal([
  163. ["DC", "org"],
  164. ["DC", "ruby-lang"],
  165. ["CN", "GOTOU Yuuzou"],
  166. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  167. ],
  168. scanner.call(
  169. "emailAddress=gotoyuzo@ruby-lang.org,CN=GOTOU Yuuzou,"+
  170. "DC=ruby-lang,DC=org")
  171. )
  172. u8 = OpenSSL::ASN1::UTF8STRING
  173. assert_equal([
  174. ["DC", "org"],
  175. ["DC", "ruby-lang"],
  176. ["O", ",=+<>#;"],
  177. ["O", ",=+<>#;"],
  178. ["OU", ""],
  179. ["OU", ""],
  180. ["L", "aaa=\"bbb, ccc\""],
  181. ["L", "aaa=\"bbb, ccc\""],
  182. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  183. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  184. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  185. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265", u8],
  186. ["2.5.4.3", "GOTOU, Yuuzou"],
  187. ["2.5.4.3", "GOTOU, Yuuzou"],
  188. ["2.5.4.3", "GOTOU, Yuuzou"],
  189. ["2.5.4.3", "GOTOU, Yuuzou"],
  190. ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
  191. ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
  192. ["1.2.840.113549.1.9.1", "gotoyuzo@ruby-lang.org"],
  193. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  194. ],
  195. scanner.call(
  196. "emailAddress=gotoyuzo@ruby-lang.org," +
  197. "1.2.840.113549.1.9.1=gotoyuzo@ruby-lang.org," +
  198. 'CN=GOTOU \"gotoyuzo\" Yuuzou,' +
  199. 'CN="GOTOU \"gotoyuzo\" Yuuzou",' +
  200. '2.5.4.3=GOTOU\,\20Yuuzou,' +
  201. '2.5.4.3=GOTOU\, Yuuzou,' +
  202. '2.5.4.3="GOTOU, Yuuzou",' +
  203. '2.5.4.3="GOTOU\, Yuuzou",' +
  204. "CN=#0C0CE5BE8CE897A4E8A395E894B5," +
  205. 'CN=\E5\BE\8C\E8\97\A4\E8\A3\95\E8\94\B5,' +
  206. "CN=\"\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5\"," +
  207. "CN=\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5," +
  208. 'L=aaa\=\"bbb\, ccc\",' +
  209. 'L="aaa=\"bbb, ccc\"",' +
  210. 'OU=,' +
  211. 'OU="",' +
  212. 'O=\,\=\+\<\>\#\;,' +
  213. 'O=",=+<>#;",' +
  214. "DC=ruby-lang," +
  215. "DC=org")
  216. )
  217. [
  218. "DC=org+DC=jp",
  219. "DC=org,DC=ruby-lang+DC=rubyist,DC=www"
  220. ].each{|dn|
  221. ex = scanner.call(dn) rescue $!
  222. dn_r = Regexp.escape(dn)
  223. assert_match(/^multi-valued RDN is not supported: #{dn_r}/, ex.message)
  224. }
  225. [
  226. ["DC=org,DC=exapmle,CN", "CN"],
  227. ["DC=org,DC=example,", ""],
  228. ["DC=org,DC=exapmle,CN=www.example.org;", "CN=www.example.org;"],
  229. ["DC=org,DC=exapmle,CN=#www.example.org", "CN=#www.example.org"],
  230. ["DC=org,DC=exapmle,CN=#777777.example.org", "CN=#777777.example.org"],
  231. ["DC=org,DC=exapmle,CN=\"www.example\".org", "CN=\"www.example\".org"],
  232. ["DC=org,DC=exapmle,CN=www.\"example.org\"", "CN=www.\"example.org\""],
  233. ["DC=org,DC=exapmle,CN=www.\"example\".org", "CN=www.\"example\".org"],
  234. ].each{|dn, msg|
  235. ex = scanner.call(dn) rescue $!
  236. assert_match(/^malformed RDN: .*=>#{Regexp.escape(msg)}/, ex.message)
  237. }
  238. dn = "CN=www.ruby-lang.org,DC=ruby-lang,DC=org"
  239. name = OpenSSL::X509::Name.parse_rfc2253(dn)
  240. assert_equal(dn, name.to_s(OpenSSL::X509::Name::RFC2253))
  241. ary = name.to_a
  242. assert_equal("DC", ary[0][0])
  243. assert_equal("DC", ary[1][0])
  244. assert_equal("CN", ary[2][0])
  245. assert_equal("org", ary[0][1])
  246. assert_equal("ruby-lang", ary[1][1])
  247. assert_equal("www.ruby-lang.org", ary[2][1])
  248. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  249. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  250. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  251. end
  252. def test_add_entry
  253. dn = [
  254. ["DC", "org"],
  255. ["DC", "ruby-lang"],
  256. ["CN", "GOTOU Yuuzou"],
  257. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  258. ["serialNumber", "123"],
  259. ]
  260. name = OpenSSL::X509::Name.new
  261. dn.each{|attr| name.add_entry(*attr) }
  262. ary = name.to_a
  263. assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
  264. assert_equal("DC", ary[0][0])
  265. assert_equal("DC", ary[1][0])
  266. assert_equal("CN", ary[2][0])
  267. assert_equal("emailAddress", ary[3][0])
  268. assert_equal("serialNumber", ary[4][0])
  269. assert_equal("org", ary[0][1])
  270. assert_equal("ruby-lang", ary[1][1])
  271. assert_equal("GOTOU Yuuzou", ary[2][1])
  272. assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
  273. assert_equal("123", ary[4][1])
  274. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  275. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  276. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  277. assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
  278. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
  279. end
  280. def test_add_entry_street
  281. # openssl/crypto/objects/obj_mac.h 1.83
  282. dn = [
  283. ["DC", "org"],
  284. ["DC", "ruby-lang"],
  285. ["CN", "GOTOU Yuuzou"],
  286. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  287. ["serialNumber", "123"],
  288. ["street", "Namiki"],
  289. ]
  290. name = OpenSSL::X509::Name.new
  291. dn.each{|attr| name.add_entry(*attr) }
  292. ary = name.to_a
  293. assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123/street=Namiki", name.to_s)
  294. assert_equal("Namiki", ary[5][1])
  295. end
  296. def test_add_entry_placing
  297. der = %w{ 30 2A
  298. 31 12
  299. 30 10 06 03 55 04 0A 0C 09 72 75 62 79 2D 6C 61 6E 67
  300. 31 14
  301. 30 08 06 03 55 04 0B 0C 01 61
  302. 30 08 06 03 55 04 0B 0C 01 62 }
  303. orig = OpenSSL::X509::Name.new([der.join].pack("H*"))
  304. assert_equal("OU=b+OU=a,O=ruby-lang", orig.to_s(OpenSSL::X509::Name::RFC2253))
  305. # Skip for now; they do not work
  306. #
  307. # dn = orig.dup
  308. # dn.add_entry("CN", "unya", loc: 0, set: 0)
  309. # assert_equal("OU=b+OU=a,O=ruby-lang,CN=unya", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  310. # dn = orig.dup
  311. # dn.add_entry("CN", "unya", loc: 0, set: 1)
  312. # assert_equal("OU=b+OU=a,O=ruby-lang+CN=unya", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  313. dn = orig.dup
  314. dn.add_entry("CN", "unya", loc: 1, set: -1)
  315. assert_equal("OU=b+OU=a,O=ruby-lang+CN=unya", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  316. # dn = orig.dup
  317. # dn.add_entry("CN", "unya", loc: 1, set: 0)
  318. # assert_equal("OU=b+OU=a,CN=unya,O=ruby-lang", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  319. dn = orig.dup
  320. dn.add_entry("CN", "unya", loc: 1, set: 1)
  321. assert_equal("CN=unya+OU=b+OU=a,O=ruby-lang", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  322. dn = orig.dup
  323. dn.add_entry("CN", "unya", loc: -1, set: -1)
  324. assert_equal("CN=unya+OU=b+OU=a,O=ruby-lang", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  325. dn = orig.dup
  326. dn.add_entry("CN", "unya", loc: -1, set: 0)
  327. assert_equal("CN=unya,OU=b+OU=a,O=ruby-lang", dn.dup.to_s(OpenSSL::X509::Name::RFC2253))
  328. end
  329. def test_to_s
  330. dn = [
  331. ["DC", "org"],
  332. ["DC", "ruby-lang"],
  333. ["CN", "フー, バー"],
  334. ]
  335. name = OpenSSL::X509::Name.new
  336. dn.each { |x| name.add_entry(*x) }
  337. assert_equal "/DC=org/DC=ruby-lang/" \
  338. "CN=\\xE3\\x83\\x95\\xE3\\x83\\xBC, \\xE3\\x83\\x90\\xE3\\x83\\xBC",
  339. name.to_s
  340. # OpenSSL escapes characters with MSB by default
  341. assert_equal \
  342. "CN=\\E3\\83\\95\\E3\\83\\BC\\, \\E3\\83\\90\\E3\\83\\BC," \
  343. "DC=ruby-lang,DC=org",
  344. name.to_s(OpenSSL::X509::Name::RFC2253)
  345. assert_equal "DC = org, DC = ruby-lang, " \
  346. "CN = \"\\E3\\83\\95\\E3\\83\\BC, \\E3\\83\\90\\E3\\83\\BC\"",
  347. name.to_s(OpenSSL::X509::Name::ONELINE)
  348. empty = OpenSSL::X509::Name.new
  349. assert_equal "", empty.to_s
  350. assert_equal "", empty.to_s(OpenSSL::X509::Name::COMPAT)
  351. assert_equal "", empty.to_s(OpenSSL::X509::Name::RFC2253)
  352. assert_equal "", empty.to_s(OpenSSL::X509::Name::ONELINE)
  353. end
  354. def test_to_utf8
  355. dn = [
  356. ["DC", "org"],
  357. ["DC", "ruby-lang"],
  358. ["CN", "フー, バー"],
  359. ]
  360. name = OpenSSL::X509::Name.new
  361. dn.each { |x| name.add_entry(*x) }
  362. str = name.to_utf8
  363. expected = String.new("CN=フー\\, バー,DC=ruby-lang,DC=org").force_encoding("UTF-8")
  364. assert_equal expected, str
  365. assert_equal Encoding.find("UTF-8"), str.encoding
  366. empty = OpenSSL::X509::Name.new
  367. assert_equal "", empty.to_utf8
  368. end
  369. def test_equals2
  370. n1 = OpenSSL::X509::Name.parse_rfc2253 'CN=a'
  371. n2 = OpenSSL::X509::Name.parse_rfc2253 'CN=a'
  372. assert_equal n1, n2
  373. assert_equal(false, n1 == 'abc')
  374. assert_equal(false, n2 == nil)
  375. end
  376. def test_spaceship
  377. n1 = OpenSSL::X509::Name.new([["CN", "a"]])
  378. n2 = OpenSSL::X509::Name.new([["CN", "a"]])
  379. n3 = OpenSSL::X509::Name.new([["CN", "ab"]])
  380. assert_equal(0, n1 <=> n2)
  381. assert_equal(-1, n1 <=> n3)
  382. assert_equal(0, n2 <=> n1)
  383. assert_equal(-1, n2 <=> n3)
  384. assert_equal(1, n3 <=> n1)
  385. assert_equal(1, n3 <=> n2)
  386. assert_equal(nil, n1 <=> 'abc')
  387. assert_equal(nil, n2 <=> 123)
  388. assert_equal(nil, n3 <=> nil)
  389. end
  390. def name_hash(name)
  391. # OpenSSL 1.0.0 uses SHA1 for canonical encoding (not just a der) of
  392. # X509Name for X509_NAME_hash.
  393. name.respond_to?(:hash_old) ? name.hash_old : name.hash
  394. end
  395. def test_hash
  396. dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org"
  397. name = OpenSSL::X509::Name.parse(dn)
  398. d = OpenSSL::Digest::MD5.digest(name.to_der)
  399. expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24
  400. assert_equal(expected, name_hash(name))
  401. #
  402. dn = "/DC=org/DC=ruby-lang/CN=baz.ruby-lang.org"
  403. name = OpenSSL::X509::Name.parse(dn)
  404. d = OpenSSL::Digest::MD5.digest(name.to_der)
  405. expected = (d[0].ord & 0xff) | (d[1].ord & 0xff) << 8 | (d[2].ord & 0xff) << 16 | (d[3].ord & 0xff) << 24
  406. assert_equal(expected, name_hash(name))
  407. end
  408. def test_equality
  409. name0 = OpenSSL::X509::Name.new([["DC", "org"], ["DC", "ruby-lang"], ["CN", "bar.ruby-lang.org"]])
  410. name1 = OpenSSL::X509::Name.new([["DC", "org"], ["DC", "ruby-lang"], ["CN", "bar.ruby-lang.org"]])
  411. name2 = OpenSSL::X509::Name.new([["DC", "org"], ["DC", "ruby-lang"], ["CN", "baz.ruby-lang.org"]])
  412. assert_equal true, name0 == name1
  413. assert_equal true, name0.eql?(name1)
  414. assert_equal false, name0 == name2
  415. assert_equal false, name0.eql?(name2)
  416. end
  417. def test_marshal
  418. name = OpenSSL::X509::Name.new([["DC", "org"], ["DC", "ruby-lang"], ["CN", "bar.ruby-lang.org"]])
  419. deserialized = Marshal.load(Marshal.dump(name))
  420. assert_equal name.to_der, deserialized.to_der
  421. end
  422. def test_dup
  423. name = OpenSSL::X509::Name.parse("/CN=ruby-lang.org")
  424. assert_equal(name.to_der, name.dup.to_der)
  425. end
  426. end
  427. end