summaryrefslogtreecommitdiff
path: root/tests/test-service.c
blob: 859d25ac3c59846351c093e45c4c975ff52e5890 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <config.h>

#include <libgupnp/gupnp-xml-doc.h>

#include <libgupnp/gupnp-context-private.h>
#include <libgupnp/gupnp-service-private.h>
#include <libgupnp/gupnp.h>

static GUPnPContext *
create_context (guint16 port, GError **error)
{
        return GUPNP_CONTEXT (g_initable_new (GUPNP_TYPE_CONTEXT,
                                              NULL,
                                              error,
                                              "host-ip",
                                              "127.0.0.1",
                                              "port",
                                              port,
                                              NULL));
}

typedef struct {
        GMainLoop *loop;
        SoupServerMessage *message;
} TestServiceNotificationCancelledData;

void
on_notify (SoupServer *server,
           SoupServerMessage *msg,
           const char *path,
           GHashTable *query,
           gpointer user_data)
{
        TestServiceNotificationCancelledData *data = user_data;

        // Pause message, quit mainlopp
#if SOUP_CHECK_VERSION(3, 1, 2)
        soup_server_message_pause (msg);
#else
        soup_server_pause_message (server, msg);
#endif
        data->message = msg;
        g_main_loop_quit (data->loop);
}

void
on_subscribe (GObject *source, GAsyncResult *res, gpointer user_data)
{
        GError *error = NULL;

        GBytes *data = soup_session_send_and_read_finish (SOUP_SESSION (source),
                                                          res,
                                                          &error);

        g_assert_no_error (error);
        g_clear_pointer (&data, g_bytes_unref);
}

static void
on_finished (SoupServerMessage *msg, TestServiceNotificationCancelledData *data)
{
        g_assert_cmpint (soup_server_message_get_status (msg),
                         ==,
                         SOUP_STATUS_INTERNAL_SERVER_ERROR);
        g_main_loop_quit (data->loop);
}

static SoupMessage *
prepare_subscribe_message (const char *subscription_uri, SoupServer *server)
{
        SoupMessage *msg = soup_message_new ("SUBSCRIBE", subscription_uri);

        GSList *uris = soup_server_get_uris (server);
        GUri *subscription =
                soup_uri_copy (uris->data, SOUP_URI_PATH, "/Notify", NULL);
        char *uri_string = g_uri_to_string (subscription);
        char *callback = g_strdup_printf ("<%s>", uri_string);
        g_free (uri_string);
        g_slist_free_full (uris, (GDestroyNotify) g_uri_unref);
        g_uri_unref (subscription);

        SoupMessageHeaders *h = soup_message_get_request_headers (msg);
        soup_message_headers_append (h, "Callback", callback);
        g_free (callback);

        soup_message_headers_append (h, "NT", "upnp:event");

        return msg;
}

static void
test_service_notification_cancelled ()
{
        // Check that the notification message is cancelled correctly if the service is
        // Shut down during sending
        GUPnPContext *context = NULL;
        GError *error = NULL;
        GUPnPRootDevice *rd;
        GUPnPServiceInfo *info = NULL;

        TestServiceNotificationCancelledData data = { NULL, NULL };

        data.loop = g_main_loop_new (NULL, FALSE);

        context = create_context (0, &error);
        g_assert_no_error (error);
        g_assert (context != NULL);

        rd = gupnp_root_device_new (context,
                                    "TestDevice.xml",
                                    DATA_PATH,
                                    &error);
        g_assert_no_error (error);
        g_assert (rd != NULL);
        gupnp_root_device_set_available (rd, TRUE);

        SoupServer *server = soup_server_new (NULL, NULL);
        soup_server_add_handler (server, "/Notify", on_notify, &data, NULL);
        soup_server_listen_local (server,
                                  0,
                                  SOUP_SERVER_LISTEN_IPV4_ONLY,
                                  &error);
        g_assert_no_error (error);

        // Generate SUBSCRIBE message
        info = gupnp_device_info_get_service (
                GUPNP_DEVICE_INFO (rd),
                "urn:test-gupnp-org:service:TestService:1");
        char *url = gupnp_service_info_get_event_subscription_url (info);

        SoupMessage *msg = prepare_subscribe_message (url, server);
        SoupSession *session = soup_session_new ();
        // FIXME: Add timeout header
        soup_session_send_and_read_async (session,
                                          msg,
                                          G_PRIORITY_DEFAULT,
                                          NULL,
                                          on_subscribe,
                                          &data);

        g_main_loop_run (data.loop);
        g_signal_connect (data.message,
                          "finished",
                          G_CALLBACK (on_finished),
                          &data);

        g_clear_object (&info);

#if SOUP_CHECK_VERSION(3, 1, 2)
        soup_server_message_unpause (data.message);
#else
        soup_server_unpause_message (server, data.message);
#endif

        g_main_loop_run (data.loop);
        g_clear_object (&rd);
        g_clear_object (&msg);
        g_clear_object (&session);
        g_clear_object (&server);
        g_clear_object (&context);
        g_main_loop_unref (data.loop);
}

static void
on_notify_failed (GUPnPService *service,
                  GList *callbacks,
                  GError *error,
                  gpointer user_data)
{
        TestServiceNotificationCancelledData *data = user_data;

        g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED);
        g_main_loop_quit (data->loop);
}

static void
test_service_notification_remote_disappears (void)
{
        // Check that the notification message is cancelled correctly if the service is
        // Shut down during sending
        GUPnPContext *context = NULL;
        GError *error = NULL;
        GUPnPRootDevice *rd;
        GUPnPServiceInfo *info = NULL;

        TestServiceNotificationCancelledData data = { NULL, NULL };

        data.loop = g_main_loop_new (NULL, FALSE);

        context = create_context (0, &error);
        g_assert_no_error (error);
        g_assert (context != NULL);

        rd = gupnp_root_device_new (context,
                                    "TestDevice.xml",
                                    DATA_PATH,
                                    &error);
        g_assert_no_error (error);
        g_assert (rd != NULL);
        gupnp_root_device_set_available (rd, TRUE);

        // Generate SUBSCRIBE message
        info = gupnp_device_info_get_service (
                GUPNP_DEVICE_INFO (rd),
                "urn:test-gupnp-org:service:TestService:1");
        char *url = gupnp_service_info_get_event_subscription_url (info);

        g_signal_connect (info,
                          "notify-failed",
                          G_CALLBACK (on_notify_failed),
                          &data);

        SoupMessage *msg = soup_message_new ("SUBSCRIBE", url);

        SoupMessageHeaders *h = soup_message_get_request_headers (msg);
        soup_message_headers_append (h, "Callback", "<http://127.0.0.1:1312>");

        soup_message_headers_append (h, "NT", "upnp:event");

        SoupSession *session = soup_session_new ();
        // FIXME: Add timeout header
        soup_session_send_and_read_async (session,
                                          msg,
                                          G_PRIORITY_DEFAULT,
                                          NULL,
                                          NULL,
                                          &data);

        g_main_loop_run (data.loop);

        g_clear_object (&info);

        g_clear_object (&rd);
        g_clear_object (&msg);
        g_clear_object (&session);
        g_clear_object (&context);
        g_main_loop_unref (data.loop);
}

int
main (int argc, char *argv[])
{
        g_test_init (&argc, &argv, NULL);

        g_test_add_func ("/service/notify/cancel",
                         test_service_notification_cancelled);

        g_test_add_func ("/service/notify/handle-remote-disappering",
                         test_service_notification_remote_disappears);

        return g_test_run ();
}