Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make XdpAppInfo more testable #1627

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
1 change: 0 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ xdp_utils_sources = files(
'xdp-app-info-host.c',
'xdp-app-info-flatpak.c',
'xdp-app-info-snap.c',
'xdp-app-info-test.c',
'xdp-usb-query.c',
)

Expand Down
1 change: 0 additions & 1 deletion src/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <stdio.h>

#include "xdp-app-info-private.h"
#include "xdp-app-info-test-private.h"
#include "xdp-host-dbus.h"
#include "xdp-utils.h"

Expand Down
4 changes: 0 additions & 4 deletions src/xdp-app-info-flatpak-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ G_DECLARE_FINAL_TYPE (XdpAppInfoFlatpak,
XDP, APP_INFO_FLATPAK,
XdpAppInfo)

gboolean xdp_is_flatpak (int pid,
gboolean *is_flatpak,
GError **error);

XdpAppInfo * xdp_app_info_flatpak_new (int pid,
int pidfd,
GError **error);
116 changes: 92 additions & 24 deletions src/xdp-app-info-flatpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ flatpak_is_valid_name (const char *string)
return TRUE;
}

gboolean
static gboolean
xdp_app_info_flatpak_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
Expand Down Expand Up @@ -208,11 +208,11 @@ xdp_app_info_flatpak_remap_path (XdpAppInfo *app_info,
NULL);

/* For apps we translate /app and /usr to the installed locations.
Also, we need to rewrite to drop the /newroot prefix added by
bubblewrap for other files to work. See
https://github.com/projectatomic/bubblewrap/pull/172
for a bit more information on the /newroot issue.
*/
* Also, we need to rewrite to drop the /newroot prefix added by
* bubblewrap for other files to work. See
* https://github.com/projectatomic/bubblewrap/pull/172
* for a bit more information on the /newroot issue.
*/

if (g_str_has_prefix (path, "/newroot/"))
path = path + strlen ("/newroot");
Expand Down Expand Up @@ -697,24 +697,69 @@ open_flatpak_info (int pid,
return g_steal_fd (&info_fd);
}

gboolean
xdp_is_flatpak (int pid,
gboolean *is_flatpak,
GError **error)
static XdpAppInfo *
xdp_app_info_flatpak_new_testing (int pid,
int pidfd,
GError **error)
{
g_autoptr(GError) local_error = NULL;
g_autofd int info_fd = -1;
g_autoptr (XdpAppInfoFlatpak) app_info_flatpak = NULL;
const char *app_id;
const char *instance_id;
const char *metadata_path;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;
g_autoptr(GKeyFile) metadata = NULL;
gboolean result;
g_auto(GStrv) shared = NULL;
gboolean has_network;
XdpAppInfoFlags flags = 0;

app_id = g_getenv ("XDG_DESKTOP_PORTAL_TEST_APP_ID");
g_assert (app_id != NULL);
instance_id = g_getenv ("XDG_DESKTOP_PORTAL_TEST_INSTANCE_ID");
g_assert (instance_id != NULL);
metadata_path = g_getenv ("XDG_DESKTOP_PORTAL_TEST_FLATPAK_METADATA");
g_assert (metadata_path != NULL);

info_fd = open_flatpak_info (pid, &local_error);
if (info_fd == -1 && !g_error_matches (local_error, XDP_APP_INFO_ERROR,
XDP_APP_INFO_ERROR_WRONG_APP_KIND))
desktop_id = g_strconcat (app_id, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));
g_assert (gappinfo != NULL);

metadata = g_key_file_new ();
result = g_key_file_load_from_file (metadata, metadata_path,
G_KEY_FILE_NONE, NULL);
g_assert (result == TRUE);

shared = g_key_file_get_string_list (metadata,
FLATPAK_METADATA_GROUP_CONTEXT,
FLATPAK_METADATA_KEY_SHARED,
NULL, NULL);

if (shared)
{
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
has_network = g_strv_contains ((const char * const *)shared,
FLATPAK_METADATA_CONTEXT_SHARED_NETWORK);
}
else
{
has_network = FALSE;
}

