text_log_upload_list_unittest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. #include "components/upload_list/text_log_upload_list.h"
  5. #include <string>
  6. #include "base/callback.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/run_loop.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/test/task_environment.h"
  14. #include "base/time/time.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace {
  17. const char kTestUploadTime[] = "1234567890";
  18. const char kTestUploadId[] = "0123456789abcdef";
  19. const char kTestLocalID[] = "fedcba9876543210";
  20. const char kTestCaptureTime[] = "2345678901";
  21. const char kTestSource[] = "test_source";
  22. class TextLogUploadListTest : public testing::Test {
  23. public:
  24. TextLogUploadListTest() = default;
  25. TextLogUploadListTest(const TextLogUploadListTest&) = delete;
  26. TextLogUploadListTest& operator=(const TextLogUploadListTest&) = delete;
  27. void SetUp() override {
  28. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  29. }
  30. protected:
  31. void WriteUploadLog(const std::string& log_data) {
  32. ASSERT_GT(base::WriteFile(log_path(), log_data.c_str(),
  33. static_cast<int>(log_data.size())),
  34. 0);
  35. }
  36. base::FilePath log_path() {
  37. return temp_dir_.GetPath().Append(FILE_PATH_LITERAL("uploads.log"));
  38. }
  39. private:
  40. base::ScopedTempDir temp_dir_;
  41. protected:
  42. base::test::TaskEnvironment task_environment_;
  43. };
  44. // These tests test that UploadList can parse a vector of log entry strings of
  45. // various formats to a vector of UploadInfo objects. See the UploadList
  46. // declaration for a description of the log entry string formats.
  47. // Test log entry string with upload time and upload ID.
  48. // This is the format that crash reports are stored in.
  49. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadId) {
  50. std::string test_entry = kTestUploadTime;
  51. test_entry += ",";
  52. test_entry.append(kTestUploadId);
  53. WriteUploadLog(test_entry);
  54. scoped_refptr<TextLogUploadList> upload_list =
  55. new TextLogUploadList(log_path());
  56. base::RunLoop run_loop;
  57. upload_list->Load(run_loop.QuitClosure());
  58. run_loop.Run();
  59. std::vector<UploadList::UploadInfo> uploads;
  60. upload_list->GetUploads(999, &uploads);
  61. EXPECT_EQ(1u, uploads.size());
  62. double time_double = uploads[0].upload_time.ToDoubleT();
  63. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  64. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  65. EXPECT_STREQ("", uploads[0].local_id.c_str());
  66. time_double = uploads[0].capture_time.ToDoubleT();
  67. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  68. }
  69. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadId_JSON) {
  70. std::stringstream stream;
  71. stream << "{";
  72. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  73. stream << "\"upload_id\":\"" << kTestUploadId << "\"";
  74. stream << "}" << std::endl;
  75. WriteUploadLog(stream.str());
  76. scoped_refptr<TextLogUploadList> upload_list =
  77. new TextLogUploadList(log_path());
  78. base::RunLoop run_loop;
  79. upload_list->Load(run_loop.QuitClosure());
  80. run_loop.Run();
  81. std::vector<UploadList::UploadInfo> uploads;
  82. upload_list->GetUploads(999, &uploads);
  83. EXPECT_EQ(1u, uploads.size());
  84. double time_double = uploads[0].upload_time.ToDoubleT();
  85. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  86. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  87. EXPECT_STREQ("", uploads[0].local_id.c_str());
  88. time_double = uploads[0].capture_time.ToDoubleT();
  89. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  90. }
  91. // Test log entry string with upload time, upload ID and local ID.
  92. // This is the old format that WebRTC logs were stored in.
  93. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdLocalId) {
  94. std::string test_entry = kTestUploadTime;
  95. test_entry += ",";
  96. test_entry.append(kTestUploadId);
  97. test_entry += ",";
  98. test_entry.append(kTestLocalID);
  99. WriteUploadLog(test_entry);
  100. scoped_refptr<TextLogUploadList> upload_list =
  101. new TextLogUploadList(log_path());
  102. base::RunLoop run_loop;
  103. upload_list->Load(run_loop.QuitClosure());
  104. run_loop.Run();
  105. std::vector<UploadList::UploadInfo> uploads;
  106. upload_list->GetUploads(999, &uploads);
  107. EXPECT_EQ(1u, uploads.size());
  108. double time_double = uploads[0].upload_time.ToDoubleT();
  109. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  110. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  111. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  112. time_double = uploads[0].capture_time.ToDoubleT();
  113. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  114. }
  115. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdLocalId_JSON) {
  116. std::stringstream stream;
  117. stream << "{";
  118. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  119. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  120. stream << "\"local_id\":\"" << kTestLocalID << "\"";
  121. stream << "}" << std::endl;
  122. WriteUploadLog(stream.str());
  123. scoped_refptr<TextLogUploadList> upload_list =
  124. new TextLogUploadList(log_path());
  125. base::RunLoop run_loop;
  126. upload_list->Load(run_loop.QuitClosure());
  127. run_loop.Run();
  128. std::vector<UploadList::UploadInfo> uploads;
  129. upload_list->GetUploads(999, &uploads);
  130. EXPECT_EQ(1u, uploads.size());
  131. double time_double = uploads[0].upload_time.ToDoubleT();
  132. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  133. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  134. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  135. time_double = uploads[0].capture_time.ToDoubleT();
  136. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  137. }
  138. // Test log entry string with upload time, upload ID and capture time.
  139. // This is the format that WebRTC logs that only have been uploaded only are
  140. // stored in.
  141. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdCaptureTime) {
  142. std::string test_entry = kTestUploadTime;
  143. test_entry += ",";
  144. test_entry.append(kTestUploadId);
  145. test_entry += ",,";
  146. test_entry.append(kTestCaptureTime);
  147. WriteUploadLog(test_entry);
  148. scoped_refptr<TextLogUploadList> upload_list =
  149. new TextLogUploadList(log_path());
  150. base::RunLoop run_loop;
  151. upload_list->Load(run_loop.QuitClosure());
  152. run_loop.Run();
  153. std::vector<UploadList::UploadInfo> uploads;
  154. upload_list->GetUploads(999, &uploads);
  155. EXPECT_EQ(1u, uploads.size());
  156. double time_double = uploads[0].upload_time.ToDoubleT();
  157. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  158. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  159. EXPECT_STREQ("", uploads[0].local_id.c_str());
  160. time_double = uploads[0].capture_time.ToDoubleT();
  161. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  162. }
  163. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdCaptureTime_JSON) {
  164. std::stringstream stream;
  165. stream << "{";
  166. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  167. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  168. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  169. stream << "}" << std::endl;
  170. WriteUploadLog(stream.str());
  171. scoped_refptr<TextLogUploadList> upload_list =
  172. new TextLogUploadList(log_path());
  173. base::RunLoop run_loop;
  174. upload_list->Load(run_loop.QuitClosure());
  175. run_loop.Run();
  176. std::vector<UploadList::UploadInfo> uploads;
  177. upload_list->GetUploads(999, &uploads);
  178. EXPECT_EQ(1u, uploads.size());
  179. double time_double = uploads[0].upload_time.ToDoubleT();
  180. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  181. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  182. EXPECT_STREQ("", uploads[0].local_id.c_str());
  183. time_double = uploads[0].capture_time.ToDoubleT();
  184. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  185. }
  186. // Test log entry string with local ID and capture time.
  187. // This is the format that WebRTC logs that only are stored locally are stored
  188. // in.
  189. TEST_F(TextLogUploadListTest, ParseLocalIdCaptureTime) {
  190. std::string test_entry = ",,";
  191. test_entry.append(kTestLocalID);
  192. test_entry += ",";
  193. test_entry.append(kTestCaptureTime);
  194. WriteUploadLog(test_entry);
  195. scoped_refptr<TextLogUploadList> upload_list =
  196. new TextLogUploadList(log_path());
  197. base::RunLoop run_loop;
  198. upload_list->Load(run_loop.QuitClosure());
  199. run_loop.Run();
  200. std::vector<UploadList::UploadInfo> uploads;
  201. upload_list->GetUploads(999, &uploads);
  202. EXPECT_EQ(1u, uploads.size());
  203. double time_double = uploads[0].upload_time.ToDoubleT();
  204. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  205. EXPECT_STREQ("", uploads[0].upload_id.c_str());
  206. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  207. time_double = uploads[0].capture_time.ToDoubleT();
  208. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  209. }
  210. TEST_F(TextLogUploadListTest, ParseLocalIdCaptureTime_JSON) {
  211. std::stringstream stream;
  212. stream << "{";
  213. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  214. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  215. stream << "}" << std::endl;
  216. WriteUploadLog(stream.str());
  217. scoped_refptr<TextLogUploadList> upload_list =
  218. new TextLogUploadList(log_path());
  219. base::RunLoop run_loop;
  220. upload_list->Load(run_loop.QuitClosure());
  221. run_loop.Run();
  222. std::vector<UploadList::UploadInfo> uploads;
  223. upload_list->GetUploads(999, &uploads);
  224. EXPECT_EQ(1u, uploads.size());
  225. double time_double = uploads[0].upload_time.ToDoubleT();
  226. EXPECT_STREQ("0", base::NumberToString(time_double).c_str());
  227. EXPECT_STREQ("", uploads[0].upload_id.c_str());
  228. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  229. time_double = uploads[0].capture_time.ToDoubleT();
  230. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  231. }
  232. // Test log entry string with upload time, upload ID, local ID and capture
  233. // time.
  234. // This is the format that WebRTC logs that are stored locally and have been
  235. // uploaded are stored in.
  236. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdLocalIdCaptureTime) {
  237. std::string test_entry = kTestUploadTime;
  238. test_entry += ",";
  239. test_entry.append(kTestUploadId);
  240. test_entry += ",";
  241. test_entry.append(kTestLocalID);
  242. test_entry += ",";
  243. test_entry.append(kTestCaptureTime);
  244. WriteUploadLog(test_entry);
  245. scoped_refptr<TextLogUploadList> upload_list =
  246. new TextLogUploadList(log_path());
  247. base::RunLoop run_loop;
  248. upload_list->Load(run_loop.QuitClosure());
  249. run_loop.Run();
  250. std::vector<UploadList::UploadInfo> uploads;
  251. upload_list->GetUploads(999, &uploads);
  252. EXPECT_EQ(1u, uploads.size());
  253. double time_double = uploads[0].upload_time.ToDoubleT();
  254. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  255. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  256. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  257. time_double = uploads[0].capture_time.ToDoubleT();
  258. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  259. }
  260. TEST_F(TextLogUploadListTest, ParseUploadTimeUploadIdLocalIdCaptureTime_JSON) {
  261. std::stringstream stream;
  262. stream << "{";
  263. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  264. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  265. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  266. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  267. stream << "}" << std::endl;
  268. WriteUploadLog(stream.str());
  269. scoped_refptr<TextLogUploadList> upload_list =
  270. new TextLogUploadList(log_path());
  271. base::RunLoop run_loop;
  272. upload_list->Load(run_loop.QuitClosure());
  273. run_loop.Run();
  274. std::vector<UploadList::UploadInfo> uploads;
  275. upload_list->GetUploads(999, &uploads);
  276. EXPECT_EQ(1u, uploads.size());
  277. double time_double = uploads[0].upload_time.ToDoubleT();
  278. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  279. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  280. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  281. time_double = uploads[0].capture_time.ToDoubleT();
  282. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  283. }
  284. TEST_F(TextLogUploadListTest, ParseMultipleEntries) {
  285. std::string test_entry;
  286. for (int i = 1; i <= 4; ++i) {
  287. test_entry.append(kTestUploadTime);
  288. test_entry += ",";
  289. test_entry.append(kTestUploadId);
  290. test_entry += ",";
  291. test_entry.append(base::NumberToString(i));
  292. test_entry += ",";
  293. test_entry.append(kTestCaptureTime);
  294. test_entry += "\n";
  295. }
  296. WriteUploadLog(test_entry);
  297. scoped_refptr<TextLogUploadList> upload_list =
  298. new TextLogUploadList(log_path());
  299. base::RunLoop run_loop;
  300. upload_list->Load(run_loop.QuitClosure());
  301. run_loop.Run();
  302. std::vector<UploadList::UploadInfo> uploads;
  303. upload_list->GetUploads(999, &uploads);
  304. EXPECT_EQ(4u, uploads.size());
  305. // The entries order should be reversed during the parsing.
  306. for (size_t i = 0; i < uploads.size(); ++i) {
  307. double time_double = uploads[i].upload_time.ToDoubleT();
  308. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  309. EXPECT_STREQ(kTestUploadId, uploads[i].upload_id.c_str());
  310. EXPECT_EQ(base::NumberToString(uploads.size() - i), uploads[i].local_id);
  311. time_double = uploads[i].capture_time.ToDoubleT();
  312. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  313. }
  314. }
  315. TEST_F(TextLogUploadListTest, ParseMultipleEntries_JSON) {
  316. std::stringstream stream;
  317. for (int i = 1; i <= 4; ++i) {
  318. stream << "{";
  319. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  320. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  321. stream << "\"local_id\":\"" << i << "\",";
  322. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  323. stream << "}" << std::endl;
  324. }
  325. WriteUploadLog(stream.str());
  326. scoped_refptr<TextLogUploadList> upload_list =
  327. new TextLogUploadList(log_path());
  328. base::RunLoop run_loop;
  329. upload_list->Load(run_loop.QuitClosure());
  330. run_loop.Run();
  331. std::vector<UploadList::UploadInfo> uploads;
  332. upload_list->GetUploads(999, &uploads);
  333. EXPECT_EQ(4u, uploads.size());
  334. // The entries order should be reversed during the parsing.
  335. for (size_t i = 0; i < uploads.size(); ++i) {
  336. double time_double = uploads[i].upload_time.ToDoubleT();
  337. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  338. EXPECT_STREQ(kTestUploadId, uploads[i].upload_id.c_str());
  339. EXPECT_EQ(base::NumberToString(uploads.size() - i), uploads[i].local_id);
  340. time_double = uploads[i].capture_time.ToDoubleT();
  341. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  342. }
  343. }
  344. TEST_F(TextLogUploadListTest, ParseWithState) {
  345. std::string test_entry;
  346. for (int i = 1; i <= 4; ++i) {
  347. test_entry.append(kTestUploadTime);
  348. test_entry += ",";
  349. test_entry.append(kTestUploadId);
  350. test_entry += ",";
  351. test_entry.append(kTestLocalID);
  352. test_entry += ",";
  353. test_entry.append(kTestCaptureTime);
  354. test_entry += ",";
  355. test_entry.append(base::NumberToString(
  356. static_cast<int>(UploadList::UploadInfo::State::Uploaded)));
  357. test_entry += "\n";
  358. }
  359. WriteUploadLog(test_entry);
  360. scoped_refptr<TextLogUploadList> upload_list =
  361. new TextLogUploadList(log_path());
  362. base::RunLoop run_loop;
  363. upload_list->Load(run_loop.QuitClosure());
  364. run_loop.Run();
  365. std::vector<UploadList::UploadInfo> uploads;
  366. upload_list->GetUploads(999, &uploads);
  367. EXPECT_EQ(4u, uploads.size());
  368. for (size_t i = 0; i < uploads.size(); ++i) {
  369. double time_double = uploads[i].upload_time.ToDoubleT();
  370. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  371. EXPECT_STREQ(kTestUploadId, uploads[i].upload_id.c_str());
  372. EXPECT_STREQ(kTestLocalID, uploads[i].local_id.c_str());
  373. time_double = uploads[i].capture_time.ToDoubleT();
  374. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  375. EXPECT_EQ(UploadList::UploadInfo::State::Uploaded, uploads[i].state);
  376. }
  377. }
  378. TEST_F(TextLogUploadListTest, ParseWithState_JSON) {
  379. std::stringstream stream;
  380. for (int i = 1; i <= 4; ++i) {
  381. stream << "{";
  382. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  383. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  384. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  385. stream << "\"capture_time\":\"" << kTestCaptureTime << "\",";
  386. stream << "\"state\":"
  387. << static_cast<int>(UploadList::UploadInfo::State::Uploaded);
  388. stream << "}" << std::endl;
  389. }
  390. WriteUploadLog(stream.str());
  391. scoped_refptr<TextLogUploadList> upload_list =
  392. new TextLogUploadList(log_path());
  393. base::RunLoop run_loop;
  394. upload_list->Load(run_loop.QuitClosure());
  395. run_loop.Run();
  396. std::vector<UploadList::UploadInfo> uploads;
  397. upload_list->GetUploads(999, &uploads);
  398. EXPECT_EQ(4u, uploads.size());
  399. for (const UploadList::UploadInfo& upload : uploads) {
  400. double time_double = upload.upload_time.ToDoubleT();
  401. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  402. EXPECT_STREQ(kTestUploadId, upload.upload_id.c_str());
  403. EXPECT_STREQ(kTestLocalID, upload.local_id.c_str());
  404. time_double = upload.capture_time.ToDoubleT();
  405. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  406. EXPECT_EQ(UploadList::UploadInfo::State::Uploaded, upload.state);
  407. }
  408. }
  409. TEST_F(TextLogUploadListTest, ParseWithSource_JSON) {
  410. std::stringstream stream;
  411. for (int i = 1; i <= 4; ++i) {
  412. stream << "{";
  413. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  414. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  415. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  416. stream << "\"capture_time\":\"" << kTestCaptureTime << "\",";
  417. stream << "\"state\":"
  418. << static_cast<int>(UploadList::UploadInfo::State::Uploaded) << ",";
  419. stream << "\"source\":\"" << kTestSource << "\"";
  420. stream << "}" << std::endl;
  421. }
  422. WriteUploadLog(stream.str());
  423. scoped_refptr<TextLogUploadList> upload_list =
  424. new TextLogUploadList(log_path());
  425. base::RunLoop run_loop;
  426. upload_list->Load(run_loop.QuitClosure());
  427. run_loop.Run();
  428. std::vector<UploadList::UploadInfo> uploads;
  429. upload_list->GetUploads(999, &uploads);
  430. EXPECT_EQ(4u, uploads.size());
  431. for (const UploadList::UploadInfo& upload : uploads) {
  432. double time_double = upload.upload_time.ToDoubleT();
  433. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  434. EXPECT_STREQ(kTestUploadId, upload.upload_id.c_str());
  435. EXPECT_STREQ(kTestLocalID, upload.local_id.c_str());
  436. time_double = upload.capture_time.ToDoubleT();
  437. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  438. EXPECT_EQ(UploadList::UploadInfo::State::Uploaded, upload.state);
  439. EXPECT_STREQ(kTestSource, upload.source.c_str());
  440. }
  441. }
  442. TEST_F(TextLogUploadListTest, ParseHybridFormat) {
  443. std::stringstream stream;
  444. for (int i = 1; i <= 4; ++i) {
  445. stream << kTestUploadTime << ",";
  446. stream << kTestUploadId << ",";
  447. stream << kTestLocalID << ",";
  448. stream << kTestCaptureTime << std::endl;
  449. }
  450. for (int i = 1; i <= 4; ++i) {
  451. stream << "{";
  452. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  453. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  454. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  455. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  456. stream << "}" << std::endl;
  457. }
  458. WriteUploadLog(stream.str());
  459. std::cout << stream.str() << std::endl;
  460. scoped_refptr<TextLogUploadList> upload_list =
  461. new TextLogUploadList(log_path());
  462. base::RunLoop run_loop;
  463. upload_list->Load(run_loop.QuitClosure());
  464. run_loop.Run();
  465. std::vector<UploadList::UploadInfo> uploads;
  466. upload_list->GetUploads(999, &uploads);
  467. EXPECT_EQ(8u, uploads.size());
  468. for (const UploadList::UploadInfo& upload : uploads) {
  469. double time_double = upload.upload_time.ToDoubleT();
  470. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  471. EXPECT_STREQ(kTestUploadId, upload.upload_id.c_str());
  472. EXPECT_STREQ(kTestLocalID, upload.local_id.c_str());
  473. time_double = upload.capture_time.ToDoubleT();
  474. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  475. }
  476. }
  477. TEST_F(TextLogUploadListTest, SkipInvalidEntry_JSON) {
  478. std::stringstream stream;
  479. // the first JSON entry contains the invalid |upload_id|.
  480. stream << "{";
  481. stream << "\"upload_time\":\"" << kTestCaptureTime << "\",";
  482. stream << "\"upload_id\":0.1234";
  483. stream << "}" << std::endl;
  484. // the second JSON entry is valid.
  485. stream << "{";
  486. stream << "\"upload_time\":\"" << kTestCaptureTime << "\",";
  487. stream << "\"upload_id\":\"" << kTestLocalID << "\"";
  488. stream << "}" << std::endl;
  489. WriteUploadLog(stream.str());
  490. scoped_refptr<TextLogUploadList> upload_list =
  491. new TextLogUploadList(log_path());
  492. base::RunLoop run_loop;
  493. upload_list->Load(run_loop.QuitClosure());
  494. run_loop.Run();
  495. std::vector<UploadList::UploadInfo> uploads;
  496. upload_list->GetUploads(999, &uploads);
  497. // The invalid JSON entry should be skipped.
  498. EXPECT_EQ(1u, uploads.size());
  499. }
  500. // Test log entry string with only single column.
  501. // Such kind of lines are considered as invalid CSV entry. They should be
  502. // skipped in parsing the log file.
  503. TEST_F(TextLogUploadListTest, SkipBlankOrCorruptedEntry) {
  504. std::string test_entry;
  505. // Add an empty line.
  506. test_entry += "\n";
  507. // Add a line with only single column.
  508. test_entry.append(kTestUploadTime);
  509. test_entry += "\n";
  510. WriteUploadLog(test_entry);
  511. scoped_refptr<TextLogUploadList> upload_list =
  512. new TextLogUploadList(log_path());
  513. base::RunLoop run_loop;
  514. upload_list->Load(run_loop.QuitClosure());
  515. run_loop.Run();
  516. std::vector<UploadList::UploadInfo> uploads;
  517. upload_list->GetUploads(999, &uploads);
  518. EXPECT_EQ(0u, uploads.size());
  519. }
  520. TEST_F(TextLogUploadListTest, ClearUsingUploadTime) {
  521. constexpr time_t kTestTime = 1234u;
  522. constexpr char kOtherEntry[] = "4567,def\n";
  523. std::string test_entry = base::NumberToString(kTestTime);
  524. test_entry.append(",abc\n");
  525. test_entry.append(kOtherEntry);
  526. WriteUploadLog(test_entry);
  527. scoped_refptr<TextLogUploadList> upload_list =
  528. new TextLogUploadList(log_path());
  529. base::RunLoop run_loop;
  530. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  531. base::Time::FromTimeT(kTestTime + 1),
  532. run_loop.QuitClosure());
  533. run_loop.Run();
  534. std::string contents;
  535. base::ReadFileToString(log_path(), &contents);
  536. EXPECT_EQ(kOtherEntry, contents);
  537. }
  538. TEST_F(TextLogUploadListTest, ClearUsingUploadTime_JSON) {
  539. constexpr time_t kTestTime = 1234u;
  540. std::stringstream stream_other_entry;
  541. stream_other_entry << "{";
  542. stream_other_entry << "\"upload_time\":\"4567\",";
  543. stream_other_entry << "\"upload_id\":\"def\"";
  544. stream_other_entry << "}" << std::endl;
  545. std::string other_entry = stream_other_entry.str();
  546. std::stringstream stream;
  547. stream << "{";
  548. stream << "\"upload_time\":\"" << kTestTime << "\",";
  549. stream << "\"upload_id\":\"abc\"";
  550. stream << "}" << std::endl;
  551. stream << other_entry;
  552. WriteUploadLog(stream.str());
  553. scoped_refptr<TextLogUploadList> upload_list =
  554. new TextLogUploadList(log_path());
  555. base::RunLoop run_loop;
  556. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  557. base::Time::FromTimeT(kTestTime + 1),
  558. run_loop.QuitClosure());
  559. run_loop.Run();
  560. std::string contents;
  561. base::ReadFileToString(log_path(), &contents);
  562. EXPECT_EQ(other_entry, contents);
  563. }
  564. TEST_F(TextLogUploadListTest, ClearUsingCaptureTime) {
  565. constexpr time_t kTestTime = 1234u;
  566. constexpr char kOtherEntry[] = "4567,def,def,7890\n";
  567. std::string test_entry = kOtherEntry;
  568. test_entry.append("4567,abc,abc,");
  569. test_entry.append(base::NumberToString(kTestTime));
  570. test_entry.append("\n");
  571. WriteUploadLog(test_entry);
  572. scoped_refptr<TextLogUploadList> upload_list =
  573. new TextLogUploadList(log_path());
  574. base::RunLoop run_loop;
  575. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  576. base::Time::FromTimeT(kTestTime + 1),
  577. run_loop.QuitClosure());
  578. run_loop.Run();
  579. std::string contents;
  580. ASSERT_TRUE(base::ReadFileToString(log_path(), &contents));
  581. EXPECT_EQ(kOtherEntry, contents);
  582. }
  583. TEST_F(TextLogUploadListTest, ClearUsingCaptureTime_JSON) {
  584. constexpr time_t kTestTime = 1234u;
  585. std::stringstream stream_other_entry;
  586. stream_other_entry << "{";
  587. stream_other_entry << "\"upload_time\":\"4567\",";
  588. stream_other_entry << "\"upload_id\":\"def\",";
  589. stream_other_entry << "\"local_id\":\"def\",";
  590. stream_other_entry << "\"capture_time\":\"7890\"";
  591. stream_other_entry << "}" << std::endl;
  592. std::string other_entry = stream_other_entry.str();
  593. std::stringstream stream;
  594. stream << "{";
  595. stream << "\"upload_time\":\"4567\",";
  596. stream << "\"upload_id\":\"abc\",";
  597. stream << "\"local_id\":\"abc\",";
  598. stream << "\"capture_time\":\"" << kTestTime << "\"";
  599. stream << "}" << std::endl;
  600. stream << other_entry;
  601. WriteUploadLog(stream.str());
  602. scoped_refptr<TextLogUploadList> upload_list =
  603. new TextLogUploadList(log_path());
  604. base::RunLoop run_loop;
  605. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  606. base::Time::FromTimeT(kTestTime + 1),
  607. run_loop.QuitClosure());
  608. run_loop.Run();
  609. std::string contents;
  610. ASSERT_TRUE(base::ReadFileToString(log_path(), &contents));
  611. EXPECT_EQ(other_entry, contents);
  612. }
  613. TEST_F(TextLogUploadListTest, ClearingAllDataDeletesFile) {
  614. constexpr time_t kTestTime = 1234u;
  615. std::string test_entry = base::NumberToString(kTestTime);
  616. test_entry.append(",abc\n");
  617. WriteUploadLog(test_entry);
  618. scoped_refptr<TextLogUploadList> upload_list =
  619. new TextLogUploadList(log_path());
  620. base::RunLoop run_loop;
  621. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  622. base::Time::FromTimeT(kTestTime + 1),
  623. run_loop.QuitClosure());
  624. run_loop.Run();
  625. EXPECT_FALSE(base::PathExists(log_path()));
  626. }
  627. TEST_F(TextLogUploadListTest, ClearingAllDataDeletesFile_JSON) {
  628. constexpr time_t kTestTime = 1234u;
  629. std::stringstream stream;
  630. stream << "{";
  631. stream << "\"upload_time\":\"" << kTestTime << "\",";
  632. stream << "\"upload_id\":\"abc\"";
  633. stream << "}" << std::endl;
  634. WriteUploadLog(stream.str());
  635. scoped_refptr<TextLogUploadList> upload_list =
  636. new TextLogUploadList(log_path());
  637. base::RunLoop run_loop;
  638. upload_list->Clear(base::Time::FromTimeT(kTestTime),
  639. base::Time::FromTimeT(kTestTime + 1),
  640. run_loop.QuitClosure());
  641. run_loop.Run();
  642. EXPECT_FALSE(base::PathExists(log_path()));
  643. }
  644. // https://crbug.com/597384
  645. TEST_F(TextLogUploadListTest, SimultaneousAccess) {
  646. std::string test_entry = kTestUploadTime;
  647. test_entry += ",";
  648. test_entry.append(kTestUploadId);
  649. test_entry += ",";
  650. test_entry.append(kTestLocalID);
  651. test_entry += ",";
  652. test_entry.append(kTestCaptureTime);
  653. WriteUploadLog(test_entry);
  654. scoped_refptr<TextLogUploadList> upload_list =
  655. new TextLogUploadList(log_path());
  656. // Queue up a bunch of loads, waiting only for the first one to complete.
  657. base::RunLoop run_loop;
  658. upload_list->Load(run_loop.QuitClosure());
  659. run_loop.Run();
  660. for (int i = 1; i <= 20; ++i) {
  661. upload_list->Load(base::OnceClosure());
  662. }
  663. // Read the list a few times to try and race one of the loads above.
  664. for (int i = 1; i <= 4; ++i) {
  665. std::vector<UploadList::UploadInfo> uploads;
  666. upload_list->GetUploads(999, &uploads);
  667. EXPECT_EQ(1u, uploads.size());
  668. double time_double = uploads[0].upload_time.ToDoubleT();
  669. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  670. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  671. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  672. time_double = uploads[0].capture_time.ToDoubleT();
  673. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  674. }
  675. // Allow the remaining loads to complete.
  676. task_environment_.RunUntilIdle();
  677. }
  678. TEST_F(TextLogUploadListTest, SimultaneousAccess_JSON) {
  679. std::stringstream stream;
  680. stream << "{";
  681. stream << "\"upload_time\":\"" << kTestUploadTime << "\",";
  682. stream << "\"upload_id\":\"" << kTestUploadId << "\",";
  683. stream << "\"local_id\":\"" << kTestLocalID << "\",";
  684. stream << "\"capture_time\":\"" << kTestCaptureTime << "\"";
  685. stream << "}" << std::endl;
  686. WriteUploadLog(stream.str());
  687. scoped_refptr<TextLogUploadList> upload_list =
  688. new TextLogUploadList(log_path());
  689. // Queue up a bunch of loads, waiting only for the first one to complete.
  690. base::RunLoop run_loop;
  691. upload_list->Load(run_loop.QuitClosure());
  692. run_loop.Run();
  693. for (int i = 1; i <= 20; ++i) {
  694. upload_list->Load(base::OnceClosure());
  695. }
  696. // Read the list a few times to try and race one of the loads above.
  697. for (int i = 1; i <= 4; ++i) {
  698. std::vector<UploadList::UploadInfo> uploads;
  699. upload_list->GetUploads(999, &uploads);
  700. EXPECT_EQ(1u, uploads.size());
  701. double time_double = uploads[0].upload_time.ToDoubleT();
  702. EXPECT_STREQ(kTestUploadTime, base::NumberToString(time_double).c_str());
  703. EXPECT_STREQ(kTestUploadId, uploads[0].upload_id.c_str());
  704. EXPECT_STREQ(kTestLocalID, uploads[0].local_id.c_str());
  705. time_double = uploads[0].capture_time.ToDoubleT();
  706. EXPECT_STREQ(kTestCaptureTime, base::NumberToString(time_double).c_str());
  707. }
  708. // Allow the remaining loads to complete.
  709. task_environment_.RunUntilIdle();
  710. }
  711. } // namespace