#!/usr/bin/env ruby
# -*- coding: binary -*-
$:.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'test/unit'
require 'rex/exploitation/opcodedb'
require 'rex/socket'
class Rex::Exploitation::OpcodeDb::UnitTest < Test::Unit::TestCase
Klass = Rex::Exploitation::OpcodeDb::Client
SrvPort = 60000
def test_meta_types
srv_cli
begin
proc_req_resp(%q{1foo2dog})
mt = @r.meta_types
assert_kind_of(Array, mt)
assert_equal(2, mt.length)
assert_equal(1, mt[0].id)
assert_equal("foo", mt[0].name)
assert_equal(2, mt[1].id)
assert_equal("dog", mt[1].name)
ensure
@s.close
end
end
def test_groups
srv_cli
begin
proc_req_resp(%q{47foo2dog})
mt = @r.groups
assert_kind_of(Array, mt)
assert_equal(2, mt.length)
assert_equal(47, mt[0].id)
assert_equal("foo", mt[0].name)
assert_equal(2, mt[1].id)
assert_equal("dog", mt[1].name)
ensure
@s.close
end
end
def test_platforms
srv_cli
begin
proc_req_resp(%q{12Windows XP SP2Windows Coolness512010})
osv = @r.platforms
assert_kind_of(Array, osv)
assert_equal(1, osv.length)
assert_equal(12, osv[0].id)
assert_equal("Windows XP SP2", osv[0].name)
assert_equal("Windows Coolness", osv[0].desc)
assert_equal(5, osv[0].maj_ver)
assert_equal(1, osv[0].min_ver)
assert_equal(2, osv[0].maj_patch_level)
assert_equal(0, osv[0].min_patch_level)
assert_equal(10, osv[0].modules)
ensure
@s.close
end
end
def test_modules
srv_cli
begin
proc_req_resp(%q{
1
kernel32.dll
4
English
4
100
2
7
403242822
100000000
40000
text
3228094
4000
true
true
false
FoolFunction
3242344
5
FoolFunctionExport
32423445
51
})
m = @r.modules
assert_kind_of(Array, m)
assert_equal(1, m[0].id)
assert_equal("kernel32.dll", m[0].name)
assert_equal(4, m[0].locale.id)
assert_equal("English", m[0].locale.name)
assert_equal(4, m[0].maj_maj_ver)
assert_equal(100, m[0].maj_min_ver)
assert_equal(2, m[0].min_maj_ver)
assert_equal(7, m[0].min_min_ver)
assert_equal(403242822, m[0].timestamp.to_i)
assert_equal(100000000, m[0].base_address)
assert_equal(40000, m[0].image_size)
assert_kind_of(Array, m[0].segments)
assert_equal("text", m[0].segments[0].type)
assert_equal(3228094, m[0].segments[0].base_address)
assert_equal(4000, m[0].segments[0].size)
assert_equal(true, m[0].segments[0].writable)
assert_equal(true, m[0].segments[0].readable)
assert_equal(false, m[0].segments[0].executable)
assert_kind_of(Array, m[0].imports)
assert_equal("FoolFunction", m[0].imports[0].name)
assert_equal(3242344, m[0].imports[0].address)
assert_equal(5, m[0].imports[0].ordinal)
assert_kind_of(Array, m[0].exports)
assert_equal("FoolFunctionExport", m[0].exports[0].name)
assert_equal(32423445, m[0].exports[0].address)
assert_equal(51, m[0].exports[0].ordinal)
ensure
@s.close
end
end
def test_locales
srv_cli
begin
proc_req_resp(%q{
4
English
5
French
})
l = @r.locales
assert_kind_of(Array, l)
assert_equal(2, l.length)
assert_equal(4, l[0].id)
assert_equal("English", l[0].name)
assert_equal(5, l[1].id)
assert_equal("French", l[1].name)
ensure
@s.close
end
end
def test_search
srv_cli
begin
proc_req_resp(%q{
400
34242324
4
jmp esp
40
reg
})
o = @r.search
assert_kind_of(Array, o)
assert_equal(1, o.length)
assert_equal(400, o[0].id)
assert_equal(34242324, o[0].address)
assert_equal(4, o[0].type.id)
assert_equal("jmp esp", o[0].type.name)
assert_equal(40, o[0].group.id)
assert_equal("reg", o[0].group.name)
ensure
@s.close
end
end
def test_statistics
srv_cli
begin
proc_req_resp(%q{
40
50
60
70
80
90
100
110
120
})
s = @r.statistics
assert_equal(40, s.modules)
assert_equal(50, s.opcodes)
assert_equal(60, s.opcode_types)
assert_equal(70, s.platforms)
assert_equal(80, s.architectures)
assert_equal(90, s.module_segments)
assert_equal(100, s.module_imports)
assert_equal(110, s.module_exports)
assert_equal(120, s.last_update.to_i)
ensure
@s.close
end
end
protected
def srv_cli
@r = Klass.new('127.0.0.1', SrvPort)
@s = Rex::Socket::TcpServer.create(
'LocalHost' => '127.0.0.1',
'LocalPort' => SrvPort)
end
def proc_req_resp(buf)
thr = Thread.new {
cli = @s.accept
@buffer = cli.get
cli.put("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\n#{buf}")
cli.close
}
end
end