3 require File.dirname(__FILE__) + '/../lib/puppettest'
7 require 'puppet/network/authstore'
9 class TestAuthStore < Test::Unit::TestCase
11 Declaration = Puppet::Network::AuthStore::Declaration
14 assert_nothing_raised {
15 store = Puppet::Network::AuthStore.new
27 Puppet[:trace] = false
28 assert_nothing_raised {
29 assert(@store.allowed?(nil, nil), "Store disallowed local access")
32 assert_raise(Puppet::DevError) {
33 @store.allowed?("kirby.madstop.com", nil)
36 assert_raise(Puppet::DevError) {
37 @store.allowed?(nil, "192.168.0.1")
46 assert_nothing_raised("Failed to @store IP address %s" % ip) {
50 assert(@store.allowed?("hosttest.com", ip), "IP %s not allowed" % ip)
53 #assert_raise(Puppet::AuthStoreError) {
54 # @store.allow("192.168.674.0")
57 assert_raise(Puppet::AuthStoreError) {
58 @store.allow("192.168.0")
69 assert_nothing_raised("Failed to @store IP range %s" % range) {
80 assert(@store.allowed?("fakename.com", ip), "IP %s is not allowed" % ip)
84 def test_iprangedenials
85 assert_nothing_raised("Failed to @store overlapping IP ranges") {
86 @store.allow("192.168.0.0/16")
87 @store.deny("192.168.0.0/24")
90 assert(@store.allowed?("fake.name", "192.168.1.50"), "/16 ip not allowed")
91 assert(! @store.allowed?("fake.name", "192.168.0.50"), "/24 ip allowed")
94 def test_subdomaindenails
95 assert_nothing_raised("Failed to @store overlapping IP ranges") {
96 @store.allow("*.madstop.com")
97 @store.deny("*.sub.madstop.com")
100 assert(@store.allowed?("hostname.madstop.com", "192.168.1.50"),
101 "hostname not allowed")
102 assert(! @store.allowed?("name.sub.madstop.com", "192.168.0.50"),
103 "subname name allowed")
106 def test_orderingstuff
107 assert_nothing_raised("Failed to @store overlapping IP ranges") {
108 @store.allow("*.madstop.com")
109 @store.deny("192.168.0.0/24")
112 assert(@store.allowed?("hostname.madstop.com", "192.168.1.50"),
113 "hostname not allowed")
114 assert(! @store.allowed?("hostname.madstop.com", "192.168.0.50"),
115 "Host allowed over IP")
119 assert_nothing_raised("Failed to add global allow") {
124 %w{hostname.com 192.168.0.4},
125 %w{localhost 192.168.0.1},
126 %w{localhost 127.0.0.1}
129 assert(@store.allowed?(*ary), "Failed to allow %s" % [ary.join(",")])
134 assert_nothing_raised do
135 assert_nil(@store.send(:store, :allow, "*.host.com"),
136 "store did not return nil")
138 assert_equal([Declaration.new(:allow, "*.host.com")],
139 @store.send(:instance_variable_get, "@declarations"),
140 "Did not store declaration")
142 # Now add another one and make sure it gets sorted appropriately
143 assert_nothing_raised do
144 assert_nil(@store.send(:store, :allow, "me.host.com"),
145 "store did not return nil")
149 Declaration.new(:allow, "me.host.com"),
150 Declaration.new(:allow, "*.host.com")
152 @store.send(:instance_variable_get, "@declarations"),
153 "Did not sort declarations")
156 def test_allow_and_deny
157 store = Puppet::Network::AuthStore.new
158 store.expects(:store).with(:allow, "host.com")
159 store.allow("host.com")
161 store = Puppet::Network::AuthStore.new
162 store.expects(:store).with(:deny, "host.com")
163 store.deny("host.com")
165 store = Puppet::Network::AuthStore.new
166 assert_nothing_raised do
167 assert_nil(store.allow("*"),
168 "allow did not return nil")
171 assert(store.globalallow?,
172 "did not enable global allow")
176 Puppet[:trace] = false
180 name-other.madstop.net
182 assert_nothing_raised("Failed to @store simple name %s" % name) {
185 assert(@store.allowed?(name, "192.168.0.1"), "Name %s not allowed" % name)
194 assert_raise(Puppet::AuthStoreError,
195 "name '%s' was allowed" % pat) {
202 assert_nothing_raised("Failed to @store domains") {
203 @store.allow("*.a.very.long.domain.name.com")
204 @store.allow("*.madstop.com")
205 @store.allow("*.some-other.net")
206 @store.allow("*.much.longer.more-other.net")
213 funtest.some-other.net
215 some.much.much.longer.more-other.net
217 assert(@store.allowed?(name, "192.168.0.1"), "Host %s not allowed" % name)
220 assert_raise(Puppet::AuthStoreError) {
221 @store.allow("domain.*.com")
224 assert(!@store.allowed?("very.long.domain.name.com", "1.2.3.4"),
225 "Long hostname allowed")
227 assert_raise(Puppet::AuthStoreError) {
228 @store.allow("domain.*.other.com")
233 def test_case_insensitivity
234 @store.allow("hostname.com")
236 %w{hostname.com Hostname.COM hostname.Com HOSTNAME.COM}.each do |name|
237 assert(@store.allowed?(name, "127.0.0.1"),
238 "did not allow %s" % name)
243 Puppet[:trace] = false
244 assert(@store.allowed?(nil, nil),
245 "Did not default to true for local checks")
246 assert_raise(Puppet::DevError, "did not fail on one input") do
247 @store.allowed?("host.com", nil)
249 assert_raise(Puppet::DevError, "did not fail on one input") do
250 @store.allowed?(nil, "192.168.0.1")
255 # Make sure more specific allows and denies win over generalities
256 def test_specific_overrides
257 @store.allow("host.madstop.com")
258 @store.deny("*.madstop.com")
260 assert(@store.allowed?("host.madstop.com", "192.168.0.1"),
261 "More specific allowal by name failed")
263 @store.allow("192.168.0.1")
264 @store.deny("192.168.0.0/24")
266 assert(@store.allowed?("host.madstop.com", "192.168.0.1"),
267 "More specific allowal by ip failed")
271 class TestAuthStoreDeclaration < PuppetTest::TestCase
273 Declaration = Puppet::Network::AuthStore::Declaration
277 @decl = Declaration.new(:allow, "hostname.com")
282 "192.168.0.1" => [:ip, IPAddr.new("192.168.0.1"), nil],
283 "2001:700:300:1800::" => [:ip, IPAddr.new("2001:700:300:1800::"), nil],
284 "2001:700:300:1800::/64" => [:ip, IPAddr.new("2001:700:300:1800::/64"), 64],
285 "192.168.0.1/32" => [:ip, IPAddr.new("192.168.0.1/32"), 32],
286 "192.168.0.1/24" => [:ip, IPAddr.new("192.168.0.1/24"), 24],
287 "192.*" => [:ip, IPAddr.new("192.0.0.0/8"), 8],
288 "192.168.*" => [:ip, IPAddr.new("192.168.0.0/16"), 16],
289 "192.168.0.*" => [:ip, IPAddr.new("192.168.0.0/24"), 24],
290 "hostname.com" => [:domain, %w{com hostname}, nil],
291 "Hostname.COM" => [:domain, %w{com hostname}, nil],
292 "billy.Hostname.COM" => [:domain, %w{com hostname billy}, nil],
293 "billy-jean.Hostname.COM" => [:domain, %w{com hostname billy-jean}, nil],
294 "*.hostname.COM" => [:domain, %w{com hostname}, 2],
295 "*.hostname.COM" => [:domain, %w{com hostname}, 2]
296 }.each do |input, output|
298 # Create a new decl each time, so values aren't cached.
299 assert_nothing_raised do
300 @decl = Declaration.new(:allow, input)
303 [:name, :pattern, :length].zip(output).each do |method, value|
304 assert_equal(value, @decl.send(method),
305 "Got incorrect value for %s from %s" % [method, input])
309 %w{192.168 hostname -hostname.com hostname.*}.each do |input|
310 assert_raise(Puppet::AuthStoreError, "Did not fail on %s" % input) do
311 @decl.pattern = input
315 ["hostname .com", "192.168 .0.1"].each do |input|
316 assert_raise(Puppet::AuthStoreError, "Did not fail on %s" % input) do
317 @decl.pattern = input
323 ["allow", :allow].each do |val|
324 assert_nothing_raised { @decl.type = val }
325 assert_equal(true, @decl.result, "did not result to true with %s" %
329 [:deny, "deny"].each do |val|
330 assert_nothing_raised { @decl.type = val }
331 assert_equal(false, @decl.result,
332 "did not result to false with %s" % val.inspect)
335 ["yay", 1, nil, false, true].each do |val|
336 assert_raise(ArgumentError, "Did not fail on %s" % val.inspect) do
344 "hostname.com" => %w{com hostname},
345 "alley.hostname.com" => %w{com hostname alley},
346 "*.hostname.com" => %w{com hostname *},
347 "*.HOSTNAME.Com" => %w{com hostname *},
348 "*.HOSTNAME.Com" => %w{com hostname *},
350 }.each do |input, output|
351 assert_equal(output, @decl.send(:munge_name, input),
352 "munged %s incorrectly" % input)
356 # Make sure people can specify TLDs
358 assert_nothing_raised {
359 @decl.pattern = "*.tld"
362 assert_equal(%w{tld}, @decl.pattern, "Failed to allow custom tld")
365 # Make sure we sort correctly.
367 # Make sure declarations with no length sort first.
368 host_exact = Declaration.new(:allow, "host.com")
369 host_range = Declaration.new(:allow, "*.host.com")
371 ip_exact = Declaration.new(:allow, "192.168.0.1")
372 ip_range = Declaration.new(:allow, "192.168.0.*")
374 assert_equal(-1, host_exact <=> host_range,
375 "exact name match did not sort first")
377 assert_equal(-1, ip_exact <=> ip_range,
378 "exact ip match did not sort first")
380 # Next make sure we sort by length
381 ip_long = Declaration.new(:allow, "192.168.*")
382 assert_equal(-1, ip_range <=> ip_long, "/16 sorted before /24 in ip")
384 # Now try it using masks
385 ip24 = Declaration.new(:allow, "192.168.0.0/24")
386 ip16 = Declaration.new(:allow, "192.168.0.0/16")
388 assert_equal(-1, ip24 <=> ip16, "/16 sorted before /24 in ip with masks")
390 # Make sure ip checks sort before host checks
391 assert_equal(-1, ip_exact <=> host_exact,
392 "IP exact did not sort before host exact")
394 assert_equal(-1, ip_range <=> host_range,
395 "IP range did not sort before host range")
397 host_long = Declaration.new(:allow, "*.domain.host.com")
399 assert_equal(-1, host_long <=> host_range, "did not sort by domain length")
401 # Now make sure denies sort before allows, for equivalent
403 host_deny = Declaration.new(:deny, "host.com")
404 assert_equal(-1, host_deny <=> host_exact, "deny did not sort before allow when exact")
406 host_range_deny = Declaration.new(:deny, "*.host.com")
407 assert_equal(-1, host_range_deny <=> host_range,
408 "deny did not sort before allow when ranged")
410 ip_allow = Declaration.new(:allow, "192.168.0.0/16")
411 ip_deny = Declaration.new(:deny, "192.168.0.0/16")
413 assert_equal(-1, ip_deny <=> ip_allow,
414 "deny did not sort before allow in ip range")
416 %w{host.com *.domain.com 192.168.0.1 192.168.0.1/24}.each do |decl|
417 assert_equal(0, Declaration.new(:allow, decl) <=>
418 Declaration.new(:allow, decl),
419 "Equivalent declarations for %s were considered different" %
426 host = Declaration.new(:allow, "host.com")
427 host.expects(:matchname?).with("host.com")
428 host.match?("host.com", "192.168.0.1")
430 ip = Declaration.new(:allow, "192.168.0.1")
431 ip.pattern.expects(:include?)
432 ip.match?("host.com", "192.168.0.1")
436 host = Declaration.new(:allow, "host.com")
437 assert(host.send(:matchname?, "host.com"), "exact did not match")
438 assert(! host.send(:matchname?, "yay.com"), "incorrect match")
440 domain = Declaration.new(:allow, "*.domain.com")
441 %w{host.domain.com domain.com very.long.domain.com very-long.domain.com
443 assert(domain.send(:matchname?, name),
444 "Did not match %s" % name)