nayutaya-webhook-dispatcher 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,248 @@
1
+
2
+ require File.dirname(__FILE__) + "/../test_helper"
3
+ require "webhook-dispatcher/acl/entry_base"
4
+
5
+ class EntryBaseTest < Test::Unit::TestCase
6
+ def setup
7
+ @klass = WebHookDispatcher::Acl::EntryBase
8
+ end
9
+
10
+ def test_initialize__all
11
+ assert_equal(
12
+ [nil, nil, nil],
13
+ @klass.new.to_a)
14
+ assert_equal(
15
+ [nil, nil, nil],
16
+ @klass.new(nil).to_a)
17
+ assert_equal(
18
+ [nil, nil, nil],
19
+ @klass.new(:all).to_a)
20
+ assert_equal(
21
+ [nil, nil, nil],
22
+ @klass.new({}).to_a)
23
+ end
24
+
25
+ def test_initialize__addr
26
+ assert_equal(
27
+ [nil, nil, nil],
28
+ @klass.new(:addr => :all).to_a)
29
+ assert_equal(
30
+ [IPAddr.new("127.0.0.1/32"), nil, nil],
31
+ @klass.new(:addr => "127.0.0.1").to_a)
32
+ assert_equal(
33
+ [IPAddr.new("127.0.0.0/8"), nil, nil],
34
+ @klass.new(:addr => IPAddr.new("127.0.0.0/8")).to_a)
35
+
36
+ assert_raise(ArgumentError) {
37
+ @klass.new(:addr => :invalid)
38
+ }
39
+ end
40
+
41
+ def test_initialize__name
42
+ assert_equal(
43
+ [nil, nil, nil],
44
+ @klass.new(:name => :all).to_a)
45
+ assert_equal(
46
+ [nil, "google.co.jp", nil],
47
+ @klass.new(:name => "GOOGLE.CO.JP").to_a)
48
+ assert_equal(
49
+ [nil, /google\.co\.jp/, nil],
50
+ @klass.new(:name => /google\.co\.jp/).to_a)
51
+
52
+ assert_raise(ArgumentError) {
53
+ @klass.new(:name => :invalid)
54
+ }
55
+ end
56
+
57
+ def test_initialize__port
58
+ assert_equal(
59
+ [nil, nil, nil],
60
+ @klass.new(:port => :all).to_a)
61
+ assert_equal(
62
+ [nil, nil, [80]],
63
+ @klass.new(:port => 80).to_a)
64
+ assert_equal(
65
+ [nil, nil, [1, 2, 3]],
66
+ @klass.new(:port => [3, 2, 1]).to_a)
67
+ assert_equal(
68
+ [nil, nil, (1..3)],
69
+ @klass.new(:port => (1..3)).to_a)
70
+
71
+ assert_raise(ArgumentError) {
72
+ @klass.new(:port => true)
73
+ }
74
+ end
75
+
76
+ def test_initialize__addr_and_name_and_port
77
+ assert_equal(
78
+ [IPAddr.new("127.0.0.1"), "localhost", [80]],
79
+ @klass.new(:addr => "127.0.0.1", :name => "localhost", :port => 80).to_a)
80
+ end
81
+
82
+ def test_initialize__invalid_parameter
83
+ assert_raise(ArgumentError) {
84
+ @klass.new(:invalid)
85
+ }
86
+ assert_raise(ArgumentError) {
87
+ @klass.new(:invalid => true)
88
+ }
89
+ end
90
+
91
+ #
92
+ # インスタンスメソッド
93
+ #
94
+
95
+ def test_equal
96
+ assert_equal(true, (@klass.new == @klass.new))
97
+ assert_equal(false, (@klass.new == nil))
98
+ assert_equal(false, (@klass.new == @klass.new(:addr => "127.0.0.1")))
99
+ assert_equal(false, (@klass.new == @klass.new(:name => "localhost")))
100
+ assert_equal(false, (@klass.new == @klass.new(:port => 80)))
101
+ end
102
+
103
+ def test_match__all
104
+ entry = @klass.new
105
+ assert_equal(true, entry.match?(nil, nil, nil))
106
+ assert_equal(true, entry.match?("127.0.0.1", nil, nil))
107
+ assert_equal(true, entry.match?(nil, "localhost", nil))
108
+ assert_equal(true, entry.match?(nil, nil, 80))
109
+ end
110
+
111
+ def test_match__addr
112
+ entry = @klass.new(:addr => "127.0.0.0/8")
113
+ assert_equal(false, entry.match?("126.255.255.255", nil, nil))
114
+ assert_equal(true, entry.match?("127.0.0.0", nil, nil))
115
+ assert_equal(true, entry.match?("127.255.255.255", nil, nil))
116
+ assert_equal(false, entry.match?("128.0.0.0", nil, nil))
117
+
118
+ entry = @klass.new(:addr => "127.0.0.1")
119
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
120
+ assert_equal(true, entry.match?("127.0.0.1", "google.co.jp", 8080))
121
+ assert_equal(false, entry.match?("192.168.0.1", "localhost", 443))
122
+ end
123
+
124
+ def test_match__name
125
+ entry = @klass.new(:name => "localhost")
126
+ assert_equal(true, entry.match?(nil, "localhost", nil))
127
+ assert_equal(true, entry.match?(nil, "LOCALHOST", nil))
128
+ assert_equal(false, entry.match?(nil, "google.co.jp", nil))
129
+
130
+ entry = @klass.new(:name => /\.co\.jp$/)
131
+ assert_equal(false, entry.match?(nil, "localhost", nil))
132
+ assert_equal(true, entry.match?(nil, "google.co.jp", nil))
133
+ assert_equal(true, entry.match?(nil, "nayutaya.co.jp", nil))
134
+
135
+ entry = @klass.new(:name => "localhost")
136
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
137
+ assert_equal(false, entry.match?("127.0.0.1", "google.co.jp", 8080))
138
+ assert_equal(true, entry.match?("192.168.0.1", "localhost", 443))
139
+ end
140
+
141
+ def test_match__port
142
+ entry = @klass.new(:port => 80)
143
+ assert_equal(false, entry.match?(nil, nil, 79))
144
+ assert_equal(true, entry.match?(nil, nil, 80))
145
+ assert_equal(false, entry.match?(nil, nil, 81))
146
+ end
147
+
148
+ def test_match__addr_and_name
149
+ entry = @klass.new(:addr => "127.0.0.1", :name => "localhost")
150
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
151
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", nil))
152
+ assert_equal(false, entry.match?("127.0.0.1", "google.co.jp", 80))
153
+ assert_equal(false, entry.match?("192.168.0.1", "localhost", 80))
154
+ end
155
+
156
+ def test_match__addr_and_port
157
+ entry = @klass.new(:addr => "127.0.0.1", :port => 80)
158
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
159
+ assert_equal(true, entry.match?("127.0.0.1", nil, 80))
160
+ assert_equal(false, entry.match?("127.0.0.1", "localhost", 8080))
161
+ assert_equal(false, entry.match?("192.168.0.1", "localhost", 80))
162
+ end
163
+
164
+ def test_match_name_and_port
165
+ entry = @klass.new(:name => "localhost", :port => 80)
166
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
167
+ assert_equal(true, entry.match?(nil, "localhost", 80))
168
+ assert_equal(false, entry.match?("127.0.0.1", "google.co.jp", 80))
169
+ assert_equal(false, entry.match?("127.0.0.1", "localhost", 8080))
170
+ end
171
+
172
+ def test_match_addr_and_name_and_port
173
+ entry = @klass.new(:addr => "127.0.0.1", :name => "localhost", :port => 80)
174
+ assert_equal(true, entry.match?("127.0.0.1", "localhost", 80))
175
+ assert_equal(false, entry.match?("192.168.0.1", "localhost", 80))
176
+ assert_equal(false, entry.match?("127.0.0.1", "google.co.jp", 80))
177
+ assert_equal(false, entry.match?("127.0.0.1", "localhost", 8080))
178
+ end
179
+
180
+ def test_value
181
+ assert_raise(NotImplementedError) {
182
+ @klass.new.value
183
+ }
184
+ end
185
+
186
+ def test_to_a
187
+ assert_equal(
188
+ [nil, nil, nil],
189
+ @klass.new.to_a)
190
+ end
191
+
192
+ def test_match_addr?
193
+ entry = @klass.new
194
+ assert_equal(true, entry.instance_eval { match_addr?(nil) })
195
+ assert_equal(true, entry.instance_eval { match_addr?(IPAddr.new("10.0.0.0")) })
196
+ assert_equal(true, entry.instance_eval { match_addr?(IPAddr.new("172.16.0.0")) })
197
+ assert_equal(true, entry.instance_eval { match_addr?(IPAddr.new("192.168.0.0")) })
198
+
199
+ entry = @klass.new(:addr => "10.0.0.0/8")
200
+ assert_equal(false, entry.instance_eval { match_addr?(nil) })
201
+ assert_equal(true, entry.instance_eval { match_addr?(IPAddr.new("10.0.0.0")) })
202
+ assert_equal(false, entry.instance_eval { match_addr?(IPAddr.new("172.16.0.0")) })
203
+ assert_equal(false, entry.instance_eval { match_addr?(IPAddr.new("192.168.0.0")) })
204
+ end
205
+
206
+ def test_match_name?
207
+ entry = @klass.new
208
+ assert_equal(true, entry.instance_eval { match_name?(nil) })
209
+ assert_equal(true, entry.instance_eval { match_name?("localhost") })
210
+ assert_equal(true, entry.instance_eval { match_name?("google.co.jp") })
211
+
212
+ entry = @klass.new(:name => "LocalHost")
213
+ assert_equal(false, entry.instance_eval { match_name?(nil) })
214
+ assert_equal(true, entry.instance_eval { match_name?("localhost") })
215
+ assert_equal(false, entry.instance_eval { match_name?("google.co.jp") })
216
+
217
+ entry = @klass.new(:name => /\.co\.jp$/)
218
+ assert_equal(false, entry.instance_eval { match_name?(nil) })
219
+ assert_equal(false, entry.instance_eval { match_name?("localhost") })
220
+ assert_equal(true, entry.instance_eval { match_name?("google.co.jp") })
221
+ end
222
+
223
+ def test_match_port?
224
+ entry = @klass.new
225
+ assert_equal(true, entry.instance_eval { match_port?(nil) })
226
+ assert_equal(true, entry.instance_eval { match_port?(0) })
227
+ assert_equal(true, entry.instance_eval { match_port?(1) })
228
+ assert_equal(true, entry.instance_eval { match_port?(2) })
229
+
230
+ entry = @klass.new(:port => 1)
231
+ assert_equal(false, entry.instance_eval { match_port?(nil) })
232
+ assert_equal(false, entry.instance_eval { match_port?(0) })
233
+ assert_equal(true, entry.instance_eval { match_port?(1) })
234
+ assert_equal(false, entry.instance_eval { match_port?(2) })
235
+
236
+ entry = @klass.new(:port => [0, 1])
237
+ assert_equal(false, entry.instance_eval { match_port?(nil) })
238
+ assert_equal(true, entry.instance_eval { match_port?(0) })
239
+ assert_equal(true, entry.instance_eval { match_port?(1) })
240
+ assert_equal(false, entry.instance_eval { match_port?(2) })
241
+
242
+ entry = @klass.new(:port => 1..2)
243
+ assert_equal(false, entry.instance_eval { match_port?(nil) })
244
+ assert_equal(false, entry.instance_eval { match_port?(0) })
245
+ assert_equal(true, entry.instance_eval { match_port?(1) })
246
+ assert_equal(true, entry.instance_eval { match_port?(2) })
247
+ end
248
+ end
data/test/acl_test.rb CHANGED
@@ -26,7 +26,7 @@ class AclTest < Test::Unit::TestCase
26
26
  def test_self_with__allow_and_deny
