| 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_to_expect_(nullptr), token_to_set_(nullptr) {} | 
|   33  |   32  | 
|   34   void DoTestStepAndWaitForResults( |   33   void DoTestStepAndWaitForResults( | 
|   35       const char* ref_url, const base::string16* token_to_expect, |   34       const char* ref_url, const base::string16* token_to_expect, | 
|   36       const base::string16* token_to_set); |   35       const base::string16* token_to_set); | 
|   37  |   36  | 
|   38   void OnAccessTokenStoresLoaded( |   37   void OnAccessTokenStoresLoaded( | 
|   39       AccessTokenStore::AccessTokenMap access_token_map, |   38       AccessTokenStore::AccessTokenMap access_token_map, | 
|   40       net::URLRequestContextGetter* context_getter); |   39       const scoped_refptr<net::URLRequestContextGetter>* context_getter); | 
|   41  |   40  | 
|   42   scoped_refptr<AccessTokenStore> token_store_; |   41   scoped_refptr<AccessTokenStore> token_store_; | 
|   43   GURL ref_url_; |   42   GURL ref_url_; | 
|   44   const base::string16* token_to_expect_; |   43   const base::string16* token_to_expect_; | 
|   45   const base::string16* token_to_set_; |   44   const base::string16* token_to_set_; | 
|   46 }; |   45 }; | 
|   47  |   46  | 
|   48 void StartTestStepFromClientThread( |   47 void StartTestStepFromClientThread( | 
|   49     scoped_refptr<AccessTokenStore>* store, |   48     scoped_refptr<AccessTokenStore>* store, | 
|   50     const AccessTokenStore::LoadAccessTokensCallback& callback) { |   49     const AccessTokenStore::LoadAccessTokensCallback& callback) { | 
|   51   ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)); |   50   ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)); | 
|   52   if (store->get() == NULL) |   51   if (store->get() == nullptr) | 
|   53     (*store) = new ChromeAccessTokenStore(); |   52     (*store) = new ChromeAccessTokenStore(); | 
|   54   (*store)->LoadAccessTokens(callback); |   53   (*store)->LoadAccessTokens(callback); | 
|   55 } |   54 } | 
|   56  |   55  | 
|   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( |   56 void GeolocationAccessTokenStoreTest::DoTestStepAndWaitForResults( | 
|   65     const char* ref_url, const base::string16* token_to_expect, |   57     const char* ref_url, const base::string16* token_to_expect, | 
|   66     const base::string16* token_to_set) { |   58     const base::string16* token_to_set) { | 
|   67   ref_url_ = GURL(ref_url); |   59   ref_url_ = GURL(ref_url); | 
|   68   token_to_expect_ = token_to_expect; |   60   token_to_expect_ = token_to_expect; | 
|   69   token_to_set_ = token_to_set; |   61   token_to_set_ = token_to_set; | 
|   70  |   62  | 
|   71   BrowserThread::PostTask( |   63   BrowserThread::PostTask( | 
|   72       kExpectedClientThreadId, FROM_HERE, |   64       kExpectedClientThreadId, FROM_HERE, | 
|   73       base::Bind( |   65       base::Bind( | 
|   74           &StartTestStepFromClientThread, |   66           &StartTestStepFromClientThread, | 
|   75           &token_store_, |   67           &token_store_, | 
|   76           base::Bind( |   68           base::Bind( | 
|   77               &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded, |   69               &GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded, | 
|   78               base::Unretained(this)))); |   70               base::Unretained(this)))); | 
|   79   content::RunMessageLoop(); |   71   content::RunMessageLoop(); | 
|   80 } |   72 } | 
|   81  |   73  | 
|   82 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded( |   74 void GeolocationAccessTokenStoreTest::OnAccessTokenStoresLoaded( | 
|   83     AccessTokenStore::AccessTokenMap access_token_map, |   75     AccessTokenStore::AccessTokenMap access_token_map, | 
|   84     net::URLRequestContextGetter* context_getter) { |   76     const scoped_refptr<net::URLRequestContextGetter>* context_getter) { | 
|   85   ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)) |   77   ASSERT_TRUE(BrowserThread::CurrentlyOn(kExpectedClientThreadId)) | 
|   86       << "Callback from token factory should be from the same thread as the " |   78       << "Callback from token factory should be from the same thread as the " | 
|   87          "LoadAccessTokenStores request was made on"; |   79          "LoadAccessTokenStores request was made on"; | 
|   88   DCHECK(context_getter); |   80   DCHECK(context_getter); | 
|   89   AccessTokenStore::AccessTokenMap::const_iterator item = |   81   AccessTokenStore::AccessTokenMap::const_iterator item = | 
|   90       access_token_map.find(ref_url_); |   82       access_token_map.find(ref_url_); | 
|   91   if (!token_to_expect_) { |   83   if (!token_to_expect_) { | 
|   92     EXPECT_TRUE(item == access_token_map.end()); |   84     EXPECT_TRUE(item == access_token_map.end()); | 
|   93   } else { |   85   } else { | 
|   94     EXPECT_FALSE(item == access_token_map.end()); |   86     EXPECT_FALSE(item == access_token_map.end()); | 
|   95     EXPECT_EQ(*token_to_expect_, item->second); |   87     EXPECT_EQ(*token_to_expect_, item->second); | 
|   96   } |   88   } | 
|   97  |   89  | 
|   98   if (token_to_set_) { |   90   if (token_to_set_) { | 
|   99     scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore()); |   91     scoped_refptr<AccessTokenStore> store(new ChromeAccessTokenStore()); | 
|  100     store->SaveAccessToken(ref_url_, *token_to_set_); |   92     store->SaveAccessToken(ref_url_, *token_to_set_); | 
|  101   } |   93   } | 
|  102   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |   94   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 
|  103                           base::MessageLoop::QuitWhenIdleClosure()); |   95                           base::MessageLoop::QuitWhenIdleClosure()); | 
|  104 } |   96 } | 
|  105  |   97  | 
|  106 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) { |   98 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, SetAcrossInstances) { | 
|  107   const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); |   99   const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); | 
|  108   const base::string16 ref_token2 = |  100   const base::string16 ref_token2 = | 
|  109       base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023"); |  101       base::ASCIIToUTF16("\1\2\3\4\5\6\7\10\11\12=023"); | 
|  110   ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  102   ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  111  |  103  | 
|  112   DoTestStepAndWaitForResults(kRefServerUrl1, NULL, &ref_token1); |  104   DoTestStepAndWaitForResults(kRefServerUrl1, nullptr, &ref_token1); | 
|  113   // Check it was set, and change to new value. |  105   // Check it was set, and change to new value. | 
|  114   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2); |  106   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, &ref_token2); | 
|  115   // And change back. |  107   // And change back. | 
|  116   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1); |  108   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token2, &ref_token1); | 
|  117   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); |  109   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr); | 
|  118  |  110  | 
|  119   // Set a second server URL |  111   // Set a second server URL | 
|  120   DoTestStepAndWaitForResults(kRefServerUrl2, NULL, &ref_token2); |  112   DoTestStepAndWaitForResults(kRefServerUrl2, nullptr, &ref_token2); | 
|  121   DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, NULL); |  113   DoTestStepAndWaitForResults(kRefServerUrl2, &ref_token2, nullptr); | 
|  122   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, NULL); |  114   DoTestStepAndWaitForResults(kRefServerUrl1, &ref_token1, nullptr); | 
|  123 } |  115 } | 
|  124  |  116  | 
|  125 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) { |  117 IN_PROC_BROWSER_TEST_F(GeolocationAccessTokenStoreTest, OldUrlRemoval) { | 
|  126   const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); |  118   const base::string16 ref_token1 = base::ASCIIToUTF16("jksdfo90,'s#\"#1*("); | 
|  127   ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  119   ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  128  |  120  | 
|  129   // Set a token for the old default network provider url. |  121   // Set a token for the old default network provider url. | 
|  130   DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, |  122   DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, | 
|  131                               NULL, &ref_token1); |  123                               nullptr, &ref_token1); | 
|  132   // Check that the token related to the old default network provider url |  124   // Check that the token related to the old default network provider url | 
|  133   // was deleted. |  125   // was deleted. | 
|  134   DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, |  126   DoTestStepAndWaitForResults(kOldDefaultNetworkProviderUrl, | 
|  135                               NULL, NULL); |  127                               nullptr, nullptr); | 
|  136 } |  128 } | 
|  137  |  129  | 
|  138 }  // namespace |  130 }  // namespace | 
| OLD | NEW |