| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // Unit tests for the SafeBrowsing storage system. | 5 // Unit tests for the SafeBrowsing storage system. |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| (...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 Time::Now())); | 1031 Time::Now())); |
| 1032 | 1032 |
| 1033 TearDownTestDatabase(database); | 1033 TearDownTestDatabase(database); |
| 1034 } | 1034 } |
| 1035 | 1035 |
| 1036 void PrintStat(const char* name) { | 1036 void PrintStat(const char* name) { |
| 1037 int value = StatsTable::current()->GetCounterValue(name); | 1037 int value = StatsTable::current()->GetCounterValue(name); |
| 1038 LOG(INFO) << StringPrintf("%s %d", name, value); | 1038 LOG(INFO) << StringPrintf("%s %d", name, value); |
| 1039 } | 1039 } |
| 1040 | 1040 |
| 1041 std::wstring GetFullSBDataPath(const std::wstring& path) { | 1041 FilePath GetFullSBDataPath(const std::wstring& path) { |
| 1042 FilePath full_path; | 1042 FilePath full_path; |
| 1043 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &full_path)); | 1043 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &full_path)); |
| 1044 full_path = full_path.AppendASCII("chrome"); | 1044 full_path = full_path.AppendASCII("chrome"); |
| 1045 full_path = full_path.AppendASCII("test"); | 1045 full_path = full_path.AppendASCII("test"); |
| 1046 full_path = full_path.AppendASCII("data"); | 1046 full_path = full_path.AppendASCII("data"); |
| 1047 full_path = full_path.AppendASCII("safe_browsing"); | 1047 full_path = full_path.AppendASCII("safe_browsing"); |
| 1048 full_path = full_path.Append(FilePath::FromWStringHack(path)); | 1048 full_path = full_path.Append(FilePath::FromWStringHack(path)); |
| 1049 CHECK(file_util::PathExists(full_path)); | 1049 CHECK(file_util::PathExists(full_path)); |
| 1050 return full_path.ToWStringHack(); | 1050 return full_path; |
| 1051 } | 1051 } |
| 1052 | 1052 |
| 1053 struct ChunksInfo { | 1053 struct ChunksInfo { |
| 1054 std::deque<SBChunk>* chunks; | 1054 std::deque<SBChunk>* chunks; |
| 1055 std::string listname; | 1055 std::string listname; |
| 1056 }; | 1056 }; |
| 1057 | 1057 |
| 1058 void PeformUpdate(const std::wstring& initial_db, | 1058 void PeformUpdate(const std::wstring& initial_db, |
| 1059 const std::vector<ChunksInfo>& chunks, | 1059 const std::vector<ChunksInfo>& chunks, |
| 1060 std::vector<SBChunkDelete>* deletes) { | 1060 std::vector<SBChunkDelete>* deletes) { |
| 1061 // TODO(pinkerton): I don't think posix has any concept of IO counters, but | 1061 // TODO(pinkerton): I don't think posix has any concept of IO counters, but |
| 1062 // we can uncomment this when we implement ProcessMetrics::GetIOCounters | 1062 // we can uncomment this when we implement ProcessMetrics::GetIOCounters |
| 1063 IoCounters before, after; | 1063 IoCounters before, after; |
| 1064 | 1064 |
| 1065 FilePath path; | 1065 FilePath path; |
| 1066 PathService::Get(base::DIR_TEMP, &path); | 1066 PathService::Get(base::DIR_TEMP, &path); |
| 1067 path = path.AppendASCII("SafeBrowsingTestDatabase"); | 1067 path = path.AppendASCII("SafeBrowsingTestDatabase"); |
| 1068 | 1068 |
| 1069 // In case it existed from a previous run. | 1069 // In case it existed from a previous run. |
| 1070 file_util::Delete(path, false); | 1070 file_util::Delete(path, false); |
| 1071 | 1071 |
| 1072 if (!initial_db.empty()) { | 1072 if (!initial_db.empty()) { |
| 1073 std::wstring full_initial_db = GetFullSBDataPath(initial_db); | 1073 FilePath full_initial_db = GetFullSBDataPath(initial_db); |
| 1074 ASSERT_TRUE(file_util::CopyFile( | 1074 ASSERT_TRUE(file_util::CopyFile(full_initial_db, path)); |
| 1075 FilePath::FromWStringHack(full_initial_db), path)); | |
| 1076 } | 1075 } |
| 1077 | 1076 |
| 1078 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create(); | 1077 SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create(); |
| 1079 database->SetSynchronous(); | 1078 database->SetSynchronous(); |
| 1080 EXPECT_TRUE(database->Init(path, NULL)); | 1079 EXPECT_TRUE(database->Init(path, NULL)); |
| 1081 | 1080 |
| 1082 Time before_time = Time::Now(); | 1081 Time before_time = Time::Now(); |
| 1083 base::ProcessHandle handle = base::Process::Current().handle(); | 1082 base::ProcessHandle handle = base::Process::Current().handle(); |
| 1084 scoped_ptr<base::ProcessMetrics> metric( | 1083 scoped_ptr<base::ProcessMetrics> metric( |
| 1085 base::ProcessMetrics::CreateProcessMetrics(handle)); | 1084 base::ProcessMetrics::CreateProcessMetrics(handle)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 } | 1121 } |
| 1123 | 1122 |
| 1124 void UpdateDatabase(const std::wstring& initial_db, | 1123 void UpdateDatabase(const std::wstring& initial_db, |
| 1125 const std::wstring& response_path, | 1124 const std::wstring& response_path, |
| 1126 const std::wstring& updates_path) { | 1125 const std::wstring& updates_path) { |
| 1127 // First we read the chunks from disk, so that this isn't counted in IO bytes. | 1126 // First we read the chunks from disk, so that this isn't counted in IO bytes. |
| 1128 std::vector<ChunksInfo> chunks; | 1127 std::vector<ChunksInfo> chunks; |
| 1129 | 1128 |
| 1130 SafeBrowsingProtocolParser parser; | 1129 SafeBrowsingProtocolParser parser; |
| 1131 if (!updates_path.empty()) { | 1130 if (!updates_path.empty()) { |
| 1132 std::wstring data_dir = GetFullSBDataPath(updates_path); | 1131 FilePath data_dir = GetFullSBDataPath(updates_path); |
| 1133 file_util::FileEnumerator file_enum( | 1132 file_util::FileEnumerator file_enum(data_dir, false, |
| 1134 FilePath::FromWStringHack(data_dir), false, | |
| 1135 file_util::FileEnumerator::FILES); | 1133 file_util::FileEnumerator::FILES); |
| 1136 while (true) { | 1134 while (true) { |
| 1137 std::wstring file = file_enum.Next().ToWStringHack(); | 1135 std::wstring file = file_enum.Next().ToWStringHack(); |
| 1138 if (file.empty()) | 1136 if (file.empty()) |
| 1139 break; | 1137 break; |
| 1140 | 1138 |
| 1141 int64 size64; | 1139 int64 size64; |
| 1142 bool result = file_util::GetFileSize(file, &size64); | 1140 bool result = file_util::GetFileSize(file, &size64); |
| 1143 CHECK(result); | 1141 CHECK(result); |
| 1144 | 1142 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1159 info.listname.resize(index); | 1157 info.listname.resize(index); |
| 1160 info.listname.erase(0, 3); // Get rid of the 000 etc. | 1158 info.listname.erase(0, 3); // Get rid of the 000 etc. |
| 1161 | 1159 |
| 1162 chunks.push_back(info); | 1160 chunks.push_back(info); |
| 1163 } | 1161 } |
| 1164 } | 1162 } |
| 1165 | 1163 |
| 1166 std::vector<SBChunkDelete>* deletes = new std::vector<SBChunkDelete>; | 1164 std::vector<SBChunkDelete>* deletes = new std::vector<SBChunkDelete>; |
| 1167 if (!response_path.empty()) { | 1165 if (!response_path.empty()) { |
| 1168 std::string update; | 1166 std::string update; |
| 1169 std::wstring full_response_path = GetFullSBDataPath(response_path); | 1167 FilePath full_response_path = GetFullSBDataPath(response_path); |
| 1170 if (file_util::ReadFileToString(full_response_path, &update)) { | 1168 if (file_util::ReadFileToString(full_response_path, &update)) { |
| 1171 int next_update; | 1169 int next_update; |
| 1172 bool result, rekey, reset; | 1170 bool result, rekey, reset; |
| 1173 std::vector<ChunkUrl> urls; | 1171 std::vector<ChunkUrl> urls; |
| 1174 result = parser.ParseUpdate(update.c_str(), | 1172 result = parser.ParseUpdate(update.c_str(), |
| 1175 static_cast<int>(update.length()), | 1173 static_cast<int>(update.length()), |
| 1176 "", | 1174 "", |
| 1177 &next_update, | 1175 &next_update, |
| 1178 &rekey, | 1176 &rekey, |
| 1179 &reset, | 1177 &reset, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1251 TEST(SafeBrowsingDatabase, DISABLED_DatabaseOldLotsofDeletesIO) { | 1249 TEST(SafeBrowsingDatabase, DISABLED_DatabaseOldLotsofDeletesIO) { |
| 1252 std::vector<ChunksInfo> chunks; | 1250 std::vector<ChunksInfo> chunks; |
| 1253 std::vector<SBChunkDelete>* deletes = new std::vector<SBChunkDelete>; | 1251 std::vector<SBChunkDelete>* deletes = new std::vector<SBChunkDelete>; |
| 1254 SBChunkDelete del; | 1252 SBChunkDelete del; |
| 1255 del.is_sub_del = false; | 1253 del.is_sub_del = false; |
| 1256 del.list_name = safe_browsing_util::kMalwareList; | 1254 del.list_name = safe_browsing_util::kMalwareList; |
| 1257 del.chunk_del.push_back(ChunkRange(3539, 3579)); | 1255 del.chunk_del.push_back(ChunkRange(3539, 3579)); |
| 1258 deletes->push_back(del); | 1256 deletes->push_back(del); |
| 1259 PeformUpdate(GetOldSafeBrowsingPath(), chunks, deletes); | 1257 PeformUpdate(GetOldSafeBrowsingPath(), chunks, deletes); |
| 1260 } | 1258 } |
| OLD | NEW |