27
27
  acl = @klass.with {
28
28
  allow :all
29
- deny :all
29
+ deny :all
30
30
  }
31
31
  assert_equal(2, acl.size)
32
32
  end
@@ -37,23 +37,89 @@ class AclTest < Test::Unit::TestCase
37
37
  }
38
38
  end
39
39
 
40
+ def test_self_allow_all
41
+ assert_equal(
42
+ @klass.with { allow :all },
43
+ @klass.allow_all)
44
+ end
45
+
46
+ def test_self_deny_all
47
+ assert_equal(
48
+ @klass.with { deny :all },
49
+ @klass.deny_all)
50
+ end
51
+
52
+ def test_self_ipaddr?
53
+ assert_equal(true, @klass.ipaddr?("127.0.0.1"))
54
+ assert_equal(true, @klass.ipaddr?(IPAddr.new("127.0.0.1")))
55
+ assert_equal(false, @klass.ipaddr?("localhost"))
56
+ assert_equal(false, @klass.ipaddr?("google.co.jp"))
57
+ end
58
+
59
+ def test_self_create_matching_targets__addr
60
+ assert_equal(
61
+ [[IPAddr.new("127.0.0.1"), nil, 80]],
62
+ @klass.create_matching_targets("127.0.0.1", 80))
63
+ assert_equal(
64
+ [[IPAddr.new("192.168.0.1"), nil, 8080]],
65
+ @klass.create_matching_targets("192.168.0.1", 8080))
66
+ end
67
+
68
+ def test_self_create_matching_targets__name
69
+ musha = Kagemusha.new(TCPSocket)
70
+ musha.defs(:gethostbyname) {
71
+ ["google.co.jp", [], 2, "72.14.203.104", "74.125.91.104", "74.125.95.104"]
72
+ }
73
+ expected = [
74
+ [IPAddr.new("72.14.203.104"), "google.co.jp", 80],
75
+ [IPAddr.new("74.125.91.104"), "google.co.jp", 80],
76
+ [IPAddr.new("74.125.95.104"), "google.co.jp", 80],
77
+ ]
78
+ assert_equal(
79
+ expected,
80
+ musha.swap { @klass.create_matching_targets("google.co.jp", 80) })
81
+ end
82
+
40
83
  #
