[GTK] Implement custom URI schemes with CustomProtocols
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Gtk / TestWebKitWebContext.cpp
1 /*
2  * Copyright (C) 2011 Igalia S.L.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2,1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include "LoadTrackingTest.h"
23 #include "WebKitTestServer.h"
24 #include <gtk/gtk.h>
25 #include <webkit2/webkit2.h>
26 #include <wtf/HashMap.h>
27 #include <wtf/gobject/GRefPtr.h>
28 #include <wtf/gobject/GUniquePtr.h>
29 #include <wtf/text/StringHash.h>
30
31 static WebKitTestServer* kServer;
32
33 static void testWebContextDefault(Test* test, gconstpointer)
34 {
35     // Check there's a single instance of the default web context.
36     g_assert(webkit_web_context_get_default() == webkit_web_context_get_default());
37 }
38
39 class PluginsTest: public Test {
40 public:
41     MAKE_GLIB_TEST_FIXTURE(PluginsTest);
42
43     PluginsTest()
44         : m_context(webkit_web_context_get_default())
45         , m_mainLoop(g_main_loop_new(0, TRUE))
46         , m_plugins(0)
47     {
48         webkit_web_context_set_additional_plugins_directory(m_context, WEBKIT_TEST_PLUGIN_DIR);
49     }
50
51     ~PluginsTest()
52     {
53         g_main_loop_unref(m_mainLoop);
54         g_list_free_full(m_plugins, g_object_unref);
55     }
56
57     static void getPluginsAsyncReadyCallback(GObject*, GAsyncResult* result, PluginsTest* test)
58     {
59         test->m_plugins = webkit_web_context_get_plugins_finish(test->m_context, result, 0);
60         g_main_loop_quit(test->m_mainLoop);
61     }
62
63     GList* getPlugins()
64     {
65         g_list_free_full(m_plugins, g_object_unref);
66         webkit_web_context_get_plugins(m_context, 0, reinterpret_cast<GAsyncReadyCallback>(getPluginsAsyncReadyCallback), this);
67         g_main_loop_run(m_mainLoop);
68         return m_plugins;
69     }
70
71     WebKitWebContext* m_context;
72     GMainLoop* m_mainLoop;
73     GList* m_plugins;
74 };
75
76 static void testWebContextGetPlugins(PluginsTest* test, gconstpointer)
77 {
78     GList* plugins = test->getPlugins();
79     g_assert(plugins);
80
81     GRefPtr<WebKitPlugin> testPlugin;
82     for (GList* item = plugins; item; item = g_list_next(item)) {
83         WebKitPlugin* plugin = WEBKIT_PLUGIN(item->data);
84         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(plugin));
85         if (!g_strcmp0(webkit_plugin_get_name(plugin), "WebKit Test PlugIn")) {
86             testPlugin = plugin;
87             break;
88         }
89     }
90     g_assert(WEBKIT_IS_PLUGIN(testPlugin.get()));
91
92     GUniquePtr<char> pluginPath(g_build_filename(WEBKIT_TEST_PLUGIN_DIR, "libTestNetscapePlugin.so", nullptr));
93     g_assert_cmpstr(webkit_plugin_get_path(testPlugin.get()), ==, pluginPath.get());
94     g_assert_cmpstr(webkit_plugin_get_description(testPlugin.get()), ==, "Simple Netscape┬« plug-in that handles test content for WebKit");
95     GList* mimeInfoList = webkit_plugin_get_mime_info_list(testPlugin.get());
96     g_assert(mimeInfoList);
97     g_assert_cmpuint(g_list_length(mimeInfoList), ==, 2);
98
99     WebKitMimeInfo* mimeInfo = static_cast<WebKitMimeInfo*>(mimeInfoList->data);
100     g_assert_cmpstr(webkit_mime_info_get_mime_type(mimeInfo), ==, "image/png");
101     g_assert_cmpstr(webkit_mime_info_get_description(mimeInfo), ==, "png image");
102     const gchar* const* extensions = webkit_mime_info_get_extensions(mimeInfo);
103     g_assert(extensions);
104     g_assert_cmpstr(extensions[0], ==, "png");
105
106     mimeInfoList = g_list_next(mimeInfoList);
107     mimeInfo = static_cast<WebKitMimeInfo*>(mimeInfoList->data);
108     g_assert_cmpstr(webkit_mime_info_get_mime_type(mimeInfo), ==, "application/x-webkit-test-netscape");
109     g_assert_cmpstr(webkit_mime_info_get_description(mimeInfo), ==, "test netscape content");
110     extensions = webkit_mime_info_get_extensions(mimeInfo);
111     g_assert(extensions);
112     g_assert_cmpstr(extensions[0], ==, "testnetscape");
113 }
114
115 static const char* kBarHTML = "<html><body>Bar</body></html>";
116 static const char* kEchoHTMLFormat = "<html><body>%s</body></html>";
117 static const char* errorDomain = "test";
118 static const int errorCode = 10;
119 static const char* errorMessage = "Error message.";
120
121 class URISchemeTest: public LoadTrackingTest {
122 public:
123     MAKE_GLIB_TEST_FIXTURE(URISchemeTest);
124
125     struct URISchemeHandler {
126         URISchemeHandler()
127             : replyLength(0)
128         {
129         }
130
131         URISchemeHandler(const char* reply, int replyLength, const char* mimeType)
132             : reply(reply)
133             , replyLength(replyLength)
134             , mimeType(mimeType)
135         {
136         }
137
138         CString reply;
139         int replyLength;
140         CString mimeType;
141     };
142
143     static void uriSchemeRequestCallback(WebKitURISchemeRequest* request, gpointer userData)
144     {
145         URISchemeTest* test = static_cast<URISchemeTest*>(userData);
146         test->m_uriSchemeRequest = request;
147         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
148
149         // FIXME: webkit_uri_scheme_request_get_web_view doesn't work with custom protocols.
150         // g_assert(webkit_uri_scheme_request_get_web_view(request) == test->m_webView);
151
152         GRefPtr<GInputStream> inputStream = adoptGRef(g_memory_input_stream_new());
153         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(inputStream.get()));
154
155         const char* scheme = webkit_uri_scheme_request_get_scheme(request);
156         g_assert(scheme);
157         g_assert(test->m_handlersMap.contains(String::fromUTF8(scheme)));
158
159         if (!g_strcmp0(scheme, "error")) {
160             GUniquePtr<GError> error(g_error_new_literal(g_quark_from_string(errorDomain), errorCode, errorMessage));
161             webkit_uri_scheme_request_finish_error(request, error.get());
162             return;
163         }
164
165         const URISchemeHandler& handler = test->m_handlersMap.get(String::fromUTF8(scheme));
166
167         if (!g_strcmp0(scheme, "echo")) {
168             char* replyHTML = g_strdup_printf(handler.reply.data(), webkit_uri_scheme_request_get_path(request));
169             g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), replyHTML, strlen(replyHTML), g_free);
170         } else if (!g_strcmp0(scheme, "closed"))
171             g_input_stream_close(inputStream.get(), 0, 0);
172         else if (!handler.reply.isNull())
173             g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), handler.reply.data(), handler.reply.length(), 0);
174
175         webkit_uri_scheme_request_finish(request, inputStream.get(), handler.replyLength, handler.mimeType.data());
176     }
177
178     void registerURISchemeHandler(const char* scheme, const char* reply, int replyLength, const char* mimeType)
179     {
180         m_handlersMap.set(String::fromUTF8(scheme), URISchemeHandler(reply, replyLength, mimeType));
181         webkit_web_context_register_uri_scheme(webkit_web_context_get_default(), scheme, uriSchemeRequestCallback, this, 0);
182     }
183
184     GRefPtr<WebKitURISchemeRequest> m_uriSchemeRequest;
185     HashMap<String, URISchemeHandler> m_handlersMap;
186 };
187
188 static void testWebContextURIScheme(URISchemeTest* test, gconstpointer)
189 {
190     test->registerURISchemeHandler("foo", kBarHTML, strlen(kBarHTML), "text/html");
191     test->loadURI("foo:blank");
192     test->waitUntilLoadFinished();
193     size_t mainResourceDataSize = 0;
194     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
195     g_assert_cmpint(mainResourceDataSize, ==, strlen(kBarHTML));
196     g_assert(!strncmp(mainResourceData, kBarHTML, mainResourceDataSize));
197
198     test->registerURISchemeHandler("echo", kEchoHTMLFormat, -1, "text/html");
199     test->loadURI("echo:hello world");
200     test->waitUntilLoadFinished();
201     GUniquePtr<char> echoHTML(g_strdup_printf(kEchoHTMLFormat, webkit_uri_scheme_request_get_path(test->m_uriSchemeRequest.get())));
202     mainResourceDataSize = 0;
203     mainResourceData = test->mainResourceData(mainResourceDataSize);
204     g_assert_cmpint(mainResourceDataSize, ==, strlen(echoHTML.get()));
205     g_assert(!strncmp(mainResourceData, echoHTML.get(), mainResourceDataSize));
206
207     test->registerURISchemeHandler("nomime", kBarHTML, -1, 0);
208     test->m_loadEvents.clear();
209     test->loadURI("nomime:foo bar");
210     test->waitUntilLoadFinished();
211     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
212
213     test->registerURISchemeHandler("empty", 0, 0, "text/html");
214     test->m_loadEvents.clear();
215     test->loadURI("empty:nothing");
216     test->waitUntilLoadFinished();
217     g_assert(!test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
218     g_assert(!test->m_loadEvents.contains(LoadTrackingTest::LoadFailed));
219
220     test->registerURISchemeHandler("error", 0, 0, 0);
221     test->m_loadEvents.clear();
222     test->loadURI("error:error");
223     test->waitUntilLoadFinished();
224     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
225     g_assert(test->m_loadFailed);
226     g_assert_error(test->m_error.get(), g_quark_from_string(errorDomain), errorCode);
227     g_assert_cmpstr(test->m_error->message, ==, errorMessage);
228
229     test->registerURISchemeHandler("closed", 0, 0, 0);
230     test->m_loadEvents.clear();
231     test->loadURI("closed:input-stream");
232     test->waitUntilLoadFinished();
233     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
234     g_assert(test->m_loadFailed);
235     g_assert_error(test->m_error.get(), G_IO_ERROR, G_IO_ERROR_CLOSED);
236 }
237
238 static void testWebContextSpellChecker(Test* test, gconstpointer)
239 {
240     WebKitWebContext* webContext = webkit_web_context_get_default();
241
242     // Check what happens if no spell checking language has been set.
243     const gchar* const* currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
244     g_assert(!currentLanguage);
245
246     // Set the language to a specific one.
247     GRefPtr<GPtrArray> languages = adoptGRef(g_ptr_array_new());
248     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en_US")));
249     g_ptr_array_add(languages.get(), 0);
250     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
251     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
252     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 1);
253     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
254
255     // Set the language string to list of valid languages.
256     g_ptr_array_remove_index_fast(languages.get(), languages->len - 1);
257     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en_GB")));
258     g_ptr_array_add(languages.get(), 0);
259     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
260     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
261     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 2);
262     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
263     g_assert_cmpstr(currentLanguage[1], ==, "en_GB");
264
265     // Try passing a wrong language along with good ones.
266     g_ptr_array_remove_index_fast(languages.get(), languages->len - 1);
267     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("bd_WR")));
268     g_ptr_array_add(languages.get(), 0);
269     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
270     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
271     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 2);
272     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
273     g_assert_cmpstr(currentLanguage[1], ==, "en_GB");
274
275     // Try passing a list with only wrong languages.
276     languages = adoptGRef(g_ptr_array_new());
277     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("bd_WR")));
278     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("wr_BD")));
279     g_ptr_array_add(languages.get(), 0);
280     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
281     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
282     g_assert(!currentLanguage);
283
284     // Check disabling and re-enabling spell checking.
285     webkit_web_context_set_spell_checking_enabled(webContext, FALSE);
286     g_assert(!webkit_web_context_get_spell_checking_enabled(webContext));
287     webkit_web_context_set_spell_checking_enabled(webContext, TRUE);
288     g_assert(webkit_web_context_get_spell_checking_enabled(webContext));
289 }
290
291 static void testWebContextLanguages(WebViewTest* test, gconstpointer)
292 {
293     static const char* expectedDefaultLanguage = "en";
294     test->loadURI(kServer->getURIForPath("/").data());
295     test->waitUntilLoadFinished();
296     size_t mainResourceDataSize = 0;
297     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
298     g_assert_cmpuint(mainResourceDataSize, ==, strlen(expectedDefaultLanguage));
299     g_assert(!strncmp(mainResourceData, expectedDefaultLanguage, mainResourceDataSize));
300
301     GRefPtr<GPtrArray> languages = adoptGRef(g_ptr_array_new());
302     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en")));
303     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("ES_es")));
304     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("dE")));
305     g_ptr_array_add(languages.get(), 0);
306     webkit_web_context_set_preferred_languages(webkit_web_context_get_default(), reinterpret_cast<const char* const*>(languages->pdata));
307
308     static const char* expectedLanguages = "en, es-es;q=0.90, de;q=0.80";
309     test->loadURI(kServer->getURIForPath("/").data());
310     test->waitUntilLoadFinished();
311     mainResourceDataSize = 0;
312     mainResourceData = test->mainResourceData(mainResourceDataSize);
313     g_assert_cmpuint(mainResourceDataSize, ==, strlen(expectedLanguages));
314     g_assert(!strncmp(mainResourceData, expectedLanguages, mainResourceDataSize));
315 }
316
317 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
318 {
319     if (message->method != SOUP_METHOD_GET) {
320         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
321         return;
322     }
323
324     if (g_str_equal(path, "/")) {
325         const char* acceptLanguage = soup_message_headers_get_one(message->request_headers, "Accept-Language");
326         soup_message_set_status(message, SOUP_STATUS_OK);
327         soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, acceptLanguage, strlen(acceptLanguage));
328         soup_message_body_complete(message->response_body);
329     } else
330         soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
331 }
332
333 class SecurityPolicyTest: public Test {
334 public:
335     MAKE_GLIB_TEST_FIXTURE(SecurityPolicyTest);
336
337     enum SecurityPolicy {
338         Local = 1 << 1,
339         NoAccess = 1 << 2,
340         DisplayIsolated = 1 << 3,
341         Secure = 1 << 4,
342         CORSEnabled = 1 << 5,
343         EmptyDocument = 1 << 6
344     };
345
346     SecurityPolicyTest()
347         : m_manager(webkit_web_context_get_security_manager(webkit_web_context_get_default()))
348     {
349     }
350
351     void verifyThatSchemeMatchesPolicy(const char* scheme, unsigned policy)
352     {
353         if (policy & Local)
354             g_assert(webkit_security_manager_uri_scheme_is_local(m_manager, scheme));
355         else
356             g_assert(!webkit_security_manager_uri_scheme_is_local(m_manager, scheme));
357         if (policy & NoAccess)
358             g_assert(webkit_security_manager_uri_scheme_is_no_access(m_manager, scheme));
359         else
360             g_assert(!webkit_security_manager_uri_scheme_is_no_access(m_manager, scheme));
361         if (policy & DisplayIsolated)
362             g_assert(webkit_security_manager_uri_scheme_is_display_isolated(m_manager, scheme));
363         else
364             g_assert(!webkit_security_manager_uri_scheme_is_display_isolated(m_manager, scheme));
365         if (policy & Secure)
366             g_assert(webkit_security_manager_uri_scheme_is_secure(m_manager, scheme));
367         else
368             g_assert(!webkit_security_manager_uri_scheme_is_secure(m_manager, scheme));
369         if (policy & CORSEnabled)
370             g_assert(webkit_security_manager_uri_scheme_is_cors_enabled(m_manager, scheme));
371         else
372             g_assert(!webkit_security_manager_uri_scheme_is_cors_enabled(m_manager, scheme));
373         if (policy & EmptyDocument)
374             g_assert(webkit_security_manager_uri_scheme_is_empty_document(m_manager, scheme));
375         else
376             g_assert(!webkit_security_manager_uri_scheme_is_empty_document(m_manager, scheme));
377     }
378
379     WebKitSecurityManager* m_manager;
380 };
381
382 static void testWebContextSecurityPolicy(SecurityPolicyTest* test, gconstpointer)
383 {
384     // VerifyThatSchemeMatchesPolicy default policy for well known schemes.
385     test->verifyThatSchemeMatchesPolicy("http", SecurityPolicyTest::CORSEnabled);
386     test->verifyThatSchemeMatchesPolicy("https", SecurityPolicyTest::CORSEnabled | SecurityPolicyTest::Secure);
387     test->verifyThatSchemeMatchesPolicy("file", SecurityPolicyTest::Local);
388     test->verifyThatSchemeMatchesPolicy("data", SecurityPolicyTest::NoAccess | SecurityPolicyTest::Secure);
389     test->verifyThatSchemeMatchesPolicy("about", SecurityPolicyTest::NoAccess | SecurityPolicyTest::Secure | SecurityPolicyTest::EmptyDocument);
390
391     // Custom scheme.
392     test->verifyThatSchemeMatchesPolicy("foo", 0);
393
394     webkit_security_manager_register_uri_scheme_as_local(test->m_manager, "foo");
395     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local);
396     webkit_security_manager_register_uri_scheme_as_no_access(test->m_manager, "foo");
397     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess);
398     webkit_security_manager_register_uri_scheme_as_display_isolated(test->m_manager, "foo");
399     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated);
400     webkit_security_manager_register_uri_scheme_as_secure(test->m_manager, "foo");
401     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure);
402     webkit_security_manager_register_uri_scheme_as_cors_enabled(test->m_manager, "foo");
403     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure
404         | SecurityPolicyTest::CORSEnabled);
405     webkit_security_manager_register_uri_scheme_as_empty_document(test->m_manager, "foo");
406     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure
407         | SecurityPolicyTest::CORSEnabled | SecurityPolicyTest::EmptyDocument);
408 }
409
410 void beforeAll()
411 {
412     kServer = new WebKitTestServer();
413     kServer->run(serverCallback);
414
415     Test::add("WebKitWebContext", "default-context", testWebContextDefault);
416     PluginsTest::add("WebKitWebContext", "get-plugins", testWebContextGetPlugins);
417     URISchemeTest::add("WebKitWebContext", "uri-scheme", testWebContextURIScheme);
418     Test::add("WebKitWebContext", "spell-checker", testWebContextSpellChecker);
419     WebViewTest::add("WebKitWebContext", "languages", testWebContextLanguages);
420     SecurityPolicyTest::add("WebKitSecurityManager", "security-policy", testWebContextSecurityPolicy);
421 }
422
423 void afterAll()
424 {
425     delete kServer;
426 }