Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 // Test cases: | |
| 6 // 1. Parses valid STH JSON in a file with valid hex encoding of log id. | |
| 7 // 2. Does not notify of invalid STH JSON. | |
| 8 // 3. Does not notify of valid JSON but in a file not hex-encoded log id. | |
|
Ryan Sleevi
2016/04/06 18:32:51
Please move this sort of description to the indivi
Eran Messeri
2016/04/07 11:38:03
Done
| |
| 9 | |
| 10 #include "chrome/browser/component_updater/sth_set_component_installer.h" | |
| 11 | |
| 12 #include <map> | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/files/file_util.h" | |
| 17 #include "base/files/scoped_temp_dir.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/run_loop.h" | |
| 21 #include "base/strings/string_util.h" | |
| 22 #include "base/strings/utf_string_conversions.h" | |
| 23 #include "base/values.h" | |
| 24 #include "base/version.h" | |
| 25 #include "components/safe_json/testing_json_parser.h" | |
| 26 #include "content/public/test/test_browser_thread_bundle.h" | |
| 27 #include "net/cert/signed_tree_head.h" | |
| 28 #include "net/cert/sth_observer.h" | |
| 29 #include "net/test/ct_test_util.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 #include "testing/platform_test.h" | |
| 32 | |
| 33 namespace component_updater { | |
| 34 | |
| 35 class StoringSTHObserver : public net::ct::STHObserver { | |
| 36 public: | |
| 37 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override { | |
| 38 sths[sth.log_id] = sth; | |
| 39 } | |
| 40 | |
| 41 std::map<std::string, net::ct::SignedTreeHead> sths; | |
| 42 }; | |
| 43 | |
| 44 class STHSetComponentInstallerTest : public PlatformTest { | |
| 45 public: | |
| 46 STHSetComponentInstallerTest() {} | |
| 47 void SetUp() override { | |
| 48 PlatformTest::SetUp(); | |
| 49 | |
| 50 // ScopedTempDir automatically does a recursive delete on the entire | |
| 51 // directory in its destructor, so no cleanup is required in TearDown. | |
| 52 // Note that all files created by this test case are created within the | |
| 53 // directory that is created here, so even though they are not explicitly | |
| 54 // created *as temp files*, they will still get cleaned up automagically. | |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 56 | |
| 57 scoped_ptr<StoringSTHObserver> observer(new StoringSTHObserver()); | |
| 58 observer_ = observer.get(); | |
| 59 traits_.reset(new STHSetComponentInstallerTraits(std::move(observer))); | |
| 60 } | |
| 61 | |
| 62 void WriteSTHToFile(const std::string& sth_json, | |
| 63 const base::FilePath& filename) { | |
| 64 ASSERT_EQ(static_cast<int32_t>(sth_json.length()), | |
| 65 base::WriteFile(filename, sth_json.data(), sth_json.length())); | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 content::TestBrowserThreadBundle thread_bundle_; | |
| 70 | |
| 71 base::ScopedTempDir temp_dir_; | |
| 72 scoped_ptr<STHSetComponentInstallerTraits> traits_; | |
| 73 StoringSTHObserver* observer_; | |
| 74 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(STHSetComponentInstallerTest); | |
| 78 }; | |
| 79 | |
| 80 TEST_F(STHSetComponentInstallerTest, CanLoadAllSTHs) { | |
| 81 const base::DictionaryValue manifest; | |
| 82 ASSERT_FALSE(traits_->VerifyInstallation(manifest, temp_dir_.path())); | |
| 83 const base::FilePath sths_dir = | |
| 84 temp_dir_.path() | |
| 85 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 86 .Append(FILE_PATH_LITERAL("all")) | |
| 87 .Append(FILE_PATH_LITERAL("sths")); | |
| 88 ASSERT_TRUE(base::CreateDirectory(sths_dir)); | |
| 89 | |
| 90 const std::string good_sth_json = net::ct::GetSampleSTHAsJson(); | |
| 91 const base::FilePath first_sth_file = | |
| 92 sths_dir.Append(FILE_PATH_LITERAL("616263.sth")); | |
| 93 WriteSTHToFile(good_sth_json, first_sth_file); | |
| 94 | |
| 95 const base::FilePath second_sth_file = | |
| 96 sths_dir.Append(FILE_PATH_LITERAL("610064.sth")); | |
| 97 WriteSTHToFile(good_sth_json, second_sth_file); | |
| 98 | |
| 99 const base::FilePath not_hex_sth_file = | |
| 100 sths_dir.Append(FILE_PATH_LITERAL("nothex.sth")); | |
| 101 WriteSTHToFile(good_sth_json, not_hex_sth_file); | |
| 102 | |
| 103 const base::FilePath invalid_sth = | |
| 104 sths_dir.Append(FILE_PATH_LITERAL("010101.sth")); | |
| 105 WriteSTHToFile(std::string("{invalid json}"), invalid_sth); | |
| 106 | |
| 107 ASSERT_TRUE(traits_->VerifyInstallation(manifest, temp_dir_.path())); | |
| 108 | |
| 109 const base::Version v("1.0"); | |
| 110 traits_->LoadSTHsFromDisk(sths_dir, v); | |
| 111 // Drain the RunLoop created by the TestBrowserThreadBundle | |
| 112 base::RunLoop().RunUntilIdle(); | |
| 113 | |
| 114 EXPECT_EQ(2u, observer_->sths.size()); | |
| 115 | |
| 116 const std::string first_log_id("abc"); | |
| 117 ASSERT_TRUE(observer_->sths.find(first_log_id) != observer_->sths.end()); | |
| 118 const net::ct::SignedTreeHead& first_sth(observer_->sths[first_log_id]); | |
| 119 EXPECT_EQ(21u, first_sth.tree_size); | |
| 120 | |
| 121 const std::string second_log_id("a\00d", 3); | |
| 122 ASSERT_TRUE(observer_->sths.find(second_log_id) != observer_->sths.end()); | |
| 123 } | |
| 124 | |
| 125 } // namespace component_updater | |
| OLD | NEW |