• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ubus/tests/test-reconnect-events.c

  1 #define _GNU_SOURCE
  2 #include <poll.h>
  3 #include <string.h>
  4 
  5 #include "libubus.h"
  6 #include "test-ubusd.h"
  7 
  8 struct counted_handler {
  9         struct ubus_event_handler ev;
 10         int count_old;
 11         int count_new;
 12         int count_other;
 13 };
 14 
 15 static void counted_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
 16                        const char *type, struct blob_attr *msg)
 17 {
 18         struct counted_handler *h = container_of(ev, struct counted_handler, ev);
 19 
 20         if (!strcmp(type, "test.old"))
 21                 h->count_old++;
 22         else if (!strcmp(type, "test.new"))
 23                 h->count_new++;
 24         else
 25                 h->count_other++;
 26 }
 27 
 28 static void counted_init(struct counted_handler *h)
 29 {
 30         memset(h, 0, sizeof(*h));
 31         h->ev.cb = counted_cb;
 32 }
 33 
 34 static void events_read(struct ubus_context *ctx)
 35 {
 36         struct pollfd pfd = { .fd = ctx->sock.fd, .events = POLLIN };
 37 
 38         while (poll(&pfd, 1, 200) > 0)
 39                 ubus_handle_event(ctx);
 40 }
 41 
 42 /* Another client sends the event, and ctx reads what ubusd passes on. */
 43 static void event_send(struct ubus_context *ctx, const char *type)
 44 {
 45         struct ubus_context *tx = ubus_connect(sock_path);
 46         struct blob_buf b = {};
 47 
 48         CHECK(tx);
 49         if (!tx)
 50                 return;
 51 
 52         blob_buf_init(&b, 0);
 53         CHECK(!ubus_send_event(tx, type, b.head));
 54         ubus_free(tx);
 55         blob_buf_free(&b);
 56 
 57         events_read(ctx);
 58 }
 59 
 60 static void ubusd_restart(struct ubus_context *ctx)
 61 {
 62         ubusd_kill();
 63         ubusd_start();
 64         CHECK(!ubus_reconnect(ctx, sock_path));
 65 }
 66 
 67 /* A handler with two patterns gets both events after a reconnect. */
 68 static void test_patterns_after_reconnect(void)
 69 {
 70         struct counted_handler h;
 71         struct ubus_context *ctx;
 72 
 73         counted_init(&h);
 74         ubusd_start();
 75         ctx = ubus_connect(sock_path);
 76         CHECK(ctx);
 77         if (!ctx)
 78                 return;
 79 
 80         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.old"));
 81         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
 82         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
 83 
 84         ubusd_restart(ctx);
 85         CHECK(h.ev.obj.id);
 86         event_send(ctx, "test.old");
 87         event_send(ctx, "test.new");
 88         event_send(ctx, "test.other");
 89         CHECK(h.count_old == 1);
 90         CHECK(h.count_new == 1);
 91         CHECK(!h.count_other);
 92 
 93         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
 94         ubus_free(ctx);
 95         ubusd_kill();
 96 }
 97 
 98 /*
 99  * The handler object is removed, then added again by the caller and given
100  * another pattern. A reconnect registers only the new pattern.
101  */
102 static void test_removed_handler_patterns_dropped(void)
103 {
104         struct counted_handler h;
105         struct ubus_context *ctx;
106 
107         counted_init(&h);
108         ubusd_start();
109         ctx = ubus_connect(sock_path);
110         CHECK(ctx);
111         if (!ctx)
112                 return;
113 
114         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.old"));
115         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
116         CHECK(!h.ev.obj.id);
117 
118         CHECK(!ubus_add_object(ctx, &h.ev.obj));
119         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
120 
121         ubusd_restart(ctx);
122         event_send(ctx, "test.old");
123         event_send(ctx, "test.new");
124         CHECK(!h.count_old);
125         CHECK(h.count_new == 1);
126 
127         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
128         ubus_free(ctx);
129         ubusd_kill();
130 }
131 
132 /*
133  * The remove of the handler object finds the connection lost. The object is
134  * forgotten with its pattern, and a later reconnect does not register it.
135  */
136 static void test_lost_handler_patterns_dropped(void)
137 {
138         struct counted_handler h;
139         struct ubus_context *ctx;
140 
141         counted_init(&h);
142         ubusd_start();
143         ctx = ubus_connect(sock_path);
144         CHECK(ctx);
145         if (!ctx)
146                 return;
147 
148         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.old"));
149         ubusd_kill();
150         CHECK(ubus_unregister_event_handler(ctx, &h.ev) ==
151               UBUS_STATUS_INVALID_ARGUMENT);
152         CHECK(!h.ev.obj.id);
153         CHECK(!ctx->objects.count);
154 
155         ubusd_start();
156         CHECK(!ubus_reconnect(ctx, sock_path));
157         CHECK(!ubus_add_object(ctx, &h.ev.obj));
158         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
159 
160         ubusd_restart(ctx);
161         event_send(ctx, "test.old");
162         event_send(ctx, "test.new");
163         CHECK(!h.count_old);
164         CHECK(h.count_new == 1);
165 
166         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
167         ubus_free(ctx);
168         ubusd_kill();
169 }
170 
171 /*
172  * Two contexts of one process each hold a handler. ubus_free() of one
173  * context leaves the patterns of the other, and a context that is shut down
174  * and connected again starts with no patterns.
175  */
176 static void test_contexts_apart(void)
177 {
178         static struct ubus_context reused;
179         struct counted_handler h1, h2, h3;
180         struct ubus_context *ctx1, *ctx2;
181 
182         counted_init(&h1);
183         counted_init(&h2);
184         counted_init(&h3);
185         ubusd_start();
186         ctx1 = ubus_connect(sock_path);
187         ctx2 = ubus_connect(sock_path);
188         CHECK(ctx1 && ctx2);
189         if (!ctx1 || !ctx2)
190                 return;
191 
192         CHECK(!ubus_register_event_handler(ctx1, &h1.ev, "test.old"));
193         CHECK(!ubus_register_event_handler(ctx2, &h2.ev, "test.new"));
194         ubus_free(ctx2);
195 
196         CHECK(!ubus_connect_ctx(&reused, sock_path));
197         CHECK(!ubus_register_event_handler(&reused, &h3.ev, "test.old"));
198         ubus_shutdown(&reused);
199         CHECK(!ubus_connect_ctx(&reused, sock_path));
200         h3.ev.obj.id = 0;
201         CHECK(!ubus_add_object(&reused, &h3.ev.obj));
202         CHECK(!ubus_register_event_handler(&reused, &h3.ev, "test.new"));
203 
204         ubusd_kill();
205         ubusd_start();
206         CHECK(!ubus_reconnect(ctx1, sock_path));
207         CHECK(!ubus_reconnect(&reused, sock_path));
208 
209         event_send(ctx1, "test.old");
210         events_read(&reused);
211         event_send(ctx1, "test.new");
212         events_read(&reused);
213         CHECK(h1.count_old == 1);
214         CHECK(!h1.count_new);
215         CHECK(!h2.count_old && !h2.count_new);
216         CHECK(!h3.count_old);
217         CHECK(h3.count_new == 1);
218 
219         CHECK(!ubus_unregister_event_handler(ctx1, &h1.ev));
220         CHECK(!ubus_unregister_event_handler(&reused, &h3.ev));
221         ubus_free(ctx1);
222         ubus_shutdown(&reused);
223         ubusd_kill();
224 }
225 
226 static int new_obj_count;
227 
228 static bool auto_new_obj_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
229                             const char *path)
230 {
231         if (!strcmp(path, "test.late"))
232                 new_obj_count++;
233 
234         return false;
235 }
236 
237 static int dummy_handler(struct ubus_context *ctx, struct ubus_object *obj,
238                          struct ubus_request_data *req, const char *method,
239                          struct blob_attr *msg)
240 {
241         return 0;
242 }
243 
244 static const struct ubus_method test_methods[] = {
245         UBUS_METHOD_NOARG("dummy", dummy_handler),
246 };
247 
248 static struct ubus_object_type test_type =
249         UBUS_OBJECT_TYPE("test_reconnect_events", test_methods);
250 
251 /*
252  * The new ubusd holds the path of a named handler already, so the reconnect
253  * cannot add it again. The handler loses its pattern with its id.
254  */
255 static void test_failed_readd_patterns_dropped(void)
256 {
257         struct ubus_object holder = {
258                 .name = "test.handler",
259                 .type = &test_type,
260                 .methods = test_methods,
261                 .n_methods = ARRAY_SIZE(test_methods),
262         };
263         struct counted_handler h;
264         struct ubus_context *ctx, *tx;
265 
266         counted_init(&h);
267         h.ev.obj.name = "test.handler";
268         h.ev.obj.type = &test_type;
269         ubusd_start();
270         ctx = ubus_connect(sock_path);
271         CHECK(ctx);
272         if (!ctx)
273                 return;
274 
275         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.old"));
276 
277         ubusd_kill();
278         ubusd_start();
279         tx = ubus_connect(sock_path);
280         CHECK(tx);
281         if (!tx)
282                 return;
283 
284         CHECK(!ubus_add_object(tx, &holder));
285         CHECK(!ubus_reconnect(ctx, sock_path));
286         CHECK(!h.ev.obj.id);
287         CHECK(!ctx->objects.count);
288         CHECK(!ubus_remove_object(tx, &holder));
289         ubus_free(tx);
290 
291         CHECK(!ubus_add_object(ctx, &h.ev.obj));
292         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
293 
294         ubusd_restart(ctx);
295         event_send(ctx, "test.old");
296         event_send(ctx, "test.new");
297         CHECK(!h.count_old);
298         CHECK(h.count_new == 1);
299 
300         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
301         ubus_free(ctx);
302         ubusd_kill();
303 }
304 
305 /*
306  * ubusd refuses an empty pattern. The handler object that the refused call
307  * added is removed again, on ubusd too, so that the caller can free it.
308  */
309 static void test_refused_register_removes_object(void)
310 {
311         struct counted_handler h;
312         struct ubus_context *ctx;
313 
314         counted_init(&h);
315         h.ev.obj.name = "test.handler";
316         h.ev.obj.type = &test_type;
317         ubusd_start();
318         ctx = ubus_connect(sock_path);
319         CHECK(ctx);
320         if (!ctx)
321                 return;
322 
323         CHECK(ubus_register_event_handler(ctx, &h.ev, "") ==
324               UBUS_STATUS_PERMISSION_DENIED);
325         CHECK(!h.ev.obj.id);
326         CHECK(!ctx->objects.count);
327         CHECK(!object_visible("test.handler"));
328 
329         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.new"));
330         ubusd_restart(ctx);
331         event_send(ctx, "test.new");
332         CHECK(h.count_new == 1);
333         CHECK(object_visible("test.handler"));
334 
335         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
336         ubus_free(ctx);
337         ubusd_kill();
338 }
339 
340 /* A refused second pattern leaves the handler and its first pattern. */
341 static void test_refused_pattern_keeps_object(void)
342 {
343         struct counted_handler h;
344         struct ubus_context *ctx;
345         uint32_t id;
346 
347         counted_init(&h);
348         ubusd_start();
349         ctx = ubus_connect(sock_path);
350         CHECK(ctx);
351         if (!ctx)
352                 return;
353 
354         CHECK(!ubus_register_event_handler(ctx, &h.ev, "test.old"));
355         id = h.ev.obj.id;
356         CHECK(ubus_register_event_handler(ctx, &h.ev, "") ==
357               UBUS_STATUS_PERMISSION_DENIED);
358         CHECK(h.ev.obj.id == id);
359         CHECK(ctx->objects.count == 1);
360 
361         event_send(ctx, "test.old");
362         CHECK(h.count_old == 1);
363         ubusd_restart(ctx);
364         event_send(ctx, "test.old");
365         CHECK(h.count_old == 2);
366 
367         CHECK(!ubus_unregister_event_handler(ctx, &h.ev));
368         ubus_free(ctx);
369         ubusd_kill();
370 }
371 
372 /* An auto subscriber learns of an object added after a reconnect. */
373 static void test_auto_subscriber_after_reconnect(void)
374 {
375         struct ubus_subscriber sub = { .new_obj_cb = auto_new_obj_cb };
376         struct ubus_object late = {
377                 .name = "test.late",
378                 .type = &test_type,
379                 .methods = test_methods,
380                 .n_methods = ARRAY_SIZE(test_methods),
381         };
382         struct pollfd pfd = { .events = POLLIN };
383         struct ubus_context *ctx, *tx;
384 
385         ubusd_start();
386         ctx = ubus_connect(sock_path);
387         CHECK(ctx);
388         if (!ctx)
389                 return;
390 
391         CHECK(!ubus_register_subscriber(ctx, &sub));
392         ubusd_restart(ctx);
393         CHECK(ctx->auto_subscribe_event_handler.obj.id);
394 
395         new_obj_count = 0;
396         tx = ubus_connect(sock_path);
397         CHECK(tx);
398         if (tx) {
399                 CHECK(!ubus_add_object(tx, &late));
400                 pfd.fd = ctx->sock.fd;
401                 while (poll(&pfd, 1, 200) > 0)
402                         ubus_handle_event(ctx);
403                 ubus_free(tx);
404         }
405         CHECK(new_obj_count == 1);
406 
407         ubus_free(ctx);
408         ubusd_kill();
409 }
410 
411 int main(int argc, char **argv)
412 {
413         int ret;
414 
415         ret = test_ubusd_init(argc, argv);
416         if (ret)
417                 return ret;
418 
419         test_patterns_after_reconnect();
420         test_removed_handler_patterns_dropped();
421         test_lost_handler_patterns_dropped();
422         test_contexts_apart();
423         test_failed_readd_patterns_dropped();
424         test_refused_register_removes_object();
425         test_refused_pattern_keeps_object();
426         test_auto_subscriber_after_reconnect();
427 
428         return test_ubusd_done();
429 }
430 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt