Skip to content

Commit

Permalink
unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisawa committed Apr 28, 2012
1 parent 34db9f5 commit 4501f9f
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/001_simple_query/example.com.zone
@@ -0,0 +1,18 @@
$ORIGIN example.com.
$TTL 1800

@ IN SOA ns1.example.com. hostmaster.example.com. (
2012042406 ; Serial
1H ; Refresh
5M ; Retry
1W ; Expire
1H ; Minimum
)

IN NS ns1.example.com.
IN NS ns2.example.com.

ns1 IN A 192.0.2.1
ns2 IN A 192.0.2.2

a IN A 192.0.2.10
5 changes: 5 additions & 0 deletions test/001_simple_query/primd.conf
@@ -0,0 +1,5 @@
zone "example.com" {
search {
data "example.com.dat";
};
};
10 changes: 10 additions & 0 deletions test/001_simple_query/test.rb
@@ -0,0 +1,10 @@
title 'Simple Query'

query 'a.example.com'

assert_status 'NOERROR'
assert_flag 'qr'
assert_flag 'aa'
assert_answer '192.0.2.10'
assert_authority 'ns1.example.com'
assert_authority 'ns2.example.com'
19 changes: 19 additions & 0 deletions test/002_cname_query/example.com.zone
@@ -0,0 +1,19 @@
$ORIGIN example.com.
$TTL 1800

@ IN SOA ns1.example.com. hostmaster.example.com. (
2012042407 ; Serial
1H ; Refresh
5M ; Retry
1W ; Expire
1H ; Minimum
)

IN NS ns1.example.com.
IN NS ns2.example.com.

ns1 IN A 192.0.2.1
ns2 IN A 192.0.2.2

a IN CNAME b
b IN A 192.0.2.10
5 changes: 5 additions & 0 deletions test/002_cname_query/primd.conf
@@ -0,0 +1,5 @@
zone "example.com" {
search {
data "example.com.dat";
};
};
11 changes: 11 additions & 0 deletions test/002_cname_query/test.rb
@@ -0,0 +1,11 @@
title 'Simple CNAME Query'

query 'a.example.com'

assert_status 'NOERROR'
assert_flag 'qr'
assert_flag 'aa'
assert_answer 'b.example.com', 'CNAME'
assert_answer '192.0.2.10'
assert_authority 'ns1.example.com'
assert_authority 'ns2.example.com'
18 changes: 18 additions & 0 deletions test/003_nxdoamin/example.com.zone
@@ -0,0 +1,18 @@
$ORIGIN example.com.
$TTL 1800

@ IN SOA ns1.example.com. hostmaster.example.com. (
2012042406 ; Serial
1H ; Refresh
5M ; Retry
1W ; Expire
1H ; Minimum
)

IN NS ns1.example.com.
IN NS ns2.example.com.

ns1 IN A 192.0.2.1
ns2 IN A 192.0.2.2

a IN A 192.0.2.10
5 changes: 5 additions & 0 deletions test/003_nxdoamin/primd.conf
@@ -0,0 +1,5 @@
zone "example.com" {
search {
data "example.com.dat";
};
};
8 changes: 8 additions & 0 deletions test/003_nxdoamin/test.rb
@@ -0,0 +1,8 @@
title 'Query nonexistent domain name'

query 'nonexistent.example.com'

assert_status 'NXDOMAIN'
assert_flag 'qr'
assert_flag 'aa'
assert_authority_type 'SOA'
18 changes: 18 additions & 0 deletions test/004_unknown_zone/example.com.zone
@@ -0,0 +1,18 @@
$ORIGIN example.com.
$TTL 1800

@ IN SOA ns1.example.com. hostmaster.example.com. (
2012042406 ; Serial
1H ; Refresh
5M ; Retry
1W ; Expire
1H ; Minimum
)

IN NS ns1.example.com.
IN NS ns2.example.com.

ns1 IN A 192.0.2.1
ns2 IN A 192.0.2.2

