2009-09-10 13:14:53 -04:00
|
|
|
require_relative 'test_base.rb'
|
2005-02-04 08:35:37 -05:00
|
|
|
require 'dl/callback'
|
2010-03-19 12:16:22 -04:00
|
|
|
require 'dl/func'
|
2010-03-19 12:18:05 -04:00
|
|
|
require 'dl/pack'
|
2005-02-04 08:35:37 -05:00
|
|
|
|
|
|
|
module DL
|
|
|
|
class TestDL < TestBase
|
2010-03-19 12:18:05 -04:00
|
|
|
def ptr2num(*list)
|
|
|
|
list.pack("p*").unpack(PackInfo::PACK_MAP[TYPE_VOIDP] + "*")
|
|
|
|
end
|
|
|
|
|
2009-10-31 21:46:27 -04:00
|
|
|
# TODO: refactor test repetition
|
|
|
|
|
2009-10-31 21:46:44 -04:00
|
|
|
def test_free_secure
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE = 4
|
|
|
|
DL.free(0)
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-31 21:46:27 -04:00
|
|
|
def test_realloc
|
|
|
|
str = "abc"
|
|
|
|
ptr_id = DL.realloc(0, 4)
|
|
|
|
ptr = CPtr.new(ptr_id, 4)
|
|
|
|
|
|
|
|
assert_equal ptr_id, ptr.to_i
|
|
|
|
|
|
|
|
cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
|
|
|
|
x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*"))
|
|
|
|
assert_equal("abc\0", ptr[0,4])
|
|
|
|
DL.free ptr_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_realloc_secure
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE = 4
|
|
|
|
DL.realloc(0, 4)
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_malloc
|
|
|
|
str = "abc"
|
|
|
|
|
|
|
|
ptr_id = DL.malloc(4)
|
|
|
|
ptr = CPtr.new(ptr_id, 4)
|
|
|
|
|
|
|
|
assert_equal ptr_id, ptr.to_i
|
|
|
|
|
|
|
|
cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
|
|
|
|
x = cfunc.call([ptr_id,str].pack("l!p").unpack("l!*"))
|
|
|
|
assert_equal("abc\0", ptr[0,4])
|
|
|
|
DL.free ptr_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_malloc_security
|
|
|
|
assert_raises(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE = 4
|
|
|
|
DL.malloc(4)
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-04 08:35:37 -05:00
|
|
|
def test_call_int()
|
|
|
|
cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi')
|
|
|
|
x = cfunc.call(["100"].pack("p").unpack("l!*"))
|
|
|
|
assert_equal(100, x)
|
|
|
|
|
|
|
|
cfunc = CFunc.new(@libc['atoi'], TYPE_INT, 'atoi')
|
|
|
|
x = cfunc.call(["-100"].pack("p").unpack("l!*"))
|
|
|
|
assert_equal(-100, x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_long()
|
|
|
|
cfunc = CFunc.new(@libc['atol'], TYPE_LONG, 'atol')
|
|
|
|
x = cfunc.call(["100"].pack("p").unpack("l!*"))
|
|
|
|
assert_equal(100, x)
|
|
|
|
cfunc = CFunc.new(@libc['atol'], TYPE_LONG, 'atol')
|
|
|
|
x = cfunc.call(["-100"].pack("p").unpack("l!*"))
|
|
|
|
assert_equal(-100, x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_call_double()
|
|
|
|
cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof')
|
|
|
|
x = cfunc.call(["0.1"].pack("p").unpack("l!*"))
|
2009-09-09 08:04:29 -04:00
|
|
|
assert_in_delta(0.1, x)
|
2005-02-04 08:35:37 -05:00
|
|
|
|
|
|
|
cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof')
|
|
|
|
x = cfunc.call(["-0.1"].pack("p").unpack("l!*"))
|
2009-09-09 08:04:29 -04:00
|
|
|
assert_in_delta(-0.1, x)
|
2005-02-04 08:35:37 -05:00
|
|
|
end
|
|
|
|
|
2010-02-02 20:23:48 -05:00
|
|
|
def test_sin
|
2011-01-30 02:15:23 -05:00
|
|
|
return if /x86_64/ =~ RUBY_PLATFORM
|
2009-09-09 08:04:29 -04:00
|
|
|
pi_2 = Math::PI/2
|
2010-02-02 20:23:48 -05:00
|
|
|
cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'),
|
|
|
|
[TYPE_DOUBLE])
|
|
|
|
x = cfunc.call(pi_2)
|
2009-09-09 08:04:29 -04:00
|
|
|
assert_equal(Math.sin(pi_2), x)
|
2005-02-04 08:35:37 -05:00
|
|
|
|
2010-02-02 20:23:48 -05:00
|
|
|
cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'),
|
|
|
|
[TYPE_DOUBLE])
|
|
|
|
x = cfunc.call(-pi_2)
|
2009-09-09 08:04:29 -04:00
|
|
|
assert_equal(Math.sin(-pi_2), x)
|
2005-02-04 08:35:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_strlen()
|
|
|
|
cfunc = CFunc.new(@libc['strlen'], TYPE_INT, 'strlen')
|
|
|
|
x = cfunc.call(["abc"].pack("p").unpack("l!*"))
|
|
|
|
assert_equal("abc".size, x)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strcpy()
|
|
|
|
buff = "xxxx"
|
|
|
|
str = "abc"
|
|
|
|
cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
|
2010-03-19 12:18:05 -04:00
|
|
|
x = cfunc.call(ptr2num(buff,str))
|
2005-02-04 08:35:37 -05:00
|
|
|
assert_equal("abc\0", buff)
|
|
|
|
assert_equal("abc\0", CPtr.new(x).to_s(4))
|
|
|
|
|
|
|
|
buff = "xxxx"
|
|
|
|
str = "abc"
|
|
|
|
cfunc = CFunc.new(@libc['strncpy'], TYPE_VOIDP, 'strncpy')
|
2010-03-19 12:18:05 -04:00
|
|
|
x = cfunc.call(ptr2num(buff,str) + [3])
|
2005-02-04 08:35:37 -05:00
|
|
|
assert_equal("abcx", buff)
|
|
|
|
assert_equal("abcx", CPtr.new(x).to_s(4))
|
|
|
|
|
|
|
|
ptr = CPtr.malloc(4)
|
|
|
|
str = "abc"
|
|
|
|
cfunc = CFunc.new(@libc['strcpy'], TYPE_VOIDP, 'strcpy')
|
2010-03-19 12:18:05 -04:00
|
|
|
x = cfunc.call([ptr.to_i, *ptr2num(str)])
|
2005-02-04 08:35:37 -05:00
|
|
|
assert_equal("abc\0", ptr[0,4])
|
|
|
|
assert_equal("abc\0", CPtr.new(x).to_s(4))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_callback()
|
|
|
|
buff = "foobarbaz"
|
|
|
|
cb = set_callback(TYPE_INT,2){|x,y| CPtr.new(x)[0] <=> CPtr.new(y)[0]}
|
|
|
|
cfunc = CFunc.new(@libc['qsort'], TYPE_VOID, 'qsort')
|
2010-03-19 12:18:05 -04:00
|
|
|
cfunc.call(ptr2num(buff) + [buff.size, 1, cb])
|
2005-02-04 08:35:37 -05:00
|
|
|
assert_equal('aabbfoorz', buff)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dlwrap()
|
|
|
|
ary = [0,1,2,4,5]
|
|
|
|
addr = dlwrap(ary)
|
|
|
|
ary2 = dlunwrap(addr)
|
|
|
|
assert_equal(ary, ary2)
|
|
|
|
end
|
2012-02-25 00:47:16 -05:00
|
|
|
|
|
|
|
def test_type_size_t
|
|
|
|
assert_equal(DL::TYPE_SSIZE_T, DL::TYPE_SIZE_T.abs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_type_uintptr_t
|
|
|
|
assert_equal(-DL::TYPE_INTPTR_T, DL::TYPE_UINTPTR_T)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sizeof_uintptr_t
|
|
|
|
assert_equal(DL::SIZEOF_VOIDP, DL::SIZEOF_INTPTR_T)
|
|
|
|
end
|
2005-02-04 08:35:37 -05:00
|
|
|
end
|
|
|
|
end # module DL
|