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