File: test_ewk2_cookie_manager.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (314 lines) | stat: -rw-r--r-- 12,981 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2012 Igalia S.L.
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include "UnitTestUtils/EWK2UnitTestBase.h"
#include "UnitTestUtils/EWK2UnitTestServer.h"
#include <stdlib.h>
#include <unistd.h>
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>

using namespace EWK2UnitTest;
using namespace WTF;

extern EWK2UnitTestEnvironment* environment;

static const char FIRST_PARTY_DOMAIN[] = "127.0.0.1";
static const char THIRD_PARTY_DOMAIN[] = "localhost";
static const char INDEX_HTML_STRING[] =
    "<html><body>"
    " <p>EFLWebKit2 Cookie Manager test</p>"
    " <img src='http://localhost:%u/image.png' width=5 height=5></img>"
    "</body></html>";

class EWK2CookieManagerTest : public EWK2UnitTestBase {
public:
    static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
    {
        if (message->method != SOUP_METHOD_GET) {
            soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
            return;
        }

        soup_message_set_status(message, SOUP_STATUS_OK);
        if (!strcmp(path, "/index.html")) {
            Eina_Strbuf* buffer = eina_strbuf_new();
            eina_strbuf_append_printf(buffer, INDEX_HTML_STRING, soup_server_get_port(server));
            soup_message_headers_replace(message->response_headers, "Set-Cookie", "foo=bar; Max-Age=60");
            soup_message_body_append(message->response_body, SOUP_MEMORY_TAKE, eina_strbuf_string_steal(buffer), eina_strbuf_length_get(buffer));
            eina_strbuf_free(buffer);
        } else if (!strcmp(path, "/image.png"))
            soup_message_headers_replace(message->response_headers, "Set-Cookie", "baz=qux; Max-Age=60");
        else
            soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);

        soup_message_body_complete(message->response_body);
    }

    static void getAcceptPolicyCallback(Ewk_Cookie_Accept_Policy policy, Ewk_Error* error, void* event_info)
    {
        ASSERT_FALSE(error);
        Ewk_Cookie_Accept_Policy* ret = static_cast<Ewk_Cookie_Accept_Policy*>(event_info);
        *ret = policy;
        ecore_main_loop_quit();
    }

    static void getHostnamesWithCookiesCallback(Eina_List* hostnames, Ewk_Error* error, void* event_info)
    {
        ASSERT_FALSE(error);

        Eina_List** ret = static_cast<Eina_List**>(event_info);
        Eina_List* l;
        void* data;
        EINA_LIST_FOREACH(hostnames, l, data)
            *ret = eina_list_append(*ret, eina_stringshare_ref(static_cast<char*>(data)));
        ecore_main_loop_quit();
    }

    static int compareHostNames(const void* hostName1, const void* hostName2)
    {
        return strcmp(static_cast<const char*>(hostName1), static_cast<const char*>(hostName2));
    }

    static void onCookiesChanged(void *eventInfo)
    {
        bool* cookiesChanged = static_cast<bool*>(eventInfo);
        *cookiesChanged = true;
    }

protected:
    Ewk_Cookie_Accept_Policy getAcceptPolicy(Ewk_Cookie_Manager* manager)
    {
        Ewk_Cookie_Accept_Policy policy = EWK_COOKIE_ACCEPT_POLICY_ALWAYS;
        ewk_cookie_manager_async_accept_policy_get(manager, getAcceptPolicyCallback, &policy);
        ecore_main_loop_begin();
        return policy;
    }

    Eina_List* getHostnamesWithCookies(Ewk_Cookie_Manager* manager)
    {
        Eina_List* ret = 0;
        ewk_cookie_manager_async_hostnames_with_cookies_get(manager, getHostnamesWithCookiesCallback, &ret);
        ecore_main_loop_begin();
        return ret;
    }

    void freeHostNames(Eina_List* hostnames)
    {
        void* data;
        EINA_LIST_FREE(hostnames, data)
            eina_stringshare_del(static_cast<char*>(data));
    }

    int countHostnamesWithCookies(Ewk_Cookie_Manager* manager)
    {
        Eina_List* hostnames = getHostnamesWithCookies(manager);
        int count = eina_list_count(hostnames);
        freeHostNames(hostnames);
        return count;
    }
};

TEST_F(EWK2CookieManagerTest, ewk_cookie_manager_accept_policy)
{
    OwnPtr<EWK2UnitTestServer> httpServer = adoptPtr(new EWK2UnitTestServer);
    httpServer->run(serverCallback);

    Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_view_context_get(webView()));
    ASSERT_TRUE(cookieManager);

    // Default policy is EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY.
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY, getAcceptPolicy(cookieManager));
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));

    Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
    ASSERT_EQ(1, eina_list_count(hostnames));
    ASSERT_STREQ(FIRST_PARTY_DOMAIN, static_cast<char*>(eina_list_nth(hostnames, 0)));
    freeHostNames(hostnames);
    ewk_cookie_manager_cookies_clear(cookieManager);

    // Change policy to EWK_COOKIE_ACCEPT_POLICY_ALWAYS
    ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_ALWAYS, getAcceptPolicy(cookieManager));
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));

    hostnames = getHostnamesWithCookies(cookieManager);
    ASSERT_EQ(2, eina_list_count(hostnames));
    hostnames = eina_list_sort(hostnames, eina_list_count(hostnames), compareHostNames);
    ASSERT_STREQ(FIRST_PARTY_DOMAIN, static_cast<char*>(eina_list_nth(hostnames, 0)));
    ASSERT_STREQ(THIRD_PARTY_DOMAIN, static_cast<char*>(eina_list_nth(hostnames, 1)));
    freeHostNames(hostnames);
    ewk_cookie_manager_cookies_clear(cookieManager);

    // Change policy to EWK_COOKIE_ACCEPT_POLICY_NEVER
    ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_NEVER);
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_NEVER, getAcceptPolicy(cookieManager));
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));
}

