PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/openssl/test_config.rb

https://github.com/nazy/ruby
Ruby | 290 lines | 284 code | 6 blank | 0 comment | 0 complexity | c3bf408259968d4d3efc69bb74dcffb2 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  1. require 'openssl'
  2. require "test/unit"
  3. require 'tempfile'
  4. require File.join(File.dirname(__FILE__), "utils.rb")
  5. class OpenSSL::TestConfig < Test::Unit::TestCase
  6. def setup
  7. file = Tempfile.open("openssl.cnf")
  8. file << <<__EOD__
  9. HOME = .
  10. [ ca ]
  11. default_ca = CA_default
  12. [ CA_default ]
  13. dir = ./demoCA
  14. certs = ./certs
  15. __EOD__
  16. file.close
  17. @it = OpenSSL::Config.new(file.path)
  18. end
  19. def test_constants
  20. assert(defined?(OpenSSL::Config::DEFAULT_CONFIG_FILE))
  21. assert_nothing_raised do
  22. OpenSSL::Config.load(OpenSSL::Config::DEFAULT_CONFIG_FILE)
  23. end
  24. end
  25. def test_s_parse
  26. c = OpenSSL::Config.parse('')
  27. assert_equal("[ default ]\n\n", c.to_s)
  28. c = OpenSSL::Config.parse(@it.to_s)
  29. assert_equal(['CA_default', 'ca', 'default'], c.sections.sort)
  30. end
  31. def test_s_parse_format
  32. c = OpenSSL::Config.parse(<<__EOC__)
  33. baz =qx\t # "baz = qx"
  34. foo::bar = baz # shortcut section::key format
  35. default::bar = baz # ditto
  36. a=\t \t # "a = ": trailing spaces are ignored
  37. =b # " = b": empty key
  38. =c # " = c": empty key (override the above line)
  39. d= # "c = ": trailing comment is ignored
  40. sq = 'foo''b\\'ar'
  41. dq ="foo""''\\""
  42. dq2 = foo""bar
  43. esc=a\\r\\n\\b\\tb
  44. foo\\bar = foo\\b\\\\ar
  45. foo\\bar::foo\\bar = baz
  46. [default1 default2]\t\t # space is allowed in section name
  47. fo =b ar # space allowed in value
  48. [emptysection]
  49. [doller ]
  50. foo=bar
  51. bar = $(foo)
  52. baz = 123$(default::bar)456${foo}798
  53. qux = ${baz}
  54. quxx = $qux.$qux
  55. __EOC__
  56. assert_equal(['default', 'default1 default2', 'doller', 'emptysection', 'foo', 'foo\\bar'], c.sections.sort)
  57. assert_equal(['', 'a', 'bar', 'baz', 'd', 'dq', 'dq2', 'esc', 'foo\\bar', 'sq'], c['default'].keys.sort)
  58. assert_equal('c', c['default'][''])
  59. assert_equal('', c['default']['a'])
  60. assert_equal('qx', c['default']['baz'])
  61. assert_equal('', c['default']['d'])
  62. assert_equal('baz', c['default']['bar'])
  63. assert_equal("foob'ar", c['default']['sq'])
  64. assert_equal("foo''\"", c['default']['dq'])
  65. assert_equal("foobar", c['default']['dq2'])
  66. assert_equal("a\r\n\b\tb", c['default']['esc'])
  67. assert_equal("foo\b\\ar", c['default']['foo\\bar'])
  68. assert_equal('baz', c['foo']['bar'])
  69. assert_equal('baz', c['foo\\bar']['foo\\bar'])
  70. assert_equal('b ar', c['default1 default2']['fo'])
  71. # dolloer
  72. assert_equal('bar', c['doller']['foo'])
  73. assert_equal('bar', c['doller']['bar'])
  74. assert_equal('123baz456bar798', c['doller']['baz'])
  75. assert_equal('123baz456bar798', c['doller']['qux'])
  76. assert_equal('123baz456bar798.123baz456bar798', c['doller']['quxx'])
  77. excn = assert_raise(OpenSSL::ConfigError) do
  78. OpenSSL::Config.parse("foo = $bar")
  79. end
  80. assert_equal("error in line 1: variable has no value", excn.message)
  81. excn = assert_raise(OpenSSL::ConfigError) do
  82. OpenSSL::Config.parse("foo = $(bar")
  83. end
  84. assert_equal("error in line 1: no close brace", excn.message)
  85. excn = assert_raise(OpenSSL::ConfigError) do
  86. OpenSSL::Config.parse("f o =b ar # no space in key")
  87. end
  88. assert_equal("error in line 1: missing equal sign", excn.message)
  89. excn = assert_raise(OpenSSL::ConfigError) do
  90. OpenSSL::Config.parse(<<__EOC__)
  91. # comment 1 # comments
  92. #
  93. # comment 2
  94. \t#comment 3
  95. [second ]\t
  96. [third # section not terminated
  97. __EOC__
  98. end
  99. assert_equal("error in line 7: missing close square bracket", excn.message)
  100. end
  101. def test_s_load
  102. # alias of new
  103. c = OpenSSL::Config.load
  104. assert_equal("", c.to_s)
  105. assert_equal([], c.sections)
  106. #
  107. file = Tempfile.open("openssl.cnf")
  108. file.close
  109. c = OpenSSL::Config.load(file.path)
  110. assert_equal("[ default ]\n\n", c.to_s)
  111. assert_equal(['default'], c.sections)
  112. end
  113. def test_initialize
  114. c = OpenSSL::Config.new
  115. assert_equal("", c.to_s)
  116. assert_equal([], c.sections)
  117. end
  118. def test_initialize_with_empty_file
  119. file = Tempfile.open("openssl.cnf")
  120. file.close
  121. c = OpenSSL::Config.new(file.path)
  122. assert_equal("[ default ]\n\n", c.to_s)
  123. assert_equal(['default'], c.sections)
  124. end
  125. def test_initialize_with_example_file
  126. assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
  127. end
  128. def test_get_value
  129. assert_equal('CA_default', @it.get_value('ca', 'default_ca'))
  130. assert_equal(nil, @it.get_value('ca', 'no such key'))
  131. assert_equal(nil, @it.get_value('no such section', 'no such key'))
  132. assert_equal('.', @it.get_value('', 'HOME'))
  133. assert_raise(TypeError) do
  134. @it.get_value(nil, 'HOME') # not allowed unlike Config#value
  135. end
  136. # fallback to 'default' ugly...
  137. assert_equal('.', @it.get_value('unknown', 'HOME'))
  138. end
  139. def test_get_value_ENV
  140. key = ENV.keys.first
  141. assert_not_nil(key) # make sure we have at least one ENV var.
  142. assert_equal(ENV[key], @it.get_value('ENV', key))
  143. end
  144. def test_value
  145. # supress deprecation warnings
  146. OpenSSL::TestUtils.silent do
  147. assert_equal('CA_default', @it.value('ca', 'default_ca'))
  148. assert_equal(nil, @it.value('ca', 'no such key'))
  149. assert_equal(nil, @it.value('no such section', 'no such key'))
  150. assert_equal('.', @it.value('', 'HOME'))
  151. assert_equal('.', @it.value(nil, 'HOME'))
  152. assert_equal('.', @it.value('HOME'))
  153. # fallback to 'default' ugly...
  154. assert_equal('.', @it.value('unknown', 'HOME'))
  155. end
  156. end
  157. def test_value_ENV
  158. OpenSSL::TestUtils.silent do
  159. key = ENV.keys.first
  160. assert_not_nil(key) # make sure we have at least one ENV var.
  161. assert_equal(ENV[key], @it.value('ENV', key))
  162. end
  163. end
  164. def test_aref
  165. assert_equal({'HOME' => '.'}, @it['default'])
  166. assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it['CA_default'])
  167. assert_equal({}, @it['no_such_section'])
  168. assert_equal({}, @it[''])
  169. end
  170. def test_section
  171. OpenSSL::TestUtils.silent do
  172. assert_equal({'HOME' => '.'}, @it.section('default'))
  173. assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it.section('CA_default'))
  174. assert_equal({}, @it.section('no_such_section'))
  175. assert_equal({}, @it.section(''))
  176. end
  177. end
  178. def test_sections
  179. assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
  180. @it['new_section'] = {'foo' => 'bar'}
  181. assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
  182. @it['new_section'] = {}
  183. assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
  184. end
  185. def test_add_value
  186. c = OpenSSL::Config.new
  187. assert_equal("", c.to_s)
  188. # add key
  189. c.add_value('default', 'foo', 'bar')
  190. assert_equal("[ default ]\nfoo=bar\n\n", c.to_s)
  191. # add another key
  192. c.add_value('default', 'baz', 'qux')
  193. assert_equal('bar', c['default']['foo'])
  194. assert_equal('qux', c['default']['baz'])
  195. # update the value
  196. c.add_value('default', 'baz', 'quxxx')
  197. assert_equal('bar', c['default']['foo'])
  198. assert_equal('quxxx', c['default']['baz'])
  199. # add section and key
  200. c.add_value('section', 'foo', 'bar')
  201. assert_equal('bar', c['default']['foo'])
  202. assert_equal('quxxx', c['default']['baz'])
  203. assert_equal('bar', c['section']['foo'])
  204. end
  205. def test_aset
  206. @it['foo'] = {'bar' => 'baz'}
  207. assert_equal({'bar' => 'baz'}, @it['foo'])
  208. @it['foo'] = {'bar' => 'qux', 'baz' => 'quxx'}
  209. assert_equal({'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
  210. # OpenSSL::Config is add only for now.
  211. @it['foo'] = {'foo' => 'foo'}
  212. assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
  213. # you cannot override or remove any section and key.
  214. @it['foo'] = {}
  215. assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
  216. end
  217. def test_each
  218. # each returns [section, key, value] array.
  219. ary = @it.map { |e| e }.sort { |a, b| a[0] <=> b[0] }
  220. assert_equal(4, ary.size)
  221. assert_equal('CA_default', ary[0][0])
  222. assert_equal('CA_default', ary[1][0])
  223. assert_equal(["ca", "default_ca", "CA_default"], ary[2])
  224. assert_equal(["default", "HOME", "."], ary[3])
  225. end
  226. def test_to_s
  227. c = OpenSSL::Config.parse("[empty]\n")
  228. assert_equal("[ default ]\n\n[ empty ]\n\n", c.to_s)
  229. end
  230. def test_inspect
  231. assert_match(/#<OpenSSL::Config sections=\[.*\]>/, @it.inspect)
  232. end
  233. def test_freeze
  234. c = OpenSSL::Config.new
  235. c['foo'] = [['key', 'value']]
  236. c.freeze
  237. # [ruby-core:18377]
  238. # RuntimeError for 1.9, TypeError for 1.8
  239. assert_raise(TypeError, /frozen/) do
  240. c['foo'] = [['key', 'wrong']]
  241. end
  242. end
  243. def test_dup
  244. assert(!@it.sections.empty?)
  245. c = @it.dup
  246. assert_equal(@it.sections.sort, c.sections.sort)
  247. @it['newsection'] = {'a' => 'b'}
  248. assert_not_equal(@it.sections.sort, c.sections.sort)
  249. end
  250. def test_clone
  251. assert(!@it.sections.empty?)
  252. c = @it.clone
  253. assert_equal(@it.sections.sort, c.sections.sort)
  254. @it['newsection'] = {'a' => 'b'}
  255. assert_not_equal(@it.sections.sort, c.sections.sort)
  256. end
  257. end