test-server.c 6.84 KB
Newer Older
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
/* test-server.c
 *
 * Copyright (C) 2016 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/>.
 */

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#include <fcntl.h>
#include <gio/gio.h>
#include <gio/gunixoutputstream.h>
#include <gio/gunixinputstream.h>
27
#include <glib-unix.h>
28
#include <jsonrpc-glib.h>
29
#include <signal.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <unistd.h>

static void
handle_notification (JsonrpcServer *server,
                     JsonrpcClient *client,
                     const gchar   *method,
                     JsonNode      *params,
                     gpointer       user_data)
{
  gint *count = user_data;

  g_assert (JSONRPC_IS_SERVER (server));
  g_assert (JSONRPC_IS_CLIENT (client));
  g_assert (method != NULL);
  g_assert (params != NULL);

  (*count)++;
}

static gboolean
handle_call (JsonrpcServer *server,
             JsonrpcClient *client,
             const gchar   *method,
53 54
             GVariant      *id,
             GVariant      *params,
55 56 57 58
             gpointer       user_data)
{
  const gchar *rootPath = NULL;
  g_autoptr(GError) error = NULL;
59
  g_auto(GVariantDict) dict = {{{0}}};
60
  gboolean r;
61

62 63 64 65
  g_assert (id != NULL);
  g_assert (params != NULL);
  g_assert (g_variant_is_of_type (id, G_VARIANT_TYPE_INT64));
  g_assert (g_variant_is_of_type (params, G_VARIANT_TYPE_VARDICT));
66

67 68 69 70 71
  g_variant_dict_init (&dict, params);
  r = g_variant_dict_lookup (&dict, "rootPath", "&s", &rootPath);
  g_assert_cmpint (r, ==, TRUE);
  g_assert_cmpstr (rootPath, ==, ".");
  g_variant_dict_clear (&dict);
72

73
  g_assert_cmpint (1, ==, g_variant_get_int64 (id));
74 75
  g_assert_cmpstr (method, ==, "initialize");

76 77 78
  g_variant_dict_init (&dict, NULL);
  g_variant_dict_insert (&dict, "foo", "s", "bar");
  r = jsonrpc_client_reply (client, id, g_variant_dict_end (&dict), NULL, &error);
79
  g_assert_no_error (error);
80
  g_assert_cmpint (r, ==, TRUE);
81 82 83 84

  return TRUE;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static void
do_something_handler (JsonrpcServer *server,
                      JsonrpcClient *client,
                      const gchar   *method,
                      GVariant      *id,
                      GVariant      *params,
                      gpointer       user_data)
{
  g_autoptr(GError) error = NULL;
  gint *count = user_data;
  gboolean r;

  g_assert_cmpstr (method, ==, "do/something");

  (*count)++;

  g_assert (g_variant_is_of_type (params, G_VARIANT_TYPE_STRING));
  g_assert_cmpstr (g_variant_get_string (params, NULL), ==, "do/something/message");

  r = jsonrpc_client_reply (client, id, g_variant_new_boolean (TRUE), NULL, &error);
  g_assert_no_error (error);
  g_assert (r);
}

109
static void
110
test_basic (gboolean use_gvariant)
111 112 113 114 115 116 117 118 119
{
  g_autoptr(JsonrpcServer) server = NULL;
  g_autoptr(JsonrpcClient) client = 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;
120 121
  g_autoptr(GVariant) message = NULL;
  g_autoptr(GVariant) return_value = NULL;
122
  g_autoptr(GError) error = NULL;
123
  GVariantDict dict;
124 125
  guint handler_id;
  gulong handle_call_id;
126 127 128 129 130
  gint pair_a[2];
  gint pair_b[2];
  gint count = 0;
  gint r;

131 132
  signal (SIGPIPE, SIG_IGN);

133 134 135
  r = g_unix_open_pipe (pair_a, FD_CLOEXEC, &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);
136

137 138 139
  r = g_unix_open_pipe (pair_b, FD_CLOEXEC, &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);
140 141 142 143 144 145 146 147 148 149 150

  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);

151 152 153
  /* Possibly upgrade connection to gvariant encoding */
  jsonrpc_client_set_use_gvariant (client, use_gvariant);

154 155 156
  server = jsonrpc_server_new ();
  jsonrpc_server_accept_io_stream (server, stream_b);

157 158 159 160
  handle_call_id = g_signal_connect (server,
                                     "handle-call",
                                     G_CALLBACK (handle_call),
                                     NULL);
161 162 163 164 165 166

  g_signal_connect (server,
                    "notification",
                    G_CALLBACK (handle_notification),
                    &count);

167
  r = jsonrpc_client_send_notification (client, "testNotification", NULL, NULL, &error);
168 169 170
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);

171 172 173
  g_variant_dict_init (&dict, NULL);
  g_variant_dict_insert (&dict, "rootPath", "s", ".");
  message = g_variant_dict_end (&dict);
174 175 176 177 178 179 180 181 182 183

  r = jsonrpc_client_call (client,
                           "initialize",
                           g_steal_pointer (&message),
                           NULL,
                           &return_value,
                           &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);
  g_assert (return_value != NULL);
184
  g_clear_pointer (&return_value, g_variant_unref);
185 186

  g_assert_cmpint (count, ==, 1);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  g_signal_handler_disconnect (server, handle_call_id);

  handler_id = jsonrpc_server_add_handler (server,
                                           "do/something",
                                           do_something_handler,
                                           &count,
                                           NULL);

  message = g_variant_new_string ("do/something/message");

  r = jsonrpc_client_call (client,
                           "do/something",
                           g_steal_pointer (&message),
                           NULL,
                           &return_value,
                           &error);
  g_assert_no_error (error);
  g_assert_cmpint (r, ==, TRUE);
  g_assert (return_value != NULL);
207
  g_clear_pointer (&return_value, g_variant_unref);
208 209 210 211

  g_assert_cmpint (count, ==, 2);

  jsonrpc_server_remove_handler (server, handler_id);
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225
static void
test_basic_json (void)
{
  test_basic (FALSE);
}

static void
test_basic_gvariant (void)
{
  test_basic (TRUE);
}

226 227 228 229 230
gint
main (gint   argc,
      gchar *argv[])
{
  g_test_init (&argc, &argv, NULL);
231 232
  g_test_add_func ("/Jsonrpc/Server/json", test_basic_json);
  g_test_add_func ("/Jsonrpc/Server/gvariant", test_basic_gvariant);
233 234
  return g_test_run ();
}