device_oauth2_token_service_unittest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright 2013 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/settings/device_oauth2_token_service.h" 6 7#include "base/message_loop.h" 8#include "base/prefs/testing_pref_service.h" 9#include "chrome/test/base/scoped_testing_local_state.h" 10#include "chrome/test/base/testing_browser_process.h" 11#include "chromeos/cryptohome/mock_cryptohome_library.h" 12#include "content/public/browser/browser_thread.h" 13#include "content/public/test/test_browser_thread.h" 14#include "net/url_request/url_request_test_util.h" 15#include "testing/gmock/include/gmock/gmock.h" 16#include "testing/gtest/include/gtest/gtest.h" 17 18using ::testing::_; 19using ::testing::AnyNumber; 20using ::testing::Return; 21using ::testing::StrEq; 22using ::testing::StrictMock; 23 24namespace chromeos { 25 26class DeviceOAuth2TokenServiceTest : public testing::Test { 27 public: 28 DeviceOAuth2TokenServiceTest() 29 : ui_thread_(content::BrowserThread::UI, &message_loop_), 30 scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()) {} 31 virtual ~DeviceOAuth2TokenServiceTest() {} 32 33 virtual void SetUp() OVERRIDE { 34 } 35 36 virtual void TearDown() OVERRIDE { 37 } 38 39 protected: 40 base::MessageLoop message_loop_; 41 content::TestBrowserThread ui_thread_; 42 ScopedTestingLocalState scoped_testing_local_state_; 43}; 44 45TEST_F(DeviceOAuth2TokenServiceTest, SaveEncryptedToken) { 46 StrictMock<MockCryptohomeLibrary> mock_cryptohome_library; 47 CryptohomeLibrary::SetForTest(&mock_cryptohome_library); 48 49 EXPECT_CALL(mock_cryptohome_library, DecryptWithSystemSalt(StrEq(""))) 50 .Times(1) 51 .WillOnce(Return("")); 52 EXPECT_CALL(mock_cryptohome_library, 53 EncryptWithSystemSalt(StrEq("test-token"))) 54 .Times(1) 55 .WillOnce(Return("encrypted")); 56 EXPECT_CALL(mock_cryptohome_library, 57 DecryptWithSystemSalt(StrEq("encrypted"))) 58 .Times(1) 59 .WillOnce(Return("test-token")); 60 61 DeviceOAuth2TokenService oauth2_service( 62 new net::TestURLRequestContextGetter(message_loop_.message_loop_proxy()), 63 scoped_testing_local_state_.Get()); 64 65 ASSERT_EQ("", oauth2_service.GetRefreshToken()); 66 oauth2_service.SetAndSaveRefreshToken("test-token"); 67 ASSERT_EQ("test-token", oauth2_service.GetRefreshToken()); 68 69 // This call won't invoke decrypt again, since the value is cached. 70 ASSERT_EQ("test-token", oauth2_service.GetRefreshToken()); 71} 72 73} // namespace chromeos 74