| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/geolocation/chrome_access_token_store.h" | 9 #include "chrome/browser/geolocation/chrome_access_token_store.h" |
| 10 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
| 11 #include "content/public/browser/access_token_store.h" | 11 #include "content/public/browser/access_token_store.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
| 14 | 14 |
| 15 using content::AccessTokenStore; | 15 using content::AccessTokenStore; |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // The token store factory implementation expects to be used from any well-known | 20 // The token store factory implementation expects to be used from any well-known |
| 21 // chrome thread other than UI. We could use any arbitrary thread; IO is | 21 // chrome thread other than UI. We could use any arbitrary thread; IO is |
| 22 // a good choice as this is the expected usage. | 22 // a good choice as this is the expected usage. |
| 23 const BrowserThread::ID kExpectedClientThreadId = BrowserThread::IO; | 23 const BrowserThread::ID kExpectedClientThreadId = BrowserThread::IO; |
| 24 const char* kRefServerUrl1 = "https://test.domain.example/foo?id=bar.bar"; | 24 const char* kRefServerUrl1 = "https://test.domain.example/foo?id=bar.bar"; |
| 25 const char* kRefServerUrl2 = "http://another.domain.example/foo?id=bar.bar#2"; | 25 const char* kRefServerUrl2 = "http://another.domain.example/foo?id=bar.bar#2"; |
| 26 const char* kOldDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; | 26 const char* kOldDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; |
| 27 | 27 |
| 28 class GeolocationAccessTokenStoreTest | 28 class GeolocationAccessTokenStoreTest : public InProcessBrowserTest { |
| 29 : public InProcessBrowserTest { | |
| 30 public: | 29 public: |
| 31 GeolocationAccessTokenStoreTest() | 30 GeolocationAccessTokenStoreTest() |
| 32 : token_to_expect_(NULL), token_to_set_(NULL) {} | 31 : token_store_(new ChromeAccessTokenStore), |
| 32 token_to_expect_(nullptr), |
| 33 token_to_set_(nullptr) {} |
| 33 | 34 |
| 34 void DoTestStepAndWaitForResults( | 35 void DoTestStepAndWaitForResults( |
| 35 const char* ref_url, const base::string16* token_to_expect, | 36 const char* ref_url, const base::string16* token_to_expect, |
| 36 const base::string16* token_to_set); | 37 const base::string16* token_to_set); |
| 37 | 38 |
| 38 void OnAccessTokenStoresLoaded( | 39 void OnAccessTokenStoresLoaded( |
| 39 AccessTokenStore::AccessTokenMap access_token_map, | 40 AccessTokenStore::AccessTokenMap access_token_map, |
| 40 net::URLRequestContextGetter* context_getter); | 41 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 41 | 42 |
| 42 scoped_refptr<AccessTokenStore> token_store_; | 43 scoped_refptr<AccessTokenStore> token_store_; |
| 43 GURL ref_url_; | 44 GURL ref_url_; |
| 44 const base::string16* token_to_expect_; | 45 const base::string16* token_to_expect_; |
| 45 const base::string16* token_to_set_; | 46 const base::string16* token_to_set_; |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 void StartTestStepFromClientThread( | |
| 49 scoped_refptr<AccessTokenStore>* store, | |
| 50 const AccessTokenStore::LoadAccessTokensCallback& callback) { | |
| 51 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)); | |
| 52 if (store->get() == NULL) | |
| 53 (*store) = new ChromeAccessTokenStore(); | |
| 54 (*store)->LoadAccessTokens(callback); | |
| 55 } | |
| 56 | |
| 57 struct TokenLoadClientForTest { | |
| 58 void NotReachedCallback(AccessTokenStore::AccessTokenMap /*tokens*/, | |
| 59 net::URLRequestContextGetter* /*context_getter*/) { | |
| 60 NOTREACHED() << "This request should have been canceled before callback"; | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 void GeolocationAccessTokenStoreTest::DoTestStepAndWaitForResults( | 49 void GeolocationAccessTokenStoreTest::DoTestStepAndWaitForResults( |
| 65 const char* ref_url, const base::string16* token_to_expect, | 50 const char* ref_url, const base::string16* token_to_expect, |
| 66 const base::string16* token_to_set) { | 51 const base::string16* token_to_set) { |
| 67 ref_url_ = GURL(ref_url); | 52 ref_url_ = GURL(ref_url); |
| 68 token_to_expect_ = token_to_expect; | 53 token_to_expect_ = token_to_expect; |
| 69 token_to_set_ = token_to_set; | 54 token_to_set_ = token_to_set; |
| 70 | 55 |
| 71 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
| 72 kExpectedClientThreadId, FROM_HERE, | 57 kExpectedClientThreadId, FROM_HERE, |
| 73 base::Bind( | 58 base::Bind( |
| 74 &StartTestStepFromClientThread, | 59 &AccessTokenStore::LoadAccessTokens, |
| 75 &token_store_, | 60 token_store_, |
| 76 base::Bind( | 61 base::Bind( |
| 77 &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded, | 62 &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded, |
| 78 base::Unretained(this)))); | 63 base::Unretained(this)))); |
| 79 content::RunMessageLoop(); | 64 content::RunMessageLoop(); |
| 80 } | 65 } |
| 81 | 66 |
| 82 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded( | 67 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded( |
| 83 AccessTokenStore::AccessTokenMap access_token_map, | 68 AccessTokenStore::AccessTokenMap access_token_map, |
| 84 net::URLRequestContextGetter* context_getter) { | 69 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { |
| 85 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)) | 70 ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)) |
| 86 << "Callback from token factory should be from the same thread as the " | 71 << "Callback from token factory should be from the same thread as the " |
| 87 "LoadAccessTokenStores request was made on"; | 72 "LoadAccessTokenStores request was made on"; |
| 88 DCHECK(context_getter); | 73 DCHECK(context_getter); |
| 89 AccessTokenStore::AccessTokenMap::const_iterator item = | 74 AccessTokenStore::AccessTokenMap::const_iterator item = |
| 90 access_token_map.find(ref_url_); | 75 access_token_map.find(ref_url_); |
| 91 if (!token_to_expect_) { | 76 if (!token_to_expect_) { |
| 92 EXPECT_TRUE(item == access_token_map.end()); | 77 EXPECT_TRUE(item == access_token_map.end()); |
| 93 } else { | 78 } else { |
| 94 EXPECT_FALSE(item == access_token_map.end()); | 79 EXPECT_FALSE(item == access_token_map.end()); |
| 95 EXPECT_EQ(*token_to_expect_, item->second); | 80 EXPECT_EQ(*token_to_expect_, item->second); |
| 96 } | 81 } |
| 97 | 82 |
| 98 if (token_to_set_) { | 83 if (token_to_set_) { |
| 99 scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore()); | 84 scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore()); |
| 100 store->SaveAccessToken(ref_url_, *token_to_set_); | 85 store->SaveAccessToken(ref_url_, *token_to_set_); |
| 101 } | 86 } |
| 102 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 103 base::MessageLoop::QuitWhenIdleClosure()); | 88 base::MessageLoop::QuitWhenIdleClosure()); |
| 104 } | 89 } |
| 105 | 90 |
| 106 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) { | 91 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) { |
| 107 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); | 92 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); |
| 108 const base::string16 ref_token2 = | 93 const base::string16 ref_token2 = |
| 109 base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023"); | 94 base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023"); |
| 110 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 | 96 |
| 112 DoTestStepAndWaitForResults(kRefServerUrl1, NULL, &ref_token1); | 97 DoTestStepAndWaitForResults(kRefServerUrl1, nullptr, &ref_token1); |
| 113 // Check it was set, and change to new value. | 98 // Check it was set, and change to new value. |
| 114 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2); | 99 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2); |
| 115 // And change back. | 100 // And change back. |
| 116 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1); | 101 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1); |
| 117 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); | 102 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr); |
| 118 | 103 |
| 119 // Set a second server URL | 104 // Set a second server URL |
| 120 DoTestStepAndWaitForResults(kRefServerUrl2, NULL, &ref_token2); | 105 DoTestStepAndWaitForResults(kRefServerUrl2, nullptr, &ref_token2); |
| 121 DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, NULL); | 106 DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, nullptr); |
| 122 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); | 107 DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr); |
| 123 } | 108 } |
| 124 | 109 |
| 125 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) { | 110 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) { |
| 126 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); | 111 const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); |
| 127 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 128 | 113 |
| 129 // Set a token for the old default network provider url. | 114 // Set a token for the old default network provider url. |
| 130 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, | 115 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, |
| 131 NULL, &ref_token1); | 116 nullptr, &ref_token1); |
| 132 // Check that the token related to the old default network provider url | 117 // Check that the token related to the old default network provider url |
| 133 // was deleted. | 118 // was deleted. |
| 134 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, | 119 DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, |
| 135 NULL, NULL); | 120 nullptr, nullptr); |
| 136 } | 121 } |
| 137 | 122 |
| 138 } // namespace | 123 } // namespace |
| OLD | NEW |