-
Notifications
You must be signed in to change notification settings - Fork 1
/
goffi_test.go
590 lines (522 loc) · 12.7 KB
/
goffi_test.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* libgoffi - libffi adapter library for Go
* Copyright 2019 clevabit GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libgoffi
import (
"reflect"
"strings"
"testing"
)
const testLibrary = "libgoffitests"
func TestSymbolCached(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
return
}
s1, err := l.Symbol("getpid")
if err != nil {
t.Errorf("Symbol getpid not available: %v", err)
l.Close()
return
}
s2, err := l.Symbol("getpid")
if err != nil {
t.Errorf("Symbol getpid not available: %v", err)
l.Close()
return
}
if s1 != s2 {
t.Errorf("Symbols for getpid are not equal: %d != %d", s1, s2)
}
l.Close()
}
func TestPassingWrongTargetObject(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
return
}
var foo string
if err := l.Import("getpid", &foo); err != nil {
if !strings.Contains(err.Error(), "target not a function type") {
t.Errorf("wrong error returned: %s", err)
}
} else {
t.Error("the import should fail due to wrong target object passed")
}
l.Close()
}
func TestLoadLibraryFailed(t *testing.T) {
_, err := NewLibrary("123libc", BindNow)
if err == nil {
t.Error("Library loading for lib 132libc shouldn't succeed")
}
}
func TestLoadLibrary(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
l.Close()
}
func TestSimplyImportSymbol(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
var getpid func() int
if err := l.Import("getpid", &getpid); err != nil {
t.Errorf("Symbol getpid failed to be imported: %v", err)
}
l.Close()
}
func TestNewImportWithoutError(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fn, err := l.NewImport("getpid", TypeInt, false)
if err != nil {
t.Errorf("Symbol getpid failed to be imported: %v", err)
} else {
if _, ok := fn.(func() int); !ok {
t.Errorf("imported function is of wrong type, expected: "+
"func() int, got: %s", reflect.TypeOf(fn).String())
}
}
l.Close()
}
func TestNewImportWithError(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fn, err := l.NewImport("getpid", TypeInt, true)
if err != nil {
t.Errorf("Symbol getpid failed to be imported: %v", err)
} else {
if _, ok := fn.(func() (int, error)); !ok {
t.Errorf("imported function is of wrong type, expected: "+
"func() (int, error), got: %s", reflect.TypeOf(fn).String())
}
}
l.Close()
}
func TestMultiReturnFailingWithoutError(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fnType := reflect.FuncOf([]reflect.Type{}, []reflect.Type{TypeInt, TypeInt}, false)
_, err = l.NewImportComplex("getpid", fnType, fnType)
if err != nil {
if !strings.Contains(err.Error(), "multiple return values for Go impossible") {
t.Errorf("wrong error message received: %v", err)
}
} else {
t.Error("error message expected")
}
l.Close()
}
func TestMultiReturnFailingMoreThan2RetVals(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fnType := reflect.FuncOf([]reflect.Type{}, []reflect.Type{TypeInt, TypeInt, TypeError}, false)
_, err = l.NewImportComplex("getpid", fnType, fnType)
if err != nil {
if !strings.Contains(err.Error(), "multiple return values for Go impossible") {
t.Errorf("wrong error message received: %v", err)
}
} else {
t.Error("error message expected")
}
l.Close()
}
func TestOnlyReturnVoid(t *testing.T) {
l, err := NewLibrary(testLibrary, BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fnType := reflect.FuncOf([]reflect.Type{}, []reflect.Type{TypeVoid}, false)
fn, err := l.NewImportComplex("empty", fnType, fnType)
if err != nil {
if !strings.Contains(err.Error(), "multiple return values for Go impossible") {
t.Errorf("wrong error message received: %v", err)
}
} else {
if _, ok := fn.(func()); !ok {
t.Errorf("imported function is of wrong type, expected: "+
"func() int, got: %s", reflect.TypeOf(fn).String())
}
}
l.Close()
}
func TestOnlyParameterVoid(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fnType := reflect.FuncOf([]reflect.Type{TypeVoid}, []reflect.Type{TypeInt}, false)
fn, err := l.NewImportComplex("getpid", fnType, fnType)
if err != nil {
if !strings.Contains(err.Error(), "multiple return values for Go impossible") {
t.Errorf("wrong error message received: %v", err)
}
} else {
if _, ok := fn.(func() int); !ok {
t.Errorf("imported function is of wrong type, expected: "+
"func() int, got: %s", reflect.TypeOf(fn).String())
}
}
l.Close()
}
func TestFailingVoidParameterNotAlone(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
fnType := reflect.FuncOf([]reflect.Type{TypeInt, TypeVoid}, []reflect.Type{TypeInt}, false)
_, err = l.NewImportComplex("fn", fnType, fnType)
if err != nil {
if !strings.Contains(err.Error(), "void is not a legal parameter type") {
t.Errorf("wrong error message received: %v", err)
}
} else {
t.Error("error message expected")
}
l.Close()
}
func TestImportSymbolFailed(t *testing.T) {
l, err := NewLibrary("libc", BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
var abc func() int
if err := l.Import("abc123", &abc); err == nil {
t.Errorf("Symbol abc123 shouldn't be importable: %v", err)
}
l.Close()
}
func TestExecuteOneParamOneReturn(t *testing.T) {
l, err := NewLibrary(testLibrary, BindLazy)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
var sqrt func(float64) float64
if err := l.Import("_sqrt", &sqrt); err != nil {
t.Errorf("Symbol sqrt failed to be imported: %v", err)
return
}
ret := sqrt(9.)
if ret != 3. {
t.Fail()
}
l.Close()
}
func TestExecuteNoParamOneReturn(t *testing.T) {
l, err := NewLibrary(testLibrary, BindNow)
if err != nil {
t.Errorf("Library failed to be initialized: %v", err)
}
var fn func() int
if err := l.Import("_sint", &fn); err != nil {
t.Errorf("Symbol _sint failed to be imported: %v", err)
} else {
fn()
}
l.Close()
}
func TestExecuteSint(t *testing.T) {
var fn func() int
libraryTestHelper(t, "_sint", testLibrary, &fn, func() {
v := fn()
if v != -1 {
t.Fail()
}
})
}
func TestExecuteSint8(t *testing.T) {
var fn func() int8
libraryTestHelper(t, "_sint8", testLibrary, &fn, func() {
v := fn()
if v != -8 {
t.Fail()
}
})
}
func TestExecuteSint16(t *testing.T) {
var fn func() int16
libraryTestHelper(t, "_sint16", testLibrary, &fn, func() {
v := fn()
if v != -16 {
t.Fail()
}
})
}
func TestExecuteSint32(t *testing.T) {
var fn func() int32
libraryTestHelper(t, "_sint32", testLibrary, &fn, func() {
v := fn()
if v != -32 {
t.Fail()
}
})
}
func TestExecuteSint64(t *testing.T) {
var fn func() int64
libraryTestHelper(t, "_sint64", testLibrary, &fn, func() {
v := fn()
if v != -64 {
t.Fail()
}
})
}
func TestExecuteUint(t *testing.T) {
var fn func() uint
libraryTestHelper(t, "_uint", testLibrary, &fn, func() {
v := fn()
if v != 1 {
t.Fail()
}
})
}
func TestExecuteUint8(t *testing.T) {
var fn func() uint8
libraryTestHelper(t, "_uint8", testLibrary, &fn, func() {
v := fn()
if v != 8 {
t.Fail()
}
})
}
func TestExecuteUint16(t *testing.T) {
var fn func() uint16
libraryTestHelper(t, "_uint16", testLibrary, &fn, func() {
v := fn()
if v != 16 {
t.Fail()
}
})
}
func TestExecuteUint32(t *testing.T) {
var fn func() uint32
libraryTestHelper(t, "_uint32", testLibrary, &fn, func() {
v := fn()
if v != 32 {
t.Fail()
}
})
}
func TestExecuteUint64(t *testing.T) {
var fn func() uint64
libraryTestHelper(t, "_uint64", testLibrary, &fn, func() {
v := fn()
if v != 64 {
t.Fail()
}
})
}
func TestExecuteFloat32(t *testing.T) {
var fn func() float32
libraryTestHelper(t, "_float", testLibrary, &fn, func() {
v := fn()
if v != 32.1 {
t.Fail()
}
})
}
func TestExecuteFloat64(t *testing.T) {
var fn func() float64
libraryTestHelper(t, "_double", testLibrary, &fn, func() {
v := fn()
if v != -64.2 {
t.Fail()
}
})
}
func TestExecuteBool(t *testing.T) {
var fn func() bool
libraryTestHelper(t, "_bool", testLibrary, &fn, func() {
v := fn()
if v != true {
t.Fail()
}
})
}
func TestExecuteIntInIntOut(t *testing.T) {
var fn func(int) int
libraryTestHelper(t, "__sint", testLibrary, &fn, func() {
v := fn(2)
if v != 1 {
t.Fail()
}
})
}
func TestExecuteInt8InInt8Out(t *testing.T) {
var fn func(int8) int8
libraryTestHelper(t, "__sint8", testLibrary, &fn, func() {
v := fn(15)
if v != 7 {
t.Fail()
}
})
}
func TestExecuteInt16InInt16Out(t *testing.T) {
var fn func(int16) int16
libraryTestHelper(t, "__sint16", testLibrary, &fn, func() {
v := fn(31)
if v != 15 {
t.Fail()
}
})
}
func TestExecuteInt32InInt32Out(t *testing.T) {
var fn func(int32) int32
libraryTestHelper(t, "__sint32", testLibrary, &fn, func() {
v := fn(63)
if v != 31 {
t.Fail()
}
})
}
func TestExecuteInt64InInt64Out(t *testing.T) {
var fn func(int64) int64
libraryTestHelper(t, "__sint64", testLibrary, &fn, func() {
v := fn(127)
if v != 63 {
t.Fail()
}
})
}
func TestExecuteUintInUintOut(t *testing.T) {
var fn func(uint) uint
libraryTestHelper(t, "__uint", testLibrary, &fn, func() {
v := fn(2)
if v != 1 {
t.Fail()
}
})
}
func TestExecuteUint8InUint8Out(t *testing.T) {
var fn func(uint8) uint8
libraryTestHelper(t, "__uint8", testLibrary, &fn, func() {
v := fn(15)
if v != 7 {
t.Fail()
}
})
}
func TestExecuteUint16InUint16Out(t *testing.T) {
var fn func(uint16) uint16
libraryTestHelper(t, "__uint16", testLibrary, &fn, func() {
v := fn(31)
if v != 15 {
t.Fail()
}
})
}
func TestExecuteUint32InUint32Out(t *testing.T) {
var fn func(uint32) uint32
libraryTestHelper(t, "__uint32", testLibrary, &fn, func() {
v := fn(63)
if v != 31 {
t.Fail()
}
})
}
func TestExecuteFloat32InFloat32Out(t *testing.T) {
var fn func(float32) float32
libraryTestHelper(t, "__float", testLibrary, &fn, func() {
v := fn(63.)
if v != 31. {
t.Fail()
}
})
}
func TestExecuteFloat64InFloat64Out(t *testing.T) {
var fn func(float64) float64
libraryTestHelper(t, "__double", testLibrary, &fn, func() {
v := fn(127.)
if v != 63. {
t.Fail()
}
})
}
func TestExecuteUint64InUint64Out(t *testing.T) {
var fn func(uint64) uint64
libraryTestHelper(t, "__uint64", testLibrary, &fn, func() {
v := fn(127)
if v != 63 {
t.Fail()
}
})
}
func TestExecuteBoolFalseInBoolTrueOut(t *testing.T) {
var fn func(bool) bool
libraryTestHelper(t, "__bool", testLibrary, &fn, func() {
v := fn(false)
if v != true {
t.Fail()
}
})
}
func TestExecuteBoolTrueInBoolFalseOut(t *testing.T) {
var fn func(bool) bool
libraryTestHelper(t, "__bool", testLibrary, &fn, func() {
v := fn(true)
if v != false {
t.Fail()
}
})
}
func TestExecuteWithErrorButNotFailing(t *testing.T) {
var fn func(bool) (bool, error)
libraryTestHelper(t, "__bool", testLibrary, &fn, func() {
v, err := fn(true)
if v != false {
t.Fail()
}
if err != nil {
t.Fail()
}
})
}
func TestExecuteCharPtrIntInCharPtrOut(t *testing.T) {
var fn func(string, int) string
libraryTestHelper(t, "_char", testLibrary, &fn, func() {
msg := "hello world"
v := fn(msg, len(msg)+1)
if v != "hello world" {
t.Errorf("expected '%s', got '%s'", msg, v)
}
})
}
func libraryTestHelper(t *testing.T, symbol, library string, fn interface{}, test func()) {
l, err := NewLibrary(library, BindNow)
if err != nil {
t.Errorf("Library %s failed to be initialized: %v", library, err)
return
}
if err := l.Import(symbol, fn); err != nil {
t.Errorf("Symbol %s failed to be imported: %v", symbol, err)
return
}
test()
l.Close()
}