*is_flatpak = info_fd != -1;
return TRUE;
flags |= XDP_APP_INFO_FLAG_SUPPORTS_OPATH;
if (has_network)
flags |= XDP_APP_INFO_FLAG_HAS_NETWORK;

app_info_flatpak = g_object_new (XDP_TYPE_APP_INFO_FLATPAK, NULL);
app_info_flatpak->flatpak_info = g_steal_pointer (&metadata);
xdp_app_info_set_identity (XDP_APP_INFO (app_info_flatpak),
FLATPAK_ENGINE_ID,
app_id,
instance_id);
xdp_app_info_set_pidfd (XDP_APP_INFO (app_info_flatpak), pidfd);
xdp_app_info_set_gappinfo (XDP_APP_INFO (app_info_flatpak), gappinfo);
xdp_app_info_set_flags (XDP_APP_INFO (app_info_flatpak), flags);

return XDP_APP_INFO (g_steal_pointer (&app_info_flatpak));
}

XdpAppInfo *
Expand All @@ -723,6 +768,7 @@ xdp_app_info_flatpak_new (int pid,
GError **error)
{
g_autoptr (XdpAppInfoFlatpak) app_info_flatpak = NULL;
XdpAppInfoFlags flags = 0;
g_autofd int info_fd = -1;
struct stat stat_buf;
g_autoptr(GError) local_error = NULL;
Expand All @@ -736,6 +782,20 @@ xdp_app_info_flatpak_new (int pid,
g_auto(GStrv) shared = NULL;
gboolean has_network;
g_autofd int bwrap_pidfd = -1;
const char *test_app_info_kind;

test_app_info_kind = g_getenv ("XDG_DESKTOP_PORTAL_TEST_APP_INFO_KIND");
if (test_app_info_kind)
{
if (g_strcmp0 (test_app_info_kind, "flatpak") != 0)
{
g_set_error (error, XDP_APP_INFO_ERROR, XDP_APP_INFO_ERROR_WRONG_APP_KIND,
"Testing requested different AppInfo kind");
return NULL;
}

return xdp_app_info_flatpak_new_testing (pid, pidfd, error);
}

info_fd = open_flatpak_info (pid, error);
if (info_fd == -1)
Expand Down Expand Up @@ -807,19 +867,27 @@ xdp_app_info_flatpak_new (int pid,
/* flatpak has a xdg-dbus-proxy running which means we can't get the pidfd
* of the connected process but we can get the pidfd of the bwrap instance
* instead. This is okay because it has the same namespaces as the calling
* process. */
* process.
*/
bwrap_pidfd = get_bwrap_pidfd (instance, error);
if (bwrap_pidfd == -1)
return NULL;

/* TODO: we can use pidfd to make sure we didn't race for sure */

flags |= XDP_APP_INFO_FLAG_SUPPORTS_OPATH;
if (has_network)
flags |= XDP_APP_INFO_FLAG_HAS_NETWORK;

app_info_flatpak = g_object_new (XDP_TYPE_APP_INFO_FLATPAK, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_flatpak),
FLATPAK_ENGINE_ID, id, instance,
bwrap_pidfd, gappinfo,
TRUE, has_network, TRUE);
app_info_flatpak->flatpak_info = g_steal_pointer (&metadata);
xdp_app_info_set_identity (XDP_APP_INFO (app_info_flatpak),
FLATPAK_ENGINE_ID,
id,
instance);
xdp_app_info_set_pidfd (XDP_APP_INFO (app_info_flatpak), bwrap_pidfd);
xdp_app_info_set_gappinfo (XDP_APP_INFO (app_info_flatpak), gappinfo);
xdp_app_info_set_flags (XDP_APP_INFO (app_info_flatpak), flags);

return XDP_APP_INFO (g_steal_pointer (&app_info_flatpak));
}
37 changes: 25 additions & 12 deletions src/xdp-app-info-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ xdp_app_info_host_get_usb_queries (XdpAppInfo *app_info)
return app_info_host->usb_queries;
}

gboolean
static gboolean
xdp_app_info_host_is_valid_sub_app_id (XdpAppInfo *app_info,
const char *sub_app_id)
{
return TRUE;
}

