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

Side by Side 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: Address comments by sorin & waffles Created 4 years, 8 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 unified diff | Download patch
OLDNEW
(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 // ScopedTempDir automatically does a recursive delete on the entire
46 // directory in its destructor, so no cleanup is required in TearDown.
47 // Note that all files created by this test case are created within the
48 // directory that is created here, so even though they are not explicitly
49 // created *as temp files*, they will still get cleaned up automagically.
Ryan Sleevi 2016/04/07 18:00:13 This seems to be explaining ScopedTempDir, but tha
Eran Messeri 2016/04/07 20:52:54 Done - removed.
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51
52 scoped_ptr<StoringSTHObserver> observer(new StoringSTHObserver());
53 observer_ = observer.get();
54 traits_.reset(new STHSetComponentInstallerTraits(std::move(observer)));
55 }
56
57 void WriteSTHToFile(const std::string& sth_json,
58 const base::FilePath& filename) {
59 ASSERT_EQ(static_cast<int32_t>(sth_json.length()),
60 base::WriteFile(filename, sth_json.data(), sth_json.length()));
61 }
62
63 base::FilePath GetSTHsDir() {
64 return temp_dir_.path()
65 .Append(FILE_PATH_LITERAL("_platform_specific"))
66 .Append(FILE_PATH_LITERAL("all"))
67 .Append(FILE_PATH_LITERAL("sths"));
68 }
69
70 void CreateSTHsDir(const base::DictionaryValue& manifest,
71 const base::FilePath& sths_dir) {
72 ASSERT_FALSE(traits_->VerifyInstallation(manifest, temp_dir_.path()));
73 ASSERT_TRUE(base::CreateDirectory(sths_dir));
74 }
75
76 void LoadSTHs(const base::DictionaryValue& manifest,
77 const base::FilePath& sths_dir) {
78 ASSERT_TRUE(traits_->VerifyInstallation(manifest, temp_dir_.path()));
79
80 const base::Version v("1.0");
81 traits_->LoadSTHsFromDisk(sths_dir, v);
82 // Drain the RunLoop created by the TestBrowserThreadBundle
83 base::RunLoop().RunUntilIdle();
84 }
85
86 protected:
87 content::TestBrowserThreadBundle thread_bundle_;
88
89 base::ScopedTempDir temp_dir_;
90 scoped_ptr<STHSetComponentInstallerTraits> traits_;
91 StoringSTHObserver* observer_;
92 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_;
93
94 private:
95 DISALLOW_COPY_AND_ASSIGN(STHSetComponentInstallerTest);
96 };
97
98 // Parses valid STH JSON in a file with valid hex encoding of log id.
99 TEST_F(STHSetComponentInstallerTest, CanLoadAllSTHs) {
100 const base::DictionaryValue manifest;
101 const base::FilePath sths_dir(GetSTHsDir());
102 CreateSTHsDir(manifest, sths_dir);
103
104 const std::string good_sth_json = net::ct::GetSampleSTHAsJson();
105 const base::FilePath first_sth_file =
106 sths_dir.Append(FILE_PATH_LITERAL("616263.sth"));
107 WriteSTHToFile(good_sth_json, first_sth_file);
108
109 const base::FilePath second_sth_file =
110 sths_dir.Append(FILE_PATH_LITERAL("610064.sth"));
111 WriteSTHToFile(good_sth_json, second_sth_file);
112
113 LoadSTHs(manifest, sths_dir);
114
115 EXPECT_EQ(2u, observer_->sths.size());
116
117 const std::string first_log_id("abc");
118 ASSERT_TRUE(observer_->sths.find(first_log_id) != observer_->sths.end());
119 const net::ct::SignedTreeHead& first_sth(observer_->sths[first_log_id]);
120 EXPECT_EQ(21u, first_sth.tree_size);
121
122 const std::string second_log_id("a\00d", 3);
123 ASSERT_TRUE(observer_->sths.find(second_log_id) != observer_->sths.end());
124 }
125
126 // Does not notify of invalid STH JSON.
127 TEST_F(STHSetComponentInstallerTest, DoesNotLoadInvalidJSON) {
128 const base::DictionaryValue manifest;
129 const base::FilePath sths_dir(GetSTHsDir());
130 CreateSTHsDir(manifest, sths_dir);
131
132 const base::FilePath invalid_sth =
133 sths_dir.Append(FILE_PATH_LITERAL("010101.sth"));
134 WriteSTHToFile(std::string("{invalid json}"), invalid_sth);
135
136 LoadSTHs(manifest, sths_dir);
137 EXPECT_EQ(0u, observer_->sths.size());
138 }
139
140 // Does not notify of valid JSON but in a file not hex-encoded log id.
141 TEST_F(STHSetComponentInstallerTest,
142 DoesNotLoadValidJSONFromFileNotHexEncoded) {
143 const base::DictionaryValue manifest;
144 const base::FilePath sths_dir(GetSTHsDir());
145 CreateSTHsDir(manifest, sths_dir);
146
147 const base::FilePath not_hex_sth_file =
148 sths_dir.Append(FILE_PATH_LITERAL("nothex.sth"));
149 WriteSTHToFile(net::ct::GetSampleSTHAsJson(), not_hex_sth_file);
150
151 LoadSTHs(manifest, sths_dir);
152 EXPECT_EQ(0u, observer_->sths.size());
153 }
154
155 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698