summaryrefslogtreecommitdiff
path: root/test/dl/test_dl2.rb
blob: 88ef0869b1c8de6587e1e1171f0d6ecf63f484a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require_relative 'test_base.rb'
require 'dl/callback'
require 'dl/func'
require 'dl/pack'

module DL
class TestDL < TestBase
  def ptr2num(*list)
    list.pack("p*").unpack(PackInfo::PACK_MAP[TYPE_VOIDP] + "*")
  end

  # TODO: refactor test repetition

  def test_free_secure
    assert_raises(SecurityError) do
      Thread.new do
        $SAFE = 4
        DL.free(0)
      end.join
    end
  end

  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')
             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')
             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

  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!*"))
    assert_in_delta(0.1, x)

    cfunc = CFunc.new(@libc['atof'], TYPE_DOUBLE, 'atof')
    x = cfunc.call(["-0.1"].pack("p").unpack("l!*"))
    assert_in_delta(-0.1, x)
  end

  def test_sin
    return if /x86_64/ =~ RUBY_PLATFORM
    pi_2 = Math::PI/2
    cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'),
                        [TYPE_DOUBLE])
    x = cfunc.call(pi_2)
    assert_equal(Math.sin(pi_2), x)

    cfunc = Function.new(CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin'),
                        [TYPE_DOUBLE])
    x = cfunc.call(-pi_2)
    assert_equal(Math.sin(-pi_2), x)
  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')
    x = cfunc.call(ptr2num(buff,str))
    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')
    x = cfunc.call(ptr2num(buff,str) + [3])
    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')
    x = cfunc.call([ptr.to_i, *ptr2num(str)])
    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')
    cfunc.call(ptr2num(buff) + [buff.size, 1, cb])
    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

  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
end
end # module DL