[GTK] Add initial WebKitWebsiteDataManager API for process configuration options
[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/glib/GRefPtr.h>
28 #include <wtf/glib/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     g_assert(webkit_web_context_get_default() != test->m_webContext.get());
38 }
39
40 static void testWebContextConfiguration(WebViewTest* test, gconstpointer)
41 {
42     WebKitWebsiteDataManager* manager = webkit_web_context_get_website_data_manager(test->m_webContext.get());
43     g_assert(WEBKIT_IS_WEBSITE_DATA_MANAGER(manager));
44     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(manager));
45
46     // Base directories are not used by TestMain.
47     g_assert(!webkit_website_data_manager_get_base_data_directory(manager));
48     g_assert(!webkit_website_data_manager_get_base_cache_directory(manager));
49
50     GUniquePtr<char> localStorageDirectory(g_build_filename(Test::dataDirectory(), "local-storage", nullptr));
51     g_assert_cmpstr(localStorageDirectory.get(), ==, webkit_website_data_manager_get_local_storage_directory(manager));
52     g_assert(g_file_test(localStorageDirectory.get(), G_FILE_TEST_IS_DIR));
53
54     test->loadURI(kServer->getURIForPath("/empty").data());
55     test->waitUntilLoadFinished();
56     test->runJavaScriptAndWaitUntilFinished("window.indexedDB.open('TestDatabase');", nullptr);
57     GUniquePtr<char> indexedDBDirectory(g_build_filename(Test::dataDirectory(), "indexeddb", nullptr));
58     g_assert_cmpstr(indexedDBDirectory.get(), ==, webkit_website_data_manager_get_indexeddb_directory(manager));
59     g_assert(g_file_test(indexedDBDirectory.get(), G_FILE_TEST_IS_DIR));
60
61     test->loadURI(kServer->getURIForPath("/appcache").data());
62     test->waitUntilLoadFinished();
63     GUniquePtr<char> applicationCacheDirectory(g_build_filename(Test::dataDirectory(), "appcache", nullptr));
64     g_assert_cmpstr(applicationCacheDirectory.get(), ==, webkit_website_data_manager_get_offline_application_cache_directory(manager));
65     GUniquePtr<char> applicationCacheDatabase(g_build_filename(applicationCacheDirectory.get(), "ApplicationCache.db", nullptr));
66     unsigned triesCount = 4;
67     while (!g_file_test(applicationCacheDatabase.get(), G_FILE_TEST_IS_REGULAR) && --triesCount)
68         test->wait(0.25);
69     g_assert(triesCount);
70
71
72     GUniquePtr<char> webSQLDirectory(g_build_filename(Test::dataDirectory(), "websql", nullptr));
73     g_assert_cmpstr(webSQLDirectory.get(), ==, webkit_website_data_manager_get_websql_directory(manager));
74 #if 0 // FIXME: We need API to set the database quota for an origin to be able to test this.
75     test->runJavaScriptAndWaitUntilFinished("db = openDatabase(\"TestDatabase\", \"1.0\", \"TestDatabase\", 1);", &error);
76     g_assert(g_file_test(webSQLDirectory.get(), G_FILE_TEST_IS_DIR));
77 #endif
78
79     GUniquePtr<char> diskCacheDirectory(g_build_filename(Test::dataDirectory(), "disk-cache", nullptr));
80     g_assert_cmpstr(diskCacheDirectory.get(), ==, webkit_website_data_manager_get_disk_cache_directory(manager));
81     g_assert(g_file_test(diskCacheDirectory.get(), G_FILE_TEST_IS_DIR));
82
83     // The default context should have a different manager with different configuration.
84     WebKitWebsiteDataManager* defaultManager = webkit_web_context_get_website_data_manager(webkit_web_context_get_default());
85     g_assert(WEBKIT_IS_WEBSITE_DATA_MANAGER(defaultManager));
86     g_assert(manager != defaultManager);
87     g_assert_cmpstr(webkit_website_data_manager_get_local_storage_directory(manager), !=, webkit_website_data_manager_get_local_storage_directory(defaultManager));
88     g_assert_cmpstr(webkit_website_data_manager_get_indexeddb_directory(manager), !=, webkit_website_data_manager_get_indexeddb_directory(defaultManager));
89     g_assert_cmpstr(webkit_website_data_manager_get_disk_cache_directory(manager), !=, webkit_website_data_manager_get_disk_cache_directory(defaultManager));
90     g_assert_cmpstr(webkit_website_data_manager_get_offline_application_cache_directory(manager), !=, webkit_website_data_manager_get_offline_application_cache_directory(defaultManager));
91     g_assert_cmpstr(webkit_website_data_manager_get_websql_directory(manager), !=, webkit_website_data_manager_get_websql_directory(defaultManager));
92
93     // Using Test::dataDirectory() we get the default configuration but for a differrent prefix.
94     GRefPtr<WebKitWebsiteDataManager> baseDataManager = adoptGRef(webkit_website_data_manager_new("base-data-directory", Test::dataDirectory(), "base-cache-directory", Test::dataDirectory(), nullptr));
95     g_assert(WEBKIT_IS_WEBSITE_DATA_MANAGER(baseDataManager.get()));
96
97     localStorageDirectory.reset(g_build_filename(Test::dataDirectory(), "localstorage", nullptr));
98     g_assert_cmpstr(webkit_website_data_manager_get_local_storage_directory(baseDataManager.get()), ==, localStorageDirectory.get());
99
100     indexedDBDirectory.reset(g_build_filename(Test::dataDirectory(), "databases", "indexeddb", nullptr));
101     g_assert_cmpstr(webkit_website_data_manager_get_indexeddb_directory(baseDataManager.get()), ==, indexedDBDirectory.get());
102
103     applicationCacheDirectory.reset(g_build_filename(Test::dataDirectory(), "applications", nullptr));
104     g_assert_cmpstr(webkit_website_data_manager_get_offline_application_cache_directory(baseDataManager.get()), ==, applicationCacheDirectory.get());
105
106     webSQLDirectory.reset(g_build_filename(Test::dataDirectory(), "databases", nullptr));
107     g_assert_cmpstr(webkit_website_data_manager_get_websql_directory(baseDataManager.get()), ==, webSQLDirectory.get());
108
109     g_assert_cmpstr(webkit_website_data_manager_get_disk_cache_directory(baseDataManager.get()), ==, Test::dataDirectory());
110
111     // Any specific configuration provided takes precedence over base dirs.
112     indexedDBDirectory.reset(g_build_filename(Test::dataDirectory(), "mycustomindexeddb", nullptr));
113     applicationCacheDirectory.reset(g_build_filename(Test::dataDirectory(), "mycustomappcache", nullptr));
114     baseDataManager = adoptGRef(webkit_website_data_manager_new("base-data-directory", Test::dataDirectory(), "base-cache-directory", Test::dataDirectory(),
115         "indexeddb-directory", indexedDBDirectory.get(), "offline-application-cache-directory", applicationCacheDirectory.get(), nullptr));
116     g_assert_cmpstr(webkit_website_data_manager_get_indexeddb_directory(baseDataManager.get()), ==, indexedDBDirectory.get());
117     g_assert_cmpstr(webkit_website_data_manager_get_offline_application_cache_directory(baseDataManager.get()), ==, applicationCacheDirectory.get());
118     // The resutl should be the same as previous manager.
119     g_assert_cmpstr(webkit_website_data_manager_get_local_storage_directory(baseDataManager.get()), ==, localStorageDirectory.get());
120     g_assert_cmpstr(webkit_website_data_manager_get_websql_directory(baseDataManager.get()), ==, webSQLDirectory.get());
121     g_assert_cmpstr(webkit_website_data_manager_get_disk_cache_directory(baseDataManager.get()), ==, Test::dataDirectory());
122 }
123
124 class PluginsTest: public Test {
125 public:
126     MAKE_GLIB_TEST_FIXTURE(PluginsTest);
127
128     PluginsTest()
129         : m_mainLoop(g_main_loop_new(nullptr, TRUE))
130         , m_plugins(nullptr)
131     {
132         webkit_web_context_set_additional_plugins_directory(m_webContext.get(), WEBKIT_TEST_PLUGIN_DIR);
133     }
134
135     ~PluginsTest()
136     {
137         g_main_loop_unref(m_mainLoop);
138         g_list_free_full(m_plugins, g_object_unref);
139     }
140
141     static void getPluginsAsyncReadyCallback(GObject*, GAsyncResult* result, PluginsTest* test)
142     {
143         test->m_plugins = webkit_web_context_get_plugins_finish(test->m_webContext.get(), result, nullptr);
144         g_main_loop_quit(test->m_mainLoop);
145     }
146
147     GList* getPlugins()
148     {
149         g_list_free_full(m_plugins, g_object_unref);
150         webkit_web_context_get_plugins(m_webContext.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(getPluginsAsyncReadyCallback), this);
151         g_main_loop_run(m_mainLoop);
152         return m_plugins;
153     }
154
155     GMainLoop* m_mainLoop;
156     GList* m_plugins;
157 };
158
159 static void testWebContextGetPlugins(PluginsTest* test, gconstpointer)
160 {
161     GList* plugins = test->getPlugins();
162     g_assert(plugins);
163
164     GRefPtr<WebKitPlugin> testPlugin;
165     for (GList* item = plugins; item; item = g_list_next(item)) {
166         WebKitPlugin* plugin = WEBKIT_PLUGIN(item->data);
167         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(plugin));
168         if (!g_strcmp0(webkit_plugin_get_name(plugin), "WebKit Test PlugIn")) {
169             testPlugin = plugin;
170             break;
171         }
172     }
173     g_assert(WEBKIT_IS_PLUGIN(testPlugin.get()));
174
175     GUniquePtr<char> pluginPath(g_build_filename(WEBKIT_TEST_PLUGIN_DIR, "libTestNetscapePlugin.so", nullptr));
176     g_assert_cmpstr(webkit_plugin_get_path(testPlugin.get()), ==, pluginPath.get());
177     g_assert_cmpstr(webkit_plugin_get_description(testPlugin.get()), ==, "Simple Netscape┬« plug-in that handles test content for WebKit");
178     GList* mimeInfoList = webkit_plugin_get_mime_info_list(testPlugin.get());
179     g_assert(mimeInfoList);
180     g_assert_cmpuint(g_list_length(mimeInfoList), ==, 2);
181
182     WebKitMimeInfo* mimeInfo = static_cast<WebKitMimeInfo*>(mimeInfoList->data);
183     g_assert_cmpstr(webkit_mime_info_get_mime_type(mimeInfo), ==, "image/png");
184     g_assert_cmpstr(webkit_mime_info_get_description(mimeInfo), ==, "png image");
185     const gchar* const* extensions = webkit_mime_info_get_extensions(mimeInfo);
186     g_assert(extensions);
187     g_assert_cmpstr(extensions[0], ==, "png");
188
189     mimeInfoList = g_list_next(mimeInfoList);
190     mimeInfo = static_cast<WebKitMimeInfo*>(mimeInfoList->data);
191     g_assert_cmpstr(webkit_mime_info_get_mime_type(mimeInfo), ==, "application/x-webkit-test-netscape");
192     g_assert_cmpstr(webkit_mime_info_get_description(mimeInfo), ==, "test netscape content");
193     extensions = webkit_mime_info_get_extensions(mimeInfo);
194     g_assert(extensions);
195     g_assert_cmpstr(extensions[0], ==, "testnetscape");
196 }
197
198 static const char* kBarHTML = "<html><body>Bar</body></html>";
199 static const char* kEchoHTMLFormat = "<html><body>%s</body></html>";
200 static const char* errorDomain = "test";
201 static const int errorCode = 10;
202 static const char* errorMessage = "Error message.";
203
204 class URISchemeTest: public LoadTrackingTest {
205 public:
206     MAKE_GLIB_TEST_FIXTURE(URISchemeTest);
207
208     struct URISchemeHandler {
209         URISchemeHandler()
210             : replyLength(0)
211         {
212         }
213
214         URISchemeHandler(const char* reply, int replyLength, const char* mimeType)
215             : reply(reply)
216             , replyLength(replyLength)
217             , mimeType(mimeType)
218         {
219         }
220
221         CString reply;
222         int replyLength;
223         CString mimeType;
224     };
225
226     static void uriSchemeRequestCallback(WebKitURISchemeRequest* request, gpointer userData)
227     {
228         URISchemeTest* test = static_cast<URISchemeTest*>(userData);
229         test->m_uriSchemeRequest = request;
230         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
231
232         g_assert(webkit_uri_scheme_request_get_web_view(request) == test->m_webView);
233
234         GRefPtr<GInputStream> inputStream = adoptGRef(g_memory_input_stream_new());
235         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(inputStream.get()));
236
237         const char* scheme = webkit_uri_scheme_request_get_scheme(request);
238         g_assert(scheme);
239         g_assert(test->m_handlersMap.contains(String::fromUTF8(scheme)));
240
241         if (!g_strcmp0(scheme, "error")) {
242             GUniquePtr<GError> error(g_error_new_literal(g_quark_from_string(errorDomain), errorCode, errorMessage));
243             webkit_uri_scheme_request_finish_error(request, error.get());
244             return;
245         }
246
247         const URISchemeHandler& handler = test->m_handlersMap.get(String::fromUTF8(scheme));
248
249         if (!g_strcmp0(scheme, "echo")) {
250             char* replyHTML = g_strdup_printf(handler.reply.data(), webkit_uri_scheme_request_get_path(request));
251             g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), replyHTML, strlen(replyHTML), g_free);
252         } else if (!g_strcmp0(scheme, "closed"))
253             g_input_stream_close(inputStream.get(), 0, 0);
254         else if (!handler.reply.isNull())
255             g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), handler.reply.data(), handler.reply.length(), 0);
256
257         webkit_uri_scheme_request_finish(request, inputStream.get(), handler.replyLength, handler.mimeType.data());
258     }
259
260     void registerURISchemeHandler(const char* scheme, const char* reply, int replyLength, const char* mimeType)
261     {
262         m_handlersMap.set(String::fromUTF8(scheme), URISchemeHandler(reply, replyLength, mimeType));
263         webkit_web_context_register_uri_scheme(m_webContext.get(), scheme, uriSchemeRequestCallback, this, 0);
264     }
265
266     GRefPtr<WebKitURISchemeRequest> m_uriSchemeRequest;
267     HashMap<String, URISchemeHandler> m_handlersMap;
268 };
269
270 static void testWebContextURIScheme(URISchemeTest* test, gconstpointer)
271 {
272     test->registerURISchemeHandler("foo", kBarHTML, strlen(kBarHTML), "text/html");
273     test->loadURI("foo:blank");
274     test->waitUntilLoadFinished();
275     size_t mainResourceDataSize = 0;
276     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
277     g_assert_cmpint(mainResourceDataSize, ==, strlen(kBarHTML));
278     g_assert(!strncmp(mainResourceData, kBarHTML, mainResourceDataSize));
279
280     test->registerURISchemeHandler("echo", kEchoHTMLFormat, -1, "text/html");
281     test->loadURI("echo:hello-world");
282     test->waitUntilLoadFinished();
283     GUniquePtr<char> echoHTML(g_strdup_printf(kEchoHTMLFormat, webkit_uri_scheme_request_get_path(test->m_uriSchemeRequest.get())));
284     mainResourceDataSize = 0;
285     mainResourceData = test->mainResourceData(mainResourceDataSize);
286     g_assert_cmpint(mainResourceDataSize, ==, strlen(echoHTML.get()));
287     g_assert(!strncmp(mainResourceData, echoHTML.get(), mainResourceDataSize));
288
289     test->loadURI("echo:with#fragment");
290     test->waitUntilLoadFinished();
291     g_assert_cmpstr(webkit_uri_scheme_request_get_path(test->m_uriSchemeRequest.get()), ==, "with");
292     g_assert_cmpstr(webkit_uri_scheme_request_get_uri(test->m_uriSchemeRequest.get()), ==, "echo:with#fragment");
293     echoHTML.reset(g_strdup_printf(kEchoHTMLFormat, webkit_uri_scheme_request_get_path(test->m_uriSchemeRequest.get())));
294     mainResourceDataSize = 0;
295     mainResourceData = test->mainResourceData(mainResourceDataSize);
296     g_assert_cmpint(mainResourceDataSize, ==, strlen(echoHTML.get()));
297     g_assert(!strncmp(mainResourceData, echoHTML.get(), mainResourceDataSize));
298
299     test->registerURISchemeHandler("nomime", kBarHTML, -1, 0);
300     test->m_loadEvents.clear();
301     test->loadURI("nomime:foo-bar");
302     test->waitUntilLoadFinished();
303     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
304
305     test->registerURISchemeHandler("empty", 0, 0, "text/html");
306     test->m_loadEvents.clear();
307     test->loadURI("empty:nothing");
308     test->waitUntilLoadFinished();
309     g_assert(!test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
310     g_assert(!test->m_loadEvents.contains(LoadTrackingTest::LoadFailed));
311
312     test->registerURISchemeHandler("error", 0, 0, 0);
313     test->m_loadEvents.clear();
314     test->loadURI("error:error");
315     test->waitUntilLoadFinished();
316     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
317     g_assert(test->m_loadFailed);
318     g_assert_error(test->m_error.get(), g_quark_from_string(errorDomain), errorCode);
319     g_assert_cmpstr(test->m_error->message, ==, errorMessage);
320
321     test->registerURISchemeHandler("closed", 0, 0, 0);
322     test->m_loadEvents.clear();
323     test->loadURI("closed:input-stream");
324     test->waitUntilLoadFinished();
325     g_assert(test->m_loadEvents.contains(LoadTrackingTest::ProvisionalLoadFailed));
326     g_assert(test->m_loadFailed);
327     g_assert_error(test->m_error.get(), G_IO_ERROR, G_IO_ERROR_CLOSED);
328 }
329
330 static void testWebContextSpellChecker(Test* test, gconstpointer)
331 {
332     WebKitWebContext* webContext = test->m_webContext.get();
333
334     // Check what happens if no spell checking language has been set.
335     const gchar* const* currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
336     g_assert(!currentLanguage);
337
338     // Set the language to a specific one.
339     GRefPtr<GPtrArray> languages = adoptGRef(g_ptr_array_new());
340     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en_US")));
341     g_ptr_array_add(languages.get(), 0);
342     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
343     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
344     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 1);
345     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
346
347     // Set the language string to list of valid languages.
348     g_ptr_array_remove_index_fast(languages.get(), languages->len - 1);
349     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en_GB")));
350     g_ptr_array_add(languages.get(), 0);
351     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
352     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
353     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 2);
354     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
355     g_assert_cmpstr(currentLanguage[1], ==, "en_GB");
356
357     // Try passing a wrong language along with good ones.
358     g_ptr_array_remove_index_fast(languages.get(), languages->len - 1);
359     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("bd_WR")));
360     g_ptr_array_add(languages.get(), 0);
361     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
362     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
363     g_assert_cmpuint(g_strv_length(const_cast<char**>(currentLanguage)), ==, 2);
364     g_assert_cmpstr(currentLanguage[0], ==, "en_US");
365     g_assert_cmpstr(currentLanguage[1], ==, "en_GB");
366
367     // Try passing a list with only wrong languages.
368     languages = adoptGRef(g_ptr_array_new());
369     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("bd_WR")));
370     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("wr_BD")));
371     g_ptr_array_add(languages.get(), 0);
372     webkit_web_context_set_spell_checking_languages(webContext, reinterpret_cast<const char* const*>(languages->pdata));
373     currentLanguage = webkit_web_context_get_spell_checking_languages(webContext);
374     g_assert(!currentLanguage);
375
376     // Check disabling and re-enabling spell checking.
377     webkit_web_context_set_spell_checking_enabled(webContext, FALSE);
378     g_assert(!webkit_web_context_get_spell_checking_enabled(webContext));
379     webkit_web_context_set_spell_checking_enabled(webContext, TRUE);
380     g_assert(webkit_web_context_get_spell_checking_enabled(webContext));
381 }
382
383 static void testWebContextLanguages(WebViewTest* test, gconstpointer)
384 {
385     static const char* expectedDefaultLanguage = "en";
386     test->loadURI(kServer->getURIForPath("/").data());
387     test->waitUntilLoadFinished();
388     size_t mainResourceDataSize = 0;
389     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
390     g_assert_cmpuint(mainResourceDataSize, ==, strlen(expectedDefaultLanguage));
391     g_assert(!strncmp(mainResourceData, expectedDefaultLanguage, mainResourceDataSize));
392
393     GRefPtr<GPtrArray> languages = adoptGRef(g_ptr_array_new());
394     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("en")));
395     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("ES_es")));
396     g_ptr_array_add(languages.get(), const_cast<gpointer>(static_cast<const void*>("dE")));
397     g_ptr_array_add(languages.get(), 0);
398     webkit_web_context_set_preferred_languages(test->m_webContext.get(), reinterpret_cast<const char* const*>(languages->pdata));
399
400     static const char* expectedLanguages = "en, es-es;q=0.90, de;q=0.80";
401     test->loadURI(kServer->getURIForPath("/").data());
402     test->waitUntilLoadFinished();
403     mainResourceDataSize = 0;
404     mainResourceData = test->mainResourceData(mainResourceDataSize);
405     g_assert_cmpuint(mainResourceDataSize, ==, strlen(expectedLanguages));
406     g_assert(!strncmp(mainResourceData, expectedLanguages, mainResourceDataSize));
407 }
408
409 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
410 {
411     if (message->method != SOUP_METHOD_GET) {
412         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
413         return;
414     }
415
416     if (g_str_equal(path, "/")) {
417         const char* acceptLanguage = soup_message_headers_get_one(message->request_headers, "Accept-Language");
418         soup_message_set_status(message, SOUP_STATUS_OK);
419         soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, acceptLanguage, strlen(acceptLanguage));
420         soup_message_body_complete(message->response_body);
421     } else if (g_str_equal(path, "/empty")) {
422         const char* emptyHTML = "<html><body></body></html>";
423         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, emptyHTML, strlen(emptyHTML));
424         soup_message_body_complete(message->response_body);
425         soup_message_set_status(message, SOUP_STATUS_OK);
426     } else if (g_str_equal(path, "/appcache")) {
427         const char* appcacheHTML = "<html manifest=appcache.manifest><body></body></html>";
428         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, appcacheHTML, strlen(appcacheHTML));
429         soup_message_body_complete(message->response_body);
430         soup_message_set_status(message, SOUP_STATUS_OK);
431     } else if (g_str_equal(path, "/appcache.manifest")) {
432         const char* appcacheManifest = "CACHE MANIFEST\nCACHE:\nappcache/foo.txt\n";
433         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, appcacheManifest, strlen(appcacheManifest));
434         soup_message_body_complete(message->response_body);
435         soup_message_set_status(message, SOUP_STATUS_OK);
436     } else if (g_str_equal(path, "/appcache/foo.txt")) {
437         soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, "foo", 3);
438         soup_message_body_complete(message->response_body);
439         soup_message_set_status(message, SOUP_STATUS_OK);
440     } else
441         soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
442 }
443
444 class SecurityPolicyTest: public Test {
445 public:
446     MAKE_GLIB_TEST_FIXTURE(SecurityPolicyTest);
447
448     enum SecurityPolicy {
449         Local = 1 << 1,
450         NoAccess = 1 << 2,
451         DisplayIsolated = 1 << 3,
452         Secure = 1 << 4,
453         CORSEnabled = 1 << 5,
454         EmptyDocument = 1 << 6
455     };
456
457     SecurityPolicyTest()
458         : m_manager(webkit_web_context_get_security_manager(m_webContext.get()))
459     {
460     }
461
462     void verifyThatSchemeMatchesPolicy(const char* scheme, unsigned policy)
463     {
464         if (policy & Local)
465             g_assert(webkit_security_manager_uri_scheme_is_local(m_manager, scheme));
466         else
467             g_assert(!webkit_security_manager_uri_scheme_is_local(m_manager, scheme));
468         if (policy & NoAccess)
469             g_assert(webkit_security_manager_uri_scheme_is_no_access(m_manager, scheme));
470         else
471             g_assert(!webkit_security_manager_uri_scheme_is_no_access(m_manager, scheme));
472         if (policy & DisplayIsolated)
473             g_assert(webkit_security_manager_uri_scheme_is_display_isolated(m_manager, scheme));
474         else
475             g_assert(!webkit_security_manager_uri_scheme_is_display_isolated(m_manager, scheme));
476         if (policy & Secure)
477             g_assert(webkit_security_manager_uri_scheme_is_secure(m_manager, scheme));
478         else
479             g_assert(!webkit_security_manager_uri_scheme_is_secure(m_manager, scheme));
480         if (policy & CORSEnabled)
481             g_assert(webkit_security_manager_uri_scheme_is_cors_enabled(m_manager, scheme));
482         else
483             g_assert(!webkit_security_manager_uri_scheme_is_cors_enabled(m_manager, scheme));
484         if (policy & EmptyDocument)
485             g_assert(webkit_security_manager_uri_scheme_is_empty_document(m_manager, scheme));
486         else
487             g_assert(!webkit_security_manager_uri_scheme_is_empty_document(m_manager, scheme));
488     }
489
490     WebKitSecurityManager* m_manager;
491 };
492
493 static void testWebContextSecurityPolicy(SecurityPolicyTest* test, gconstpointer)
494 {
495     // VerifyThatSchemeMatchesPolicy default policy for well known schemes.
496     test->verifyThatSchemeMatchesPolicy("http", SecurityPolicyTest::CORSEnabled);
497     test->verifyThatSchemeMatchesPolicy("https", SecurityPolicyTest::CORSEnabled | SecurityPolicyTest::Secure);
498     test->verifyThatSchemeMatchesPolicy("file", SecurityPolicyTest::Local);
499     test->verifyThatSchemeMatchesPolicy("data", SecurityPolicyTest::NoAccess | SecurityPolicyTest::Secure);
500     test->verifyThatSchemeMatchesPolicy("about", SecurityPolicyTest::NoAccess | SecurityPolicyTest::Secure | SecurityPolicyTest::EmptyDocument);
501
502     // Custom scheme.
503     test->verifyThatSchemeMatchesPolicy("foo", 0);
504
505     webkit_security_manager_register_uri_scheme_as_local(test->m_manager, "foo");
506     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local);
507     webkit_security_manager_register_uri_scheme_as_no_access(test->m_manager, "foo");
508     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess);
509     webkit_security_manager_register_uri_scheme_as_display_isolated(test->m_manager, "foo");
510     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated);
511     webkit_security_manager_register_uri_scheme_as_secure(test->m_manager, "foo");
512     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure);
513     webkit_security_manager_register_uri_scheme_as_cors_enabled(test->m_manager, "foo");
514     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure
515         | SecurityPolicyTest::CORSEnabled);
516     webkit_security_manager_register_uri_scheme_as_empty_document(test->m_manager, "foo");
517     test->verifyThatSchemeMatchesPolicy("foo", SecurityPolicyTest::Local | SecurityPolicyTest::NoAccess | SecurityPolicyTest::DisplayIsolated | SecurityPolicyTest::Secure
518         | SecurityPolicyTest::CORSEnabled | SecurityPolicyTest::EmptyDocument);
519 }
520
521 static void testWebContextSecurityFileXHR(WebViewTest* test, gconstpointer)
522 {
523     GUniquePtr<char> fileURL(g_strdup_printf("file://%s/simple.html", Test::getResourcesDir(Test::WebKit2Resources).data()));
524     test->loadURI(fileURL.get());
525     test->waitUntilLoadFinished();
526
527     GUniquePtr<char> jsonURL(g_strdup_printf("file://%s/simple.json", Test::getResourcesDir().data()));
528     GUniquePtr<char> xhr(g_strdup_printf("var xhr = new XMLHttpRequest; xhr.open(\"GET\", \"%s\"); xhr.send();", jsonURL.get()));
529
530     // By default file access is not allowed, this will fail with a cross-origin error.
531     GUniqueOutPtr<GError> error;
532     WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
533     g_assert(!javascriptResult);
534     g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
535
536     // Allow file access from file URLs.
537     webkit_settings_set_allow_file_access_from_file_urls(webkit_web_view_get_settings(test->m_webView), TRUE);
538     test->loadURI(fileURL.get());
539     test->waitUntilLoadFinished();
540     javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
541     g_assert(javascriptResult);
542     g_assert(!error);
543
544     // It isn't still possible to load file from an HTTP URL.
545     test->loadURI(kServer->getURIForPath("/").data());
546     test->waitUntilLoadFinished();
547     javascriptResult = test->runJavaScriptAndWaitUntilFinished(xhr.get(), &error.outPtr());
548     g_assert(!javascriptResult);
549     g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
550
551     webkit_settings_set_allow_file_access_from_file_urls(webkit_web_view_get_settings(test->m_webView), FALSE);
552 }
553
554 void beforeAll()
555 {
556     kServer = new WebKitTestServer();
557     kServer->run(serverCallback);
558
559     Test::add("WebKitWebContext", "default-context", testWebContextDefault);
560     WebViewTest::add("WebKitWebContext", "configuration", testWebContextConfiguration);
561     PluginsTest::add("WebKitWebContext", "get-plugins", testWebContextGetPlugins);
562     URISchemeTest::add("WebKitWebContext", "uri-scheme", testWebContextURIScheme);
563     Test::add("WebKitWebContext", "spell-checker", testWebContextSpellChecker);
564     WebViewTest::add("WebKitWebContext", "languages", testWebContextLanguages);
565     SecurityPolicyTest::add("WebKitSecurityManager", "security-policy", testWebContextSecurityPolicy);
566     WebViewTest::add("WebKitSecurityManager", "file-xhr", testWebContextSecurityFileXHR);
567 }
568
569 void afterAll()
570 {
571     delete kServer;
572 }