1/*
2 * Copyright (C) 2017 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 "TestMain.h"
23#include <wtf/glib/GUniquePtr.h>
24
25static void testSecurityOriginBasicConstructor(Test*, gconstpointer)
26{
27 WebKitSecurityOrigin* origin = webkit_security_origin_new("http", "127.0.0.1", 1234);
28 g_assert_nonnull(origin);
29 GUniquePtr<char> asString(webkit_security_origin_to_string(origin));
30 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1:1234");
31 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
32 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
33 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 1234);
34 g_assert_false(webkit_security_origin_is_opaque(origin));
35 webkit_security_origin_unref(origin);
36}
37
38static void testSecurityOriginURIConstructor(Test*, gconstpointer)
39{
40 WebKitSecurityOrigin* origin = webkit_security_origin_new_for_uri("http://127.0.0.1:1234");
41 g_assert_nonnull(origin);
42 GUniquePtr<char> asString(webkit_security_origin_to_string(origin));
43 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1:1234");
44 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
45 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
46 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 1234);
47 g_assert_false(webkit_security_origin_is_opaque(origin));
48 webkit_security_origin_unref(origin);
49
50 origin = webkit_security_origin_new_for_uri("http://127.0.0.1:1234/this/path/?should=be#ignored");
51 g_assert_nonnull(origin);
52 asString.reset(webkit_security_origin_to_string(origin));
53 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1:1234");
54 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
55 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
56 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 1234);
57 g_assert_false(webkit_security_origin_is_opaque(origin));
58 webkit_security_origin_unref(origin);
59}
60
61static void testSecurityOriginDefaultPort(Test*, gconstpointer)
62{
63 WebKitSecurityOrigin* origin = webkit_security_origin_new("http", "127.0.0.1", 0);
64 g_assert_nonnull(origin);
65 GUniquePtr<char> asString(webkit_security_origin_to_string(origin));
66 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1");
67 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
68 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
69 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
70 g_assert_false(webkit_security_origin_is_opaque(origin));
71 webkit_security_origin_unref(origin);
72
73 origin = webkit_security_origin_new("http", "127.0.0.1", 80);
74 g_assert_nonnull(origin);
75 asString.reset(webkit_security_origin_to_string(origin));
76 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1");
77 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
78 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
79 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
80 g_assert_false(webkit_security_origin_is_opaque(origin));
81 webkit_security_origin_unref(origin);
82
83 origin = webkit_security_origin_new_for_uri("http://127.0.0.1");
84 g_assert_nonnull(origin);
85 asString.reset(webkit_security_origin_to_string(origin));
86 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1");
87 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
88 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
89 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
90 g_assert_false(webkit_security_origin_is_opaque(origin));
91 webkit_security_origin_unref(origin);
92
93 origin = webkit_security_origin_new_for_uri("http://127.0.0.1:80");
94 g_assert_nonnull(origin);
95 asString.reset(webkit_security_origin_to_string(origin));
96 g_assert_cmpstr(asString.get(), ==, "http://127.0.0.1");
97 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "http");
98 g_assert_cmpstr(webkit_security_origin_get_host(origin), ==, "127.0.0.1");
99 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
100 g_assert_false(webkit_security_origin_is_opaque(origin));
101 webkit_security_origin_unref(origin);
102}
103
104static void testSecurityOriginFileURI(Test*, gconstpointer)
105{
106 WebKitSecurityOrigin* origin = webkit_security_origin_new_for_uri("file:///abcdefg");
107 g_assert_nonnull(origin);
108 GUniquePtr<char> asString(webkit_security_origin_to_string(origin));
109 g_assert_cmpstr(asString.get(), ==, "file://");
110 g_assert_cmpstr(webkit_security_origin_get_protocol(origin), ==, "file");
111 g_assert_null(webkit_security_origin_get_host(origin));
112 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
113 g_assert_false(webkit_security_origin_is_opaque(origin));
114 webkit_security_origin_unref(origin);
115}
116
117static void testOpaqueSecurityOrigin(Test*, gconstpointer)
118{
119 WebKitSecurityOrigin* origin = webkit_security_origin_new_for_uri("data:Lali ho!");
120 g_assert_nonnull(origin);
121 GUniquePtr<char> asString(webkit_security_origin_to_string(origin));
122 g_assert_null(asString);
123 g_assert_null(webkit_security_origin_get_protocol(origin));
124 g_assert_null(webkit_security_origin_get_host(origin));
125 g_assert_cmpint(webkit_security_origin_get_port(origin), ==, 0);
126 g_assert_true(webkit_security_origin_is_opaque(origin));
127 webkit_security_origin_unref(origin);
128}
129
130void beforeAll()
131{
132 Test::add("WebKitSecurityOrigin", "basic-constructor", testSecurityOriginBasicConstructor);
133 Test::add("WebKitSecurityOrigin", "uri-constructor", testSecurityOriginURIConstructor);
134 Test::add("WebKitSecruityOrigin", "default-port", testSecurityOriginDefaultPort);
135 Test::add("WebKitSecurityOrigin", "file-uri", testSecurityOriginFileURI);
136 Test::add("WebKitSecruityOrigin", "opaque-origin", testOpaqueSecurityOrigin);
137}
138
139void afterAll()
140{
141}
142