Debian Codegraph @

src/testlib/mock-transport.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "mock-transport.h"
9
10 #include "ws/cockpitjson.h"
11
12 #include <gio/gio.h>
13
14 typedef CockpitTransportClass MockTransportClass;
15
16 typedef struct {
17 gpointer data;
18 GDestroyNotify func;
19 } Trash;
20
21 G_DEFINE_TYPE (MockTransport, mock_transport, COCKPIT_TYPE_TRANSPORT);
22
23 static void
24 trash_free (gpointer data)
25 {
26 Trash *trash = data;
27 (trash->func) (trash->data);
28 g_free (trash);
29 }
30
31 static void
32 trash_push (MockTransport *self,
33 gpointer data,
34 GDestroyNotify func)
35 {
36 Trash *trash = g_new0 (Trash, 1);
37 g_assert (func != NULL);
38 trash->data = data;
39 trash->func = func;
40 self->trash = g_list_prepend (self->trash, trash);
41 }
42
43 static void
44 mock_transport_init (MockTransport *self)
45 {
46 self->channels = g_hash_table_new_full (g_str_hash, g_str_equal,
47 g_free, (GDestroyNotify)g_queue_free);
48 self->control = g_queue_new ();
49 }
50
51 static void
52 mock_transport_get_property (GObject *object,
53 guint prop_id,
54 GValue *value,
55 GParamSpec *pspec)
56 {
57 switch (prop_id)
58 {
59 case 1:
60 g_value_set_string (value, "mock-name");
61 break;
62 default:
63 g_assert_not_reached ();
64 break;
65 }
66 }
67
68 static void
69 mock_transport_set_property (GObject *object,
70 guint prop_id,
71 const GValue *value,
72 GParamSpec *pspec)
73 {
74 switch (prop_id)
75 {
76 case 1:
77 break;
78 default:
79 g_assert_not_reached ();
80 break;
81 }
82 }
83
84 static void
85 mock_transport_finalize (GObject *object)
86 {
87 MockTransport *self = (MockTransport *)object;
88
89 g_free (self->problem);
90 g_queue_free (self->control);
91 g_list_free_full (self->trash, trash_free);
92 g_hash_table_destroy (self->channels);
93
94 G_OBJECT_CLASS (mock_transport_parent_class)->finalize (object);
95 }
96
97 static void
98 mock_transport_send (CockpitTransport *transport,
99 const gchar *channel_id,
100 GBytes *data)
101 {
102 MockTransport *self = (MockTransport *)transport;
103 JsonObject *object;
104 GError *error = NULL;
105 GQueue *queue;
106
107 if (!channel_id)
108 {
109 object = cockpit_json_parse_bytes (data, &error);
110 g_assert_no_error (error);
111 g_queue_push_tail (self->control, object);
112 trash_push (self, object, (GDestroyNotify)json_object_unref);
113 }
114 else
115 {
116 queue = g_hash_table_lookup (self->channels, channel_id);
117 if (!queue)
118 {
119 queue = g_queue_new ();
120 g_hash_table_insert (self->channels, g_strdup (channel_id), queue);
121 }
122 g_queue_push_tail (queue, g_bytes_ref (data));
123 trash_push (self, data, (GDestroyNotify)g_bytes_unref);
124 }
125 self->count++;
126 }
127
128 static void
129 mock_transport_close (CockpitTransport *transport,
130 const gchar *problem)
131 {
132 MockTransport *self = (MockTransport *)transport;
133 g_assert (!self->closed);
134 self->problem = g_strdup (problem);
135 self->closed = TRUE;
136 cockpit_transport_emit_closed (transport, problem);
137 }
138
139 static void
140 mock_transport_class_init (MockTransportClass *klass)
141 {
142 GObjectClass *object_class = G_OBJECT_CLASS (klass);
143 CockpitTransportClass *transport_class = COCKPIT_TRANSPORT_CLASS (klass);
144 object_class->finalize = mock_transport_finalize;
145 object_class->get_property = mock_transport_get_property;
146 object_class->set_property = mock_transport_set_property;
147 g_object_class_override_property (object_class, 1, "name");
148 transport_class->send = mock_transport_send;
149 transport_class->close = mock_transport_close;
150 }
151
152 MockTransport *
153 mock_transport_new (void)
154 {
155 return g_object_new (MOCK_TYPE_TRANSPORT, NULL);
156 }
157
158 GBytes *
159 mock_transport_pop_channel (MockTransport *mock,
160 const gchar *channel_id)
161 {
162 GQueue *queue;
163
164 g_assert (channel_id != NULL);
165
166 queue = g_hash_table_lookup (mock->channels, channel_id);
167 if (queue)
168 return g_queue_pop_head (queue);
169 return NULL;
170 }
171
172 JsonObject *
173 mock_transport_pop_control (MockTransport *mock)
174 {
175 return g_queue_pop_head (mock->control);
176 }
177
178 guint
179 mock_transport_count_sent (MockTransport *mock)
180 {
181 return mock->count;
182 }
183
184 GBytes *
185 mock_transport_combine_output (MockTransport *transport,
186 const gchar *channel_id,
187 guint *count)
188 {
189 GByteArray *combined;
190 GBytes *block;
191
192 if (count)
193 *count = 0;
194
195 combined = g_byte_array_new ();
196 for (;;)
197 {
198 block = mock_transport_pop_channel (transport, channel_id);
199 if (!block)
200 break;
201
202 g_byte_array_append (combined, g_bytes_get_data (block, NULL), g_bytes_get_size (block));
203 if (count)
204 (*count)++;
205 }
206 return g_byte_array_free_to_bytes (combined);
207 }