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. | |
| 9 | |
| 10 #include "chrome/browser/component_updater/sth_set_component_installer.h" | |
| 11 | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 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 "net/cert/signed_tree_head.h" | |
| 27 #include "net/cert/sth_observer.h" | |
| 28 #include "net/test/ct_test_util.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 #include "testing/platform_test.h" | |
| 31 | |
| 32 // using component_updater::STHSetComponentInstallerTraits; | |
| 33 | |
| 34 namespace component_updater { | |
| 35 | |
| 36 class StoringSTHObserver : public net::ct::STHObserver { | |
| 37 public: | |
| 38 void NewSTHObserved(const net::ct::SignedTreeHead& sth) override { | |
| 39 sths.push_back(sth); | |
| 40 } | |
| 41 | |
| 42 std::vector<net::ct::SignedTreeHead> sths; | |
| 43 }; | |
| 44 | |
| 45 class STHSetComponentInstallerTest : public PlatformTest { | |
| 46 public: | |
| 47 STHSetComponentInstallerTest() {} | |
| 48 void SetUp() override { | |
| 49 PlatformTest::SetUp(); | |
| 50 | |
| 51 // ScopedTempDir automatically does a recursive delete on the entire | |
| 52 // directory in its destructor, so no cleanup is required in TearDown. | |
| 53 // Note that all files created by this test case are created within the | |
| 54 // directory that is created here, so even though they are not explicitly | |
| 55 // created *as temp files*, they will still get cleaned up automagically. | |
| 56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 57 | |
| 58 scoped_ptr<StoringSTHObserver> observer(new StoringSTHObserver()); | |
| 59 observer_ = observer.get(); | |
| 60 traits_.reset(new STHSetComponentInstallerTraits(std::move(observer))); | |
| 61 } | |
| 62 | |
| 63 void WriteSTHToFile(const std::string& sth_json, | |
| 64 const base::FilePath& filename) { | |
| 65 ASSERT_EQ(static_cast<int32_t>(sth_json.length()), | |
| 66 base::WriteFile(filename, sth_json.data(), sth_json.length())); | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 base::MessageLoopForIO message_loop_; | |
| 71 base::RunLoop run_loop_; | |
| 72 | |
| 73 base::ScopedTempDir temp_dir_; | |
| 74 scoped_ptr<STHSetComponentInstallerTraits> traits_; | |
| 75 StoringSTHObserver* observer_; | |
| 76 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | |
| 77 | |
| 78 private: | |
| 79 DISALLOW_COPY_AND_ASSIGN(STHSetComponentInstallerTest); | |
| 80 }; | |
| 81 | |
| 82 TEST_F(STHSetComponentInstallerTest, CanLoadAllSTHs) { | |
| 83 const base::DictionaryValue manifest; | |
| 84 ASSERT_FALSE(traits_->VerifyInstallation(manifest, temp_dir_.path())); | |
| 85 const base::FilePath sths_dir = | |
| 86 temp_dir_.path() | |
| 87 .Append(FILE_PATH_LITERAL("_platform_specific")) | |
| 88 .Append(FILE_PATH_LITERAL("all")) | |
| 89 .Append(FILE_PATH_LITERAL("sths")); | |
| 90 ASSERT_TRUE(base::CreateDirectory(sths_dir)); | |
| 91 | |
| 92 std::string good_sth_json = net::ct::GetSampleSTHAsJson(); | |
| 93 const base::FilePath first_sth = | |
| 94 sths_dir.Append(FILE_PATH_LITERAL("0a0b0c.sth")); | |
| 95 WriteSTHToFile(good_sth_json, first_sth); | |
| 96 | |
| 97 const base::FilePath second_sth = | |
| 98 sths_dir.Append(FILE_PATH_LITERAL("0a000d.sth")); | |
| 99 WriteSTHToFile(good_sth_json, second_sth); | |
| 100 | |
| 101 const base::FilePath not_hex_sth_file = | |
| 102 sths_dir.Append(FILE_PATH_LITERAL("nothex.sth")); | |
| 103 WriteSTHToFile(good_sth_json, not_hex_sth_file); | |
| 104 | |
| 105 const base::FilePath invalid_sth = | |
| 106 sths_dir.Append(FILE_PATH_LITERAL("010101.sth")); | |
| 107 WriteSTHToFile(std::string("{invalid json}"), invalid_sth); | |
| 108 | |
| 109 ASSERT_TRUE(traits_->VerifyInstallation(manifest, temp_dir_.path())); | |
| 110 | |
| 111 base::Version v("1.0"); | |
| 112 traits_->LoadSTHsFromDisk(sths_dir, v); | |
| 113 run_loop_.RunUntilIdle(); | |
| 114 | |
| 115 // Currently test does not wait long enough for the observer_ | |
| 116 // to be notified of the new STHs because the STHSetComponentInstallerTraits | |
| 117 // posts a task to the IO thread, not covered by the run_loop_. | |
| 118 // TODO(eranm): Make the STHSetComponentInstallerTraits use a provided | |
| 119 // RunLoop or make the test wait correctly for tasks on the IO thread. | |
| 120 //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.
| |
| 121 } | |
| 122 | |
| 123 } // namespace component_updater | |
| OLD | NEW |