summaryrefslogtreecommitdiff
path: root/test/drb/test_acl.rb
blob: 7fe30f4c4ace0e3d2c16c3fc0d9d187224bc674b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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'

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|
      assert(a.match(h))
      assert(b.match(h))
    end
  end

  def test_ip_v6
    a = ACL::ACLEntry.new('::ffff:192.0.0.0/104')
    assert(! a.match(@hosts['localhost']))
    assert(a.match(@hosts['yum']))
    assert(a.match(@hosts['ipv6']))
    assert(! a.match(@hosts['too']))
  end

  def test_ip
    a = ACL::ACLEntry.new('192.0.0.0/8')
    assert(! a.match(@hosts['localhost']))
    assert(a.match(@hosts['yum']))

    a = ACL::ACLEntry.new('192.168.0.1/255.255.0.255')
    assert(! a.match(@hosts['localhost']))
    assert(! a.match(@hosts['yum']))
    assert(a.match(@hosts['x68k']))

    a = ACL::ACLEntry.new('192.168.1.0/24')
    assert(! a.match(@hosts['localhost']))
    assert(a.match(@hosts['yum']))
    assert(a.match(@hosts['x68k']))

    a = ACL::ACLEntry.new('92.0.0.0/8')
    assert(! a.match(@hosts['localhost']))
    assert(! a.match(@hosts['yum']))
    assert(! a.match(@hosts['x68k']))

    a = ACL::ACLEntry.new('127.0.0.1/255.0.0.255')
    assert(a.match(@hosts['localhost']))
    assert(! a.match(@hosts['yum']))
    assert(! a.match(@hosts['x68k']))
  end

  def test_name
    a = ACL::ACLEntry.new('*.jp')
    assert(! a.match(@hosts['localhost']))
    assert(a.match(@hosts['yum']))

    a = ACL::ACLEntry.new('yum.*.jp')
    assert(a.match(@hosts['yum']))
    assert(! a.match(@hosts['lc630']))

    a = ACL::ACLEntry.new('*.macos.or.jp')
    assert(a.match(@hosts['yum']))
    assert(a.match(@hosts['lc630']))
    assert(! a.match(@hosts['lib30']))
  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|
      assert(a.match(h))
    end
  end

  def test_all_2
    a = build(%w(localhost 127.0.0.0/8 yum.* *))
    @hostlist.each do |h|
      assert(a.match(h))
    end
  end

  def test_1
    a = build(%w(192.0.0.1/255.0.0.255 yum.*.jp))
    assert(a.match(@hosts['yum']))
    assert(a.match(@hosts['x68k']))
    assert(! a.match(@hosts['lc630']))
  end

  def test_2
    a = build(%w(*.linux.or.jp))
    assert(!a.match(@hosts['yum']))
    assert(a.match(@hosts['x68k']))
    assert(!a.match(@hosts['lc630']))
  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|
      assert(a.allow_addr?(h))
    end
  end

  def test_not_0
    a = ACL.new([], ACL::ALLOW_DENY)
    @hostlist.each do |h|
      assert(! a.allow_addr?(h))
    end
  end

  def test_1
    data = %w(deny all
              allow localhost
              allow x68k.*)

    a = ACL.new(data)
    assert(a.allow_addr?(@hosts['x68k']))
    assert(a.allow_addr?(@hosts['localhost']))
    assert(! a.allow_addr?(@hosts['lc630']))
  end

  def test_not_1
    data = %w(deny 192.0.0.0/8
              allow localhost
              allow x68k.*)

    a = ACL.new(data, ACL::ALLOW_DENY)
    assert(!a.allow_addr?(@hosts['x68k']))
    assert(a.allow_addr?(@hosts['localhost']))
    assert(! a.allow_addr?(@hosts['lc630']))
  end
end