PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/test/externals/ruby1.9/openssl/test_x509name.rb

https://github.com/rkh/jruby
Ruby | 266 lines | 245 code | 21 blank | 0 comment | 1 complexity | 673949ff5c906b669b476e01f868768b MD5 | raw file
  1. begin
  2. require "openssl"
  3. rescue LoadError
  4. end
  5. require "test/unit"
  6. if defined?(OpenSSL)
  7. class OpenSSL::TestX509Name < Test::Unit::TestCase
  8. OpenSSL::ASN1::ObjectId.register(
  9. "1.2.840.113549.1.9.1", "emailAddress", "emailAddress")
  10. OpenSSL::ASN1::ObjectId.register(
  11. "2.5.4.5", "serialNumber", "serialNumber")
  12. def setup
  13. @obj_type_tmpl = Hash.new(OpenSSL::ASN1::PRINTABLESTRING)
  14. @obj_type_tmpl.update(OpenSSL::X509::Name::OBJECT_TYPE_TEMPLATE)
  15. end
  16. def teardown
  17. end
  18. def test_s_new
  19. dn = [ ["C", "JP"], ["O", "example"], ["CN", "www.example.jp"] ]
  20. name = OpenSSL::X509::Name.new(dn)
  21. ary = name.to_a
  22. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  23. assert_equal("C", ary[0][0])
  24. assert_equal("O", ary[1][0])
  25. assert_equal("CN", ary[2][0])
  26. assert_equal("JP", ary[0][1])
  27. assert_equal("example", ary[1][1])
  28. assert_equal("www.example.jp", ary[2][1])
  29. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  30. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
  31. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  32. dn = [
  33. ["countryName", "JP"],
  34. ["organizationName", "example"],
  35. ["commonName", "www.example.jp"]
  36. ]
  37. name = OpenSSL::X509::Name.new(dn)
  38. ary = name.to_a
  39. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  40. assert_equal("C", ary[0][0])
  41. assert_equal("O", ary[1][0])
  42. assert_equal("CN", ary[2][0])
  43. assert_equal("JP", ary[0][1])
  44. assert_equal("example", ary[1][1])
  45. assert_equal("www.example.jp", ary[2][1])
  46. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  47. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[1][2])
  48. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  49. name = OpenSSL::X509::Name.new(dn, @obj_type_tmpl)
  50. ary = name.to_a
  51. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  52. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  53. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
  54. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  55. dn = [
  56. ["countryName", "JP", OpenSSL::ASN1::PRINTABLESTRING],
  57. ["organizationName", "example", OpenSSL::ASN1::PRINTABLESTRING],
  58. ["commonName", "www.example.jp", OpenSSL::ASN1::PRINTABLESTRING]
  59. ]
  60. name = OpenSSL::X509::Name.new(dn)
  61. ary = name.to_a
  62. assert_equal("/C=JP/O=example/CN=www.example.jp", name.to_s)
  63. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[0][2])
  64. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[1][2])
  65. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  66. dn = [
  67. ["DC", "org"],
  68. ["DC", "ruby-lang"],
  69. ["CN", "GOTOU Yuuzou"],
  70. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  71. ["serialNumber", "123"],
  72. ]
  73. name = OpenSSL::X509::Name.new(dn)
  74. ary = name.to_a
  75. assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
  76. assert_equal("DC", ary[0][0])
  77. assert_equal("DC", ary[1][0])
  78. assert_equal("CN", ary[2][0])
  79. assert_equal("emailAddress", ary[3][0])
  80. assert_equal("serialNumber", ary[4][0])
  81. assert_equal("org", ary[0][1])
  82. assert_equal("ruby-lang", ary[1][1])
  83. assert_equal("GOTOU Yuuzou", ary[2][1])
  84. assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
  85. assert_equal("123", ary[4][1])
  86. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  87. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  88. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  89. assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
  90. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
  91. name_from_der = OpenSSL::X509::Name.new(name.to_der)
  92. assert_equal(name_from_der.to_s, name.to_s)
  93. assert_equal(name_from_der.to_a, name.to_a)
  94. assert_equal(name_from_der.to_der, name.to_der)
  95. end
  96. def test_s_parse
  97. dn = "/DC=org/DC=ruby-lang/CN=www.ruby-lang.org"
  98. name = OpenSSL::X509::Name.parse(dn)
  99. assert_equal(dn, name.to_s)
  100. ary = name.to_a
  101. assert_equal("DC", ary[0][0])
  102. assert_equal("DC", ary[1][0])
  103. assert_equal("CN", ary[2][0])
  104. assert_equal("org", ary[0][1])
  105. assert_equal("ruby-lang", ary[1][1])
  106. assert_equal("www.ruby-lang.org", ary[2][1])
  107. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  108. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  109. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  110. dn2 = "DC=org, DC=ruby-lang, CN=www.ruby-lang.org"
  111. name = OpenSSL::X509::Name.parse(dn)
  112. ary = name.to_a
  113. assert_equal(dn, name.to_s)
  114. assert_equal("org", ary[0][1])
  115. assert_equal("ruby-lang", ary[1][1])
  116. assert_equal("www.ruby-lang.org", ary[2][1])
  117. name = OpenSSL::X509::Name.parse(dn, @obj_type_tmpl)
  118. ary = name.to_a
  119. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  120. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  121. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[2][2])
  122. end
  123. def test_s_parse_rfc2253
  124. scanner = OpenSSL::X509::Name::RFC2253DN.method(:scan)
  125. assert_equal([["C", "JP"]], scanner.call("C=JP"))
  126. assert_equal([
  127. ["DC", "org"],
  128. ["DC", "ruby-lang"],
  129. ["CN", "GOTOU Yuuzou"],
  130. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  131. ],
  132. scanner.call(
  133. "emailAddress=gotoyuzo@ruby-lang.org,CN=GOTOU Yuuzou,"+
  134. "DC=ruby-lang,DC=org")
  135. )
  136. u8 = OpenSSL::ASN1::UTF8STRING
  137. assert_equal([
  138. ["DC", "org"],
  139. ["DC", "ruby-lang"],
  140. ["O", ",=+<>#;"],
  141. ["O", ",=+<>#;"],
  142. ["OU", ""],
  143. ["OU", ""],
  144. ["L", "aaa=\"bbb, ccc\""],
  145. ["L", "aaa=\"bbb, ccc\""],
  146. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  147. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  148. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265"],
  149. ["CN", "\345\276\214\350\227\244\350\243\225\350\224\265", u8],
  150. ["2.5.4.3", "GOTOU, Yuuzou"],
  151. ["2.5.4.3", "GOTOU, Yuuzou"],
  152. ["2.5.4.3", "GOTOU, Yuuzou"],
  153. ["2.5.4.3", "GOTOU, Yuuzou"],
  154. ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
  155. ["CN", "GOTOU \"gotoyuzo\" Yuuzou"],
  156. ["1.2.840.113549.1.9.1", "gotoyuzo@ruby-lang.org"],
  157. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  158. ],
  159. scanner.call(
  160. "emailAddress=gotoyuzo@ruby-lang.org," +
  161. "1.2.840.113549.1.9.1=gotoyuzo@ruby-lang.org," +
  162. 'CN=GOTOU \"gotoyuzo\" Yuuzou,' +
  163. 'CN="GOTOU \"gotoyuzo\" Yuuzou",' +
  164. '2.5.4.3=GOTOU\,\20Yuuzou,' +
  165. '2.5.4.3=GOTOU\, Yuuzou,' +
  166. '2.5.4.3="GOTOU, Yuuzou",' +
  167. '2.5.4.3="GOTOU\, Yuuzou",' +
  168. "CN=#0C0CE5BE8CE897A4E8A395E894B5," +
  169. 'CN=\E5\BE\8C\E8\97\A4\E8\A3\95\E8\94\B5,' +
  170. "CN=\"\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5\"," +
  171. "CN=\xE5\xBE\x8C\xE8\x97\xA4\xE8\xA3\x95\xE8\x94\xB5," +
  172. 'L=aaa\=\"bbb\, ccc\",' +
  173. 'L="aaa=\"bbb, ccc\"",' +
  174. 'OU=,' +
  175. 'OU="",' +
  176. 'O=\,\=\+\<\>\#\;,' +
  177. 'O=",=+<>#;",' +
  178. "DC=ruby-lang," +
  179. "DC=org")
  180. )
  181. [
  182. "DC=org+DC=jp",
  183. "DC=org,DC=ruby-lang+DC=rubyist,DC=www"
  184. ].each{|dn|
  185. ex = scanner.call(dn) rescue $!
  186. dn_r = Regexp.escape(dn)
  187. assert_match(/^multi-valued RDN is not supported: #{dn_r}/, ex.message)
  188. }
  189. [
  190. ["DC=org,DC=exapmle,CN", "CN"],
  191. ["DC=org,DC=example,", ""],
  192. ["DC=org,DC=exapmle,CN=www.example.org;", "CN=www.example.org;"],
  193. ["DC=org,DC=exapmle,CN=#www.example.org", "CN=#www.example.org"],
  194. ["DC=org,DC=exapmle,CN=#777777.example.org", "CN=#777777.example.org"],
  195. ["DC=org,DC=exapmle,CN=\"www.example\".org", "CN=\"www.example\".org"],
  196. ["DC=org,DC=exapmle,CN=www.\"example.org\"", "CN=www.\"example.org\""],
  197. ["DC=org,DC=exapmle,CN=www.\"example\".org", "CN=www.\"example\".org"],
  198. ].each{|dn, msg|
  199. ex = scanner.call(dn) rescue $!
  200. assert_match(/^malformed RDN: .*=>#{Regexp.escape(msg)}/, ex.message)
  201. }
  202. dn = "CN=www.ruby-lang.org,DC=ruby-lang,DC=org"
  203. name = OpenSSL::X509::Name.parse_rfc2253(dn)
  204. assert_equal(dn, name.to_s(OpenSSL::X509::Name::RFC2253))
  205. ary = name.to_a
  206. assert_equal("DC", ary[0][0])
  207. assert_equal("DC", ary[1][0])
  208. assert_equal("CN", ary[2][0])
  209. assert_equal("org", ary[0][1])
  210. assert_equal("ruby-lang", ary[1][1])
  211. assert_equal("www.ruby-lang.org", ary[2][1])
  212. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  213. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  214. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  215. end
  216. def test_add_entry
  217. dn = [
  218. ["DC", "org"],
  219. ["DC", "ruby-lang"],
  220. ["CN", "GOTOU Yuuzou"],
  221. ["emailAddress", "gotoyuzo@ruby-lang.org"],
  222. ["serialNumber", "123"],
  223. ]
  224. name = OpenSSL::X509::Name.new
  225. dn.each{|attr| name.add_entry(*attr) }
  226. ary = name.to_a
  227. assert_equal("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou/emailAddress=gotoyuzo@ruby-lang.org/serialNumber=123", name.to_s)
  228. assert_equal("DC", ary[0][0])
  229. assert_equal("DC", ary[1][0])
  230. assert_equal("CN", ary[2][0])
  231. assert_equal("emailAddress", ary[3][0])
  232. assert_equal("serialNumber", ary[4][0])
  233. assert_equal("org", ary[0][1])
  234. assert_equal("ruby-lang", ary[1][1])
  235. assert_equal("GOTOU Yuuzou", ary[2][1])
  236. assert_equal("gotoyuzo@ruby-lang.org", ary[3][1])
  237. assert_equal("123", ary[4][1])
  238. assert_equal(OpenSSL::ASN1::IA5STRING, ary[0][2])
  239. assert_equal(OpenSSL::ASN1::IA5STRING, ary[1][2])
  240. assert_equal(OpenSSL::ASN1::UTF8STRING, ary[2][2])
  241. assert_equal(OpenSSL::ASN1::IA5STRING, ary[3][2])
  242. assert_equal(OpenSSL::ASN1::PRINTABLESTRING, ary[4][2])
  243. end
  244. end
  245. end