1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | package org.chromium.android_webview.test; |
6 | |
7 | import android.test.AndroidTestCase; |
8 | import android.test.suitebuilder.annotation.SmallTest; |
9 | |
10 | import org.chromium.android_webview.HttpAuthDatabase; |
11 | import org.chromium.base.test.util.Feature; |
12 | |
13 | /** |
14 | * Test suite for HttpAuthDatabase. |
15 | */ |
16 | public class HttpAuthDatabaseTest extends AndroidTestCase { |
17 | |
18 | private static final String TEST_DATABASE = "http_auth_for_HttpAuthDatabaseTest.db"; |
19 | |
20 | @Override |
21 | protected void setUp() throws Exception { |
22 | super.setUp(); |
23 | getContext().deleteDatabase(TEST_DATABASE); |
24 | } |
25 | |
26 | @Override |
27 | protected void tearDown() throws Exception { |
28 | getContext().deleteDatabase(TEST_DATABASE); |
29 | super.tearDown(); |
30 | } |
31 | |
32 | @SmallTest |
33 | @Feature({"AndroidWebView"}) |
34 | public void testAccessHttpAuthUsernamePassword() throws Exception { |
35 | HttpAuthDatabase instance = new HttpAuthDatabase(getContext(), TEST_DATABASE); |
36 | |
37 | String host = "http://localhost:8080"; |
38 | String realm = "testrealm"; |
39 | String userName = "user"; |
40 | String password = "password"; |
41 | |
42 | String[] result = instance.getHttpAuthUsernamePassword(host, realm); |
43 | assertNull(result); |
44 | |
45 | instance.setHttpAuthUsernamePassword(host, realm, userName, password); |
46 | result = instance.getHttpAuthUsernamePassword(host, realm); |
47 | assertNotNull(result); |
48 | assertEquals(userName, result[0]); |
49 | assertEquals(password, result[1]); |
50 | |
51 | String newPassword = "newpassword"; |
52 | instance.setHttpAuthUsernamePassword(host, realm, userName, newPassword); |
53 | result = instance.getHttpAuthUsernamePassword(host, realm); |
54 | assertNotNull(result); |
55 | assertEquals(userName, result[0]); |
56 | assertEquals(newPassword, result[1]); |
57 | |
58 | String newUserName = "newuser"; |
59 | instance.setHttpAuthUsernamePassword(host, realm, newUserName, newPassword); |
60 | result = instance.getHttpAuthUsernamePassword(host, realm); |
61 | assertNotNull(result); |
62 | assertEquals(newUserName, result[0]); |
63 | assertEquals(newPassword, result[1]); |
64 | |
65 | instance.setHttpAuthUsernamePassword(host, realm, null, password); |
66 | result = instance.getHttpAuthUsernamePassword(host, realm); |
67 | assertNotNull(result); |
68 | assertNull(result[0]); |
69 | assertEquals(password, result[1]); |
70 | |
71 | instance.setHttpAuthUsernamePassword(host, realm, userName, null); |
72 | result = instance.getHttpAuthUsernamePassword(host, realm); |
73 | assertNotNull(result); |
74 | assertEquals(userName, result[0]); |
75 | assertEquals(null, result[1]); |
76 | |
77 | instance.setHttpAuthUsernamePassword(host, realm, null, null); |
78 | result = instance.getHttpAuthUsernamePassword(host, realm); |
79 | assertNotNull(result); |
80 | assertNull(result[0]); |
81 | assertNull(result[1]); |
82 | |
83 | instance.setHttpAuthUsernamePassword(host, realm, newUserName, newPassword); |
84 | result = instance.getHttpAuthUsernamePassword(host, realm); |
85 | assertNotNull(result); |
86 | assertEquals(newUserName, result[0]); |
87 | assertEquals(newPassword, result[1]); |
88 | } |
89 | } |