BVB Source Codes

cockpit Show test-bridge.c Source code

Return Download cockpit: download test-bridge.c Source code - Download cockpit Source code - Type:.c
  1. /*
  2.  * This file is part of Cockpit.
  3.  *
  4.  * Copyright (C) 2016 Red Hat, Inc.
  5.  *
  6.  * Cockpit is free software; you can redistribute it and/or modify it
  7.  * under the terms of the GNU Lesser General Public License as published by
  8.  * the Free Software Foundation; either version 2.1 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * Cockpit is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public License
  17.  * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include "common/cockpitjson.h"
  23. #include "common/cockpitpipe.h"
  24. #include "common/cockpitpipetransport.h"
  25. #include "common/cockpittest.h"
  26.  
  27. #include <string.h>
  28.  
  29. static gboolean
  30. on_recv_get_bytes (CockpitTransport *transport,
  31.                    const gchar *channel,
  32.                    GBytes *data,
  33.                    gpointer user_data)
  34. {
  35.   GBytes **result = (GBytes **)user_data;
  36.   g_assert_cmpstr (channel, ==, NULL);
  37.   g_assert (result != NULL);
  38.   g_assert (*result == NULL);
  39.   *result = g_bytes_ref (data);
  40.   return TRUE;
  41. }
  42.  
  43. static void
  44. on_closed_not_reached (CockpitTransport *transport,
  45.                        const gchar *problem)
  46. {
  47.   g_assert_cmpstr (problem, ==, NULL);
  48.   g_assert_not_reached ();
  49. }
  50.  
  51. static void
  52. test_bridge_init (void)
  53. {
  54.   CockpitTransport *transport;
  55.   CockpitPipe *pipe;
  56.   GBytes *bytes = NULL;
  57.   JsonObject *object;
  58.   JsonObject *os_release;
  59.   JsonObject *packages;
  60.   GError *error = NULL;
  61.   GList *list;
  62.  
  63.   const gchar *argv[] = {
  64.     BUILDDIR "/cockpit-bridge",
  65.     NULL
  66.   };
  67.  
  68.   pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE);
  69.   transport = cockpit_pipe_transport_new (pipe);
  70.   g_object_unref (pipe);
  71.  
  72.   g_signal_connect (transport, "recv", G_CALLBACK (on_recv_get_bytes), &bytes);
  73.   g_signal_connect (transport, "closed", G_CALLBACK (on_closed_not_reached), NULL);
  74.  
  75.   while (bytes == NULL)
  76.     g_main_context_iteration (NULL, TRUE);
  77.  
  78.   g_signal_handlers_disconnect_by_func (transport, on_recv_get_bytes, &bytes);
  79.   g_signal_handlers_disconnect_by_func (transport, on_closed_not_reached, NULL);
  80.  
  81.   g_object_unref (transport);
  82.  
  83.   object = cockpit_json_parse_bytes (bytes, &error);
  84.   g_assert_no_error (error);
  85.   g_bytes_unref (bytes);
  86.  
  87.   g_assert_cmpstr (json_object_get_string_member (object, "command"), ==, "init");
  88.  
  89.   /* Make sure we've included /etc/os-release information */
  90.   os_release = json_object_get_object_member (object, "os-release");
  91.   g_assert (os_release != NULL);
  92.   g_assert (json_object_has_member (os_release, "NAME"));
  93.  
  94.   /* Make sure we have right packages listed */
  95.   packages = json_object_get_object_member (object, "packages");
  96.   g_assert (packages != NULL);
  97.   list = json_object_get_members (packages);
  98.   list = g_list_sort (list, (GCompareFunc)strcmp);
  99.   g_assert_cmpuint (g_list_length (list), ==, 3);
  100.   g_assert_cmpstr (list->data, ==, "another");
  101.   g_assert_cmpstr (list->next->data, ==, "second");
  102.   g_assert_cmpstr (list->next->next->data, ==, "test");
  103.   g_list_free (list);
  104.  
  105.   json_object_unref (object);
  106. }
  107.  
  108. #if 0
  109. static void
  110. on_closed_get_problem (CockpitTransport *transport,
  111.                        const gchar *problem,
  112.                        gpointer user_data)
  113. {
  114.   gchar **result = (gchar **)user_data;
  115.   g_assert (result != NULL);
  116.   g_assert (*result == NULL);
  117.   g_assert (problem != NULL);
  118.   *result = g_strdup (problem);
  119. }
  120. #endif
  121.  
  122. static void
  123. on_closed_set_flag (CockpitTransport *transport,
  124.                     const gchar *problem,
  125.                     gpointer user_data)
  126. {
  127.   gboolean *flag = (gboolean *)user_data;
  128.   *flag = TRUE;
  129. }
  130.  
  131. static gboolean
  132. on_control_get_close (CockpitTransport *transport,
  133.                       const gchar *command,
  134.                       const gchar *channel,
  135.                       JsonObject *options,
  136.                       GBytes *payload,
  137.                       gpointer user_data)
  138. {
  139.   JsonObject **result = (JsonObject **)user_data;
  140.   g_assert (result != NULL);
  141.   g_assert (*result == NULL);
  142.   g_assert (command != NULL);
  143.   if (g_str_equal (command, "close"))
  144.     {
  145.       *result = json_object_ref (options);
  146.       return TRUE;
  147.     }
  148.   return FALSE;
  149. }
  150.  
  151. typedef struct {
  152.     const gchar *host;
  153.     guint64 version;
  154. } InitProblem;
  155.  
  156. static void
  157. test_bridge_init_problem (gconstpointer user_data)
  158. {
  159.   const InitProblem *fixture = user_data;
  160.   CockpitTransport *transport;
  161.   CockpitPipe *pipe;
  162.   GBytes *bytes;
  163.   gboolean closed = FALSE;
  164.   JsonObject *input;
  165.  
  166.   g_assert (fixture != NULL);
  167.  
  168.   const gchar *argv[] = {
  169.     BUILDDIR "/cockpit-bridge",
  170.     NULL
  171.   };
  172.  
  173.   pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE);
  174.   transport = cockpit_pipe_transport_new (pipe);
  175.   g_object_unref (pipe);
  176.  
  177.   /* First send the actual init message */
  178.   input = json_object_new ();
  179.   json_object_set_string_member (input, "command", "init");
  180.   if (fixture->version != 0)
  181.     json_object_set_int_member (input, "version", fixture->version);
  182.   if (fixture->host)
  183.     json_object_set_string_member (input, "host", fixture->host);
  184.   bytes = cockpit_json_write_bytes (input);
  185.   cockpit_transport_send (transport, NULL, bytes);
  186.   g_bytes_unref (bytes);
  187.   json_object_unref (input);
  188.  
  189.   /* The bridge should terminate */
  190.   g_signal_connect (transport, "closed", G_CALLBACK (on_closed_set_flag), &closed);
  191.  
  192.   while (!closed)
  193.     g_main_context_iteration (NULL, TRUE);
  194.  
  195.   g_signal_handlers_disconnect_by_func (transport, on_closed_set_flag, &closed);
  196.  
  197.   g_object_unref (transport);
  198.  
  199.   /* Just checking that it closes by itself here */
  200. }
  201.  
  202. typedef struct {
  203.     const gchar *host;
  204.     const gchar *open_host;
  205.     const gchar *problem;
  206. } OpenProblem;
  207.  
  208. static void
  209. test_bridge_open_problem (gconstpointer user_data)
  210. {
  211.   const OpenProblem *fixture = user_data;
  212.   CockpitTransport *transport;
  213.   CockpitPipe *pipe;
  214.   GBytes *bytes;
  215.   JsonObject *object = NULL;
  216.   JsonObject *input;
  217.  
  218.   g_assert (fixture != NULL);
  219.   g_assert (fixture->problem != NULL);
  220.  
  221.   const gchar *argv[] = {
  222.     BUILDDIR "/cockpit-bridge",
  223.     NULL
  224.   };
  225.  
  226.   pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE);
  227.   transport = cockpit_pipe_transport_new (pipe);
  228.   g_object_unref (pipe);
  229.  
  230.   /* First send the actual init message */
  231.   input = json_object_new ();
  232.   json_object_set_string_member (input, "command", "init");
  233.   json_object_set_int_member (input, "version", 1);
  234.   if (fixture->host)
  235.     json_object_set_string_member (input, "host", fixture->host);
  236.   bytes = cockpit_json_write_bytes (input);
  237.   cockpit_transport_send (transport, NULL, bytes);
  238.   g_bytes_unref (bytes);
  239.   json_object_unref (input);
  240.  
  241.   /* Next maybe send an open message */
  242.   input = json_object_new ();
  243.   json_object_set_string_member (input, "command", "open");
  244.   json_object_set_string_member (input, "channel", "444");
  245.   json_object_set_string_member (input, "payload", "null");
  246.   if (fixture->open_host)
  247.     json_object_set_string_member (input, "host", fixture->open_host);
  248.   bytes = cockpit_json_write_bytes (input);
  249.   cockpit_transport_send (transport, NULL, bytes);
  250.   g_bytes_unref (bytes);
  251.   json_object_unref (input);
  252.  
  253.   /* Listen for a close message */
  254.   g_signal_connect (transport, "control", G_CALLBACK (on_control_get_close), &object);
  255.  
  256.   while (object == NULL)
  257.     g_main_context_iteration (NULL, TRUE);
  258.  
  259.   g_signal_handlers_disconnect_by_func (transport, on_control_get_close, &object);
  260.  
  261.   g_object_unref (transport);
  262.  
  263.   g_assert_cmpstr (json_object_get_string_member (object, "problem"), ==, fixture->problem);
  264.   json_object_unref (object);
  265. }
  266.  
  267. static InitProblem bad_version = {
  268.     .version = 5,
  269. };
  270.  
  271. static InitProblem missing_version = {
  272.     .version = 0,
  273. };
  274.  
  275. static InitProblem missing_host = {
  276.     .version = 1,
  277. };
  278.  
  279. static OpenProblem wrong_host = {
  280.     .host = "marmalade",
  281.     .open_host = "juggs",
  282.     .problem = "not-supported",
  283. };
  284.  
  285. int
  286. main (int argc,
  287.       char *argv[])
  288. {
  289.   g_setenv ("XDG_DATA_DIRS", SRCDIR "/src/bridge/mock-resource/system", TRUE);
  290.   g_setenv ("XDG_DATA_HOME", SRCDIR "/src/bridge/mock-resource/home", TRUE);
  291.  
  292.   cockpit_test_init (&argc, &argv);
  293.  
  294.   g_test_add_func ("/bridge/init-message", test_bridge_init);
  295.   g_test_add_data_func ("/bridge/bad-version", &bad_version, test_bridge_init_problem);
  296.   g_test_add_data_func ("/bridge/missing-version", &missing_version, test_bridge_init_problem);
  297.   g_test_add_data_func ("/bridge/missing-host", &missing_host, test_bridge_init_problem);
  298.   g_test_add_data_func ("/bridge/wrong-host", &wrong_host, test_bridge_open_problem);
  299.  
  300.   return g_test_run ();
  301. }
  302.  
downloadtest-bridge.c Source code - Download cockpit Source code
Related Source Codes/Software:
elasticsearch-kopf - web admin interface for elasticsearch 2017-05-07
xunlei-lixian - Thunderbolt offline download script 2017-05-07
xadmin - Drop-in replacement of Django admin comes with lot... 2017-05-07
p2pspider - DHT Spider + BitTorrent Client = P2P Spider 2017-05-07
Backbone.localStorage - A localStorage adapter for Backbone.j 2017-05-07
phenomic - 2017-05-07
shoulda - Makes tests easy on the fingers and the eyes ... 2017-05-07
mio - Metal IO library for Rust 2017-05-07
nova - OpenStack Compute (Nova) ht... 2017-05-07
gobyexample - Go by Example https://gobye... 2017-05-07
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
huginn - Create agents that monitor and act on your behalf.... 2017-06-06
system-design-primer - Learn how to design large-scale systems. Prep for ... 2017-06-06
design-patterns-for-humans - Design Patterns for Humans - An ultra-simplified e... 2017-06-06
developer-roadmap - Roadmap to becoming a web developer in 2017 2017-06-06
nylas-mail - 2017-06-06

 Back to top