1/*
2 * Copyright (C) 2012 Igalia S.L.
3 * Copyright (C) 2012 Intel Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
23#include "UnitTestUtils/EWK2UnitTestBase.h"
24#include "UnitTestUtils/EWK2UnitTestEnvironment.h"
25#include "UnitTestUtils/EWK2UnitTestServer.h"
26#include <EWebKit2.h>
27#include <Ecore.h>
28#include <Eina.h>
29#include <unistd.h>
30
31using namespace EWK2UnitTest;
32
33extern EWK2UnitTestEnvironment* environment;
34
35static const char FIRST_PARTY_DOMAIN[] = "127.0.0.1";
36static const char THIRD_PARTY_DOMAIN[] = "localhost";
37static const char TMP_COOKIE_FILE_TXT[] = "/tmp/cookies.txt";
38static const char TMP_COOKIE_FILE_SQLITE[] = "/tmp/cookies.db";
39static const char INDEX_HTML_STRING[] =
40 "<html><body>"
41 " <p>EFLWebKit2 Cookie Manager test</p>"
42 " <img src='http://localhost:%u/image.png' width=5 height=5></img>"
43 "</body></html>";
44
45static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
46{
47 if (message->method != SOUP_METHOD_GET) {
48 soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
49 return;
50 }
51
52 soup_message_set_status(message, SOUP_STATUS_OK);
53 if (!strcmp(path, "/index.html")) {
54 Eina_Strbuf* buffer = eina_strbuf_new();
55 eina_strbuf_append_printf(buffer, INDEX_HTML_STRING, soup_server_get_port(server));
56 soup_message_headers_replace(message->response_headers, "Set-Cookie", "foo=bar; Max-Age=60");
57 soup_message_body_append(message->response_body, SOUP_MEMORY_TAKE, eina_strbuf_string_steal(buffer), eina_strbuf_length_get(buffer));
58 eina_strbuf_string_free(buffer);
59 } else if (!strcmp(path, "/image.png"))
60 soup_message_headers_replace(message->response_headers, "Set-Cookie", "baz=qux; Max-Age=60");
61 else
62 FAIL();
63 soup_message_body_complete(message->response_body);
64}
65
66static void getAcceptPolicyCallback(Ewk_Cookie_Accept_Policy policy, Ewk_Web_Error* error, void* event_info)
67{
68 ASSERT(!error);
69 Ewk_Cookie_Accept_Policy* ret = static_cast<Ewk_Cookie_Accept_Policy*>(event_info);
70 *ret = policy;
71 ecore_main_loop_quit();
72}
73
74static Ewk_Cookie_Accept_Policy getAcceptPolicy(Ewk_Cookie_Manager* manager)
75{
76 Ewk_Cookie_Accept_Policy policy = EWK_COOKIE_ACCEPT_POLICY_ALWAYS;
77 ewk_cookie_manager_async_accept_policy_get(manager, getAcceptPolicyCallback, &policy);
78 ecore_main_loop_begin();
79 return policy;
80}
81
82static void getHostnamesWithCookiesCallback(Eina_List* hostnames, Ewk_Web_Error* error, void* event_info)
83{
84 ASSERT(!error);
85
86 Eina_List** ret = static_cast<Eina_List**>(event_info);
87 Eina_List* l;
88 void* data;
89 EINA_LIST_FOREACH(hostnames, l, data)
90 *ret = eina_list_append(*ret, eina_stringshare_ref(static_cast<char*>(data)));
91 ecore_main_loop_quit();
92}
93
94static Eina_List* getHostnamesWithCookies(Ewk_Cookie_Manager* manager)
95{
96 Eina_List* ret = 0;
97 ewk_cookie_manager_async_hostnames_with_cookies_get(manager, getHostnamesWithCookiesCallback, &ret);
98 ecore_main_loop_begin();
99 return ret;
100}
101
102static void freeHostNames(Eina_List* hostnames)
103{
104 void* data;
105 EINA_LIST_FREE(hostnames, data)
106 eina_stringshare_del(static_cast<char*>(data));
107}
108
109static int countHostnamesWithCookies(Ewk_Cookie_Manager* manager)
110{
111 Eina_List* hostnames = getHostnamesWithCookies(manager);
112 int count = eina_list_count(hostnames);
113 freeHostNames(hostnames);
114 return count;
115}
116
117TEST_F(EWK2UnitTestBase, ewk_cookie_manager_accept_policy)
118{
119 g_type_init();
120 ecore_main_loop_glib_integrate();
121
122 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
123 httpServer->run(serverCallback);
124
125 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
126 ASSERT(cookieManager);
127
128 // Default policy is EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY.
129 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY);
130 loadUrlSync(httpServer->getURIForPath("/index.html").data());
131
132 Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
133 ASSERT_EQ(eina_list_count(hostnames), 1);
134 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), FIRST_PARTY_DOMAIN);
135 freeHostNames(hostnames);
136 ewk_cookie_manager_cookies_clear(cookieManager);
137
138 // Change policy to EWK_COOKIE_ACCEPT_POLICY_ALWAYS
139 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
140 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
141 loadUrlSync(httpServer->getURIForPath("/index.html").data());
142
143 hostnames = getHostnamesWithCookies(cookieManager);
144 ASSERT_EQ(eina_list_count(hostnames), 2);
145 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), FIRST_PARTY_DOMAIN);
146 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 1)), THIRD_PARTY_DOMAIN);
147 freeHostNames(hostnames);
148 ewk_cookie_manager_cookies_clear(cookieManager);
149
150 // Change policy to EWK_COOKIE_ACCEPT_POLICY_NEVER
151 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_NEVER);
152 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_NEVER);
153 loadUrlSync(httpServer->getURIForPath("/index.html").data());
154 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
155
156 delete httpServer;
157}
158
159TEST_F(EWK2UnitTestBase, ewk_cookie_manager_cookies_delete)
160{
161 g_type_init();
162 ecore_main_loop_glib_integrate();
163
164 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
165 httpServer->run(serverCallback);
166
167 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
168 ASSERT(cookieManager);
169
170 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
171 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
172
173 loadUrlSync(httpServer->getURIForPath("/index.html").data());
174 Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
175 ASSERT_EQ(eina_list_count(hostnames), 2);
176 freeHostNames(hostnames);
177
178 // Delete first party cookie
179 ewk_cookie_manager_hostname_cookies_clear(cookieManager, FIRST_PARTY_DOMAIN);
180 hostnames = getHostnamesWithCookies(cookieManager);
181 ASSERT_EQ(eina_list_count(hostnames), 1);
182 ASSERT_STREQ(static_cast<char*>(eina_list_nth(hostnames, 0)), THIRD_PARTY_DOMAIN);
183 freeHostNames(hostnames);
184
185 // Delete third party cookie
186 ewk_cookie_manager_hostname_cookies_clear(cookieManager, THIRD_PARTY_DOMAIN);
187 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
188
189 // Get all cookies again
190 loadUrlSync(httpServer->getURIForPath("/index.html").data());
191 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
192
193 // Clear all cookies
194 ewk_cookie_manager_cookies_clear(cookieManager);
195 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
196
197 delete httpServer;
198}
199
200static void cleanUpCookieFiles()
201{
202 unlink(TMP_COOKIE_FILE_TXT);
203 unlink(TMP_COOKIE_FILE_SQLITE);
204}
205
206TEST_F(EWK2UnitTestBase, ewk_cookie_manager_permanent_storage)
207{
208 g_type_init();
209 ecore_main_loop_glib_integrate();
210
211 EWK2UnitTestServer* httpServer = new EWK2UnitTestServer();
212 httpServer->run(serverCallback);
213
214 // initial clean up
215 cleanUpCookieFiles();
216
217 Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_context_default_get());
218 ASSERT(cookieManager);
219
220 ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
221 ASSERT_EQ(getAcceptPolicy(cookieManager), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
222
223 // Text storage using a new file.
224 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_TXT, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
225 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
226
227 loadUrlSync(httpServer->getURIForPath("/index.html").data());
228 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
229
230 // SQLite storage using a new file.
231 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_SQLITE, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
232 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
233
234 loadUrlSync(httpServer->getURIForPath("/index.html").data());
235 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
236
237 // Text storage using an existing file.
238 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_TXT, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
239 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
240 ewk_cookie_manager_cookies_clear(cookieManager);
241 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
242
243 // SQLite storage with an existing file.
244 ewk_cookie_manager_persistent_storage_set(cookieManager, TMP_COOKIE_FILE_SQLITE, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
245 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 2);
246 ewk_cookie_manager_cookies_clear(cookieManager);
247 ASSERT_EQ(countHostnamesWithCookies(cookieManager), 0);
248
249 // Final clean up
250 cleanUpCookieFiles();
251}
252