41
84
  # インスタンスメソッド
42
85
  #
43
86
 
87
+ def test_equal__empty
88
+ a, b = @klass.new, @klass.new
89
+ assert_equal(true, (a == b))
90
+ end
91
+
92
+ def test_equal__same_size_but_not_equal
93
+ a, b = @klass.new, @klass.new
94
+ a.add_allow(:addr => "127.0.0.0/8")
95
+ b.add_deny(:addr => "127.0.0.0/8")
96
+ assert_equal(false, (a == b))
97
+ end
98
+
99
+ def test_equal__same
100
+ a, b = @klass.new, @klass.new
101
+ a.add_allow(:addr => "127.0.0.0/8")
102
+ b.add_allow(:addr => "127.0.0.0/8")
103
+ assert_equal(true, (a == b))
104
+ end
105
+
106
+ def test_equal__other_class
107
+ assert_equal(false, (@klass.new == nil))
108
+ end
109
+
44
110
  def test_add_allow
45
111
  assert_equal(0, @acl.size)
46
- @acl.add_allow("0.0.0.0/0")
112
+ @acl.add_allow(:addr => "0.0.0.0/0")
47
113
  assert_equal(1, @acl.size)
48
- @acl.add_allow("0.0.0.0/0")
114
+ @acl.add_allow(:addr => "0.0.0.0/0")
49
115
  assert_equal(2, @acl.size)
50
116
  end
51
117
 
52
118
  def test_add_deny
53
119
  assert_equal(0, @acl.size)
54
- @acl.add_deny("0.0.0.0/0")
120
+ @acl.add_deny(:addr => "0.0.0.0/0")
55
121
  assert_equal(1, @acl.size)
56
- @acl.add_deny("0.0.0.0/0")
122
+ @acl.add_deny(:addr => "0.0.0.0/0")
57
123
  assert_equal(2, @acl.size)
58
124
  end
59
125
 
@@ -80,24 +146,24 @@ class AclTest < Test::Unit::TestCase
80
146
 
81
147
  def test_allow?
82
148
  assert_equal(true, @acl.allow?("127.0.0.1"))
83
- @acl.add_deny("127.0.0.0/8")
84
- assert_equal(false, @acl.allow?("127.0.0.1"))
85
- @acl.add_allow("127.0.0.0/8")
86
- assert_equal(true, @acl.allow?("127.0.0.1"))
149
+ @acl.add_deny(:addr => "127.0.0.0/8")
150
+ assert_equal(false, @acl.allow?("127.0.0.1", 80))
151
+ @acl.add_allow(:port => 80)
152
+ assert_equal(true, @acl.allow?("127.0.0.1", 80))
87
153
  end
88
154
 
89
155
  def test_deny?
90
156
  assert_equal(false, @acl.deny?("127.0.0.1"))