TEST_F(EWK2CookieManagerTest, ewk_cookie_manager_changes_watch)
{
    OwnPtr<EWK2UnitTestServer> httpServer = adoptPtr(new EWK2UnitTestServer);
    httpServer->run(serverCallback);

    Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_view_context_get(webView()));
    ASSERT_TRUE(cookieManager);

    ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_ALWAYS, getAcceptPolicy(cookieManager));

    // Watch for changes
    bool cookiesChanged = false;
    ewk_cookie_manager_changes_watch(cookieManager, onCookiesChanged, &cookiesChanged);

    // Check for cookie changes notifications
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));

    while (!cookiesChanged)
        ecore_main_loop_iterate();
    ASSERT_TRUE(cookiesChanged);

    cookiesChanged = false;
    ewk_cookie_manager_cookies_clear(cookieManager);
    while (!cookiesChanged)
        ecore_main_loop_iterate();
    ASSERT_TRUE(cookiesChanged);

    // Stop watching for notifications
    ewk_cookie_manager_changes_watch(cookieManager, 0, 0);
    cookiesChanged = false;
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));
    ASSERT_FALSE(cookiesChanged);

    // Watch again for notifications
    ewk_cookie_manager_changes_watch(cookieManager, onCookiesChanged, &cookiesChanged);

    // Make sure we don't get notifications when loading setting an existing persistent storage
    char textStorage1[] = "/tmp/txt-cookie.XXXXXX";
    ASSERT_TRUE(mktemp(textStorage1));
    char textStorage2[] = "/tmp/txt-cookie.XXXXXX";
    ASSERT_TRUE(mktemp(textStorage2));

    ewk_cookie_manager_persistent_storage_set(cookieManager, textStorage1, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));

    cookiesChanged = false;
    ewk_cookie_manager_persistent_storage_set(cookieManager, textStorage2, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    ewk_cookie_manager_persistent_storage_set(cookieManager, textStorage1, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));

    ASSERT_FALSE(cookiesChanged);

    // Final clean up.
    ewk_cookie_manager_changes_watch(cookieManager, 0, 0);
    unlink(textStorage1);
    unlink(textStorage2);
}

TEST_F(EWK2CookieManagerTest, ewk_cookie_manager_cookies_delete)
{
    OwnPtr<EWK2UnitTestServer> httpServer = adoptPtr(new EWK2UnitTestServer);
    httpServer->run(serverCallback);

    Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_view_context_get(webView()));
    ASSERT_TRUE(cookieManager);

    ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_ALWAYS, getAcceptPolicy(cookieManager));

    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    Eina_List* hostnames = getHostnamesWithCookies(cookieManager);
    ASSERT_EQ(2, eina_list_count(hostnames));
    freeHostNames(hostnames);

    // Delete first party cookie
    ewk_cookie_manager_hostname_cookies_clear(cookieManager, FIRST_PARTY_DOMAIN);
    hostnames = getHostnamesWithCookies(cookieManager);
    ASSERT_EQ(1, eina_list_count(hostnames));
    ASSERT_STREQ(THIRD_PARTY_DOMAIN, static_cast<char*>(eina_list_nth(hostnames, 0)));
    freeHostNames(hostnames);

    // Delete third party cookie
    ewk_cookie_manager_hostname_cookies_clear(cookieManager, THIRD_PARTY_DOMAIN);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    // Get all cookies again
    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));

    // Clear all cookies
    ewk_cookie_manager_cookies_clear(cookieManager);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));
}

TEST_F(EWK2CookieManagerTest, DISABLED_ewk_cookie_manager_permanent_storage)
{
    OwnPtr<EWK2UnitTestServer> httpServer = adoptPtr(new EWK2UnitTestServer);
    httpServer->run(serverCallback);

    // Generate unique names for cookie storages.
    char textStorage[] = "/tmp/txt-cookie.XXXXXX";
    ASSERT_TRUE(mktemp(textStorage));
    char sqliteStorage[] = "/tmp/sqlite-cookie.XXXXXX";
    ASSERT_TRUE(mktemp(sqliteStorage));

    Ewk_Cookie_Manager* cookieManager = ewk_context_cookie_manager_get(ewk_view_context_get(webView()));
    ASSERT_TRUE(cookieManager);

    ewk_cookie_manager_accept_policy_set(cookieManager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
    ASSERT_EQ(EWK_COOKIE_ACCEPT_POLICY_ALWAYS, getAcceptPolicy(cookieManager));

    // Text storage using a new file.
    ewk_cookie_manager_persistent_storage_set(cookieManager, textStorage, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));

    // SQLite storage using a new file.
    ewk_cookie_manager_persistent_storage_set(cookieManager, sqliteStorage, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    ASSERT_TRUE(loadUrlSync(httpServer->getURLForPath("/index.html").data()));
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));

    // Text storage using an existing file.
    ewk_cookie_manager_persistent_storage_set(cookieManager, textStorage, EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));
    ewk_cookie_manager_cookies_clear(cookieManager);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    // SQLite storage with an existing file.
    ewk_cookie_manager_persistent_storage_set(cookieManager, sqliteStorage, EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
    ASSERT_EQ(2, countHostnamesWithCookies(cookieManager));
    ewk_cookie_manager_cookies_clear(cookieManager);
    ASSERT_EQ(0, countHostnamesWithCookies(cookieManager));

    // Final clean up.
    unlink(textStorage);
    unlink(sqliteStorage);
}