| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "../common/test-log.h" |
| 10 |
#include <wplua/wplua.h> |
| 11 |
|
| 12 |
enum { |
| 13 |
PROP_0, |
| 14 |
PROP_TEST_STRING, |
| 15 |
PROP_TEST_INT, |
| 16 |
PROP_TEST_UINT, |
| 17 |
PROP_TEST_INT64, |
| 18 |
PROP_TEST_UINT64, |
| 19 |
PROP_TEST_FLOAT, |
| 20 |
PROP_TEST_DOUBLE, |
| 21 |
PROP_TEST_BOOLEAN, |
| 22 |
}; |
| 23 |
|
| 24 |
typedef struct _TestObject TestObject; |
| 25 |
struct _TestObject |
| 26 |
{ |
| 27 |
GObject parent; |
| 28 |
gchar *test_string; |
| 29 |
gint test_int; |
| 30 |
guint test_uint; |
| 31 |
gint64 test_int64; |
| 32 |
guint64 test_uint64; |
| 33 |
gfloat test_float; |
| 34 |
gdouble test_double; |
| 35 |
gboolean test_boolean; |
| 36 |
}; |
| 37 |
|
| 38 |
typedef struct _TestObjectClass TestObjectClass; |
| 39 |
struct _TestObjectClass |
| 40 |
{ |
| 41 |
GObjectClass parent_class; |
| 42 |
|
| 43 |
void (*change) (TestObject * self, const gchar * str, gint integer); |
| 44 |
}; |
| 45 |
|
| 46 |
G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT) |
| 47 |
|
| 48 |
#define TEST_TYPE_OBJECT (test_object_get_type ()) |
| 49 |
_GLIB_DEFINE_AUTOPTR_CHAINUP (TestObject, GObject) |
| 50 |
|
| 51 |
static inline TestObject * TEST_OBJECT (gpointer ptr) { |
| 52 |
return G_TYPE_CHECK_INSTANCE_CAST (ptr, TEST_TYPE_OBJECT, TestObject); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
test_object_init (TestObject * self) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
test_object_finalize (GObject * object) |
| 62 |
{ |
| 63 |
TestObject *self = TEST_OBJECT (object); |
| 64 |
g_free (self->test_string); |
| 65 |
G_OBJECT_CLASS (test_object_parent_class)->finalize (object); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
test_object_get_property (GObject * object, guint id, GValue * value, |
| 70 |
GParamSpec * pspec) |
| 71 |
{ |
| 72 |
TestObject *self = TEST_OBJECT (object); |
| 73 |
|
| 74 |
switch (id) { |
| 75 |
case PROP_TEST_STRING: |
| 76 |
g_value_set_string (value, self->test_string); |
| 77 |
break; |
| 78 |
case PROP_TEST_INT: |
| 79 |
g_value_set_int (value, self->test_int); |
| 80 |
break; |
| 81 |
case PROP_TEST_UINT: |
| 82 |
g_value_set_uint (value, self->test_uint); |
| 83 |
break; |
| 84 |
case PROP_TEST_INT64: |
| 85 |
g_value_set_int64 (value, self->test_int64); |
| 86 |
break; |
| 87 |
case PROP_TEST_UINT64: |
| 88 |
g_value_set_uint64 (value, self->test_uint64); |
| 89 |
break; |
| 90 |
case PROP_TEST_FLOAT: |
| 91 |
g_value_set_float (value, self->test_float); |
| 92 |
break; |
| 93 |
case PROP_TEST_DOUBLE: |
| 94 |
g_value_set_double (value, self->test_double); |
| 95 |
break; |
| 96 |
case PROP_TEST_BOOLEAN: |
| 97 |
g_value_set_boolean (value, self->test_boolean); |
| 98 |
break; |
| 99 |
default: |
| 100 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, id, pspec); |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
test_object_set_property (GObject * object, guint id, const GValue * value, |
| 107 |
GParamSpec * pspec) |
| 108 |
{ |
| 109 |
TestObject *self = TEST_OBJECT (object); |
| 110 |
|
| 111 |
switch (id) { |
| 112 |
case PROP_TEST_STRING: |
| 113 |
g_free (self->test_string); |
| 114 |
self->test_string = g_value_dup_string (value); |
| 115 |
break; |
| 116 |
case PROP_TEST_INT: |
| 117 |
self->test_int = g_value_get_int (value); |
| 118 |
break; |
| 119 |
case PROP_TEST_UINT: |
| 120 |
self->test_uint = g_value_get_uint (value); |
| 121 |
break; |
| 122 |
case PROP_TEST_INT64: |
| 123 |
self->test_int64 = g_value_get_int64 (value); |
| 124 |
break; |
| 125 |
case PROP_TEST_UINT64: |
| 126 |
self->test_uint64 = g_value_get_uint64 (value); |
| 127 |
break; |
| 128 |
case PROP_TEST_FLOAT: |
| 129 |
self->test_float = g_value_get_float (value); |
| 130 |
break; |
| 131 |
case PROP_TEST_DOUBLE: |
| 132 |
self->test_double = g_value_get_double (value); |
| 133 |
break; |
| 134 |
case PROP_TEST_BOOLEAN: |
| 135 |
self->test_boolean = g_value_get_boolean (value); |
| 136 |
break; |
| 137 |
default: |
| 138 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, id, pspec); |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static void |
| 144 |
test_object_change (TestObject * self, const gchar * str, gint integer) |
| 145 |
{ |
| 146 |
g_free (self->test_string); |
| 147 |
self->test_string = g_strdup_printf ("changed: %s", str); |
| 148 |
g_object_notify (G_OBJECT (self), "test-string"); |
| 149 |
self->test_int = integer; |
| 150 |
g_object_notify (G_OBJECT (self), "test-int"); |
| 151 |
|
| 152 |
gint ret = 0; |
| 153 |
g_signal_emit_by_name (self, "acquire", &ret); |
| 154 |
self->test_int64 = ret; |
| 155 |
g_object_notify (G_OBJECT (self), "test-int64"); |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
test_object_class_init (TestObjectClass * klass) |
| 160 |
{ |
| 161 |
GObjectClass *obj_class = (GObjectClass *) klass; |
| 162 |
|
| 163 |
obj_class->finalize = test_object_finalize; |
| 164 |
obj_class->get_property = test_object_get_property; |
| 165 |
obj_class->set_property = test_object_set_property; |
| 166 |
|
| 167 |
g_object_class_install_property (obj_class, PROP_TEST_STRING, |
| 168 |
g_param_spec_string ("test-string", "test-string", "blurb", NULL, |
| 169 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 170 |
|
| 171 |
g_object_class_install_property (obj_class, PROP_TEST_INT, |
| 172 |
g_param_spec_int ("test-int", "test-int", "blurb", |
| 173 |
G_MININT, G_MAXINT, 0, |
| 174 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 175 |
|
| 176 |
g_object_class_install_property (obj_class, PROP_TEST_UINT, |
| 177 |
g_param_spec_uint ("test-uint", "test-uint", "blurb", |
| 178 |
0, G_MAXUINT, 0, |
| 179 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 180 |
|
| 181 |
g_object_class_install_property (obj_class, PROP_TEST_INT64, |
| 182 |
g_param_spec_int64 ("test-int64", "test-int64", "blurb", |
| 183 |
G_MININT64, G_MAXINT64, 0, |
| 184 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 185 |
|
| 186 |
g_object_class_install_property (obj_class, PROP_TEST_UINT64, |
| 187 |
g_param_spec_uint64 ("test-uint64", "test-uint64", "blurb", |
| 188 |
0, G_MAXUINT64, 0, |
| 189 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 190 |
|
| 191 |
g_object_class_install_property (obj_class, PROP_TEST_FLOAT, |
| 192 |
g_param_spec_float ("test-float", "test-float", "blurb", |
| 193 |
-20.0f, 20.0f, 0, |
| 194 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 195 |
|
| 196 |
g_object_class_install_property (obj_class, PROP_TEST_DOUBLE, |
| 197 |
g_param_spec_double ("test-double", "test-double", "blurb", |
| 198 |
-20.0, 20.0, 0.0, |
| 199 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 200 |
|
| 201 |
g_object_class_install_property (obj_class, PROP_TEST_BOOLEAN, |
| 202 |
g_param_spec_boolean ("test-boolean", "test-boolean", "blurb", FALSE, |
| 203 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 204 |
|
| 205 |
g_signal_new ("change", G_TYPE_FROM_CLASS (klass), |
| 206 |
G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST, |
| 207 |
G_STRUCT_OFFSET (TestObjectClass, change), NULL, NULL, NULL, |
| 208 |
G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_INT); |
| 209 |
klass->change = test_object_change; |
| 210 |
|
| 211 |
g_signal_new ("acquire", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, |
| 212 |
0, NULL, NULL, NULL, G_TYPE_INT, 0); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
test_object_toggle (TestObject * self) |
| 217 |
{ |
| 218 |
self->test_boolean = !self->test_boolean; |
| 219 |
g_object_notify (G_OBJECT (self), "test-boolean"); |
| 220 |
} |
| 221 |
|
| 222 |
static int |
| 223 |
l_test_object_toggle (lua_State * L) |
| 224 |
{ |
| 225 |
TestObject * self = wplua_checkobject (L, 1, TEST_TYPE_OBJECT); |
| 226 |
test_object_toggle (self); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
static const luaL_Reg l_test_object_methods[] = { |
| 231 |
{ "toggle", l_test_object_toggle }, |
| 232 |
{ NULL, NULL } |
| 233 |
}; |
| 234 |
|
| 235 |
static int |
| 236 |
l_test_object_new (lua_State * L) |
| 237 |
{ |
| 238 |
wplua_pushobject (L, g_object_new (TEST_TYPE_OBJECT, NULL)); |
| 239 |
return 1; |
| 240 |
} |
| 241 |
|
| 242 |
static gboolean |
| 243 |
test_load_and_call (lua_State * L, const gchar *buf, gsize size, |
| 244 |
int nargs, int nres, GError **error) |
| 245 |
{ |
| 246 |
int argstop = lua_gettop (L); |
| 247 |
int sandbox = wplua_push_sandbox (L); |
| 248 |
|
| 249 |
if (!wplua_load_buffer (L, buf, size, error)) { |
| 250 |
lua_pop (L, nargs + sandbox); |
| 251 |
return FALSE; |
| 252 |
} |
| 253 |
|
| 254 |
/* push sandbox() and the chunk below the arguments */ |
| 255 |
lua_rotate (L, argstop, -nargs); |
| 256 |
|
| 257 |
return wplua_pcall (L, nargs + sandbox, nres, error); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
test_wplua_basic () |
| 262 |
{ |
| 263 |
lua_State *L = wplua_new (); |
| 264 |
wplua_unref (L); |
| 265 |
} |
| 266 |
|
| 267 |
static void |
| 268 |
test_wplua_construct () |
| 269 |
{ |
| 270 |
g_autoptr (GObject) obj = NULL; |
| 271 |
g_autoptr (GError) error = NULL; |
| 272 |
lua_State *L = wplua_new (); |
| 273 |
|
| 274 |
wplua_register_type_methods(L, TEST_TYPE_OBJECT, |
| 275 |
l_test_object_new, l_test_object_methods); |
| 276 |
|
| 277 |
const gchar code[] = |
| 278 |
"o = TestObject_new()\n" |
| 279 |
"assert (type(o) == 'userdata')\n"; |
| 280 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 281 |
g_assert_no_error (error); |
| 282 |
|
| 283 |
g_assert_cmpint (lua_getglobal (L, "o"), ==, LUA_TUSERDATA); |
| 284 |
g_assert_true (wplua_checkobject (L, -1, TEST_TYPE_OBJECT)); |
| 285 |
obj = wplua_toobject (L, -1); |
| 286 |
g_assert_nonnull (obj); |
| 287 |
g_object_ref (obj); |
| 288 |
g_assert_cmpint (obj->ref_count, ==, 2); |
| 289 |
|
| 290 |
wplua_unref (L); |
| 291 |
g_assert_cmpint (obj->ref_count, ==, 1); |
| 292 |
} |
| 293 |
|
| 294 |
static void |
| 295 |
test_wplua_properties () |
| 296 |
{ |
| 297 |
TestObject *obj = NULL; |
| 298 |
g_autoptr (GError) error = NULL; |
| 299 |
lua_State *L = wplua_new (); |
| 300 |
|
| 301 |
wplua_register_type_methods(L, TEST_TYPE_OBJECT, |
| 302 |
l_test_object_new, l_test_object_methods); |
| 303 |
|
| 304 |
const gchar code[] = |
| 305 |
"o = TestObject_new()\n" |
| 306 |
"o['test-string'] = 'string from lua'\n" |
| 307 |
"o['test-int'] = -15\n" |
| 308 |
"o['test-uint'] = 1123456789\n" |
| 309 |
"o['test-int64'] = -5123456789\n" |
| 310 |
"o['test-uint64'] = 15123456789\n" |
| 311 |
"o['test-float'] = 3.1415\n" |
| 312 |
"o['test-double'] = 0.123456789\n" |
| 313 |
"o['test-boolean'] = true\n"; |
| 314 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 315 |
g_assert_no_error (error); |
| 316 |
|
| 317 |
g_assert_cmpint (lua_getglobal (L, "o"), ==, LUA_TUSERDATA); |
| 318 |
g_assert_true (wplua_checkobject (L, -1, TEST_TYPE_OBJECT)); |
| 319 |
obj = wplua_toobject (L, -1); |
| 320 |
g_assert_nonnull (obj); |
| 321 |
|
| 322 |
g_assert_cmpstr (obj->test_string, ==, "string from lua"); |
| 323 |
g_assert_cmpint (obj->test_int, ==, -15); |
| 324 |
g_assert_cmpuint (obj->test_uint, ==, 1123456789); |
| 325 |
g_assert_cmpint (obj->test_int64, ==, -5123456789); |
| 326 |
g_assert_cmpuint (obj->test_uint64, ==, 15123456789); |
| 327 |
g_assert_cmpfloat_with_epsilon (obj->test_float, 3.1415, 0.00001); |
| 328 |
g_assert_cmpfloat_with_epsilon (obj->test_double, 0.123456789, 0.0000000001); |
| 329 |
g_assert_true (obj->test_boolean); |
| 330 |
|
| 331 |
const gchar code2[] = |
| 332 |
"assert (o['test-string'] == 'string from lua')\n" |
| 333 |
"assert (o['test-int'] == -15)\n" |
| 334 |
"assert (o['test-uint'] == 1123456789)\n" |
| 335 |
"assert (o['test-int64'] == -5123456789)\n" |
| 336 |
"assert (o['test-uint64'] == 15123456789)\n" |
| 337 |
"assert (math.abs (o['test-float'] - 3.1415) < 0.00001)\n" |
| 338 |
"assert (math.abs (o['test-double'] - 0.123456789) < 0.0000000001)\n" |
| 339 |
"assert (o['test-boolean'] == true)\n"; |
| 340 |
test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); |
| 341 |
g_assert_no_error (error); |
| 342 |
|
| 343 |
wplua_unref (L); |
| 344 |
} |
| 345 |
|
| 346 |
static void |
| 347 |
test_wplua_closure () |
| 348 |
{ |
| 349 |
GClosure *closure; |
| 350 |
g_autoptr (GError) error = NULL; |
| 351 |
lua_State *L = wplua_new (); |
| 352 |
|
| 353 |
lua_pushstring (L, "some string"); |
| 354 |
lua_setglobal (L, "expected_str"); |
| 355 |
|
| 356 |
const gchar code[] = |
| 357 |
"f_was_called = false\n" |
| 358 |
"function f(s)\n" |
| 359 |
" assert(s == expected_str)\n" |
| 360 |
" f_was_called = true\n" |
| 361 |
"end\n"; |
| 362 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 363 |
g_assert_no_error (error); |
| 364 |
|
| 365 |
lua_getglobal (L, "f"); |
| 366 |
closure = wplua_function_to_closure (L, -1); |
| 367 |
g_assert_nonnull (closure); |
| 368 |
g_closure_ref (closure); |
| 369 |
g_closure_sink (closure); |
| 370 |
lua_pop (L, 1); |
| 371 |
|
| 372 |
{ |
| 373 |
GValue s = G_VALUE_INIT; |
| 374 |
g_value_init (&s, G_TYPE_STRING); |
| 375 |
g_value_set_static_string (&s, "some string"); |
| 376 |
g_closure_invoke (closure, NULL, 1, &s, NULL); |
| 377 |
} |
| 378 |
|
| 379 |
lua_getglobal (L, "f_was_called"); |
| 380 |
g_assert_true (lua_isboolean (L, -1)); |
| 381 |
g_assert_true (lua_toboolean (L, -1)); |
| 382 |
|
| 383 |
wplua_unref (L); |
| 384 |
|
| 385 |
g_assert_true (closure->is_invalid); |
| 386 |
g_closure_unref (closure); |
| 387 |
} |
| 388 |
|
| 389 |
static void |
| 390 |
test_wplua_signals () |
| 391 |
{ |
| 392 |
g_autoptr (GError) error = NULL; |
| 393 |
lua_State *L = wplua_new (); |
| 394 |
|
| 395 |
wplua_register_type_methods(L, TEST_TYPE_OBJECT, |
| 396 |
l_test_object_new, l_test_object_methods); |
| 397 |
|
| 398 |
const gchar code[] = |
| 399 |
"o = TestObject_new()\n" |
| 400 |
"\n" |
| 401 |
"o:connect('acquire', function (obj)\n" |
| 402 |
" assert(obj == o)\n" |
| 403 |
" return 42\n" |
| 404 |
" end)\n" |
| 405 |
"\n" |
| 406 |
"o:connect('notify::test-string', function (obj, pspec)\n" |
| 407 |
" assert(pspec == 'test-string')\n" |
| 408 |
" assert(obj[pspec] == 'changed: by Lua')\n" |
| 409 |
" end)\n" |
| 410 |
"\n" |
| 411 |
"o:call('change', 'by Lua', 55)\n" |
| 412 |
"\n" |
| 413 |
"assert(o['test-string'] == 'changed: by Lua')\n" |
| 414 |
"assert(o['test-int'] == 55)\n" |
| 415 |
"assert(o['test-int64'] == 42)\n" |
| 416 |
"\n" |
| 417 |
"o['test-boolean'] = true\n" |
| 418 |
"o:toggle()\n" |
| 419 |
"assert(o['test-boolean'] == false)\n"; |
| 420 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 421 |
g_assert_no_error (error); |
| 422 |
wplua_unref (L); |
| 423 |
} |
| 424 |
|
| 425 |
static void |
| 426 |
test_wplua_sandbox_script () |
| 427 |
{ |
| 428 |
g_autoptr (GError) error = NULL; |
| 429 |
lua_State *L = wplua_new (); |
| 430 |
|
| 431 |
wplua_register_type_methods(L, TEST_TYPE_OBJECT, |
| 432 |
l_test_object_new, l_test_object_methods); |
| 433 |
|
| 434 |
const gchar code[] = |
| 435 |
"SANDBOX_EXPORT = {\n" |
| 436 |
" Test = TestObject_new,\n" |
| 437 |
" Table = { test = 'foobar' }\n" |
| 438 |
"}\n"; |
| 439 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 440 |
g_assert_no_error (error); |
| 441 |
|
| 442 |
wplua_enable_sandbox (L, WP_LUA_SANDBOX_ISOLATE_ENV); |
| 443 |
|
| 444 |
const gchar code2[] = |
| 445 |
"o = TestObject_new()\n"; |
| 446 |
test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); |
| 447 |
g_debug ("expected error: %s", error ? error->message : "null"); |
| 448 |
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); |
| 449 |
g_clear_error (&error); |
| 450 |
|
| 451 |
const gchar code3[] = |
| 452 |
"o = Test()\n" |
| 453 |
"o:toggle()\n"; |
| 454 |
test_load_and_call (L, code3, sizeof (code3) - 1, 0, 0, &error); |
| 455 |
g_assert_no_error (error); |
| 456 |
|
| 457 |
const gchar code4[] = |
| 458 |
"assert(string.len(Table.test) == 6)\n"; |
| 459 |
test_load_and_call (L, code4, sizeof (code4) - 1, 0, 0, &error); |
| 460 |
g_assert_no_error (error); |
| 461 |
|
| 462 |
const gchar code5[] = |
| 463 |
"o:call('change', 'by Lua', 55)\n"; |
| 464 |
test_load_and_call (L, code5, sizeof (code5) - 1, 0, 0, &error); |
| 465 |
g_debug ("expected error: %s", error ? error->message : "null"); |
| 466 |
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); |
| 467 |
g_clear_error (&error); |
| 468 |
|
| 469 |
const gchar code6[] = |
| 470 |
"string.test = 'hello world'\n"; |
| 471 |
test_load_and_call (L, code6, sizeof (code6) - 1, 0, 0, &error); |
| 472 |
g_debug ("expected error: %s", error ? error->message : "null"); |
| 473 |
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); |
| 474 |
g_clear_error (&error); |
| 475 |
|
| 476 |
const gchar code7[] = |
| 477 |
"Table.test = 'hello world'\n"; |
| 478 |
test_load_and_call (L, code7, sizeof (code7) - 1, 0, 0, &error); |
| 479 |
g_debug ("expected error: %s", error ? error->message : "null"); |
| 480 |
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); |
| 481 |
g_clear_error (&error); |
| 482 |
|
| 483 |
wplua_unref (L); |
| 484 |
} |
| 485 |
|
| 486 |
static void |
| 487 |
test_wplua_sandbox_config () |
| 488 |
{ |
| 489 |
g_autoptr (GError) error = NULL; |
| 490 |
lua_State *L = wplua_new (); |
| 491 |
|
| 492 |
wplua_enable_sandbox (L, 0); |
| 493 |
|
| 494 |
const gchar code3[] = |
| 495 |
"o = { answer = 42 }\n"; |
| 496 |
test_load_and_call (L, code3, sizeof (code3) - 1, 0, 0, &error); |
| 497 |
g_assert_no_error (error); |
| 498 |
|
| 499 |
/* no assert() in minimal_std mode, resort to other means of failure */ |
| 500 |
const gchar code4[] = |
| 501 |
"if (o.answer ~= 42) then\n" |
| 502 |
" non_existent_function()\n" |
| 503 |
"end\n"; |
| 504 |
test_load_and_call (L, code4, sizeof (code4) - 1, 0, 0, &error); |
| 505 |
g_assert_no_error (error); |
| 506 |
|
| 507 |
/* string.* is protected */ |
| 508 |
const gchar code6[] = |
| 509 |
"string.test = 'hello world'\n"; |
| 510 |
test_load_and_call (L, code6, sizeof (code6) - 1, 0, 0, &error); |
| 511 |
g_debug ("expected error: %s", error ? error->message : "null"); |
| 512 |
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME); |
| 513 |
g_clear_error (&error); |
| 514 |
|
| 515 |
/* this would be an error if the assert function was exported, but it's not */ |
| 516 |
const gchar code7[] = |
| 517 |
"assert = 'hello world'\n"; |
| 518 |
test_load_and_call (L, code7, sizeof (code7) - 1, 0, 0, &error); |
| 519 |
g_assert_no_error (error); |
| 520 |
|
| 521 |
wplua_unref (L); |
| 522 |
} |
| 523 |
|
| 524 |
static void |
| 525 |
test_wplua_convert_asv () |
| 526 |
{ |
| 527 |
g_autoptr (GError) error = NULL; |
| 528 |
lua_State *L = wplua_new (); |
| 529 |
|
| 530 |
g_autoptr (GVariant) v = g_variant_new_parsed ("@a{sv} { " |
| 531 |
"'test-int': <42>, " |
| 532 |
"'test-double': <3.14>, " |
| 533 |
"'test-string': <'foobar'>, " |
| 534 |
"'test-boolean': <true>, " |
| 535 |
"'nested-table': <@a{sv} { 'string': <'baz'> }> " |
| 536 |
"}"); |
| 537 |
wplua_gvariant_to_lua (L, v); |
| 538 |
lua_setglobal (L, "o"); |
| 539 |
|
| 540 |
const gchar code2[] = |
| 541 |
"assert (o['test-string'] == 'foobar')\n" |
| 542 |
"assert (o['test-int'] == 42)\n" |
| 543 |
"assert (math.abs (o['test-double'] - 3.14) < 0.0000000001)\n" |
| 544 |
"assert (o['test-boolean'] == true)\n" |
| 545 |
"assert (o['nested-table']['string'] == 'baz')\n"; |
| 546 |
test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); |
| 547 |
g_assert_no_error (error); |
| 548 |
|
| 549 |
lua_getglobal (L, "o"); |
| 550 |
g_autoptr (GVariant) fromlua = wplua_lua_to_gvariant (L, -1); |
| 551 |
|
| 552 |
gint64 test_int = 0; |
| 553 |
g_assert_true (g_variant_lookup (fromlua, "test-int", "x", &test_int)); |
| 554 |
g_assert_cmpint (test_int, ==, 42); |
| 555 |
|
| 556 |
gdouble test_double = 0; |
| 557 |
g_assert_true (g_variant_lookup (fromlua, "test-double", "d", &test_double)); |
| 558 |
g_assert_cmpfloat_with_epsilon (test_double, 3.14, 0.0000000001); |
| 559 |
|
| 560 |
const gchar *test_str = NULL; |
| 561 |
g_assert_true (g_variant_lookup (fromlua, "test-string", "&s", &test_str)); |
| 562 |
g_assert_cmpstr (test_str, ==, "foobar"); |
| 563 |
|
| 564 |
gboolean test_boolean = FALSE; |
| 565 |
g_assert_true (g_variant_lookup (fromlua, "test-boolean", "b", &test_boolean)); |
| 566 |
g_assert_true (test_boolean); |
| 567 |
|
| 568 |
g_autoptr (GVariant) nested = NULL; |
| 569 |
g_assert_true (g_variant_lookup (fromlua, "nested-table", "@a{sv}", &nested)); |
| 570 |
g_assert_nonnull (nested); |
| 571 |
g_assert_true (g_variant_is_of_type (nested, G_VARIANT_TYPE_VARDICT)); |
| 572 |
|
| 573 |
test_str = NULL; |
| 574 |
g_assert_true (g_variant_lookup (nested, "string", "&s", &test_str)); |
| 575 |
g_assert_cmpstr (test_str, ==, "baz"); |
| 576 |
|
| 577 |
wplua_unref (L); |
| 578 |
} |
| 579 |
|
| 580 |
static void |
| 581 |
test_wplua_convert_gvariant_array () |
| 582 |
{ |
| 583 |
g_autoptr (GError) error = NULL; |
| 584 |
lua_State *L = wplua_new (); |
| 585 |
|
| 586 |
g_autoptr (GVariant) v = g_variant_new_parsed ("@av [ " |
| 587 |
"<42>, <3.14>, <'foobar'>, <true>, " |
| 588 |
"<@a{sv} { 'string': <'baz'> }> ]"); |
| 589 |
wplua_gvariant_to_lua (L, v); |
| 590 |
lua_setglobal (L, "o"); |
| 591 |
|
| 592 |
const gchar code2[] = |
| 593 |
"assert (o[1] == 42)\n" |
| 594 |
"assert (math.abs (o[2] - 3.14) < 0.0000000001)\n" |
| 595 |
"assert (o[3] == 'foobar')\n" |
| 596 |
"assert (o[4] == true)\n" |
| 597 |
"assert (o[5]['string'] == 'baz')\n"; |
| 598 |
test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); |
| 599 |
g_assert_no_error (error); |
| 600 |
|
| 601 |
lua_getglobal (L, "o"); |
| 602 |
g_autoptr (GVariant) fromlua = wplua_lua_to_gvariant (L, -1); |
| 603 |
|
| 604 |
gint64 test_int = 0; |
| 605 |
g_assert_true (g_variant_lookup (fromlua, "1", "x", &test_int)); |
| 606 |
g_assert_cmpint (test_int, ==, 42); |
| 607 |
|
| 608 |
gdouble test_double = 0; |
| 609 |
g_assert_true (g_variant_lookup (fromlua, "2", "d", &test_double)); |
| 610 |
g_assert_cmpfloat_with_epsilon (test_double, 3.14, 0.0000000001); |
| 611 |
|
| 612 |
const gchar *test_str = NULL; |
| 613 |
g_assert_true (g_variant_lookup (fromlua, "3", "&s", &test_str)); |
| 614 |
g_assert_cmpstr (test_str, ==, "foobar"); |
| 615 |
|
| 616 |
gboolean test_boolean = FALSE; |
| 617 |
g_assert_true (g_variant_lookup (fromlua, "4", "b", &test_boolean)); |
| 618 |
g_assert_true (test_boolean); |
| 619 |
|
| 620 |
g_autoptr (GVariant) nested = NULL; |
| 621 |
g_assert_true (g_variant_lookup (fromlua, "5", "@a{sv}", &nested)); |
| 622 |
g_assert_nonnull (nested); |
| 623 |
g_assert_true (g_variant_is_of_type (nested, G_VARIANT_TYPE_VARDICT)); |
| 624 |
|
| 625 |
test_str = NULL; |
| 626 |
g_assert_true (g_variant_lookup (nested, "string", "&s", &test_str)); |
| 627 |
g_assert_cmpstr (test_str, ==, "baz"); |
| 628 |
|
| 629 |
wplua_unref (L); |
| 630 |
} |
| 631 |
static void |
| 632 |
test_wplua_convert_wp_properties () |
| 633 |
{ |
| 634 |
g_autoptr (GError) error = NULL; |
| 635 |
lua_State *L = wplua_new (); |
| 636 |
|
| 637 |
const gchar code[] = |
| 638 |
"props = { " |
| 639 |
" ['test-int'] = 42, " |
| 640 |
" ['test-double'] = 3.14, " |
| 641 |
" ['test-string'] = 'foobar', " |
| 642 |
" ['test-boolean'] = false, " |
| 643 |
"}\n"; |
| 644 |
test_load_and_call (L, code, sizeof (code) - 1, 0, 0, &error); |
| 645 |
g_assert_no_error (error); |
| 646 |
|
| 647 |
lua_getglobal (L, "props"); |
| 648 |
g_autoptr (WpProperties) fromlua = wplua_table_to_properties (L, -1); |
| 649 |
|
| 650 |
g_assert_cmpstr (wp_properties_get (fromlua, "test-int"), ==, "42"); |
| 651 |
g_assert_cmpstr (wp_properties_get (fromlua, "test-double"), ==, "3.14"); |
| 652 |
g_assert_cmpstr (wp_properties_get (fromlua, "test-string"), ==, "foobar"); |
| 653 |
g_assert_cmpstr (wp_properties_get (fromlua, "test-boolean"), ==, "false"); |
| 654 |
|
| 655 |
lua_pop(L, 1); |
| 656 |
wplua_properties_to_table (L, fromlua); |
| 657 |
lua_setglobal (L, "fromc"); |
| 658 |
|
| 659 |
const gchar code2[] = |
| 660 |
"assert (fromc['test-string'] == 'foobar')\n" |
| 661 |
"assert (fromc['test-int'] == '42')\n" |
| 662 |
"assert (fromc['test-double'] == '3.14')\n" |
| 663 |
"assert (fromc['test-boolean'] == 'false')\n"; |
| 664 |
test_load_and_call (L, code2, sizeof (code2) - 1, 0, 0, &error); |
| 665 |
g_assert_no_error (error); |
| 666 |
|
| 667 |
wplua_unref (L); |
| 668 |
} |
| 669 |
|
| 670 |
static void |
| 671 |
test_wplua_script_arguments () |
| 672 |
{ |
| 673 |
g_autoptr (GError) error = NULL; |
| 674 |
lua_State *L = wplua_new (); |
| 675 |
|
| 676 |
g_autoptr (GVariant) v = g_variant_new_parsed ("@a{sv} { " |
| 677 |
"'test-int': <42>, " |
| 678 |
"'test-double': <3.14>, " |
| 679 |
"'test-string': <'foobar'>, " |
| 680 |
"'test-boolean': <true>, " |
| 681 |
"'nested-table': <@a{sv} { 'string': <'baz'> }> " |
| 682 |
"}"); |
| 683 |
wplua_gvariant_to_lua (L, v); |
| 684 |
|
| 685 |
const gchar code2[] = |
| 686 |
"local o = ...\n" |
| 687 |
"assert (o['test-string'] == 'foobar')\n" |
| 688 |
"assert (o['test-int'] == 42)\n" |
| 689 |
"assert (math.abs (o['test-double'] - 3.14) < 0.0000000001)\n" |
| 690 |
"assert (o['test-boolean'] == true)\n" |
| 691 |
"assert (o['nested-table']['string'] == 'baz')\n"; |
| 692 |
test_load_and_call (L, code2, sizeof (code2) - 1, 1, 0, &error); |
| 693 |
g_assert_no_error (error); |
| 694 |
|
| 695 |
/* same test, but with sandbox enabled */ |
| 696 |
wplua_enable_sandbox (L, WP_LUA_SANDBOX_ISOLATE_ENV); |
| 697 |
wplua_gvariant_to_lua (L, v); |
| 698 |
test_load_and_call (L, code2, sizeof (code2) - 1, 1, 0, &error); |
| 699 |
g_assert_no_error (error); |
| 700 |
|
| 701 |
wplua_unref (L); |
| 702 |
} |
| 703 |
|
| 704 |
gint |
| 705 |
main (gint argc, gchar *argv[]) |
| 706 |
{ |
| 707 |
g_test_init (&argc, &argv, NULL); |
| 708 |
wp_init (WP_INIT_ALL); |
| 709 |
|
| 710 |
g_test_add_func ("/wplua/basic", test_wplua_basic); |
| 711 |
g_test_add_func ("/wplua/construct", test_wplua_construct); |
| 712 |
g_test_add_func ("/wplua/properties", test_wplua_properties); |
| 713 |
g_test_add_func ("/wplua/closure", test_wplua_closure); |
| 714 |
g_test_add_func ("/wplua/signals", test_wplua_signals); |
| 715 |
g_test_add_func ("/wplua/sandbox/script", test_wplua_sandbox_script); |
| 716 |
g_test_add_func ("/wplua/sandbox/config", test_wplua_sandbox_config); |
| 717 |
g_test_add_func ("/wplua/convert/asv", test_wplua_convert_asv); |
| 718 |
g_test_add_func ("/wplua/convert/gvariant_array", |
| 719 |
test_wplua_convert_gvariant_array); |
| 720 |
g_test_add_func ("/wplua/convert/wp_properties", |
| 721 |
test_wplua_convert_wp_properties); |
| 722 |
g_test_add_func ("/wplua/script_arguments", test_wplua_script_arguments); |
| 723 |
|
| 724 |
return g_test_run (); |
| 725 |
} |