OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 // Unit tests for MockGaiaAuthenticator. | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/port.h" | |
9 #include "chrome/browser/sync/protocol/service_constants.h" | |
10 #include "chrome/browser/sync/test/engine/mock_gaia_authenticator.h" | |
11 #include "chrome/common/net/gaia/gaia_authenticator.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace { | |
15 | |
16 // Test if authentication succeeds for a mock user added earlier. | |
17 TEST(MockGaiaAuthenticatorTest, TestAuthenticationSuccess) { | |
18 browser_sync::MockGaiaAuthenticator | |
19 mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
20 "some random url"); | |
21 | |
22 // Initialize a mock user, and add to mock authenticator. | |
23 browser_sync::MockUser mock_user; | |
24 mock_user.email = "test"; | |
25 mock_user.passwd = "passwd"; | |
26 mock_user.auth_token = "SomeAuthToken"; | |
27 mock_user.lsid = "SomeLSID"; | |
28 mock_user.sid = "SomeSID"; | |
29 mock_user.auth_error = gaia::None; | |
30 mock_gaia_auth.AddMockUser(mock_user); | |
31 | |
32 // Assert away ... | |
33 ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd")); | |
34 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
35 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
36 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
37 } | |
38 | |
39 // Test if authentication fails for a mock user that was never added. | |
40 TEST(MockGaiaAuthenticatorTest, TestAuthenticationFailure) { | |
41 browser_sync::MockGaiaAuthenticator | |
42 mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
43 "some random url"); | |
44 | |
45 // At this point, in real code, we would be adding mock users to our mock | |
46 // object. However, in this unittest, we exercise the path where this step is | |
47 // missing, and assert that the outcome is still consistent with that of the | |
48 // real GaiaAuthenticator. | |
49 | |
50 // Assert away ... | |
51 ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd")); | |
52 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), ""); | |
53 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), ""); | |
54 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), ""); | |
55 } | |
56 | |
57 // Test if authentication fails after a mock user is removed. | |
58 TEST(MockGaiaAuthenticatorTest, TestRemoveMockUser) { | |
59 // Instantiate authenticator. | |
60 browser_sync::MockGaiaAuthenticator | |
61 mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
62 "some random url"); | |
63 | |
64 // Add our mock user | |
65 mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID", | |
66 "SomeSID", gaia::None); | |
67 | |
68 // Make sure authentication succeeds. | |
69 ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd")); | |
70 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
71 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
72 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
73 | |
74 // Remove the just-added user from our list. | |
75 mock_gaia_auth.RemoveMockUser("test"); | |
76 | |
77 // ... and authentication should fail. | |
78 ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd")); | |
79 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), ""); | |
80 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), ""); | |
81 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), ""); | |
82 } | |
83 | |
84 // Test if authentication fails after all mock users are removed. | |
85 TEST(MockGaiaAuthenticatorTest, TestRemoveAllMockUsers) { | |
86 // Instantiate authenticator. | |
87 browser_sync::MockGaiaAuthenticator | |
88 mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
89 "some random url"); | |
90 | |
91 // Add our sample mock user. | |
92 mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID", | |
93 "SomeSID", gaia::None); | |
94 | |
95 // Make sure authentication succeeds | |
96 ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd")); | |
97 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
98 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
99 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
100 | |
101 // Now remove all mock users. | |
102 mock_gaia_auth.RemoveAllMockUsers(); | |
103 | |
104 // And confirm that authentication fails. | |
105 ASSERT_FALSE(mock_gaia_auth.Authenticate("test", "passwd")); | |
106 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), ""); | |
107 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), ""); | |
108 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), ""); | |
109 } | |
110 | |
111 // Test authentication with saved credentials. | |
112 TEST(MockGaiaAuthenticatorTest, TestSavedCredentials) { | |
113 // Instantiate authenticator. | |
114 browser_sync::MockGaiaAuthenticator | |
115 mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, | |
116 "some random url"); | |
117 | |
118 // Add our sample mock user. | |
119 mock_gaia_auth.AddMockUser("test", "passwd", "SomeAuthToken", "SomeLSID", | |
120 "SomeSID", gaia::None); | |
121 | |
122 // Ask to save credentials. | |
123 ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd", true)); | |
124 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
125 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
126 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
127 | |
128 // Now make a call that uses saved credentials, and assert that we get the | |
129 // same tokens back. | |
130 ASSERT_TRUE(mock_gaia_auth.Authenticate()); | |
131 ASSERT_STREQ(mock_gaia_auth.email().c_str(), "test"); | |
132 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
133 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
134 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
135 | |
136 // Now clear the saved credentials by toggling the flag while authenticating. | |
137 ASSERT_TRUE(mock_gaia_auth.Authenticate("test", "passwd", false)); | |
138 | |
139 // Test if saved credentials have been cleared. | |
140 ASSERT_STREQ(mock_gaia_auth.email().c_str(), ""); | |
141 | |
142 // Assert that current authentication session still succeeds (we only asked | |
143 // not to save it for future requests.) | |
144 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), "SomeAuthToken"); | |
145 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), "SomeSID"); | |
146 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), "SomeLSID"); | |
147 | |
148 // Now try to use saved credentials: | |
149 ASSERT_STREQ(mock_gaia_auth.email().c_str(), ""); | |
150 ASSERT_FALSE(mock_gaia_auth.Authenticate()); | |
151 | |
152 // And assert that any future requests that rely on saved credentials fail. | |
153 ASSERT_STREQ(mock_gaia_auth.auth_token().c_str(), ""); | |
154 ASSERT_STREQ(mock_gaia_auth.sid().c_str(), ""); | |
155 ASSERT_STREQ(mock_gaia_auth.lsid().c_str(), ""); | |
156 } | |
157 | |
158 } // namespace | |
OLD | NEW |