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

Unified Diff: chrome/browser/component_updater/sth_set_component_installer_unittest.cc

Issue 1853753003: Certificate Transparency: New component for obtaining fresh STHs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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: chrome/browser/component_updater/sth_set_component_installer_unittest.cc
diff --git a/chrome/browser/component_updater/sth_set_component_installer_unittest.cc b/chrome/browser/component_updater/sth_set_component_installer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..703c12003e00e79cbad0165854d2a6999b9b28ed
--- /dev/null
+++ b/chrome/browser/component_updater/sth_set_component_installer_unittest.cc
@@ -0,0 +1,123 @@
+// 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.
+
+// Test cases:
+// 1. Parses valid STH JSON in a file with valid hex encoding of log id.
+// 2. Does not notify of invalid STH JSON.
+// 3. Does not notify of valid JSON but in a file not hex-encoded log id.
+
+#include "chrome/browser/component_updater/sth_set_component_installer.h"
+
+#include <string>
+#include <vector>
+
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "base/version.h"
+#include "components/safe_json/testing_json_parser.h"
+#include "net/cert/signed_tree_head.h"
+#include "net/cert/sth_observer.h"
+#include "net/test/ct_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+// using component_updater::STHSetComponentInstallerTraits;
+
+namespace component_updater {
+
+class StoringSTHObserver : public net::ct::STHObserver {
+ public:
+ void NewSTHObserved(const net::ct::SignedTreeHead& sth) override {
+ sths.push_back(sth);
+ }
+
+ std::vector<net::ct::SignedTreeHead> sths;
+};
+
+class STHSetComponentInstallerTest : public PlatformTest {
+ public:
+ STHSetComponentInstallerTest() {}
+ void SetUp() override {
+ PlatformTest::SetUp();
+
+ // ScopedTempDir automatically does a recursive delete on the entire
+ // directory in its destructor, so no cleanup is required in TearDown.
+ // Note that all files created by this test case are created within the
+ // directory that is created here, so even though they are not explicitly
+ // created *as temp files*, they will still get cleaned up automagically.
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+
+ scoped_ptr<StoringSTHObserver> observer(new StoringSTHObserver());
+ observer_ = observer.get();
+ traits_.reset(new STHSetComponentInstallerTraits(std::move(observer)));
+ }
+
+ void WriteSTHToFile(const std::string& sth_json,
+ const base::FilePath& filename) {
+ ASSERT_EQ(static_cast<int32_t>(sth_json.length()),
+ base::WriteFile(filename, sth_json.data(), sth_json.length()));
+ }
+
+ protected:
+ base::MessageLoopForIO message_loop_;
+ base::RunLoop run_loop_;
+
+ base::ScopedTempDir temp_dir_;
+ scoped_ptr<STHSetComponentInstallerTraits> traits_;
+ StoringSTHObserver* observer_;
+ safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(STHSetComponentInstallerTest);
+};
+
+TEST_F(STHSetComponentInstallerTest, CanLoadAllSTHs) {
+ const base::DictionaryValue manifest;
+ ASSERT_FALSE(traits_->VerifyInstallation(manifest, temp_dir_.path()));
+ const base::FilePath sths_dir =
+ temp_dir_.path()
+ .Append(FILE_PATH_LITERAL("_platform_specific"))
+ .Append(FILE_PATH_LITERAL("all"))
+ .Append(FILE_PATH_LITERAL("sths"));
+ ASSERT_TRUE(base::CreateDirectory(sths_dir));
+
+ std::string good_sth_json = net::ct::GetSampleSTHAsJson();
+ const base::FilePath first_sth =
+ sths_dir.Append(FILE_PATH_LITERAL("0a0b0c.sth"));
+ WriteSTHToFile(good_sth_json, first_sth);
+
+ const base::FilePath second_sth =
+ sths_dir.Append(FILE_PATH_LITERAL("0a000d.sth"));
+ WriteSTHToFile(good_sth_json, second_sth);
+
+ const base::FilePath not_hex_sth_file =
+ sths_dir.Append(FILE_PATH_LITERAL("nothex.sth"));
+ WriteSTHToFile(good_sth_json, not_hex_sth_file);
+
+ const base::FilePath invalid_sth =
+ sths_dir.Append(FILE_PATH_LITERAL("010101.sth"));
+ WriteSTHToFile(std::string("{invalid json}"), invalid_sth);
+
+ ASSERT_TRUE(traits_->VerifyInstallation(manifest, temp_dir_.path()));
+
+ base::Version v("1.0");
+ traits_->LoadSTHsFromDisk(sths_dir, v);
+ run_loop_.RunUntilIdle();
+
+ // Currently test does not wait long enough for the observer_
+ // to be notified of the new STHs because the STHSetComponentInstallerTraits
+ // posts a task to the IO thread, not covered by the run_loop_.
+ // TODO(eranm): Make the STHSetComponentInstallerTraits use a provided
+ // RunLoop or make the test wait correctly for tasks on the IO thread.
+ //EXPECT_EQ(2u, observer_->sths.size());
waffles 2016/04/01 17:01:55 I think this can be done with a BrowserThreadBundl
Eran Messeri 2016/04/04 13:10:02 Done, thanks! That's exactly what I was after.
+}
+
+} // namespace component_updater

Powered by Google App Engine
This is Rietveld 408576698