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

Side by Side Diff: chrome/browser/chromeos/system_logs/single_log_source_unittest.cc

Issue 2844163005: Add SingleLogSource to system_logs sources (Closed)
Patch Set: Creating new file must be accompanied by FLAG_WRITE Created 3 years, 7 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 2017 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/chromeos/system_logs/single_log_source.h"
6
7 #include <string>
8
9 #include "base/bind.h"
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/run_loop.h"
15 #include "base/test/scoped_task_scheduler.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace system_logs {
19
20 class SingleLogSourceTest : public ::testing::Test {
21 public:
22 SingleLogSourceTest()
23 : source_(SingleLogSource::SupportedSource::kMessages),
24 num_callback_calls_(0) {
25 CHECK(dir_.CreateUniqueTempDir());
26 log_file_path_ = dir_.GetPath().Append("log_file");
27
28 // Create the dummy log file for writing.
29 base::File new_file;
30 new_file.Initialize(log_file_path_, base::File::FLAG_CREATE_ALWAYS |
31 base::File::FLAG_WRITE);
32 new_file.Close();
33 CHECK(base::PathExists(log_file_path_));
34
35 // Open the dummy log file for reading from within the log source.
36 source_.file_.Initialize(log_file_path_,
37 base::File::FLAG_OPEN | base::File::FLAG_READ);
38 CHECK(source_.file_.IsValid());
39 }
40
41 ~SingleLogSourceTest() override {}
42
43 // Writes a string to |log_file_path_|.
44 bool WriteFile(const std::string& input) {
45 return base::WriteFile(log_file_path_, input.data(), input.size());
46 }
47 // Appends a string to |log_file_path_|.
48 bool AppendToFile(const std::string& input) {
49 return base::AppendToFile(log_file_path_, input.data(), input.size());
50 }
51
52 // Calls source_.Fetch() to start a logs fetch operation. Passes in
53 // OnFileRead() as a callback. Runs until Fetch() has completed.
54 void FetchFromSource() {
55 source_.Fetch(
56 base::Bind(&SingleLogSourceTest::OnFileRead, base::Unretained(this)));
57 base::RunLoop().RunUntilIdle();
58 }
59
60 // Callback for fetching logs from |source_|. Overwrites the previous stored
61 // value of |latest_response_|.
62 void OnFileRead(SystemLogsResponse* response) {
63 ++num_callback_calls_;
64 if (response->empty())
65 return;
66
67 // Since |source_| represents a single log source, it should only return a
68 // single string result.
69 EXPECT_EQ(1U, response->size());
70 latest_response_ = std::move(response->begin()->second);
71 }
72
73 int num_callback_calls() const { return num_callback_calls_; }
74
75 const std::string& latest_response() const { return latest_response_; }
76
77 const base::FilePath& log_file_path() const { return log_file_path_; }
78
79 private:
80 // Unit under test.
81 SingleLogSource source_;
82
83 // Counts the number of times that |source_| has invoked the callback.
84 int num_callback_calls_;
85
86 // Stores the string response returned from |source_| the last time it invoked
87 // OnFileRead.
88 std::string latest_response_;
89
90 // Temporary dir for creating a dummy log file.
91 base::ScopedTempDir dir_;
92
93 // Path to the dummy log file in |dir_|.
94 base::FilePath log_file_path_;
95
96 // Used for running scheduled tasks.
97 base::test::ScopedTaskScheduler scheduler_;
afakhry 2017/05/11 00:56:54 Can you also add a member of type content::TestBro
98
99 DISALLOW_COPY_AND_ASSIGN(SingleLogSourceTest);
100 };
101
102 TEST_F(SingleLogSourceTest, EmptyFile) {
103 FetchFromSource();
104
105 EXPECT_EQ(1, num_callback_calls());
106 EXPECT_EQ("", latest_response());
107 }
108
109 TEST_F(SingleLogSourceTest, SingleRead) {
110 EXPECT_TRUE(AppendToFile("Hello world!\n"));
111 FetchFromSource();
112
113 EXPECT_EQ(1, num_callback_calls());
114 EXPECT_EQ("Hello world!\n", latest_response());
115 }
116
117 TEST_F(SingleLogSourceTest, IncrementalReads) {
118 EXPECT_TRUE(AppendToFile("Hello world!\n"));
119 FetchFromSource();
120
121 EXPECT_EQ(1, num_callback_calls());
122 EXPECT_EQ("Hello world!\n", latest_response());
123
124 EXPECT_TRUE(AppendToFile("The quick brown fox jumps over the lazy dog\n"));
125 FetchFromSource();
126
127 EXPECT_EQ(2, num_callback_calls());
128 EXPECT_EQ("The quick brown fox jumps over the lazy dog\n", latest_response());
129
130 EXPECT_TRUE(AppendToFile("Some like it hot.\nSome like it cold\n"));
131 FetchFromSource();
132
133 EXPECT_EQ(3, num_callback_calls());
134 EXPECT_EQ("Some like it hot.\nSome like it cold\n", latest_response());
135
136 // As a sanity check, read entire contents of file separately to make sure it
137 // was written incrementally, and hence read incrementally.
138 std::string file_contents;
139 EXPECT_TRUE(base::ReadFileToString(log_file_path(), &file_contents));
140 EXPECT_EQ(
141 "Hello world!\nThe quick brown fox jumps over the lazy dog\n"
142 "Some like it hot.\nSome like it cold\n",
143 file_contents);
144 }
145
146 // The log files read by SingleLogSource are not expected to be overwritten.
147 // This test is just to ensure that the SingleLogSource class is robust enough
148 // not to break in the event of an overwrite.
149 TEST_F(SingleLogSourceTest, FileOverwrite) {
150 EXPECT_TRUE(AppendToFile("0123456789\n"));
151 FetchFromSource();
152
153 EXPECT_EQ(1, num_callback_calls());
154 EXPECT_EQ("0123456789\n", latest_response());
155
156 // Overwrite the file.
157 EXPECT_TRUE(WriteFile("abcdefg\n"));
158 FetchFromSource();
159
160 // Should re-read from the beginning.
161 EXPECT_EQ(2, num_callback_calls());
162 EXPECT_EQ("abcdefg\n", latest_response());
163
164 // Append to the file to make sure incremental read still works.
165 EXPECT_TRUE(AppendToFile("hijk\n"));
166 FetchFromSource();
167
168 EXPECT_EQ(3, num_callback_calls());
169 EXPECT_EQ("hijk\n", latest_response());
170
171 // Overwrite again, this time with a longer length than the existing file.
172 // Previous contents:
173 // abcdefg~hijk~ <-- "~" is a single-char representation of newline.
174 // New contents:
175 // lmnopqrstuvwxyz~ <-- excess text beyond end of prev contents: "yz~"
176 EXPECT_TRUE(WriteFile("lmnopqrstuvwxyz\n"));
177 FetchFromSource();
178
179 EXPECT_EQ(4, num_callback_calls());
180 EXPECT_EQ("yz\n", latest_response());
181 }
182
183 TEST_F(SingleLogSourceTest, IncompleteLines) {
184 EXPECT_TRUE(AppendToFile("0123456789"));
185 FetchFromSource();
186
187 EXPECT_EQ(1, num_callback_calls());
188 EXPECT_EQ("", latest_response());
189
190 EXPECT_TRUE(AppendToFile("abcdefg"));
191 FetchFromSource();
192
193 EXPECT_EQ(2, num_callback_calls());
194 EXPECT_EQ("", latest_response());
195
196 EXPECT_TRUE(AppendToFile("hijk\n"));
197 FetchFromSource();
198
199 EXPECT_EQ(3, num_callback_calls());
200 // All the previously written text should be read this time.
201 EXPECT_EQ("0123456789abcdefghijk\n", latest_response());
202
203 EXPECT_TRUE(AppendToFile("Hello world\n"));
204 EXPECT_TRUE(AppendToFile("Goodbye world"));
205 FetchFromSource();
206
207 // Partial whole-line reads are not supported. The last byte of the read must
208 // be a new line.
209 EXPECT_EQ(4, num_callback_calls());
210 EXPECT_EQ("", latest_response());
211
212 EXPECT_TRUE(AppendToFile("\n"));
213 FetchFromSource();
214
215 EXPECT_EQ(5, num_callback_calls());
216 EXPECT_EQ("Hello world\nGoodbye world\n", latest_response());
217 }
218
219 TEST_F(SingleLogSourceTest, Anonymize) {
220 EXPECT_TRUE(AppendToFile("My MAC address is: 11:22:33:44:55:66\n"));
221 FetchFromSource();
222
223 EXPECT_EQ(1, num_callback_calls());
224 EXPECT_EQ("My MAC address is: 11:22:33:00:00:01\n", latest_response());
225
226 // Suppose the write operation is not atomic, and the MAC address is written
227 // across two separate writes.
228 EXPECT_TRUE(AppendToFile("Your MAC address is: AB:88:C"));
229 FetchFromSource();
230
231 EXPECT_EQ(2, num_callback_calls());
232 EXPECT_EQ("", latest_response());
233
234 EXPECT_TRUE(AppendToFile("D:99:EF:77\n"));
235 FetchFromSource();
236
237 EXPECT_EQ(3, num_callback_calls());
238 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response());
239 }
240
241 } // namespace system_logs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698