summaryrefslogtreecommitdiff
path: root/tests/mock-flatpak.c
blob: c340c781c93778aca04abd54febc8e1828a07e70 (plain)
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
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
 * Copyright © 2018-2021 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "libglnx.h"

#include <glib.h>
#include <gio/gio.h>

#include "flatpak-context-private.h"

int
main (int argc,
      char **argv)
{
  int i;

  g_info ("This is a mock implementation of `flatpak run` for the portal");

  for (i = 0; i < argc; i++)
    g_print ("argv[%d] = %s\n", i, argv[i]);

  for (i = 0; i < argc; i++)
    {
      if (g_str_has_prefix (argv[i], "--env-fd="))
        {
          g_autoptr(FlatpakContext) context = flatpak_context_new ();
          const char *value = argv[i] + strlen ("--env-fd=");
          g_autoptr(GError) error = NULL;
          guint64 fd;
          gchar *endptr;
          GHashTableIter iter;
          gpointer k, v;

          fd = g_ascii_strtoull (value, &endptr, 10);

          if (endptr == NULL || *endptr != '\0' || fd > G_MAXINT)
            g_error ("Not a valid file descriptor: %s", value);

          flatpak_context_parse_env_fd (context, (int) fd, &error);
          g_assert_no_error (error);

          g_hash_table_iter_init (&iter, context->env_vars);

          while (g_hash_table_iter_next (&iter, &k, &v))
            g_print ("env[%s] = %s\n", (const char *) k, (const char *) v);
        }
    }

  for (i = 0; i < 256; i++)
    {
      struct stat stat_buf;

      if (fstat (i, &stat_buf) < 0)
        {
          int saved_errno = errno;

          g_assert_cmpint (saved_errno, ==, EBADF);
        }
      else
        {
          g_print ("fd[%d] = (dev=%" G_GUINT64_FORMAT " ino=%" G_GUINT64_FORMAT ")\n",
                   i,
                   (guint64) stat_buf.st_dev,
                   (guint64) stat_buf.st_ino);
        }
    }

  return 0;
}