Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: components/safe_browsing_db/v4_store_unittest.cc

Issue 2066083002: SafeBrowising: Read and write V4Store from/to disk (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@01_UpdateDbAndStores
Patch Set: Nit: better test names. Tests for WriteToDisk Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/safe_browsing_db/v4_store_unittest.cc
diff --git a/components/safe_browsing_db/v4_store_unittest.cc b/components/safe_browsing_db/v4_store_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9824742f62e377a378a8f016a70ff68173f7abb2
--- /dev/null
+++ b/components/safe_browsing_db/v4_store_unittest.cc
@@ -0,0 +1,142 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/files/file_util.h"
+#include "base/run_loop.h"
+#include "base/test/test_simple_task_runner.h"
+#include "components/safe_browsing_db/v4_store.h"
+#include "components/safe_browsing_db/v4_store.pb.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/platform_test.h"
+
+namespace safe_browsing {
+
+class V4StoreTest : public PlatformTest {
+ public:
+ V4StoreTest() : task_runner_(new base::TestSimpleTaskRunner) {}
+
+ void TearDown() override {
+ base::DeleteFile(store_path_, false);
+ PlatformTest::TearDown();
+ }
+
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
+ base::FilePath store_path_;
+ content::TestBrowserThreadBundle thread_bundle_;
+};
+
+TEST_F(V4StoreTest, TestReadFromEmptyFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ V4Store* store = new V4Store(task_runner_, store_path_);
Nathan Parker 2016/06/16 00:07:50 Leaked ptr, here an below. Could just put these o
vakh (use Gerrit instead) 2016/06/16 08:25:19 Done.
+ EXPECT_EQ(FILE_EMPTY_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromAbsentFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+ base::DeleteFile(store_path_, false);
+
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(FILE_UNREADABLE_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromInvalidContentsFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+ std::string invalid_contents = "Chromium";
+ base::WriteFile(store_path_, invalid_contents.c_str(),
+ invalid_contents.size());
+
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(PROTO_PARSING_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromUnexpectedMagicFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ V4StoreFileFormat file_format;
+ file_format.set_magic_number(111);
+ std::string file_format_string;
+ file_format.SerializeToString(&file_format_string);
+ base::WriteFile(store_path_, file_format_string.c_str(),
+ file_format_string.size());
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(UNEXPECTED_MAGIC_NUMBER_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromLowVersionFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ V4StoreFileFormat file_format;
+ file_format.set_magic_number(0x600D71FE);
+ file_format.set_version_number(2);
+ std::string file_format_string;
+ file_format.SerializeToString(&file_format_string);
+ base::WriteFile(store_path_, file_format_string.c_str(),
+ file_format_string.size());
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(FILE_VERSION_TOO_LOW_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromNoHashPrefixInfoFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ V4StoreFileFormat file_format;
+ file_format.set_magic_number(0x600D71FE);
+ file_format.set_version_number(9);
+ std::string file_format_string;
+ file_format.SerializeToString(&file_format_string);
+ base::WriteFile(store_path_, file_format_string.c_str(),
+ file_format_string.size());
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(HASH_PREFIX_INFO_MISSING_FAILURE, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestReadFromNoHashPrefixesFile) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ V4StoreFileFormat file_format;
+ file_format.set_magic_number(0x600D71FE);
+ file_format.set_version_number(9);
+ ListUpdateResponse* list_update_response =
+ file_format.mutable_list_update_response();
+ list_update_response->set_platform_type(LINUX_PLATFORM);
+
+ std::string file_format_string;
+ file_format.SerializeToString(&file_format_string);
+ base::WriteFile(store_path_, file_format_string.c_str(),
+ file_format_string.size());
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(SUCCESS, store->ReadFromDisk());
+}
+
+TEST_F(V4StoreTest, TestWriteNoResponseType) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ ListUpdateResponse list_update_response;
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_FALSE(store->WriteToDisk(list_update_response));
+}
+
+TEST_F(V4StoreTest, TestWritePartialResponseType) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ ListUpdateResponse list_update_response;
+ list_update_response.set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
+ V4Store* store = new V4Store(task_runner_, store_path_);
+ EXPECT_FALSE(store->WriteToDisk(list_update_response));
+}
+
+TEST_F(V4StoreTest, TestWriteFullResponseType) {
+ base::CreateAndOpenTemporaryFile(&store_path_);
+
+ ListUpdateResponse list_update_response;
+ list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE);
+ V4Store* written_store = new V4Store(task_runner_, store_path_);
+ EXPECT_TRUE(written_store->WriteToDisk(list_update_response));
+
+ V4Store* read_store = new V4Store(task_runner_, store_path_);
+ EXPECT_EQ(SUCCESS, read_store->ReadFromDisk());
Nathan Parker 2016/06/16 00:07:50 If you're going to test writing+reading you might
vakh (use Gerrit instead) 2016/06/16 08:25:19 Done. Verifying that the state read back from disk
+}
+
+} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698