summaryrefslogtreecommitdiff
path: root/test/dl/test_cptr.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/dl/test_cptr.rb')
-rw-r--r--test/dl/test_cptr.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/test/dl/test_cptr.rb b/test/dl/test_cptr.rb
index c03abbe73e..3c4b5e6867 100644
--- a/test/dl/test_cptr.rb
+++ b/test/dl/test_cptr.rb
@@ -3,6 +3,42 @@ require_relative '../ruby/envutil'
module DL
class TestCPtr < TestBase
+ def test_to_ptr_string
+ str = "hello world"
+ ptr = CPtr[str]
+ assert ptr.tainted?, 'pointer should be tainted'
+ assert_equal str.length, ptr.size
+ assert_equal 'hello', ptr[0,5]
+ end
+
+ def test_to_ptr_io
+ buf = CPtr.malloc(10)
+ File.open(__FILE__, 'r') do |f|
+ ptr = CPtr.to_ptr f
+ fread = CFunc.new(@libc['fread'], TYPE_VOID, 'fread')
+ fread.call([buf.to_i, DL::SIZEOF_CHAR, buf.size - 1, ptr.to_i])
+ end
+
+ File.open(__FILE__, 'r') do |f|
+ assert_equal f.read(9), buf.to_s
+ end
+ end
+
+ def test_to_ptr_with_ptr
+ ptr = CPtr.new 0
+ ptr2 = CPtr.to_ptr Struct.new(:to_ptr).new(ptr)
+ assert_equal ptr, ptr2
+
+ assert_raises(DL::DLError) do
+ CPtr.to_ptr Struct.new(:to_ptr).new(nil)
+ end
+ end
+
+ def test_to_ptr_with_num
+ ptr = CPtr.new 0
+ assert_equal ptr, CPtr[0]
+ end
+
def test_equals
ptr = CPtr.new 0
ptr2 = CPtr.new 0