1/*
2 * Copyright (C) 2010 Collabora Ltd.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 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 "autotoolsconfig.h"
21#include <gtk/gtk.h>
22#include <libsoup/soup.h>
23#include <webkit/webkit.h>
24
25// Make sure the session is initialized properly when webkit_get_default_session() is called.
26static void test_globals_default_session()
27{
28    g_test_bug("36754");
29
30    SoupSession* session = webkit_get_default_session();
31    soup_session_remove_feature_by_type(session, WEBKIT_TYPE_SOUP_AUTH_DIALOG);
32
33    // This makes sure our initialization ran.
34    g_assert(soup_session_get_feature(session, SOUP_TYPE_CONTENT_DECODER) != NULL);
35
36    // Creating a WebView should make sure the session is
37    // initialized, and not mess with our changes.
38    WebKitWebView* web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
39    g_object_ref_sink(web_view);
40    g_object_unref(web_view);
41
42    // These makes sure our modification was kept.
43    g_assert(soup_session_get_feature(session, SOUP_TYPE_CONTENT_DECODER) != NULL);
44    g_assert(soup_session_get_feature(session, WEBKIT_TYPE_SOUP_AUTH_DIALOG) == NULL);
45}
46
47static void test_globals_security_policy()
48{
49    // Check default policy for well known schemes.
50    WebKitSecurityPolicy policy = webkit_get_security_policy_for_uri_scheme("http");
51    guint mask = WEBKIT_SECURITY_POLICY_CORS_ENABLED;
52    g_assert_cmpuint(policy & mask, ==, mask);
53
54    policy = webkit_get_security_policy_for_uri_scheme("https");
55    mask = WEBKIT_SECURITY_POLICY_SECURE | WEBKIT_SECURITY_POLICY_CORS_ENABLED;
56    g_assert_cmpuint(policy & mask, ==, mask);
57
58    policy = webkit_get_security_policy_for_uri_scheme("file");
59    mask = WEBKIT_SECURITY_POLICY_LOCAL;
60    g_assert_cmpuint(policy & mask, ==, mask);
61
62    policy = webkit_get_security_policy_for_uri_scheme("data");
63    mask = WEBKIT_SECURITY_POLICY_NO_ACCESS_TO_OTHER_SCHEME | WEBKIT_SECURITY_POLICY_SECURE;
64    g_assert_cmpuint(policy & mask, ==, mask);
65
66    policy = webkit_get_security_policy_for_uri_scheme("about");
67    mask = WEBKIT_SECURITY_POLICY_NO_ACCESS_TO_OTHER_SCHEME | WEBKIT_SECURITY_POLICY_SECURE | WEBKIT_SECURITY_POLICY_EMPTY_DOCUMENT;
68    g_assert_cmpuint(policy & mask, ==, mask);
69
70    // Custom scheme.
71    policy = webkit_get_security_policy_for_uri_scheme("foo");
72    g_assert(!policy);
73
74    policy |= WEBKIT_SECURITY_POLICY_LOCAL;
75    webkit_set_security_policy_for_uri_scheme("foo", policy);
76    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
77
78    policy |= WEBKIT_SECURITY_POLICY_NO_ACCESS_TO_OTHER_SCHEME;
79    webkit_set_security_policy_for_uri_scheme("foo", policy);
80    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
81
82    policy |= WEBKIT_SECURITY_POLICY_DISPLAY_ISOLATED;
83    webkit_set_security_policy_for_uri_scheme("foo", policy);
84    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
85
86    policy |= WEBKIT_SECURITY_POLICY_SECURE;
87    webkit_set_security_policy_for_uri_scheme("foo", policy);
88    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
89
90    policy |= WEBKIT_SECURITY_POLICY_CORS_ENABLED;
91    webkit_set_security_policy_for_uri_scheme("foo", policy);
92    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
93
94    policy |= WEBKIT_SECURITY_POLICY_EMPTY_DOCUMENT;
95    webkit_set_security_policy_for_uri_scheme("foo", policy);
96    g_assert_cmpuint(webkit_get_security_policy_for_uri_scheme("foo"), ==, policy);
97}
98
99int main(int argc, char** argv)
100{
101    gtk_test_init(&argc, &argv, NULL);
102
103    g_test_bug_base("https://bugs.webkit.org/");
104    g_test_add_func("/webkit/globals/default_session",
105                    test_globals_default_session);
106    g_test_add_func("/webkit/globals/security-policy",
107                    test_globals_security_policy);
108    return g_test_run();
109}
110
111