File: test_ewk2_auth_request.cpp

package info (click to toggle)
qtwebkit 2.3.4.dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 290,116 kB
  • ctags: 272,544
  • sloc: cpp: 1,417,496; python: 85,048; ansic: 39,353; perl: 38,858; ruby: 10,313; objc: 9,505; xml: 8,679; asm: 3,864; yacc: 2,458; sh: 1,237; lex: 813; makefile: 592; java: 228; php: 79
file content (195 lines) | stat: -rw-r--r-- 8,130 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
/*
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

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

using namespace EWK2UnitTest;

extern EWK2UnitTestEnvironment* environment;

static const char TEST_USERNAME[] = "username";
static const char TEST_PASSWORD[] = "password";
static const char EXPECTED_AUTHORIZATION[] = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="; // Base64 encoding of "username:password".
static const char INDEX_HTML_STRING[] =
    "<html>"
    "<head><title>EFLWebKit2 Authentication test</title></head>"
    "<body></body></html>";

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

    if (!strcmp(path, "/index.html")) {
        const char* authorization = soup_message_headers_get_one(message->request_headers, "Authorization");
        // Require authentication
        if (authorization && !strcmp(authorization, EXPECTED_AUTHORIZATION)) {
            // Successful authentication.
            soup_message_set_status(message, SOUP_STATUS_OK);
            soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, INDEX_HTML_STRING, strlen(INDEX_HTML_STRING));
        } else {
            // No (valid) authorization header provided by the client, request authentication.
            soup_message_set_status(message, SOUP_STATUS_UNAUTHORIZED);
            soup_message_headers_append(message->response_headers, "WWW-Authenticate", "Basic realm=\"my realm\"");
        }
    } else
        soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);

    soup_message_body_complete(message->response_body);
}

static void onAuthenticationRequest(void* userData, Evas_Object*, void* eventInfo)
{
    Ewk_Auth_Request** returnRequest = static_cast<Ewk_Auth_Request**>(userData);
    ASSERT_TRUE(returnRequest);

    Ewk_Auth_Request* request = static_cast<Ewk_Auth_Request*>(eventInfo);
    ASSERT_TRUE(request);

    *returnRequest = ewk_object_ref(request);
}

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

    Ewk_Auth_Request* authenticationRequest = 0;
    evas_object_smart_callback_add(webView(), "authentication,request", onAuthenticationRequest, &authenticationRequest);

    ewk_view_url_set(webView(), httpServer->getURLForPath("/index.html").data());

    while (!authenticationRequest)
        ecore_main_loop_iterate();

    ASSERT_TRUE(authenticationRequest);
    evas_object_smart_callback_del(webView(), "authentication,request", onAuthenticationRequest);

    EXPECT_STREQ("my realm", ewk_auth_request_realm_get(authenticationRequest));
    EXPECT_STREQ("127.0.0.1", ewk_auth_request_host_get(authenticationRequest));
    EXPECT_FALSE(ewk_auth_request_retrying_get(authenticationRequest));

    ASSERT_TRUE(ewk_auth_request_authenticate(authenticationRequest, TEST_USERNAME, TEST_PASSWORD));

    ewk_object_unref(authenticationRequest);

    ASSERT_TRUE(waitUntilTitleChangedTo("EFLWebKit2 Authentication test"));
}

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

    Ewk_Auth_Request* authenticationRequest = 0;
    evas_object_smart_callback_add(webView(), "authentication,request", onAuthenticationRequest, &authenticationRequest);

    ewk_view_url_set(webView(), httpServer->getURLForPath("/index.html").data());

    while (!authenticationRequest)
        ecore_main_loop_iterate();

    ASSERT_TRUE(authenticationRequest);

    EXPECT_STREQ("my realm", ewk_auth_request_realm_get(authenticationRequest));
    EXPECT_STREQ("127.0.0.1", ewk_auth_request_host_get(authenticationRequest));
    EXPECT_FALSE(ewk_auth_request_retrying_get(authenticationRequest));

    ASSERT_TRUE(ewk_auth_request_authenticate(authenticationRequest, TEST_USERNAME, "wrongpassword"));

    ewk_object_unref(authenticationRequest);
    authenticationRequest = 0;

    // We expect a second authentication request since the first one failed.
    while (!authenticationRequest)
        ecore_main_loop_iterate();
    evas_object_smart_callback_del(webView(), "authentication,request", onAuthenticationRequest);

    EXPECT_STREQ("my realm", ewk_auth_request_realm_get(authenticationRequest));
    EXPECT_STREQ("127.0.0.1", ewk_auth_request_host_get(authenticationRequest));
    EXPECT_TRUE(ewk_auth_request_retrying_get(authenticationRequest));

    // Now provide the right password.
    ASSERT_TRUE(ewk_auth_request_authenticate(authenticationRequest, TEST_USERNAME, TEST_PASSWORD));

    ewk_object_unref(authenticationRequest);

    ASSERT_TRUE(waitUntilTitleChangedTo("EFLWebKit2 Authentication test"));
}

static void onResourceLoadResponse(void* userData, Evas_Object*, void* eventInfo)
{
    int* statusCode = static_cast<int*>(userData);
    ASSERT_TRUE(statusCode);

    Ewk_Resource_Load_Response* response = static_cast<Ewk_Resource_Load_Response*>(eventInfo);
    ASSERT_TRUE(response);

    if (!ewk_resource_main_resource_get(response->resource))
        return;

    *statusCode = ewk_url_response_status_code_get(response->response);
}

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

    Ewk_Auth_Request* authenticationRequest = 0;
    evas_object_smart_callback_add(webView(), "authentication,request", onAuthenticationRequest, &authenticationRequest);

    ewk_view_url_set(webView(), httpServer->getURLForPath("/index.html").data());

    while (!authenticationRequest)
        ecore_main_loop_iterate();

    ASSERT_TRUE(authenticationRequest);
    evas_object_smart_callback_del(webView(), "authentication,request", onAuthenticationRequest);

    EXPECT_STREQ("my realm", ewk_auth_request_realm_get(authenticationRequest));
    EXPECT_STREQ("127.0.0.1", ewk_auth_request_host_get(authenticationRequest));
    EXPECT_FALSE(ewk_auth_request_retrying_get(authenticationRequest));

    int statusCode = 0;
    evas_object_smart_callback_add(webView(), "resource,request,response", onResourceLoadResponse, &statusCode);

    // Will attempt to continue without authentication by default.
    ewk_object_unref(authenticationRequest);

    while (!statusCode)
        ecore_main_loop_iterate();

    // We should get a "402 Unauthorized" error.
    EXPECT_EQ(SOUP_STATUS_UNAUTHORIZED, statusCode);

    evas_object_smart_callback_del(webView(), "resource,request,response", onResourceLoadResponse);
}