| OLD | NEW | 
|     1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |     1 // Copyright (c) 2012 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 <algorithm> |     5 #include <algorithm> | 
|     6 #include <fstream> |     6 #include <fstream> | 
|     7  |     7  | 
|     8 #include "base/auto_reset.h" |     8 #include "base/auto_reset.h" | 
|     9 #include "base/files/file_path.h" |     9 #include "base/files/file_path.h" | 
|    10 #include "base/files/file_util.h" |    10 #include "base/files/file_util.h" | 
|    11 #include "base/files/scoped_temp_dir.h" |    11 #include "base/files/scoped_temp_dir.h" | 
|    12 #include "base/i18n/case_conversion.h" |    12 #include "base/i18n/case_conversion.h" | 
|    13 #include "base/path_service.h" |    13 #include "base/path_service.h" | 
|    14 #include "base/run_loop.h" |    14 #include "base/run_loop.h" | 
|    15 #include "base/strings/string16.h" |    15 #include "base/strings/string16.h" | 
|    16 #include "base/strings/string_util.h" |    16 #include "base/strings/string_util.h" | 
|    17 #include "base/strings/stringprintf.h" |  | 
|    18 #include "base/strings/utf_string_conversions.h" |    17 #include "base/strings/utf_string_conversions.h" | 
|    19 #include "chrome/browser/autocomplete/in_memory_url_index.h" |    18 #include "chrome/browser/autocomplete/in_memory_url_index.h" | 
|    20 #include "chrome/browser/autocomplete/url_index_private_data.h" |    19 #include "chrome/browser/autocomplete/url_index_private_data.h" | 
|    21 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |    20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 
|    22 #include "chrome/browser/history/history_service_factory.h" |    21 #include "chrome/browser/history/history_service_factory.h" | 
|    23 #include "chrome/common/chrome_paths.h" |    22 #include "chrome/common/chrome_paths.h" | 
|    24 #include "chrome/test/base/history_index_restore_observer.h" |    23 #include "chrome/test/base/history_index_restore_observer.h" | 
|    25 #include "chrome/test/base/testing_profile.h" |    24 #include "chrome/test/base/testing_profile.h" | 
|    26 #include "components/bookmarks/test/bookmark_test_helpers.h" |    25 #include "components/bookmarks/test/bookmark_test_helpers.h" | 
|    27 #include "components/history/core/browser/history_backend.h" |    26 #include "components/history/core/browser/history_backend.h" | 
|    28 #include "components/history/core/browser/history_database.h" |    27 #include "components/history/core/browser/history_database.h" | 
|    29 #include "components/history/core/browser/history_service.h" |    28 #include "components/history/core/browser/history_service.h" | 
|    30 #include "components/omnibox/in_memory_url_index_types.h" |    29 #include "components/omnibox/in_memory_url_index_types.h" | 
|    31 #include "content/public/browser/browser_thread.h" |  | 
|    32 #include "content/public/test/test_browser_thread_bundle.h" |    30 #include "content/public/test/test_browser_thread_bundle.h" | 
|    33 #include "sql/transaction.h" |    31 #include "sql/transaction.h" | 
|    34 #include "testing/gtest/include/gtest/gtest.h" |    32 #include "testing/gtest/include/gtest/gtest.h" | 
|    35  |    33  | 
|    36 using base::ASCIIToUTF16; |    34 using base::ASCIIToUTF16; | 
|    37  |    35  | 
|    38 // The test version of the history url database table ('url') is contained in |    36 // The test version of the history url database table ('url') is contained in | 
|    39 // a database file created from a text file('url_history_provider_test.db.txt'). |    37 // a database file created from a text file('url_history_provider_test.db.txt'). | 
|    40 // The only difference between this table and a live 'urls' table from a |    38 // The only difference between this table and a live 'urls' table from a | 
|    41 // profile is that the last_visit_time column in the test table contains a |    39 // profile is that the last_visit_time column in the test table contains a | 
|    42 // number specifying the number of days relative to 'today' to which the |    40 // number specifying the number of days relative to 'today' to which the | 
|    43 // absolute time should be set during the test setup stage. |    41 // absolute time should be set during the test setup stage. | 
|    44 // |    42 // | 
|    45 // The format of the test database text file is of a SQLite .dump file. |    43 // The format of the test database text file is of a SQLite .dump file. | 
|    46 // Note that only lines whose first character is an upper-case letter are |    44 // Note that only lines whose first character is an upper-case letter are | 
|    47 // processed when creating the test database. |    45 // processed when creating the test database. | 
|    48  |    46  | 
|    49 namespace { |    47 namespace { | 
|    50 const size_t kInvalid = base::string16::npos; |    48 const size_t kInvalid = base::string16::npos; | 
|    51 const size_t kMaxMatches = 3; |    49 const size_t kMaxMatches = 3; | 
|    52 const char kTestLanguages[] = "en,ja,hi,zh"; |    50 const char kTestLanguages[] = "en,ja,hi,zh"; | 
|    53 const char kClientWhitelistedScheme[] = "xyz"; |  | 
|    54  |    51  | 
|    55 // Helper function to set lower case |lower_string| and |lower_terms| (words |    52 // Helper function to set lower case |lower_string| and |lower_terms| (words | 
|    56 // list) based on supplied |search_string| and |cursor_position|. If |    53 // list) based on supplied |search_string| and |cursor_position|. If | 
|    57 // |cursor_position| is set and useful (not at either end of the string), allow |    54 // |cursor_position| is set and useful (not at either end of the string), allow | 
|    58 // the |search_string| to be broken at |cursor_position|. We do this by |    55 // the |search_string| to be broken at |cursor_position|. We do this by | 
|    59 // pretending there's a space where the cursor is. |lower_terms| are obtained by |    56 // pretending there's a space where the cursor is. |lower_terms| are obtained by | 
|    60 // splitting the |lower_string| on whitespace into tokens. |    57 // splitting the |lower_string| on whitespace into tokens. | 
|    61 void StringToTerms(const char* search_string, |    58 void StringToTerms(const char* search_string, | 
|    62                    size_t cursor_position, |    59                    size_t cursor_position, | 
|    63                    base::string16* lower_string, |    60                    base::string16* lower_string, | 
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   132   sql::Connection& GetDB(); |   129   sql::Connection& GetDB(); | 
|   133  |   130  | 
|   134   // Pass-through functions to simplify our friendship with InMemoryURLIndex. |   131   // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 
|   135   URLIndexPrivateData* GetPrivateData() const; |   132   URLIndexPrivateData* GetPrivateData() const; | 
|   136   base::CancelableTaskTracker* GetPrivateDataTracker() const; |   133   base::CancelableTaskTracker* GetPrivateDataTracker() const; | 
|   137   void ClearPrivateData(); |   134   void ClearPrivateData(); | 
|   138   void set_history_dir(const base::FilePath& dir_path); |   135   void set_history_dir(const base::FilePath& dir_path); | 
|   139   bool GetCacheFilePath(base::FilePath* file_path) const; |   136   bool GetCacheFilePath(base::FilePath* file_path) const; | 
|   140   void PostRestoreFromCacheFileTask(); |   137   void PostRestoreFromCacheFileTask(); | 
|   141   void PostSaveToCacheFileTask(); |   138   void PostSaveToCacheFileTask(); | 
|   142   const SchemeSet& scheme_whitelist(); |   139   const std::set<std::string>& scheme_whitelist(); | 
|   143  |   140  | 
|   144  |   141  | 
|   145   // Pass-through functions to simplify our friendship with URLIndexPrivateData. |   142   // Pass-through functions to simplify our friendship with URLIndexPrivateData. | 
|   146   bool UpdateURL(const history::URLRow& row); |   143   bool UpdateURL(const history::URLRow& row); | 
|   147   bool DeleteURL(const GURL& url); |   144   bool DeleteURL(const GURL& url); | 
|   148  |   145  | 
|   149   // Data verification helper functions. |   146   // Data verification helper functions. | 
|   150   void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); |   147   void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); | 
|   151   void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); |   148   void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); | 
|   152   void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, |   149   void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   192 } |   189 } | 
|   193  |   190  | 
|   194 void InMemoryURLIndexTest::PostRestoreFromCacheFileTask() { |   191 void InMemoryURLIndexTest::PostRestoreFromCacheFileTask() { | 
|   195   url_index_->PostRestoreFromCacheFileTask(); |   192   url_index_->PostRestoreFromCacheFileTask(); | 
|   196 } |   193 } | 
|   197  |   194  | 
|   198 void InMemoryURLIndexTest::PostSaveToCacheFileTask() { |   195 void InMemoryURLIndexTest::PostSaveToCacheFileTask() { | 
|   199   url_index_->PostSaveToCacheFileTask(); |   196   url_index_->PostSaveToCacheFileTask(); | 
|   200 } |   197 } | 
|   201  |   198  | 
|   202 const SchemeSet& InMemoryURLIndexTest::scheme_whitelist() { |   199 const std::set<std::string>& InMemoryURLIndexTest::scheme_whitelist() { | 
|   203   return url_index_->scheme_whitelist(); |   200   return url_index_->scheme_whitelist(); | 
|   204 } |   201 } | 
|   205  |   202  | 
|   206 bool InMemoryURLIndexTest::UpdateURL(const history::URLRow& row) { |   203 bool InMemoryURLIndexTest::UpdateURL(const history::URLRow& row) { | 
|   207   return GetPrivateData()->UpdateURL(history_service_, |   204   return GetPrivateData()->UpdateURL(history_service_, | 
|   208                                      row, |   205                                      row, | 
|   209                                      url_index_->languages_, |   206                                      url_index_->languages_, | 
|   210                                      url_index_->scheme_whitelist_, |   207                                      url_index_->scheme_whitelist_, | 
|   211                                      GetPrivateDataTracker()); |   208                                      GetPrivateDataTracker()); | 
|   212 } |   209 } | 
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   317 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { |   314 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { | 
|   318     return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); |   315     return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); | 
|   319 } |   316 } | 
|   320  |   317  | 
|   321 bool InMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { |   318 bool InMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { | 
|   322   return true; |   319   return true; | 
|   323 } |   320 } | 
|   324  |   321  | 
|   325 void InMemoryURLIndexTest::InitializeInMemoryURLIndex() { |   322 void InMemoryURLIndexTest::InitializeInMemoryURLIndex() { | 
|   326   DCHECK(!url_index_); |   323   DCHECK(!url_index_); | 
|   327  |   324   url_index_.reset(new InMemoryURLIndex(nullptr, history_service_, | 
|   328   SchemeSet client_schemes_to_whitelist; |   325                                         base::FilePath(), kTestLanguages)); | 
|   329   client_schemes_to_whitelist.insert(kClientWhitelistedScheme); |  | 
|   330   url_index_.reset(new InMemoryURLIndex( |  | 
|   331       nullptr, history_service_, content::BrowserThread::GetBlockingPool(), |  | 
|   332       base::FilePath(), kTestLanguages, client_schemes_to_whitelist)); |  | 
|   333   url_index_->Init(); |   326   url_index_->Init(); | 
|   334   url_index_->RebuildFromHistory(history_database_); |   327   url_index_->RebuildFromHistory(history_database_); | 
|   335 } |   328 } | 
|   336  |   329  | 
|   337 void InMemoryURLIndexTest::CheckTerm( |   330 void InMemoryURLIndexTest::CheckTerm( | 
|   338     const URLIndexPrivateData::SearchTermCacheMap& cache, |   331     const URLIndexPrivateData::SearchTermCacheMap& cache, | 
|   339     base::string16 term) const { |   332     base::string16 term) const { | 
|   340   URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( |   333   URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( | 
|   341       cache.find(term)); |   334       cache.find(term)); | 
|   342   ASSERT_TRUE(cache.end() != cache_iter) |   335   ASSERT_TRUE(cache.end() != cache_iter) | 
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   942   history::URLRows deleted_rows; |   935   history::URLRows deleted_rows; | 
|   943   deleted_rows.push_back(matches[0].url_info); |   936   deleted_rows.push_back(matches[0].url_info); | 
|   944   url_index_->OnURLsDeleted(nullptr, false, false, deleted_rows, |   937   url_index_->OnURLsDeleted(nullptr, false, false, deleted_rows, | 
|   945                             std::set<GURL>()); |   938                             std::set<GURL>()); | 
|   946   EXPECT_TRUE(url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"), |   939   EXPECT_TRUE(url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"), | 
|   947                                                base::string16::npos, |   940                                                base::string16::npos, | 
|   948                                                kMaxMatches).empty()); |   941                                                kMaxMatches).empty()); | 
|   949 } |   942 } | 
|   950  |   943  | 
|   951 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { |   944 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { | 
|   952   std::string client_whitelisted_url = |  | 
|   953       base::StringPrintf("%s://foo", kClientWhitelistedScheme); |  | 
|   954   struct TestData { |   945   struct TestData { | 
|   955     const std::string url_spec; |   946     const std::string url_spec; | 
|   956     const bool expected_is_whitelisted; |   947     const bool expected_is_whitelisted; | 
|   957   } data[] = { |   948   } data[] = { | 
|   958     // URLs with whitelisted schemes. |   949     // URLs with whitelisted schemes. | 
|   959     { "about:histograms", true }, |   950     { "about:histograms", true }, | 
 |   951     { "chrome://settings", true }, | 
|   960     { "file://localhost/Users/joeschmoe/sekrets", true }, |   952     { "file://localhost/Users/joeschmoe/sekrets", true }, | 
|   961     { "ftp://public.mycompany.com/myfile.txt", true }, |   953     { "ftp://public.mycompany.com/myfile.txt", true }, | 
|   962     { "http://www.google.com/translate", true }, |   954     { "http://www.google.com/translate", true }, | 
|   963     { "https://www.gmail.com/", true }, |   955     { "https://www.gmail.com/", true }, | 
|   964     { "mailto:support@google.com", true }, |   956     { "mailto:support@google.com", true }, | 
|   965     { client_whitelisted_url, true }, |  | 
|   966     // URLs with unacceptable schemes. |   957     // URLs with unacceptable schemes. | 
|   967     { "aaa://www.dummyhost.com;frammy", false }, |   958     { "aaa://www.dummyhost.com;frammy", false }, | 
|   968     { "aaas://www.dummyhost.com;frammy", false }, |   959     { "aaas://www.dummyhost.com;frammy", false }, | 
|   969     { "acap://suzie@somebody.com", false }, |   960     { "acap://suzie@somebody.com", false }, | 
|   970     { "cap://cal.example.com/Company/Holidays", false }, |   961     { "cap://cal.example.com/Company/Holidays", false }, | 
|   971     { "cid:foo4*foo1@bar.net", false }, |   962     { "cid:foo4*foo1@bar.net", false }, | 
|   972     { "crid://example.com/foobar", false }, |   963     { "crid://example.com/foobar", false }, | 
|   973     { "data:image/png;base64,iVBORw0KGgoAAAANSUhE=", false }, |   964     { "data:image/png;base64,iVBORw0KGgoAAAANSUhE=", false }, | 
|   974     { "dict://dict.org/d:shortcake:", false }, |   965     { "dict://dict.org/d:shortcake:", false }, | 
|   975     { "dns://192.168.1.1/ftp.example.org?type=A", false }, |   966     { "dns://192.168.1.1/ftp.example.org?type=A", false }, | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1017     { "tftp://example.com/mystartupfile", false }, |  1008     { "tftp://example.com/mystartupfile", false }, | 
|  1018     { "tip://123.123.123.123/?urn:xopen:xid", false }, |  1009     { "tip://123.123.123.123/?urn:xopen:xid", false }, | 
|  1019     { "tv:nbc.com", false }, |  1010     { "tv:nbc.com", false }, | 
|  1020     { "urn:foo:A123,456", false }, |  1011     { "urn:foo:A123,456", false }, | 
|  1021     { "vemmi://zeus.mctel.fr/demo", false }, |  1012     { "vemmi://zeus.mctel.fr/demo", false }, | 
|  1022     { "wais://www.mydomain.net:8765/mydatabase", false }, |  1013     { "wais://www.mydomain.net:8765/mydatabase", false }, | 
|  1023     { "xmpp:node@example.com", false }, |  1014     { "xmpp:node@example.com", false }, | 
|  1024     { "xmpp://guest@example.com", false }, |  1015     { "xmpp://guest@example.com", false }, | 
|  1025   }; |  1016   }; | 
|  1026  |  1017  | 
|  1027   const SchemeSet& whitelist(scheme_whitelist()); |  1018   const std::set<std::string>& whitelist(scheme_whitelist()); | 
|  1028   for (size_t i = 0; i < arraysize(data); ++i) { |  1019   for (size_t i = 0; i < arraysize(data); ++i) { | 
|  1029     GURL url(data[i].url_spec); |  1020     GURL url(data[i].url_spec); | 
|  1030     EXPECT_EQ(data[i].expected_is_whitelisted, |  1021     EXPECT_EQ(data[i].expected_is_whitelisted, | 
|  1031               URLIndexPrivateData::URLSchemeIsWhitelisted(url, whitelist)); |  1022               URLIndexPrivateData::URLSchemeIsWhitelisted(url, whitelist)); | 
|  1032   } |  1023   } | 
|  1033 } |  1024 } | 
|  1034  |  1025  | 
|  1035 TEST_F(InMemoryURLIndexTest, ReadVisitsFromHistory) { |  1026 TEST_F(InMemoryURLIndexTest, ReadVisitsFromHistory) { | 
|  1036   const HistoryInfoMap& history_info_map = GetPrivateData()->history_info_map_; |  1027   const HistoryInfoMap& history_info_map = GetPrivateData()->history_info_map_; | 
|  1037  |  1028  | 
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1316   bool GetCacheFilePath(base::FilePath* file_path) const; |  1307   bool GetCacheFilePath(base::FilePath* file_path) const; | 
|  1317  |  1308  | 
|  1318   content::TestBrowserThreadBundle thread_bundle_; |  1309   content::TestBrowserThreadBundle thread_bundle_; | 
|  1319   base::ScopedTempDir temp_dir_; |  1310   base::ScopedTempDir temp_dir_; | 
|  1320   scoped_ptr<InMemoryURLIndex> url_index_; |  1311   scoped_ptr<InMemoryURLIndex> url_index_; | 
|  1321 }; |  1312 }; | 
|  1322  |  1313  | 
|  1323 void InMemoryURLIndexCacheTest::SetUp() { |  1314 void InMemoryURLIndexCacheTest::SetUp() { | 
|  1324   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |  1315   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 
|  1325   base::FilePath path(temp_dir_.path()); |  1316   base::FilePath path(temp_dir_.path()); | 
|  1326   url_index_.reset(new InMemoryURLIndex( |  1317   url_index_.reset( | 
|  1327       nullptr, nullptr, content::BrowserThread::GetBlockingPool(), path, |  1318       new InMemoryURLIndex(nullptr, nullptr, path, kTestLanguages)); | 
|  1328       kTestLanguages, SchemeSet())); |  | 
|  1329 } |  1319 } | 
|  1330  |  1320  | 
|  1331 void InMemoryURLIndexCacheTest::TearDown() { |  1321 void InMemoryURLIndexCacheTest::TearDown() { | 
|  1332   if (url_index_) |  1322   if (url_index_) | 
|  1333     url_index_->Shutdown(); |  1323     url_index_->Shutdown(); | 
|  1334 } |  1324 } | 
|  1335  |  1325  | 
|  1336 void InMemoryURLIndexCacheTest::set_history_dir( |  1326 void InMemoryURLIndexCacheTest::set_history_dir( | 
|  1337     const base::FilePath& dir_path) { |  1327     const base::FilePath& dir_path) { | 
|  1338   return url_index_->set_history_dir(dir_path); |  1328   return url_index_->set_history_dir(dir_path); | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|  1353   ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |  1343   ASSERT_TRUE(GetCacheFilePath(&full_file_path)); | 
|  1354   std::vector<base::FilePath::StringType> actual_parts; |  1344   std::vector<base::FilePath::StringType> actual_parts; | 
|  1355   full_file_path.GetComponents(&actual_parts); |  1345   full_file_path.GetComponents(&actual_parts); | 
|  1356   ASSERT_EQ(expected_parts.size(), actual_parts.size()); |  1346   ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 
|  1357   size_t count = expected_parts.size(); |  1347   size_t count = expected_parts.size(); | 
|  1358   for (size_t i = 0; i < count; ++i) |  1348   for (size_t i = 0; i < count; ++i) | 
|  1359     EXPECT_EQ(expected_parts[i], actual_parts[i]); |  1349     EXPECT_EQ(expected_parts[i], actual_parts[i]); | 
|  1360   // Must clear the history_dir_ to satisfy the dtor's DCHECK. |  1350   // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 
|  1361   set_history_dir(base::FilePath()); |  1351   set_history_dir(base::FilePath()); | 
|  1362 } |  1352 } | 
| OLD | NEW |