1 /* test-discoverer.c
2  *
3  * Copyright 2019 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 
22 #include <glib.h>
23 #include <libecal/libecal.h>
24 
25 #include "gcal-simple-server.h"
26 #include "gcal-source-discoverer.h"
27 
28 static GcalSimpleServer*
init_server(void)29 init_server (void)
30 {
31   g_autoptr (GcalSimpleServer) server = NULL;
32 
33   server = gcal_simple_server_new ();
34   gcal_simple_server_start (server);
35 
36   return g_steal_pointer (&server);
37 }
38 
39 /*********************************************************************************************************************/
40 
41 static void
discovered_file_cb(GObject * source_object,GAsyncResult * result,gpointer user_data)42 discovered_file_cb (GObject      *source_object,
43                     GAsyncResult *result,
44                     gpointer      user_data)
45 {
46   g_autoptr (GPtrArray) sources = NULL;
47   g_autoptr (GError) error = NULL;
48   GMainLoop *mainloop = user_data;
49 
50   sources = gcal_discover_sources_from_uri_finish (result, &error);
51   g_assert_no_error (error);
52   g_assert_cmpuint (sources->len, ==, 1);
53 
54   g_main_loop_quit (mainloop);
55 }
56 
57 static void
discoverer_file(void)58 discoverer_file (void)
59 {
60   g_autoptr (GcalSimpleServer) server = NULL;
61   g_autoptr (GMainLoop) mainloop = NULL;
62   g_autoptr (SoupURI) uri = NULL;
63   g_autofree gchar *uri_str = NULL;
64 
65   server = init_server ();
66   uri = gcal_simple_server_get_uri (server);
67   soup_uri_set_path (uri, "/public/calendar");
68 
69   uri_str = soup_uri_to_string (uri, FALSE);
70 
71   mainloop = g_main_loop_new (NULL, FALSE);
72 
73   gcal_discover_sources_from_uri (uri_str,
74                                   NULL,
75                                   NULL,
76                                   NULL,
77                                   discovered_file_cb,
78                                   mainloop);
79 
80   g_main_loop_run (mainloop);
81 }
82 
83 /*********************************************************************************************************************/
84 
85 #if 0
86 
87 static void
88 discovered_webdav_unauthorized_cb (GObject      *source_object,
89                                    GAsyncResult *result,
90                                    gpointer      user_data)
91 {
92   g_autoptr (GPtrArray) sources = NULL;
93   g_autoptr (GError) error = NULL;
94   GMainLoop *mainloop = user_data;
95 
96   sources = gcal_discover_sources_from_uri_finish (result, &error);
97   g_assert_error (error, GCAL_SOURCE_DISCOVERER_ERROR, GCAL_SOURCE_DISCOVERER_ERROR_UNAUTHORIZED);
98 
99   g_main_loop_quit (mainloop);
100 }
101 
102 static void
103 discoverer_webdav_unauthorized (void)
104 {
105   g_autoptr (GcalSimpleServer) server = NULL;
106   g_autoptr (GMainLoop) mainloop = NULL;
107   g_autoptr (SoupURI) uri = NULL;
108   g_autofree gchar *uri_str = NULL;
109 
110   server = init_server ();
111   uri = gcal_simple_server_get_uri (server);
112   soup_uri_set_path (uri, "/secret-area/calendar");
113 
114   uri_str = soup_uri_to_string (uri, FALSE);
115 
116   mainloop = g_main_loop_new (NULL, FALSE);
117 
118   gcal_discover_sources_from_uri (uri_str,
119                                   NULL,
120                                   NULL,
121                                   NULL,
122                                   discovered_webdav_unauthorized_cb,
123                                   mainloop);
124 
125   g_main_loop_run (mainloop);
126 }
127 
128 /*********************************************************************************************************************/
129 
130 // TODO: Implement raw CalDAV server in GcalSimpleServer
131 
132 static void
133 discoverer_webdav_auth_cb (GObject      *source_object,
134                            GAsyncResult *result,
135                            gpointer      user_data)
136 {
137   g_autoptr (GPtrArray) sources = NULL;
138   g_autoptr (GError) error = NULL;
139   GMainLoop *mainloop = user_data;
140 
141   sources = gcal_discover_sources_from_uri_finish (result, &error);
142   g_assert_no_error (error);
143 
144   g_main_loop_quit (mainloop);
145 }
146 
147 static void
148 discoverer_webdav_auth (void)
149 {
150   g_autoptr (GcalSimpleServer) server = NULL;
151   g_autoptr (GMainLoop) mainloop = NULL;
152   g_autoptr (SoupURI) uri = NULL;
153   g_autofree gchar *uri_str = NULL;
154 
155   server = init_server ();
156   uri = gcal_simple_server_get_uri (server);
157   soup_uri_set_path (uri, "/secret-area/dav");
158 
159   uri_str = soup_uri_to_string (uri, FALSE);
160 
161   mainloop = g_main_loop_new (NULL, FALSE);
162 
163   gcal_discover_sources_from_uri (uri_str,
164                                   "feaneron",
165                                   "idonotmaintainanything",
166                                   NULL,
167                                   discoverer_webdav_auth_cb,
168                                   mainloop);
169 
170   g_main_loop_run (mainloop);
171 }
172 
173 #endif
174 
175 /*********************************************************************************************************************/
176 
177 gint
main(gint argc,gchar * argv[])178 main (gint   argc,
179       gchar *argv[])
180 {
181   g_setenv ("TZ", "UTC", TRUE);
182 
183   g_test_init (&argc, &argv, NULL);
184 
185   g_test_add_func ("/discoverer/file", discoverer_file);
186   //g_test_add_func ("/discoverer/webdav/unauthorized", discoverer_webdav_unauthorized);
187 
188   return g_test_run ();
189 }
190 
191