| 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 #include "chrome/browser/component_updater/sth_set_component_installer.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/values.h" |
| 19 #include "base/version.h" |
| 20 #include "components/safe_json/testing_json_parser.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "net/cert/signed_tree_head.h" |
| 23 #include "net/cert/sth_observer.h" |
| 24 #include "net/test/ct_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "testing/platform_test.h" |
| 27 |
| 28 namespace component_updater { |
| 29 |
| 30 class StoringSTHObserver : public net::ct::STHObserver { |
| 31 public: |
| 32 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override { |
| 33 sths[sth.log_id] = sth; |
| 34 } |
| 35 |
| 36 std::map<std::string, net::ct::SignedTreeHead> sths; |
| 37 }; |
| 38 |
| 39 class STHSetComponentInstallerTest : public PlatformTest { |
| 40 public: |
| 41 STHSetComponentInstallerTest() {} |
| 42 void SetUp() override { |
| 43 PlatformTest::SetUp(); |
| 44 |
| 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 46 |
| 47 scoped_ptr<StoringSTHObserver> observer(new StoringSTHObserver()); |
| 48 observer_ = observer.get(); |
| 49 traits_.reset(new STHSetComponentInstallerTraits(std::move(observer))); |
| 50 } |
| 51 |
| 52 void WriteSTHToFile(const std::string& sth_json, |
| 53 const base::FilePath& filename) { |
| 54 ASSERT_EQ(static_cast<int32_t>(sth_json.length()), |
| 55 base::WriteFile(filename, sth_json.data(), sth_json.length())); |
| 56 } |
| 57 |
| 58 base::FilePath GetSTHsDir() { |
| 59 return temp_dir_.path() |
| 60 .Append(FILE_PATH_LITERAL("_platform_specific")) |
| 61 .Append(FILE_PATH_LITERAL("all")) |
| 62 .Append(FILE_PATH_LITERAL("sths")); |
| 63 } |
| 64 |
| 65 void CreateSTHsDir(const base::DictionaryValue& manifest, |
| 66 const base::FilePath& sths_dir) { |
| 67 ASSERT_FALSE(traits_->VerifyInstallation(manifest, temp_dir_.path())); |
| 68 ASSERT_TRUE(base::CreateDirectory(sths_dir)); |
| 69 } |
| 70 |
| 71 void LoadSTHs(const base::DictionaryValue& manifest, |
| 72 const base::FilePath& sths_dir) { |
| 73 ASSERT_TRUE(traits_->VerifyInstallation(manifest, temp_dir_.path())); |
| 74 |
| 75 const base::Version v("1.0"); |
| 76 traits_->LoadSTHsFromDisk(sths_dir, v); |
| 77 // Drain the RunLoop created by the TestBrowserThreadBundle |
| 78 base::RunLoop().RunUntilIdle(); |
| 79 } |
| 80 |
| 81 protected: |
| 82 content::TestBrowserThreadBundle thread_bundle_; |
| 83 |
| 84 base::ScopedTempDir temp_dir_; |
| 85 scoped_ptr<STHSetComponentInstallerTraits> traits_; |
| 86 StoringSTHObserver* observer_; |
| 87 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; |
| 88 |
| 89 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(STHSetComponentInstallerTest); |
| 91 }; |
| 92 |
| 93 // Parses valid STH JSON in a file with valid hex encoding of log id. |
| 94 TEST_F(STHSetComponentInstallerTest, CanLoadAllSTHs) { |
| 95 const base::DictionaryValue manifest; |
| 96 const base::FilePath sths_dir(GetSTHsDir()); |
| 97 CreateSTHsDir(manifest, sths_dir); |
| 98 |
| 99 const std::string good_sth_json = net::ct::GetSampleSTHAsJson(); |
| 100 const base::FilePath first_sth_file = |
| 101 sths_dir.Append(FILE_PATH_LITERAL("616263.sth")); |
| 102 WriteSTHToFile(good_sth_json, first_sth_file); |
| 103 |
| 104 const base::FilePath second_sth_file = |
| 105 sths_dir.Append(FILE_PATH_LITERAL("610064.sth")); |
| 106 WriteSTHToFile(good_sth_json, second_sth_file); |
| 107 |
| 108 LoadSTHs(manifest, sths_dir); |
| 109 |
| 110 EXPECT_EQ(2u, observer_->sths.size()); |
| 111 |
| 112 const std::string first_log_id("abc"); |
| 113 ASSERT_TRUE(observer_->sths.find(first_log_id) != observer_->sths.end()); |
| 114 const net::ct::SignedTreeHead& first_sth(observer_->sths[first_log_id]); |
| 115 EXPECT_EQ(21u, first_sth.tree_size); |
| 116 |
| 117 const std::string second_log_id("a\00d", 3); |
| 118 ASSERT_TRUE(observer_->sths.find(second_log_id) != observer_->sths.end()); |
| 119 } |
| 120 |
| 121 // Does not notify of invalid STH JSON. |
| 122 TEST_F(STHSetComponentInstallerTest, DoesNotLoadInvalidJSON) { |
| 123 const base::DictionaryValue manifest; |
| 124 const base::FilePath sths_dir(GetSTHsDir()); |
| 125 CreateSTHsDir(manifest, sths_dir); |
| 126 |
| 127 const base::FilePath invalid_sth = |
| 128 sths_dir.Append(FILE_PATH_LITERAL("010101.sth")); |
| 129 WriteSTHToFile(std::string("{invalid json}"), invalid_sth); |
| 130 |
| 131 LoadSTHs(manifest, sths_dir); |
| 132 EXPECT_EQ(0u, observer_->sths.size()); |
| 133 } |
| 134 |
| 135 // Does not notify of valid JSON but in a file not hex-encoded log id. |
| 136 TEST_F(STHSetComponentInstallerTest, |
| 137 DoesNotLoadValidJSONFromFileNotHexEncoded) { |
| 138 const base::DictionaryValue manifest; |
| 139 const base::FilePath sths_dir(GetSTHsDir()); |
| 140 CreateSTHsDir(manifest, sths_dir); |
| 141 |
| 142 const base::FilePath not_hex_sth_file = |
| 143 sths_dir.Append(FILE_PATH_LITERAL("nothex.sth")); |
| 144 WriteSTHToFile(net::ct::GetSampleSTHAsJson(), not_hex_sth_file); |
| 145 |
| 146 LoadSTHs(manifest, sths_dir); |
| 147 EXPECT_EQ(0u, observer_->sths.size()); |
| 148 } |
| 149 |
| 150 } // namespace component_updater |
| OLD | NEW |