/* * Functions to load the symbols and init the environment */ static void init_syms(void) { if (real_open) return; VIR_MOCK_REAL_INIT(open); VIR_MOCK_REAL_INIT(opendir); VIR_MOCK_REAL_INIT(access); }
static void init_syms(void) { if (real_setmntent) return; VIR_MOCK_REAL_INIT(setmntent); VIR_MOCK_REAL_INIT(statfs); VIR_MOCK_REAL_INIT(canonicalize_file_name); }
static void init_syms(void) { if (real_fopen) return; VIR_MOCK_REAL_INIT(fopen); VIR_MOCK_REAL_INIT(access); VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat); VIR_MOCK_REAL_INIT_ALT(stat, __xstat); VIR_MOCK_REAL_INIT(mkdir); VIR_MOCK_REAL_INIT(open); }
VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, DBusMessage *, message, int, timeout_milliseconds, DBusError *, error) { DBusMessage *reply = NULL; const char *service = dbus_message_get_destination(message); const char *member = dbus_message_get_member(message); VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); if (STREQ(service, "org.freedesktop.machine1")) { if (getenv("FAIL_BAD_SERVICE")) { dbus_set_error_const(error, "org.freedesktop.systemd.badthing", "Something went wrong creating the machine"); } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } } else if (STREQ(service, "org.freedesktop.login1")) { char *supported = getenv("RESULT_SUPPORT"); DBusMessageIter iter; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &supported)) goto error; } else if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListActivatableNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = "org.freedesktop.machine1"; const char *svc3 = "org.freedesktop.login1"; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if (!getenv("FAIL_NO_SERVICE") && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; if (!getenv("FAIL_NO_SERVICE") && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc3)) goto error; dbus_message_iter_close_container(&iter, &sub); } else if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = "org.freedesktop.systemd1"; const char *svc3 = "org.freedesktop.login1"; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED")) && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; if ((!getenv("FAIL_NO_SERVICE") && !getenv("FAIL_NOT_REGISTERED")) && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc3)) goto error; dbus_message_iter_close_container(&iter, &sub); } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } return reply; error: dbus_message_unref(reply); return NULL; }
VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, DBusMessage *, message, int, timeout_milliseconds, DBusError *, error) { DBusMessage *reply = NULL; const char *service = dbus_message_get_destination(message); const char *member = dbus_message_get_member(message); VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); if (STREQ(service, "org.freedesktop.PolicyKit1") && STREQ(member, "CheckAuthorization")) { char *type; char *pidkey; unsigned int pidval; char *timekey; unsigned long long timeval; char *uidkey; int uidval; char *actionid; char **details; size_t detailslen; int allowInteraction; char *cancellationId; const char **retdetails = NULL; size_t retdetailslen = 0; const char *retdetailscancelled[] = { "polkit.dismissed", "true", }; int is_authorized = 1; int is_challenge = 0; if (virDBusMessageRead(message, "(sa{sv})sa&{ss}us", &type, 3, &pidkey, "u", &pidval, &timekey, "t", &timeval, &uidkey, "i", &uidval, &actionid, &detailslen, &details, &allowInteraction, &cancellationId) < 0) goto error; if (STREQ(actionid, "org.libvirt.test.success")) { is_authorized = 1; is_challenge = 0; } else if (STREQ(actionid, "org.libvirt.test.challenge")) { is_authorized = 0; is_challenge = 1; } else if (STREQ(actionid, "org.libvirt.test.cancelled")) { is_authorized = 0; is_challenge = 0; retdetails = retdetailscancelled; retdetailslen = ARRAY_CARDINALITY(retdetailscancelled) / 2; } else if (STREQ(actionid, "org.libvirt.test.details")) { size_t i; is_authorized = 0; is_challenge = 0; for (i = 0; i < detailslen / 2; i++) { if (STREQ(details[i * 2], "org.libvirt.test.person") && STREQ(details[(i * 2) + 1], "Fred")) { is_authorized = 1; is_challenge = 0; } } } else { is_authorized = 0; is_challenge = 0; } VIR_FREE(type); VIR_FREE(pidkey); VIR_FREE(timekey); VIR_FREE(uidkey); VIR_FREE(actionid); VIR_FREE(cancellationId); virStringListFreeCount(details, detailslen); if (virDBusCreateReply(&reply, "(bba&{ss})", is_authorized, is_challenge, retdetailslen, retdetails) < 0) goto error; } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } return reply; error: virDBusMessageUnref(reply); return NULL; }
VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block, DBusMessage *, DBusConnection *, connection, DBusMessage *, message, int, timeout_milliseconds, DBusError *, error) { DBusMessage *reply = NULL; const char *service = dbus_message_get_destination(message); const char *member = dbus_message_get_member(message); size_t i; size_t nargs = 0; char **args = NULL; char *type = NULL; VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block); if (STREQ(service, "org.freedesktop.DBus") && STREQ(member, "ListNames")) { const char *svc1 = "org.foo.bar.wizz"; const char *svc2 = VIR_FIREWALL_FIREWALLD_SERVICE; DBusMessageIter iter; DBusMessageIter sub; reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub); if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc1)) goto error; if (!fwDisabled && !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &svc2)) goto error; dbus_message_iter_close_container(&iter, &sub); } else if (STREQ(service, VIR_FIREWALL_FIREWALLD_SERVICE) && STREQ(member, "passthrough")) { bool isAdd = false; bool doError = false; if (virDBusMessageDecode(message, "sa&s", &type, &nargs, &args) < 0) goto error; for (i = 0; i < nargs; i++) { /* Fake failure on the command with this IP addr */ if (STREQ(args[i], "-A")) { isAdd = true; } else if (isAdd && STREQ(args[i], "192.168.122.255")) { doError = true; } } if (fwBuf) { if (STREQ(type, "ipv4")) virBufferAddLit(fwBuf, IPTABLES_PATH); else if (STREQ(type, "ipv4")) virBufferAddLit(fwBuf, IP6TABLES_PATH); else virBufferAddLit(fwBuf, EBTABLES_PATH); } for (i = 0; i < nargs; i++) { if (fwBuf) { virBufferAddLit(fwBuf, " "); virBufferEscapeShell(fwBuf, args[i]); } } if (fwBuf) virBufferAddLit(fwBuf, "\n"); if (doError) { dbus_set_error_const(error, "org.firewalld.error", "something bad happened"); } else { if (nargs == 1 && STREQ(type, "ipv4") && STREQ(args[0], "-L")) { if (virDBusCreateReply(&reply, "s", TEST_FILTER_TABLE_LIST) < 0) goto error; } else if (nargs == 3 && STREQ(type, "ipv4") && STREQ(args[0], "-t") && STREQ(args[1], "nat") && STREQ(args[2], "-L")) { if (virDBusCreateReply(&reply, "s", TEST_NAT_TABLE_LIST) < 0) goto error; } else { if (virDBusCreateReply(&reply, "s", "success") < 0) goto error; } } } else { reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN); } cleanup: VIR_FREE(type); for (i = 0; i < nargs; i++) VIR_FREE(args[i]); VIR_FREE(args); return reply; error: virDBusMessageUnref(reply); reply = NULL; if (error && !dbus_error_is_set(error)) dbus_set_error_const(error, "org.firewalld.error", "something unexpected happened"); goto cleanup; }