PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/cache/ruby/2.7.0/gems/domain_name-0.5.20190701/test/test_domain_name.rb

https://gitlab.com/gitnyasha/zimcreative
Ruby | 317 lines | 285 code | 19 blank | 13 comment | 6 complexity | 753dc3e19b04fe7014be748f11bb5abd MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require 'helper'
  3. require 'ipaddr'
  4. class TestDomainName < Test::Unit::TestCase
  5. test "raise ArgumentError if hostname starts with a dot" do
  6. [
  7. # Leading dot.
  8. '.com',
  9. '.example',
  10. '.example.com',
  11. '.example.example',
  12. ].each { |hostname|
  13. assert_raises(ArgumentError) { DomainName.new(hostname) }
  14. }
  15. end
  16. test "accept a String-alike for initialization" do
  17. Object.new.tap { |obj|
  18. def obj.to_str
  19. "Example.org"
  20. end
  21. assert_equal "example.org", DomainName.new(obj).hostname
  22. }
  23. Object.new.tap { |obj|
  24. def obj.to_str
  25. 123
  26. end
  27. assert_raises(TypeError) { DomainName.new(obj) }
  28. }
  29. Object.new.tap { |obj|
  30. assert_raises(TypeError) { DomainName.new(obj) }
  31. }
  32. end
  33. test "parse canonical domain names correctly" do
  34. [
  35. # Mixed case.
  36. ['COM', nil, false, 'com', true],
  37. ['example.COM', 'example.com', true, 'com', true],
  38. ['WwW.example.COM', 'example.com', true, 'com', true],
  39. # Unlisted TLD.
  40. ['example', 'example', false, 'example', false],
  41. ['example.example', 'example.example', false, 'example', false],
  42. ['b.example.example', 'example.example', false, 'example', false],
  43. ['a.b.example.example', 'example.example', false, 'example', false],
  44. # Listed, but non-Internet, TLD.
  45. ['local', 'local', false, 'local', false],
  46. ['example.local', 'example.local', false, 'local', false],
  47. ['b.example.local', 'example.local', false, 'local', false],
  48. ['a.b.example.local', 'example.local', false, 'local', false],
  49. # TLD with only 1 rule.
  50. ['biz', nil, false, 'biz', true],
  51. ['domain.biz', 'domain.biz', true, 'biz', true],
  52. ['b.domain.biz', 'domain.biz', true, 'biz', true],
  53. ['a.b.domain.biz', 'domain.biz', true, 'biz', true],
  54. # TLD with some 2-level rules.
  55. ['com', nil, false, 'com', true],
  56. ['example.com', 'example.com', true, 'com', true],
  57. ['b.example.com', 'example.com', true, 'com', true],
  58. ['a.b.example.com', 'example.com', true, 'com', true],
  59. ['uk.com', nil, false, 'com', true],
  60. ['example.uk.com', 'example.uk.com', true, 'com', true],
  61. ['b.example.uk.com', 'example.uk.com', true, 'com', true],
  62. ['a.b.example.uk.com', 'example.uk.com', true, 'com', true],
  63. ['test.ac', 'test.ac', true, 'ac', true],
  64. # TLD with only 1 (wildcard) rule.
  65. ['bd', nil, false, 'bd', true],
  66. ['c.bd', nil, false, 'bd', true],
  67. ['b.c.bd', 'b.c.bd', true, 'bd', true],
  68. ['a.b.c.bd', 'b.c.bd', true, 'bd', true],
  69. # More complex TLD.
  70. ['jp', nil, false, 'jp', true],
  71. ['test.jp', 'test.jp', true, 'jp', true],
  72. ['www.test.jp', 'test.jp', true, 'jp', true],
  73. ['ac.jp', nil, false, 'jp', true],
  74. ['test.ac.jp', 'test.ac.jp', true, 'jp', true],
  75. ['www.test.ac.jp', 'test.ac.jp', true, 'jp', true],
  76. ['kyoto.jp', nil, false, 'jp', true],
  77. ['test.kyoto.jp', 'test.kyoto.jp', true, 'jp', true],
  78. ['ide.kyoto.jp', nil, false, 'jp', true],
  79. ['b.ide.kyoto.jp', 'b.ide.kyoto.jp', true, 'jp', true],
  80. ['a.b.ide.kyoto.jp', 'b.ide.kyoto.jp', true, 'jp', true],
  81. ['c.kobe.jp', nil, false, 'jp', true],
  82. ['b.c.kobe.jp', 'b.c.kobe.jp', true, 'jp', true],
  83. ['a.b.c.kobe.jp', 'b.c.kobe.jp', true, 'jp', true],
  84. ['city.kobe.jp', 'city.kobe.jp', true, 'jp', true],
  85. ['www.city.kobe.jp', 'city.kobe.jp', true, 'jp', true],
  86. # TLD with a wildcard rule and exceptions.
  87. ['ck', nil, false, 'ck', true],
  88. ['test.ck', nil, false, 'ck', true],
  89. ['b.test.ck', 'b.test.ck', true, 'ck', true],
  90. ['a.b.test.ck', 'b.test.ck', true, 'ck', true],
  91. ['www.ck', 'www.ck', true, 'ck', true],
  92. ['www.www.ck', 'www.ck', true, 'ck', true],
  93. # US K12.
  94. ['us', nil, false, 'us', true],
  95. ['test.us', 'test.us', true, 'us', true],
  96. ['www.test.us', 'test.us', true, 'us', true],
  97. ['ak.us', nil, false, 'us', true],
  98. ['test.ak.us', 'test.ak.us', true, 'us', true],
  99. ['www.test.ak.us', 'test.ak.us', true, 'us', true],
  100. ['k12.ak.us', nil, false, 'us', true],
  101. ['test.k12.ak.us', 'test.k12.ak.us', true, 'us', true],
  102. ['www.test.k12.ak.us', 'test.k12.ak.us', true, 'us', true],
  103. # IDN labels. (modified; currently DomainName always converts U-labels to A-labels)
  104. ['食狮.com.cn', 'xn--85x722f.com.cn', true, 'cn', true],
  105. ['食狮.公司.cn', 'xn--85x722f.xn--55qx5d.cn', true, 'cn', true],
  106. ['www.食狮.公司.cn', 'xn--85x722f.xn--55qx5d.cn', true, 'cn', true],
  107. ['shishi.公司.cn', 'shishi.xn--55qx5d.cn', true, 'cn', true],
  108. ['公司.cn', nil, false, 'cn', true],
  109. ['食狮.中国', 'xn--85x722f.xn--fiqs8s', true, 'xn--fiqs8s', true],
  110. ['www.食狮.中国', 'xn--85x722f.xn--fiqs8s', true, 'xn--fiqs8s', true],
  111. ['shishi.中国', 'shishi.xn--fiqs8s', true, 'xn--fiqs8s', true],
  112. ['中国', nil, false, 'xn--fiqs8s', true],
  113. # Same as above, but punycoded.
  114. ['xn--85x722f.com.cn', 'xn--85x722f.com.cn', true, 'cn', true],
  115. ['xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn', true, 'cn', true],
  116. ['www.xn--85x722f.xn--55qx5d.cn', 'xn--85x722f.xn--55qx5d.cn', true, 'cn', true],
  117. ['shishi.xn--55qx5d.cn', 'shishi.xn--55qx5d.cn', true, 'cn', true],
  118. ['xn--55qx5d.cn', nil, false, 'cn', true],
  119. ['xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s', true, 'xn--fiqs8s', true],
  120. ['www.xn--85x722f.xn--fiqs8s', 'xn--85x722f.xn--fiqs8s', true, 'xn--fiqs8s', true],
  121. ['shishi.xn--fiqs8s', 'shishi.xn--fiqs8s', true, 'xn--fiqs8s', true],
  122. ['xn--fiqs8s', nil, false, 'xn--fiqs8s', true],
  123. ].each { |hostname, domain, canonical, tld, canonical_tld|
  124. dn = DomainName.new(hostname)
  125. assert_equal(domain, dn.domain, hostname + ':domain')
  126. assert_equal(canonical, dn.canonical?, hostname + ':canoninal?')
  127. assert_equal(tld, dn.tld, hostname + ':tld')
  128. assert_equal(canonical_tld, dn.canonical_tld?, hostname + ':canoninal_tld?')
  129. }
  130. end
  131. test "compare hostnames correctly" do
  132. [
  133. ["foo.com", "abc.foo.com", 1],
  134. ["COM", "abc.foo.com", 1],
  135. ["abc.def.foo.com", "foo.com", -1],
  136. ["abc.def.foo.com", "ABC.def.FOO.com", 0],
  137. ["abc.def.foo.com", "bar.com", nil],
  138. ].each { |x, y, v|
  139. dx, dy = DomainName(x), DomainName(y)
  140. [
  141. [dx, y, v],
  142. [dx, dy, v],
  143. [dy, x, v ? -v : v],
  144. [dy, dx, v ? -v : v],
  145. ].each { |a, b, expected|
  146. assert_equal expected, a <=> b
  147. case expected
  148. when 1
  149. assert_equal(true, a > b)
  150. assert_equal(true, a >= b)
  151. assert_equal(false, a == b)
  152. assert_equal(false, a <= b)
  153. assert_equal(false, a < b)
  154. when -1
  155. assert_equal(true, a < b)
  156. assert_equal(true, a <= b)
  157. assert_equal(false, a == b)
  158. assert_equal(false, a >= b)
  159. assert_equal(false, a > b)
  160. when 0
  161. assert_equal(false, a < b)
  162. assert_equal(true, a <= b)
  163. assert_equal(true, a == b)
  164. assert_equal(true, a >= b)
  165. assert_equal(false, a > b)
  166. when nil
  167. assert_equal(nil, a < b)
  168. assert_equal(nil, a <= b)
  169. assert_equal(false, a == b)
  170. assert_equal(nil, a >= b)
  171. assert_equal(nil, a > b)
  172. end
  173. }
  174. }
  175. end
  176. test "check cookie domain correctly" do
  177. {
  178. 'com' => [
  179. ['com', false],
  180. ['example.com', false],
  181. ['foo.example.com', false],
  182. ['bar.foo.example.com', false],
  183. ],
  184. 'example.com' => [
  185. ['com', false],
  186. ['example.com', true],
  187. ['foo.example.com', false],
  188. ['bar.foo.example.com', false],
  189. ],
  190. 'foo.example.com' => [
  191. ['com', false],
  192. ['example.com', true],
  193. ['foo.example.com', true],
  194. ['foo.Example.com', true],
  195. ['bar.foo.example.com', false],
  196. ['bar.Foo.Example.com', false],
  197. ],
  198. 'b.sapporo.jp' => [
  199. ['jp', false],
  200. ['sapporo.jp', false],
  201. ['b.sapporo.jp', false],
  202. ['a.b.sapporo.jp', false],
  203. ],
  204. 'b.c.sapporo.jp' => [
  205. ['jp', false],
  206. ['sapporo.jp', false],
  207. ['c.sapporo.jp', false],
  208. ['b.c.sapporo.jp', true],
  209. ['a.b.c.sapporo.jp', false],
  210. ],
  211. 'b.c.d.sapporo.jp' => [
  212. ['jp', false],
  213. ['sapporo.jp', false],
  214. ['d.sapporo.jp', false],
  215. ['c.d.sapporo.jp', true],
  216. ['b.c.d.sapporo.jp', true],
  217. ['a.b.c.d.sapporo.jp', false],
  218. ],
  219. 'city.sapporo.jp' => [
  220. ['jp', false],
  221. ['sapporo.jp', false],
  222. ['city.sapporo.jp', true],
  223. ['a.city.sapporo.jp', false],
  224. ],
  225. 'b.city.sapporo.jp' => [
  226. ['jp', false],
  227. ['sapporo.jp', false],
  228. ['city.sapporo.jp', true],
  229. ['b.city.sapporo.jp', true],
  230. ['a.b.city.sapporo.jp', false],
  231. ],
  232. }.each_pair { |host, pairs|
  233. dn = DomainName(host)
  234. assert_equal(true, dn.cookie_domain?(host.upcase, true), dn.to_s)
  235. assert_equal(true, dn.cookie_domain?(host.downcase, true), dn.to_s)
  236. assert_equal(false, dn.cookie_domain?("www." << host, true), dn.to_s)
  237. pairs.each { |domain, expected|
  238. assert_equal(expected, dn.cookie_domain?(domain), "%s - %s" % [dn.to_s, domain])
  239. assert_equal(expected, dn.cookie_domain?(DomainName(domain)), "%s - %s" % [dn.to_s, domain])
  240. }
  241. }
  242. end
  243. test "parse IPv4 addresseses" do
  244. a = '192.168.10.20'
  245. dn = DomainName(a)
  246. assert_equal(a, dn.hostname)
  247. assert_equal(true, dn.ipaddr?)
  248. assert_equal(IPAddr.new(a), dn.ipaddr)
  249. assert_equal(true, dn.cookie_domain?(a))
  250. assert_equal(true, dn.cookie_domain?(a, true))
  251. assert_equal(true, dn.cookie_domain?(dn))
  252. assert_equal(true, dn.cookie_domain?(dn, true))
  253. assert_equal(false, dn.cookie_domain?('168.10.20'))
  254. assert_equal(false, dn.cookie_domain?('20'))
  255. assert_equal(nil, dn.superdomain)
  256. end
  257. test "parse IPv6 addresseses" do
  258. a = '2001:200:dff:fff1:216:3eff:feb1:44d7'
  259. b = '2001:0200:0dff:fff1:0216:3eff:feb1:44d7'
  260. [b, b.upcase, "[#{b}]", "[#{b.upcase}]"].each { |host|
  261. dn = DomainName(host)
  262. assert_equal("[#{a}]", dn.uri_host)
  263. assert_equal(a, dn.hostname)
  264. assert_equal(true, dn.ipaddr?)
  265. assert_equal(IPAddr.new(a), dn.ipaddr)
  266. assert_equal(true, dn.cookie_domain?(host))
  267. assert_equal(true, dn.cookie_domain?(host, true))
  268. assert_equal(true, dn.cookie_domain?(dn))
  269. assert_equal(true, dn.cookie_domain?(dn, true))
  270. assert_equal(true, dn.cookie_domain?(a))
  271. assert_equal(true, dn.cookie_domain?(a, true))
  272. assert_equal(nil, dn.superdomain)
  273. }
  274. end
  275. test "get superdomain" do
  276. [
  277. %w[www.sub.example.local sub.example.local example.local local],
  278. %w[www.sub.example.com sub.example.com example.com com],
  279. ].each { |domain, *superdomains|
  280. dn = DomainName(domain)
  281. superdomains.each { |superdomain|
  282. sdn = DomainName(superdomain)
  283. assert_equal sdn, dn.superdomain
  284. dn = sdn
  285. }
  286. assert_equal nil, dn.superdomain
  287. }
  288. end
  289. test "have idn methods" do
  290. dn = DomainName("金八先生.B組.3年.日本語ドメイン名Example.日本")
  291. assert_equal "xn--44q1cv48kq8x.xn--b-gf6c.xn--3-pj3b.xn--example-6q4fyliikhk162btq3b2zd4y2o.xn--wgv71a", dn.hostname
  292. assert_equal "金八先生.b組.3年.日本語ドメイン名example.日本", dn.hostname_idn
  293. assert_equal "xn--example-6q4fyliikhk162btq3b2zd4y2o.xn--wgv71a", dn.domain
  294. assert_equal "日本語ドメイン名example.日本", dn.domain_idn
  295. assert_equal "xn--wgv71a", dn.tld
  296. assert_equal "日本", dn.tld_idn
  297. end
  298. end