PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ipaddress/ipv6_test.rb

https://gitlab.com/intruxxer/ipaddress
Ruby | 428 lines | 350 code | 69 blank | 9 comment | 2 complexity | 2c5a3ba0f8aa91d4f35ae618650bad15 MD5 | raw file
  1. require 'test_helper'
  2. class IPv6Test < Test::Unit::TestCase
  3. def setup
  4. @klass = IPAddress::IPv6
  5. @compress_addr = {
  6. "2001:db8:0000:0000:0008:0800:200c:417a" => "2001:db8::8:800:200c:417a",
  7. "2001:db8:0:0:8:800:200c:417a" => "2001:db8::8:800:200c:417a",
  8. "ff01:0:0:0:0:0:0:101" => "ff01::101",
  9. "0:0:0:0:0:0:0:1" => "::1",
  10. "0:0:0:0:0:0:0:0" => "::"}
  11. @valid_ipv6 = { # Kindly taken from the python IPy library
  12. "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210" => 338770000845734292534325025077361652240,
  13. "1080:0000:0000:0000:0008:0800:200C:417A" => 21932261930451111902915077091070067066,
  14. "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066,
  15. "1080:0::8:800:200C:417A" => 21932261930451111902915077091070067066,
  16. "1080::8:800:200C:417A" => 21932261930451111902915077091070067066,
  17. "FF01:0:0:0:0:0:0:43" => 338958331222012082418099330867817087043,
  18. "FF01:0:0::0:0:43" => 338958331222012082418099330867817087043,
  19. "FF01::43" => 338958331222012082418099330867817087043,
  20. "0:0:0:0:0:0:0:1" => 1,
  21. "0:0:0::0:0:1" => 1,
  22. "::1" => 1,
  23. "0:0:0:0:0:0:0:0" => 0,
  24. "0:0:0::0:0:0" => 0,
  25. "::" => 0,
  26. "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066,
  27. "1080::8:800:200C:417A" => 21932261930451111902915077091070067066}
  28. @invalid_ipv6 = [":1:2:3:4:5:6:7",
  29. ":1:2:3:4:5:6:7",
  30. "2002:516:2:200",
  31. "dd"]
  32. @networks = {
  33. "2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32",
  34. "2001:db8:1:1:1:1:1::/32" => "2001:db8::/32",
  35. "2001:db8::1/64" => "2001:db8::/64"}
  36. @ip = @klass.new "2001:db8::8:800:200c:417a/64"
  37. @network = @klass.new "2001:db8:8:800::/64"
  38. @arr = [8193,3512,0,0,8,2048,8204,16762]
  39. @hex = "20010db80000000000080800200c417a"
  40. end
  41. def test_attribute_address
  42. addr = "2001:0db8:0000:0000:0008:0800:200c:417a"
  43. assert_equal addr, @ip.address
  44. end
  45. def test_initialize
  46. assert_instance_of @klass, @ip
  47. @valid_ipv6.keys.each do |ip|
  48. assert_nothing_raised {@klass.new ip}
  49. end
  50. @invalid_ipv6.each do |ip|
  51. assert_raise(ArgumentError) {@klass.new ip}
  52. end
  53. assert_equal 64, @ip.prefix
  54. assert_raise(ArgumentError) {
  55. @klass.new "::10.1.1.1"
  56. }
  57. end
  58. def test_attribute_groups
  59. assert_equal @arr, @ip.groups
  60. end
  61. def test_method_hexs
  62. arr = "2001:0db8:0000:0000:0008:0800:200c:417a".split(":")
  63. assert_equal arr, @ip.hexs
  64. end
  65. def test_method_to_i
  66. @valid_ipv6.each do |ip,num|
  67. assert_equal num, @klass.new(ip).to_i
  68. end
  69. end
  70. def test_method_bits
  71. bits = "0010000000000001000011011011100000000000000000000" +
  72. "000000000000000000000000000100000001000000000000010000" +
  73. "0000011000100000101111010"
  74. assert_equal bits, @ip.bits
  75. end
  76. def test_method_prefix=()
  77. ip = @klass.new "2001:db8::8:800:200c:417a"
  78. assert_equal 128, ip.prefix
  79. ip.prefix = 64
  80. assert_equal 64, ip.prefix
  81. assert_equal "2001:db8::8:800:200c:417a/64", ip.to_string
  82. end
  83. def test_method_mapped?
  84. assert_equal false, @ip.mapped?
  85. ip6 = @klass.new "::ffff:1234:5678"
  86. assert_equal true, ip6.mapped?
  87. end
  88. def test_method_literal
  89. str = "2001-0db8-0000-0000-0008-0800-200c-417a.ipv6-literal.net"
  90. assert_equal str, @ip.literal
  91. end
  92. def test_method_group
  93. @arr.each_with_index do |val,index|
  94. assert_equal val, @ip[index]
  95. end
  96. end
  97. def test_method_ipv4?
  98. assert_equal false, @ip.ipv4?
  99. end
  100. def test_method_ipv6?
  101. assert_equal true, @ip.ipv6?
  102. end
  103. def test_method_network?
  104. assert_equal true, @network.network?
  105. assert_equal false, @ip.network?
  106. end
  107. def test_method_network_u128
  108. assert_equal 42540766411282592856903984951653826560, @ip.network_u128
  109. end
  110. def test_method_broadcast_u128
  111. assert_equal 42540766411282592875350729025363378175, @ip.broadcast_u128
  112. end
  113. def test_method_size
  114. ip = @klass.new("2001:db8::8:800:200c:417a/64")
  115. assert_equal 2**64, ip.size
  116. ip = @klass.new("2001:db8::8:800:200c:417a/32")
  117. assert_equal 2**96, ip.size
  118. ip = @klass.new("2001:db8::8:800:200c:417a/120")
  119. assert_equal 2**8, ip.size
  120. ip = @klass.new("2001:db8::8:800:200c:417a/124")
  121. assert_equal 2**4, ip.size
  122. end
  123. def test_method_include?
  124. assert_equal true, @ip.include?(@ip)
  125. # test prefix on same address
  126. included = @klass.new "2001:db8::8:800:200c:417a/128"
  127. not_included = @klass.new "2001:db8::8:800:200c:417a/46"
  128. assert_equal true, @ip.include?(included)
  129. assert_equal false, @ip.include?(not_included)
  130. # test address on same prefix
  131. included = @klass.new "2001:db8::8:800:200c:0/64"
  132. not_included = @klass.new "2001:db8:1::8:800:200c:417a/64"
  133. assert_equal true, @ip.include?(included)
  134. assert_equal false, @ip.include?(not_included)
  135. # general test
  136. included = @klass.new "2001:db8::8:800:200c:1/128"
  137. not_included = @klass.new "2001:db8:1::8:800:200c:417a/76"
  138. assert_equal true, @ip.include?(included)
  139. assert_equal false, @ip.include?(not_included)
  140. end
  141. def test_method_to_hex
  142. assert_equal @hex, @ip.to_hex
  143. end
  144. def test_method_to_s
  145. assert_equal "2001:db8::8:800:200c:417a", @ip.to_s
  146. end
  147. def test_method_to_string
  148. assert_equal "2001:db8::8:800:200c:417a/64", @ip.to_string
  149. end
  150. def test_method_to_string_uncompressed
  151. str = "2001:0db8:0000:0000:0008:0800:200c:417a/64"
  152. assert_equal str, @ip.to_string_uncompressed
  153. end
  154. def test_method_data
  155. if RUBY_VERSION < "2.0"
  156. str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
  157. else
  158. str = " \x01\r\xB8\x00\x00\x00\x00\x00\b\b\x00 \fAz".b
  159. end
  160. assert_equal str, @ip.data
  161. end
  162. def test_method_reverse
  163. str = "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa"
  164. assert_equal str, @klass.new("3ffe:505:2::f").reverse
  165. end
  166. def test_method_compressed
  167. assert_equal "1:1:1::1", @klass.new("1:1:1:0:0:0:0:1").compressed
  168. assert_equal "1:0:1::1", @klass.new("1:0:1:0:0:0:0:1").compressed
  169. assert_equal "1:0:0:1::1", @klass.new("1:0:0:1:0:0:0:1").compressed
  170. assert_equal "1::1:0:0:1", @klass.new("1:0:0:0:1:0:0:1").compressed
  171. assert_equal "1::1", @klass.new("1:0:0:0:0:0:0:1").compressed
  172. end
  173. def test_method_unspecified?
  174. assert_equal true, @klass.new("::").unspecified?
  175. assert_equal false, @ip.unspecified?
  176. end
  177. def test_method_loopback?
  178. assert_equal true, @klass.new("::1").loopback?
  179. assert_equal false, @ip.loopback?
  180. end
  181. def test_method_network
  182. @networks.each do |addr,net|
  183. ip = @klass.new addr
  184. assert_instance_of @klass, ip.network
  185. assert_equal net, ip.network.to_string
  186. end
  187. end
  188. def test_method_each
  189. ip = @klass.new("2001:db8::4/125")
  190. arr = []
  191. ip.each {|i| arr << i.compressed}
  192. expected = ["2001:db8::","2001:db8::1","2001:db8::2",
  193. "2001:db8::3","2001:db8::4","2001:db8::5",
  194. "2001:db8::6","2001:db8::7"]
  195. assert_equal expected, arr
  196. end
  197. def test_method_compare
  198. ip1 = @klass.new("2001:db8:1::1/64")
  199. ip2 = @klass.new("2001:db8:2::1/64")
  200. ip3 = @klass.new("2001:db8:1::2/64")
  201. ip4 = @klass.new("2001:db8:1::1/65")
  202. # ip2 should be greater than ip1
  203. assert_equal true, ip2 > ip1
  204. assert_equal false, ip1 > ip2
  205. assert_equal false, ip2 < ip1
  206. # ip3 should be less than ip2
  207. assert_equal true, ip2 > ip3
  208. assert_equal false, ip2 < ip3
  209. # ip1 should be less than ip3
  210. assert_equal true, ip1 < ip3
  211. assert_equal false, ip1 > ip3
  212. assert_equal false, ip3 < ip1
  213. # ip1 should be equal to itself
  214. assert_equal true, ip1 == ip1
  215. # ip4 should be greater than ip1
  216. assert_equal true, ip1 < ip4
  217. assert_equal false, ip1 > ip4
  218. # test sorting
  219. arr = ["2001:db8:1::1/64","2001:db8:1::1/65",
  220. "2001:db8:1::2/64","2001:db8:2::1/64"]
  221. assert_equal arr, [ip1,ip2,ip3,ip4].sort.map{|s| s.to_string}
  222. end
  223. def test_classmethod_expand
  224. compressed = "2001:db8:0:cd30::"
  225. expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
  226. assert_equal expanded, @klass.expand(compressed)
  227. assert_not_equal expanded, @klass.expand("2001:0db8:0::cd3")
  228. assert_not_equal expanded, @klass.expand("2001:0db8::cd30")
  229. assert_not_equal expanded, @klass.expand("2001:0db8::cd3")
  230. end
  231. def test_classmethod_compress
  232. compressed = "2001:db8:0:cd30::"
  233. expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
  234. assert_equal compressed, @klass.compress(expanded)
  235. assert_not_equal compressed, @klass.compress("2001:0db8:0::cd3")
  236. assert_not_equal compressed, @klass.compress("2001:0db8::cd30")
  237. assert_not_equal compressed, @klass.compress("2001:0db8::cd3")
  238. end
  239. def test_classmethod_parse_data
  240. str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
  241. ip = @klass.parse_data str
  242. assert_instance_of @klass, ip
  243. assert_equal "2001:0db8:0000:0000:0008:0800:200c:417a", ip.address
  244. assert_equal "2001:db8::8:800:200c:417a/128", ip.to_string
  245. end
  246. def test_classhmethod_parse_u128
  247. @valid_ipv6.each do |ip,num|
  248. assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s
  249. end
  250. end
  251. def test_classmethod_parse_hex
  252. assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
  253. end
  254. end # class IPv6Test
  255. class IPv6UnspecifiedTest < Test::Unit::TestCase
  256. def setup
  257. @klass = IPAddress::IPv6::Unspecified
  258. @ip = @klass.new
  259. @s = "::"
  260. @str = "::/128"
  261. @string = "0000:0000:0000:0000:0000:0000:0000:0000/128"
  262. @u128 = 0
  263. @address = "::"
  264. end
  265. def test_initialize
  266. assert_nothing_raised {@klass.new}
  267. assert_instance_of @klass, @ip
  268. end
  269. def test_attributes
  270. assert_equal @address, @ip.compressed
  271. assert_equal 128, @ip.prefix
  272. assert_equal true, @ip.unspecified?
  273. assert_equal @s, @ip.to_s
  274. assert_equal @str, @ip.to_string
  275. assert_equal @string, @ip.to_string_uncompressed
  276. assert_equal @u128, @ip.to_u128
  277. end
  278. def test_method_ipv6?
  279. assert_equal true, @ip.ipv6?
  280. end
  281. end # class IPv6UnspecifiedTest
  282. class IPv6LoopbackTest < Test::Unit::TestCase
  283. def setup
  284. @klass = IPAddress::IPv6::Loopback
  285. @ip = @klass.new
  286. @s = "::1"
  287. @str = "::1/128"
  288. @string = "0000:0000:0000:0000:0000:0000:0000:0001/128"
  289. @u128 = 1
  290. @address = "::1"
  291. end
  292. def test_initialize
  293. assert_nothing_raised {@klass.new}
  294. assert_instance_of @klass, @ip
  295. end
  296. def test_attributes
  297. assert_equal @address, @ip.compressed
  298. assert_equal 128, @ip.prefix
  299. assert_equal true, @ip.loopback?
  300. assert_equal @s, @ip.to_s
  301. assert_equal @str, @ip.to_string
  302. assert_equal @string, @ip.to_string_uncompressed
  303. assert_equal @u128, @ip.to_u128
  304. end
  305. def test_method_ipv6?
  306. assert_equal true, @ip.ipv6?
  307. end
  308. end # class IPv6LoopbackTest
  309. class IPv6MappedTest < Test::Unit::TestCase
  310. def setup
  311. @klass = IPAddress::IPv6::Mapped
  312. @ip = @klass.new("::172.16.10.1")
  313. @s = "::ffff:172.16.10.1"
  314. @str = "::ffff:172.16.10.1/128"
  315. @string = "0000:0000:0000:0000:0000:ffff:ac10:0a01/128"
  316. @u128 = 281473568475649
  317. @address = "::ffff:ac10:a01"
  318. @valid_mapped = {'::13.1.68.3' => 281470899930115,
  319. '0:0:0:0:0:ffff:129.144.52.38' => 281472855454758,
  320. '::ffff:129.144.52.38' => 281472855454758}
  321. @valid_mapped_ipv6 = {'::0d01:4403' => 281470899930115,
  322. '0:0:0:0:0:ffff:8190:3426' => 281472855454758,
  323. '::ffff:8190:3426' => 281472855454758}
  324. @valid_mapped_ipv6_conversion = {'::0d01:4403' => "13.1.68.3",
  325. '0:0:0:0:0:ffff:8190:3426' => "129.144.52.38",
  326. '::ffff:8190:3426' => "129.144.52.38"}
  327. end
  328. def test_initialize
  329. assert_nothing_raised {@klass.new("::172.16.10.1")}
  330. assert_instance_of @klass, @ip
  331. @valid_mapped.each do |ip, u128|
  332. assert_nothing_raised {@klass.new ip}
  333. assert_equal u128, @klass.new(ip).to_u128
  334. end
  335. @valid_mapped_ipv6.each do |ip, u128|
  336. assert_nothing_raised {@klass.new ip}
  337. assert_equal u128, @klass.new(ip).to_u128
  338. end
  339. end
  340. def test_mapped_from_ipv6_conversion
  341. @valid_mapped_ipv6_conversion.each do |ip6,ip4|
  342. assert_equal ip4, @klass.new(ip6).ipv4.to_s
  343. end
  344. end
  345. def test_attributes
  346. assert_equal @address, @ip.compressed
  347. assert_equal 128, @ip.prefix
  348. assert_equal @s, @ip.to_s
  349. assert_equal @str, @ip.to_string
  350. assert_equal @string, @ip.to_string_uncompressed
  351. assert_equal @u128, @ip.to_u128
  352. end
  353. def test_method_ipv6?
  354. assert_equal true, @ip.ipv6?
  355. end
  356. def test_mapped?
  357. assert_equal true, @ip.mapped?
  358. end
  359. end # class IPv6MappedTest