PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/drb/test_acl.rb

https://gitlab.com/0072016/0072016-ruby
Ruby | 198 lines | 158 code | 34 blank | 6 comment | 0 complexity | 05a7db87430bfa00628548e77cf0cada MD5 | raw file
  1. # frozen_string_literal: false
  2. # acltest.rb - ACL unit test
  3. # Copyright (c) 2000 Masatoshi SEKI
  4. #
  5. # acltest.rb is copyrighted free software by Masatoshi SEKI.
  6. # You can redistribute it and/or modify it under the same terms as Ruby.
  7. require 'test/unit'
  8. require 'drb/acl'
  9. module DRbTests
  10. class SampleHosts
  11. def initialize
  12. list = %w(127.0.0.1 localhost
  13. 192.168.1.1 x68k.linux.or.jp
  14. 192.168.1.2 lc630.macos.or.jp
  15. 192.168.1.3 lib30.win32.or.jp
  16. 192.168.1.4 ns00.linux.or.jp
  17. 192.168.1.5 yum.macos.or.jp
  18. ::ffff:192.168.1.5 ipv6.macos.or.jp
  19. ::192.168.1.5 too.yumipv6.macos.or.jp
  20. 192.168.1.254 comstarz.foo.or.jp)
  21. @hostlist = Array.new(list.size / 2)
  22. @hostlist.each_index do |idx|
  23. @hostlist[idx] = ["AF_INET", 10000, list[idx * 2 + 1], list[idx * 2]]
  24. end
  25. @hosts = Hash.new
  26. @hostlist.each do |h|
  27. @hosts[h[2].split('.')[0]] = h
  28. end
  29. end
  30. attr_reader(:hostlist, :hosts)
  31. end
  32. class ACLEntryTest < Test::Unit::TestCase
  33. HOSTS = SampleHosts.new
  34. def setup
  35. @hostlist = HOSTS.hostlist
  36. @hosts = HOSTS.hosts
  37. end
  38. def test_all
  39. a = ACL::ACLEntry.new("*")
  40. b = ACL::ACLEntry.new("all")
  41. @hostlist.each do |h|
  42. assert(a.match(h))
  43. assert(b.match(h))
  44. end
  45. end
  46. def test_ip_v6
  47. a = ACL::ACLEntry.new('::ffff:192.0.0.0/104')
  48. assert(! a.match(@hosts['localhost']))
  49. assert(a.match(@hosts['yum']))
  50. assert(a.match(@hosts['ipv6']))
  51. assert(! a.match(@hosts['too']))
  52. end
  53. def test_ip
  54. a = ACL::ACLEntry.new('192.0.0.0/8')
  55. assert(! a.match(@hosts['localhost']))
  56. assert(a.match(@hosts['yum']))
  57. a = ACL::ACLEntry.new('192.168.0.1/255.255.0.255')
  58. assert(! a.match(@hosts['localhost']))
  59. assert(! a.match(@hosts['yum']))
  60. assert(a.match(@hosts['x68k']))
  61. a = ACL::ACLEntry.new('192.168.1.0/24')
  62. assert(! a.match(@hosts['localhost']))
  63. assert(a.match(@hosts['yum']))
  64. assert(a.match(@hosts['x68k']))
  65. a = ACL::ACLEntry.new('92.0.0.0/8')
  66. assert(! a.match(@hosts['localhost']))
  67. assert(! a.match(@hosts['yum']))
  68. assert(! a.match(@hosts['x68k']))
  69. a = ACL::ACLEntry.new('127.0.0.1/255.0.0.255')
  70. assert(a.match(@hosts['localhost']))
  71. assert(! a.match(@hosts['yum']))
  72. assert(! a.match(@hosts['x68k']))
  73. end
  74. def test_name
  75. a = ACL::ACLEntry.new('*.jp')
  76. assert(! a.match(@hosts['localhost']))
  77. assert(a.match(@hosts['yum']))
  78. a = ACL::ACLEntry.new('yum.*.jp')
  79. assert(a.match(@hosts['yum']))
  80. assert(! a.match(@hosts['lc630']))
  81. a = ACL::ACLEntry.new('*.macos.or.jp')
  82. assert(a.match(@hosts['yum']))
  83. assert(a.match(@hosts['lc630']))
  84. assert(! a.match(@hosts['lib30']))
  85. end
  86. end
  87. class ACLListTest < Test::Unit::TestCase
  88. HOSTS = SampleHosts.new
  89. def setup
  90. @hostlist = HOSTS.hostlist
  91. @hosts = HOSTS.hosts
  92. end
  93. private
  94. def build(list)
  95. acl= ACL::ACLList.new
  96. list.each do |s|
  97. acl.add s
  98. end
  99. acl
  100. end
  101. public
  102. def test_all_1
  103. a = build(%w(all))
  104. @hostlist.each do |h|
  105. assert(a.match(h))
  106. end
  107. end
  108. def test_all_2
  109. a = build(%w(localhost 127.0.0.0/8 yum.* *))
  110. @hostlist.each do |h|
  111. assert(a.match(h))
  112. end
  113. end
  114. def test_1
  115. a = build(%w(192.0.0.1/255.0.0.255 yum.*.jp))
  116. assert(a.match(@hosts['yum']))
  117. assert(a.match(@hosts['x68k']))
  118. assert(! a.match(@hosts['lc630']))
  119. end
  120. def test_2
  121. a = build(%w(*.linux.or.jp))
  122. assert(!a.match(@hosts['yum']))
  123. assert(a.match(@hosts['x68k']))
  124. assert(!a.match(@hosts['lc630']))
  125. end
  126. end
  127. class ACLTest < Test::Unit::TestCase
  128. HOSTS = SampleHosts.new
  129. def setup
  130. @hostlist = HOSTS.hostlist
  131. @hosts = HOSTS.hosts
  132. end
  133. def test_0
  134. a = ACL.new
  135. @hostlist.each do |h|
  136. assert(a.allow_addr?(h))
  137. end
  138. end
  139. def test_not_0
  140. a = ACL.new([], ACL::ALLOW_DENY)
  141. @hostlist.each do |h|
  142. assert(! a.allow_addr?(h))
  143. end
  144. end
  145. def test_1
  146. data = %w(deny all
  147. allow localhost
  148. allow x68k.*)
  149. a = ACL.new(data)
  150. assert(a.allow_addr?(@hosts['x68k']))
  151. assert(a.allow_addr?(@hosts['localhost']))
  152. assert(! a.allow_addr?(@hosts['lc630']))
  153. end
  154. def test_not_1
  155. data = %w(deny 192.0.0.0/8
  156. allow localhost
  157. allow x68k.*)
  158. a = ACL.new(data, ACL::ALLOW_DENY)
  159. assert(!a.allow_addr?(@hosts['x68k']))
  160. assert(a.allow_addr?(@hosts['localhost']))
  161. assert(! a.allow_addr?(@hosts['lc630']))
  162. end
  163. end
  164. end