OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 #include "chrome/browser/chromeos/login/cryptohome_op.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/message_loop.h" |
| 10 #include "base/ref_counted.h" |
| 11 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/chromeos/cros/mock_library_loader.h" |
| 13 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" |
| 14 #include "chrome/browser/chromeos/login/auth_attempt_state.h" |
| 15 #include "chrome/browser/chromeos/login/mock_auth_attempt_state_resolver.h" |
| 16 #include "chrome/browser/chromeos/login/test_attempt_state.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using ::testing::AnyNumber; |
| 21 using ::testing::Invoke; |
| 22 using ::testing::Return; |
| 23 using ::testing::_; |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 class CryptohomeOpTest : public ::testing::Test { |
| 28 public: |
| 29 CryptohomeOpTest() |
| 30 : message_loop_(MessageLoop::TYPE_UI), |
| 31 ui_thread_(ChromeThread::UI, &message_loop_), |
| 32 io_thread_(ChromeThread::IO), |
| 33 username_("me@nowhere.org"), |
| 34 hash_ascii_("0a010000000000a0"), |
| 35 state_(username_, "", hash_ascii_, "", ""), |
| 36 resolver_(new MockAuthAttemptStateResolver), |
| 37 mock_library_(new MockCryptohomeLibrary) { |
| 38 } |
| 39 |
| 40 virtual ~CryptohomeOpTest() {} |
| 41 |
| 42 virtual void SetUp() { |
| 43 CrosLibrary::TestApi* test_api = CrosLibrary::Get()->GetTestApi(); |
| 44 |
| 45 MockLibraryLoader* loader = new MockLibraryLoader(); |
| 46 ON_CALL(*loader, Load(_)) |
| 47 .WillByDefault(Return(true)); |
| 48 EXPECT_CALL(*loader, Load(_)) |
| 49 .Times(AnyNumber()); |
| 50 |
| 51 // Passes ownership of |loader| to CrosLibrary. |
| 52 test_api->SetLibraryLoader(loader, true); |
| 53 // |mock_library_| is mine, though. |
| 54 test_api->SetCryptohomeLibrary(mock_library_.get(), false); |
| 55 |
| 56 io_thread_.Start(); |
| 57 } |
| 58 |
| 59 virtual void TearDown() { |
| 60 // Prevent bogus gMock leak check from firing. |
| 61 chromeos::CrosLibrary::TestApi* test_api = |
| 62 chromeos::CrosLibrary::Get()->GetTestApi(); |
| 63 test_api->SetLibraryLoader(NULL, false); |
| 64 } |
| 65 |
| 66 void ExpectMigrate(bool passing_old_hash, const std::string& hash) { |
| 67 if (passing_old_hash) { |
| 68 EXPECT_CALL(*(mock_library_.get()), AsyncMigrateKey(username_, |
| 69 hash, |
| 70 hash_ascii_, |
| 71 _)) |
| 72 .Times(1) |
| 73 .RetiresOnSaturation(); |
| 74 } else { |
| 75 EXPECT_CALL(*(mock_library_.get()), AsyncMigrateKey(username_, |
| 76 hash_ascii_, |
| 77 hash, |
| 78 _)) |
| 79 .Times(1) |
| 80 .RetiresOnSaturation(); |
| 81 } |
| 82 } |
| 83 |
| 84 void ExpectMount() { |
| 85 EXPECT_CALL(*(mock_library_.get()), AsyncMount(username_, hash_ascii_, _)) |
| 86 .Times(1) |
| 87 .RetiresOnSaturation(); |
| 88 } |
| 89 |
| 90 void ExpectMountGuest() { |
| 91 EXPECT_CALL(*(mock_library_.get()), AsyncMountForBwsi(_)) |
| 92 .Times(1) |
| 93 .RetiresOnSaturation(); |
| 94 } |
| 95 |
| 96 void ExpectRemove() { |
| 97 EXPECT_CALL(*(mock_library_.get()), AsyncRemove(username_, _)) |
| 98 .Times(1) |
| 99 .RetiresOnSaturation(); |
| 100 } |
| 101 |
| 102 void RunTest(CryptohomeOp* op) { |
| 103 EXPECT_CALL(*(resolver_.get()), Resolve()) |
| 104 .Times(1) |
| 105 .RetiresOnSaturation(); |
| 106 |
| 107 EXPECT_TRUE(op->Initiate()); |
| 108 // Force IO thread to finish tasks so I can verify |state_|. |
| 109 io_thread_.Stop(); |
| 110 } |
| 111 |
| 112 void RunMountTest(CryptohomeOp* op, bool outcome, int code) { |
| 113 mock_library_->SetAsyncBehavior(outcome, code); |
| 114 |
| 115 RunTest(op); |
| 116 |
| 117 EXPECT_EQ(outcome, state_.offline_outcome()); |
| 118 EXPECT_EQ(code, state_.offline_code()); |
| 119 } |
| 120 |
| 121 void RunNonMountTest(CryptohomeOp* op, bool outcome, int code) { |
| 122 mock_library_->SetAsyncBehavior(outcome, code); |
| 123 |
| 124 RunTest(op); |
| 125 |
| 126 if (outcome) { |
| 127 EXPECT_EQ(false, state_.offline_complete()); |
| 128 EXPECT_EQ(false, state_.offline_outcome()); |
| 129 EXPECT_EQ(kCryptohomeMountErrorNone, state_.offline_code()); |
| 130 } else { |
| 131 EXPECT_EQ(true, state_.offline_complete()); |
| 132 EXPECT_EQ(outcome, state_.offline_outcome()); |
| 133 EXPECT_EQ(code, state_.offline_code()); |
| 134 } |
| 135 } |
| 136 |
| 137 MessageLoop message_loop_; |
| 138 ChromeThread ui_thread_; |
| 139 ChromeThread io_thread_; |
| 140 std::string username_; |
| 141 std::string hash_ascii_; |
| 142 TestAttemptState state_; |
| 143 scoped_ptr<MockAuthAttemptStateResolver> resolver_; |
| 144 scoped_refptr<CryptohomeOp> op_; |
| 145 scoped_ptr<MockCryptohomeLibrary> mock_library_; |
| 146 |
| 147 }; |
| 148 |
| 149 TEST_F(CryptohomeOpTest, MountSuccess) { |
| 150 ExpectMount(); |
| 151 scoped_refptr<CryptohomeOp> op(new MountAttempt(&state_, resolver_.get())); |
| 152 RunMountTest(op.get(), true, kCryptohomeMountErrorNone); |
| 153 } |
| 154 |
| 155 TEST_F(CryptohomeOpTest, MountFatal) { |
| 156 ExpectMount(); |
| 157 scoped_refptr<CryptohomeOp> op(new MountAttempt(&state_, resolver_.get())); |
| 158 RunMountTest(op.get(), false, kCryptohomeMountErrorFatal); |
| 159 } |
| 160 |
| 161 TEST_F(CryptohomeOpTest, MountKeyFailure) { |
| 162 ExpectMount(); |
| 163 scoped_refptr<CryptohomeOp> op(new MountAttempt(&state_, resolver_.get())); |
| 164 RunMountTest(op.get(), false, kCryptohomeMountErrorKeyFailure); |
| 165 } |
| 166 |
| 167 TEST_F(CryptohomeOpTest, MountRecreated) { |
| 168 ExpectMount(); |
| 169 scoped_refptr<CryptohomeOp> op(new MountAttempt(&state_, resolver_.get())); |
| 170 RunMountTest(op.get(), true, kCryptohomeMountErrorRecreated); |
| 171 } |
| 172 |
| 173 TEST_F(CryptohomeOpTest, MountGuestSuccess) { |
| 174 ExpectMountGuest(); |
| 175 scoped_refptr<CryptohomeOp> op(new MountGuestAttempt(&state_, |
| 176 resolver_.get())); |
| 177 RunMountTest(op.get(), true, kCryptohomeMountErrorNone); |
| 178 } |
| 179 |
| 180 TEST_F(CryptohomeOpTest, MountGuestFatal) { |
| 181 ExpectMountGuest(); |
| 182 scoped_refptr<CryptohomeOp> op(new MountGuestAttempt(&state_, |
| 183 resolver_.get())); |
| 184 RunMountTest(op.get(), false, kCryptohomeMountErrorFatal); |
| 185 } |
| 186 |
| 187 TEST_F(CryptohomeOpTest, MigrateSuccessPassOld) { |
| 188 ExpectMigrate(true, ""); |
| 189 scoped_refptr<CryptohomeOp> op(new MigrateAttempt(&state_, |
| 190 resolver_.get(), |
| 191 true, |
| 192 "")); |
| 193 RunNonMountTest(op.get(), true, kCryptohomeMountErrorNone); |
| 194 } |
| 195 |
| 196 TEST_F(CryptohomeOpTest, MigrateSuccessPassNew) { |
| 197 ExpectMigrate(false, ""); |
| 198 scoped_refptr<CryptohomeOp> op(new MigrateAttempt(&state_, |
| 199 resolver_.get(), |
| 200 false, |
| 201 "")); |
| 202 RunNonMountTest(op.get(), true, kCryptohomeMountErrorNone); |
| 203 } |
| 204 |
| 205 TEST_F(CryptohomeOpTest, MigrateKeyFailure) { |
| 206 ExpectMigrate(true, ""); |
| 207 scoped_refptr<CryptohomeOp> op(new MigrateAttempt(&state_, |
| 208 resolver_.get(), |
| 209 true, |
| 210 "")); |
| 211 RunNonMountTest(op.get(), false, kCryptohomeMountErrorKeyFailure); |
| 212 } |
| 213 |
| 214 TEST_F(CryptohomeOpTest, RemoveSuccess) { |
| 215 ExpectRemove(); |
| 216 scoped_refptr<CryptohomeOp> op(new RemoveAttempt(&state_, resolver_.get())); |
| 217 RunNonMountTest(op.get(), true, kCryptohomeMountErrorNone); |
| 218 } |
| 219 |
| 220 TEST_F(CryptohomeOpTest, RemoveFailure) { |
| 221 ExpectRemove(); |
| 222 scoped_refptr<CryptohomeOp> op(new RemoveAttempt(&state_, resolver_.get())); |
| 223 RunNonMountTest(op.get(), false, kCryptohomeMountErrorKeyFailure); |
| 224 } |
| 225 |
| 226 } // namespace chromeos |
OLD | NEW |