PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/drb/test_acl.rb

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