Skip to content

Commit

Permalink
Added tests for more ffi corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jan 14, 2018
1 parent 3dea791 commit 4f77295
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/b6b_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ union b6b_ffi_val {
unsigned int uint;
long slong;
unsigned long ulong;
#if INT64_MAX > LONG_MAX
int64_t sint64;
uint64_t uint64;
#endif
float f;
double d;
void *voidp;
Expand Down Expand Up @@ -197,6 +199,7 @@ static struct b6b_obj *b6b_ffi_decode(const void *val,
memcpy(&valu.ushort, val, sizeof(valu.ushort));
return b6b_int_new((b6b_int)*(unsigned short *)val);
}
#if INT64_MAX > LONG_MAX
else if (type == &ffi_type_sint64) {
memcpy(&valu.sint64, val, sizeof(valu.sint64));
return b6b_int_new((b6b_int)valu.sint64);
Expand All @@ -205,6 +208,7 @@ static struct b6b_obj *b6b_ffi_decode(const void *val,
memcpy(&valu.uint64, val, sizeof(valu.uint64));
return b6b_int_new((b6b_int)valu.uint64);
}
#endif
else if (type == &ffi_type_float) {
memcpy(&valu.f, val, sizeof(valu.f));
return b6b_float_new((b6b_float)valu.f);
Expand Down Expand Up @@ -459,6 +463,7 @@ static int b6b_ffi_encode(struct b6b_interp *interp,
if (p)
*p = &val->ushort;
}
#if INT64_MAX > LONG_MAX
else if (type == &ffi_type_sint64) {
if (!b6b_as_int(o) || (o->i < INT64_MIN) || (o->i > INT64_MAX))
return 0;
Expand All @@ -475,6 +480,7 @@ static int b6b_ffi_encode(struct b6b_interp *interp,
if (p)
*p = &val->uint64;
}
#endif
else if (type == &ffi_type_float) {
if (!b6b_as_float(o))
return 0;
Expand Down
4 changes: 4 additions & 0 deletions tests/b6b_test_ffi_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,9 @@ int main()
assert(strcmp(interp.fg->_->s, "abc") == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{[$ffi.buf abc] addres}", 23) == B6B_ERR);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions tests/b6b_test_ffi_dlopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ int main()
36) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{[$ffi.dlopen libz.so.1] dlsyx crc32}",
37) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{[$ffi.dlopen libz.so.1] dlsym}",
Expand Down
52 changes: 52 additions & 0 deletions tests/b6b_test_ffi_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,58 @@ int main()
assert(b6b_call_copy(&interp, "{$ffi.pack . 100}", 17) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack i a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack I a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack l a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack L a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack p a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack b a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack B a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack h a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack H a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack q a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack Q a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack f a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.pack d a}", 15) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$ffi.pack iIlLpbBhHqQfd 65535 65536 1048575 4294967291 1234 127 255 80 8080 4294967297 1099511627775 1.33 1.444444}",
Expand Down
8 changes: 8 additions & 0 deletions tests/b6b_test_ffi_unpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ int main()
assert(b6b_call_copy(&interp, "{$ffi.unpack bbb xy}", 20) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.unpack . xy}", 18) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$ffi.unpack bI xy}", 19) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$ffi.unpack iIlLpbBhHqQfd [$ffi.pack iIlLpbBhHqQfd 65535 65536 1048575 4294967291 1234 127 255 80 8080 4294967297 1099511627775 1.33 1.444444]}",
Expand Down

0 comments on commit 4f77295

Please sign in to comment.