91
- @acl.add_deny("127.0.0.0/8")
92
- assert_equal(true, @acl.deny?("127.0.0.1"))
93
- @acl.add_allow("127.0.0.0/8")
94
- assert_equal(false, @acl.deny?("127.0.0.1"))
157
+ @acl.add_deny(:addr => "127.0.0.0/8")
158
+ assert_equal(true, @acl.deny?("127.0.0.1", 80))
159
+ @acl.add_allow(:port => 80)
160
+ assert_equal(false, @acl.deny?("127.0.0.1", 80))
95
161
  end
96
162
 
97
163
  def test_complex1
98
164
  @acl.with {
99
- deny :all
100
- allow "127.0.0.0/8"
165
+ deny :all
166
+ allow :addr => "127.0.0.0/8"
101
167
  }
102
168
 
103
169
  assert_equal(false, @acl.allow?("126.255.255.255"))
@@ -108,10 +174,10 @@ class AclTest < Test::Unit::TestCase
108
174
 
109
175
  def test_complex2
110
176
  @acl.with {
111
- deny :all
112
- allow "192.168.1.0/24"
113
- allow "192.168.3.0/24"
114
- deny "192.168.1.127"
177
+ deny :all
178
+ allow :addr => "192.168.1.0/24"
179
+ allow :addr => "192.168.3.0/24"
180
+ deny :addr => "192.168.1.127"
115
181
  }
116
182
 
117
183
  assert_equal(false, @acl.allow?("192.168.0.0"))
@@ -123,23 +189,54 @@ class AclTest < Test::Unit::TestCase
123
189
  assert_equal(true, @acl.allow?("192.168.3.255"))
124
190
  end
125
191
 
126
- #
127
- # RecordBaseクラス
128
- #
192
+ def test_complex3
193
+ @acl.with {
194
+ deny :all
195
+ allow :port => 80
196
+ deny :name => "localhost"
197
+ }
129
198
 
130
- def test_record_base_initialize
131
- assert_equal(
132
- IPAddr.new("127.0.0.0/8"),
133
- @klass::RecordBase.new(IPAddr.new("127.0.0.0/8")).ipaddr)
134
- assert_equal(
135
- IPAddr.new("127.0.0.0/8"),
136
- @klass::RecordBase.new("127.0.0.0/8").ipaddr)
137
- assert_equal(
138
- IPAddr.new("0.0.0.0/0"),
139
- @klass::RecordBase.new(:all).ipaddr)
199
+ assert_equal(true, @acl.allow?("127.0.0.1", 80))
200
+ assert_equal(false, @acl.allow?("localhost", 80))
201
+ assert_equal(true, @acl.allow?("google.co.jp", 80))
202
+ end
140
203
 
141
- assert_raise(ArgumentError) {
142
- @klass::RecordBase.new(:invalid)
204
+ def test_complex4
205
+ name, aliases, type, *addresses = TCPSocket.gethostbyname("google.co.jp")
206
+
207
+ addresses.each { |addr|
208
+ acl = @klass.new
209
+ acl.add_allow(:all)
210
+ acl.add_deny(:addr => addr)
211
+ assert_equal(true, acl.allow?("localhost"))
212
+ assert_equal(true, acl.allow?("yahoo.co.jp"))
213
+ assert_equal(false, acl.allow?("google.co.jp"))
214
+ assert_equal(false, acl.allow?(addr))
143
215
  }
144
216
  end
217
+
218
+ def test_complex5
219
+ name, aliases, type, *addresses = TCPSocket.gethostbyname("google.co.jp")
220
+
221
+ addresses.each { |addr|
222
+ acl = @klass.new
223
+ acl.add_deny(:all)
224
+ acl.add_allow(:addr => addr)
225
+ assert_equal(false, acl.allow?("localhost"))
226
+ assert_equal(false, acl.allow?("yahoo.co.jp"))
227
+ assert_equal(false, acl.allow?("google.co.jp"))
228
+ assert_equal(true, acl.allow?(addr))
229
+ }
230
+ end
231
+
232
+ def test_complex6
233
+ name, aliases, type, *addresses = TCPSocket.gethostbyname("google.co.jp")
234
+
235
+ @acl.add_deny(:all)
236
+ addresses.each { |addr| @acl.add_allow(:addr => addr) }
237
+
238
+ assert_equal(false, @acl.allow?("localhost"))
239
+ assert_equal(false, @acl.allow?("yahoo.co.jp"))
240
+ assert_equal(true, @acl.allow?("google.co.jp"))
241
+ end
145
242
  end