gboolean
static gboolean
xdp_app_info_host_validate_autostart (XdpAppInfo *app_info,
GKeyFile *keyfile,
const char * const *autostart_exec,
Expand All @@ -61,7 +61,7 @@ xdp_app_info_host_validate_autostart (XdpAppInfo *app_info,
return TRUE;
}

gboolean
static gboolean
xdp_app_info_host_validate_dynamic_launcher (XdpAppInfo *app_info,
GKeyFile *key_file,
GError **error)
Expand Down Expand Up @@ -193,13 +193,12 @@ xdp_app_info_host_new_full (const char *app_id,
XdpAppInfoHost *app_info_host;

app_info_host = g_object_new (XDP_TYPE_APP_INFO_HOST, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_host),
/* engine, app id, instance */
NULL, app_id, NULL,
pidfd, gappinfo,
/* supports_opath */ TRUE,
/* has_network */ TRUE,
/* requires_pid_mapping */ FALSE);
xdp_app_info_set_identity (XDP_APP_INFO (app_info_host), NULL, app_id, NULL);
xdp_app_info_set_pidfd (XDP_APP_INFO (app_info_host), pidfd);
xdp_app_info_set_gappinfo (XDP_APP_INFO (app_info_host), gappinfo);
xdp_app_info_set_flags (XDP_APP_INFO (app_info_host),
XDP_APP_INFO_FLAG_HAS_NETWORK |
XDP_APP_INFO_FLAG_SUPPORTS_OPATH);

return app_info_host;
}
Expand Down Expand Up @@ -228,11 +227,25 @@ XdpAppInfo *
xdp_app_info_host_new (int pid,
int pidfd)
{
g_autofree char *app_id = NULL;
const char *test_app_info_kind = NULL;
g_autofree char *owned_app_id = NULL;
const char *app_id = NULL;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;

app_id = get_appid_from_pid (pid);
test_app_info_kind = g_getenv ("XDG_DESKTOP_PORTAL_TEST_APP_INFO_KIND");
if (test_app_info_kind)
{
g_assert (g_strcmp0 (test_app_info_kind, "host") == 0);

app_id = g_getenv ("XDG_DESKTOP_PORTAL_TEST_APP_ID");
}
else
{
owned_app_id = get_appid_from_pid (pid);
app_id = owned_app_id;
}

desktop_id = g_strconcat (app_id, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));

Expand Down
28 changes: 19 additions & 9 deletions src/xdp-app-info-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

#include "xdp-app-info.h"

typedef enum
{
XDP_APP_INFO_FLAG_HAS_NETWORK = (1 << 0),
XDP_APP_INFO_FLAG_SUPPORTS_OPATH = (1 << 1),
} XdpAppInfoFlags;

struct _XdpAppInfoClass
{
GObjectClass parent_class;
Expand All @@ -46,12 +52,16 @@ struct _XdpAppInfoClass
GError **error);
};

void xdp_app_info_initialize (XdpAppInfo *app_info,
const char *engine,
const char *app_id,
const char *instance,
int pidfd,
GAppInfo *gappinfo,
gboolean supports_opath,
gboolean has_network,
gboolean requires_pid_mapping);
void xdp_app_info_set_identity (XdpAppInfo *app_info,
const char *engine,
const char *app_id,
const char *instance);

void xdp_app_info_set_pidfd (XdpAppInfo *app_info,
int pidfd);

void xdp_app_info_set_gappinfo (XdpAppInfo *app_info,
GAppInfo *gappinfo);

void xdp_app_info_set_flags (XdpAppInfo *app_info,
XdpAppInfoFlags flags);
4 changes: 0 additions & 4 deletions src/xdp-app-info-snap-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ G_DECLARE_FINAL_TYPE (XdpAppInfoSnap,
XDP, APP_INFO_SNAP,
XdpAppInfo)

gboolean xdp_is_snap (int pid,
gboolean *is_snap,
GError **error);

XdpAppInfo * xdp_app_info_snap_new (int pid,
int pidfd,
GError **error);
Expand Down
Loading
Loading