2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2003-10-20 19:24:00 +04:00
|
|
|
# acltest.rb - ACL unit test
|
|
|
|
# Copyright (c) 2000 Masatoshi SEKI
|
|
|
|
#
|
|
|
|
# acltest.rb is copyrighted free software by Masatoshi SEKI.
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'drb/acl'
|
|
|
|
|
2014-06-01 07:07:58 +04:00
|
|
|
module DRbTests
|
|
|
|
|
2003-10-20 19:24:00 +04:00
|
|
|
class SampleHosts
|
|
|
|
def initialize
|
|
|
|
list = %w(127.0.0.1 localhost
|
|
|
|
192.168.1.1 x68k.linux.or.jp
|
|
|
|
192.168.1.2 lc630.macos.or.jp
|
|
|
|
192.168.1.3 lib30.win32.or.jp
|
|
|
|
192.168.1.4 ns00.linux.or.jp
|
|
|
|
192.168.1.5 yum.macos.or.jp
|
|
|
|
::ffff:192.168.1.5 ipv6.macos.or.jp
|
|
|
|
::192.168.1.5 too.yumipv6.macos.or.jp
|
|
|
|
192.168.1.254 comstarz.foo.or.jp)
|
|
|
|
|
|
|
|
@hostlist = Array.new(list.size / 2)
|
|
|
|
@hostlist.each_index do |idx|
|
|
|
|
@hostlist[idx] = ["AF_INET", 10000, list[idx * 2 + 1], list[idx * 2]]
|
|
|
|
end
|
|
|
|
|
|
|
|
@hosts = Hash.new
|
|
|
|
@hostlist.each do |h|
|
|
|
|
@hosts[h[2].split('.')[0]] = h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
attr_reader(:hostlist, :hosts)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class ACLEntryTest < Test::Unit::TestCase
|
|
|
|
HOSTS = SampleHosts.new
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@hostlist = HOSTS.hostlist
|
|
|
|
@hosts = HOSTS.hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_all
|
|
|
|
a = ACL::ACLEntry.new("*")
|
|
|
|
b = ACL::ACLEntry.new("all")
|
|
|
|
@hostlist.each do |h|
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, h)
|
|
|
|
assert_operator(b, :match, h)
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ip_v6
|
|
|
|
a = ACL::ACLEntry.new('::ffff:192.0.0.0/104')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_operator(a, :match, @hosts['ipv6'])
|
|
|
|
assert_not_operator(a, :match, @hosts['too'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ip
|
|
|
|
a = ACL::ACLEntry.new('192.0.0.0/8')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
2017-10-21 12:53:52 +03:00
|
|
|
a = ACL::ACLEntry.new('192.168.1.0/255.255.255.0')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
2017-10-21 12:53:52 +03:00
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, @hosts['x68k'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
|
|
|
a = ACL::ACLEntry.new('192.168.1.0/24')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_operator(a, :match, @hosts['x68k'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
|
|
|
a = ACL::ACLEntry.new('92.0.0.0/8')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_not_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_not_operator(a, :match, @hosts['x68k'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
2017-10-21 12:53:52 +03:00
|
|
|
a = ACL::ACLEntry.new('127.0.0.0/255.0.0.0')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_not_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_not_operator(a, :match, @hosts['x68k'])
|
2017-10-21 12:53:52 +03:00
|
|
|
|
|
|
|
assert_raise(IPAddr::InvalidPrefixError) {
|
|
|
|
ACL::ACLEntry.new('192.168.0.0/33')
|
2017-10-21 17:22:04 +03:00
|
|
|
}
|
|
|
|
assert_raise(IPAddr::InvalidPrefixError) {
|
2017-10-21 16:34:22 +03:00
|
|
|
ACL::ACLEntry.new('192.168.0.0/255.255.0.255')
|
2017-10-21 12:53:52 +03:00
|
|
|
}
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_name
|
|
|
|
a = ACL::ACLEntry.new('*.jp')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['localhost'])
|
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
|
|
|
a = ACL::ACLEntry.new('yum.*.jp')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_not_operator(a, :match, @hosts['lc630'])
|
2003-10-20 19:24:00 +04:00
|
|
|
|
|
|
|
a = ACL::ACLEntry.new('*.macos.or.jp')
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_operator(a, :match, @hosts['lc630'])
|
|
|
|
assert_not_operator(a, :match, @hosts['lib30'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ACLListTest < Test::Unit::TestCase
|
|
|
|
HOSTS = SampleHosts.new
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@hostlist = HOSTS.hostlist
|
|
|
|
@hosts = HOSTS.hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def build(list)
|
|
|
|
acl= ACL::ACLList.new
|
|
|
|
list.each do |s|
|
|
|
|
acl.add s
|
|
|
|
end
|
|
|
|
acl
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
def test_all_1
|
|
|
|
a = build(%w(all))
|
|
|
|
@hostlist.each do |h|
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, h)
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_all_2
|
|
|
|
a = build(%w(localhost 127.0.0.0/8 yum.* *))
|
|
|
|
@hostlist.each do |h|
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, h)
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_1
|
2017-10-21 12:53:52 +03:00
|
|
|
a = build(%w(192.168.1.0/255.255.255.252 yum.*.jp))
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :match, @hosts['x68k'])
|
2017-10-21 12:53:52 +03:00
|
|
|
assert_operator(a, :match, @hosts['lc630'])
|
|
|
|
assert_operator(a, :match, @hosts['lib30'])
|
|
|
|
assert_not_operator(a, :match, @hosts['ns00'])
|
|
|
|
assert_operator(a, :match, @hosts['yum'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_2
|
|
|
|
a = build(%w(*.linux.or.jp))
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :match, @hosts['yum'])
|
|
|
|
assert_operator(a, :match, @hosts['x68k'])
|
|
|
|
assert_not_operator(a, :match, @hosts['lc630'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ACLTest < Test::Unit::TestCase
|
|
|
|
HOSTS = SampleHosts.new
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@hostlist = HOSTS.hostlist
|
|
|
|
@hosts = HOSTS.hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_0
|
|
|
|
a = ACL.new
|
|
|
|
@hostlist.each do |h|
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :allow_addr?, h)
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_0
|
|
|
|
a = ACL.new([], ACL::ALLOW_DENY)
|
|
|
|
@hostlist.each do |h|
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :allow_addr?, h)
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_1
|
|
|
|
data = %w(deny all
|
|
|
|
allow localhost
|
|
|
|
allow x68k.*)
|
|
|
|
|
|
|
|
a = ACL.new(data)
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_operator(a, :allow_addr?, @hosts['x68k'])
|
|
|
|
assert_operator(a, :allow_addr?, @hosts['localhost'])
|
|
|
|
assert_not_operator(a, :allow_addr?, @hosts['lc630'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_1
|
|
|
|
data = %w(deny 192.0.0.0/8
|
|
|
|
allow localhost
|
|
|
|
allow x68k.*)
|
|
|
|
|
|
|
|
a = ACL.new(data, ACL::ALLOW_DENY)
|
2017-04-07 16:26:12 +03:00
|
|
|
assert_not_operator(a, :allow_addr?, @hosts['x68k'])
|
|
|
|
assert_operator(a, :allow_addr?, @hosts['localhost'])
|
|
|
|
assert_not_operator(a, :allow_addr?, @hosts['lc630'])
|
2003-10-20 19:24:00 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-01 07:07:58 +04:00
|
|
|
end
|