File: test-gauntlet.c

package info (click to toggle)
jsonrpc-glib 3.44.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: ansic: 3,704; makefile: 13; javascript: 5
file content (139 lines) | stat: -rw-r--r-- 4,692 bytes parent folder | download | duplicates (4)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* test-gauntlet.c
 *
 * Copyright © 2018 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
#include <glib-unix.h>
#include <jsonrpc-glib.h>
#include <signal.h>

static GMainLoop *main_loop;

static void
server_handle_call (JsonrpcClient *server,
                    const gchar   *method,
                    GVariant      *id,
                    GVariant      *params,
                    gpointer       user_data)
{
  g_assert (JSONRPC_IS_CLIENT (server));
  g_assert (g_str_equal (method, "do/something"));
  g_assert (id != NULL);
  g_assert (g_variant_is_of_type (id, G_VARIANT_TYPE_INT64));
  g_assert (params != NULL);
  g_assert (g_variant_is_of_type (params, G_VARIANT_TYPE_INT64));
  g_assert (g_variant_get_int64 (params) == 42);
  g_assert (user_data == NULL);

  jsonrpc_client_close (server, NULL, NULL);
}

static void
call_cb (GObject      *object,
         GAsyncResult *result,
         gpointer      user_data)
{
  g_autoptr(GVariant) res = NULL;
  g_autoptr(GError) error = NULL;
  gboolean r;

  r = jsonrpc_client_call_finish (JSONRPC_CLIENT (object), result, &res, &error);

  if (r == FALSE)
    {
      g_assert_cmpint (error->domain, ==, G_IO_ERROR);
#if 0
      /* We can't really guarantee this, given the ways the socket errors
       * can be propagated.
       */
      g_assert (error->code == 0 || error->code == G_IO_ERROR_NOT_CONNECTED);
#endif
    }

  g_main_loop_quit (main_loop);
}

static void
test_gauntlet (void)
{
  g_autoptr(JsonrpcClient) client = NULL;
  g_autoptr(JsonrpcClient) server = NULL;
  g_autoptr(GInputStream) input_a = NULL;
  g_autoptr(GInputStream) input_b = NULL;
  g_autoptr(GOutputStream) output_a = NULL;
  g_autoptr(GOutputStream) output_b = NULL;
  g_autoptr(GIOStream) stream_a = NULL;
  g_autoptr(GIOStream) stream_b = NULL;
  g_autoptr(GError) error = NULL;
  gboolean r;
  gint pair_a[2];
  gint pair_b[2];

  signal (SIGPIPE, SIG_IGN);

  main_loop = g_main_loop_new (NULL, FALSE);

  r = g_unix_open_pipe (pair_a, FD_CLOEXEC, &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);

  r = g_unix_open_pipe (pair_b, FD_CLOEXEC, &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);

  input_a = g_unix_input_stream_new (pair_a[0], TRUE);
  input_b = g_unix_input_stream_new (pair_b[0], TRUE);
  output_a = g_unix_output_stream_new (pair_a[1], TRUE);
  output_b = g_unix_output_stream_new (pair_b[1], TRUE);

  stream_a = g_simple_io_stream_new (input_a, output_b);
  stream_b = g_simple_io_stream_new (input_b, output_a);

  client = jsonrpc_client_new (stream_a);
  jsonrpc_client_start_listening (client);

  server = jsonrpc_client_new (stream_b);
  g_signal_connect (server,
                    "handle-call",
                    G_CALLBACK (server_handle_call),
                    NULL);
  jsonrpc_client_start_listening (server);

  /* Try a call, peer will close instead of replying */
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, call_cb, NULL);
  g_main_loop_run (main_loop);

  /* Try a call, peer is closed, expect failure */
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (42), NULL, NULL, NULL);
  jsonrpc_client_call_async (client, "do/something", g_variant_new_int32 (43), NULL, call_cb, NULL);
  g_main_loop_run (main_loop);
}

gint
main (gint   argc,
      gchar *argv[])
{
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/Jsonrpc/Client/gauntlet", test_gauntlet);
  return g_test_run ();
}