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

/TDDBC_Yokohama2_PCUnit/Ruby/doc/ruby/ruby-1.9.2/test/drb/test_acl.rb

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