a IN A 192.0.2.10
5 changes: 5 additions & 0 deletions test/004_unknown_zone/primd.conf
@@ -0,0 +1,5 @@
zone "example.com" {
search {
data "example.com.dat";
};
};
6 changes: 6 additions & 0 deletions test/004_unknown_zone/test.rb
@@ -0,0 +1,6 @@
title 'Query nonexistent zone'

query 'zone.nonexistent'

assert_status 'SERVFAIL'
assert_flag 'qr'
9 changes: 9 additions & 0 deletions test/Makefile
@@ -0,0 +1,9 @@
all: test

test:
ruby tester.rb

clean:
find . -name "*.dat" -exec rm {} \;
find . -name "*.bak" -exec rm {} \;
find . -name "*.log" -exec rm {} \;
147 changes: 147 additions & 0 deletions test/tester.rb
@@ -0,0 +1,147 @@
#!/usr/bin/env ruby
require 'pp'

class Tester
TEST_PORT = 5353
PRIMD_DIR = '../primd'
SCRIPT_DIR = '../scripts'

def initialize(testdir)
@testdir = testdir
@ok = true
@status = nil; @flags = []; @records = {}
end

def ok?
@ok
end

def prepare
system("#{SCRIPT_DIR}/primdns-updatezone #{@testdir}")
start_primd
end

def start
open("#{@testdir}/test.rb") do |io|
lines = io.readlines
instance_eval(lines.join)
end
system("killall primd")
end

private
def start_primd
fork do
exec("#{PRIMD_DIR}/primd -d -f -p #{TEST_PORT} -c #{@testdir}/primd.conf >#{@testdir}/test.log")
end
end

def parse_result(result)
section = nil

result.split("\n").each do |line|
if line =~ /^;; ->>HEADER<<- .* status: (.*),/
@status = $1
end

if line =~ /^;; flags: (.*);/
@flags = $1.split(/\s+/)
end

section = :answer if line =~ /^;; ANSWER/
section = :authority if line =~ /^;; AUTHORITY/
section = :additional if line =~ /^;; ADDITIONAL/

if line =~ /^([^\s]+)\s+(\d+)\s+IN\s+([^\s]+)\s+(.+)$/
r = { :name => $1.chomp('.'), :ttl => $2, :type => $3, :rdata => $4.chomp('.') }
@records[section] ||= []
@records[section] << r
end
end
end

def title(t)
puts "- #{@testdir}: #{t} -"
end

def query(q)
r = `dig -p #{TEST_PORT} @127.0.0.1 #{q}`
parse_result(r)

pp @status
pp @flags
pp @records
end

def assert_status(status)
if @status == status
ok(status)
else
ng(status)
end
end

def assert_flag(flag)
if @flags.include?(flag)
ok(flag)
else
ng(flag)
end
end

def assert_answer(rdata, type = nil)
@records[:answer].each do |answer|
if type == nil || answer[:type] == type
if answer[:rdata] == rdata
ok(rdata, type)
return
end
end
end
ng(rdata, type)
end

def assert_authority(name)
@records[:authority].each do |ns|
if ns[:rdata] == name
ok(name)
return
end
end
ng(name)
end

def assert_authority_type(type)
@records[:authority].each do |ns|
if ns[:type] == type
ok(type)
return
end
end
ng(type)
end

def ok(param, type = nil)
t = (type != nil) ? "/#{type}" : ''
caller_name = caller[0].sub(/.*`(.+)'.*/, '\1')
puts "[ OK ] #{caller_name} \"#{param}\"#{t}"
end

def ng(param, type = nil)
t = (type != nil) ? "#{type} " : ''
caller_name = caller[0].sub(/.*`(.+)'.*/, '\1')
puts "[ FAIL ] #{caller_name} \"#{param}\"#{t}"
@ok = false
end
end


Dir.glob('*') do |dir|
if File.directory?(dir)
tester = Tester.new(dir)
tester.prepare
sleep 0.5

tester.start
end
end

0 comments on commit 4501f9f

Please sign in to comment.