1 // Copyright 2013 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.
7 #include "base/files/file.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/media/webrtc_log_uploader.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 const char kTestTime
[] = "time";
19 const char kTestReportId
[] = "report-id";
20 const char kTestLocalId
[] = "local-id";
22 class WebRtcLogUploaderTest
: public testing::Test
{
24 WebRtcLogUploaderTest() {}
26 bool VerifyNumberOfLines(int expected_lines
) {
27 std::vector
<std::string
> lines
= GetLinesFromListFile();
28 EXPECT_EQ(expected_lines
, static_cast<int>(lines
.size()));
29 return expected_lines
== static_cast<int>(lines
.size());
32 bool VerifyLastLineHasAllInfo() {
33 std::string last_line
= GetLastLineFromListFile();
34 if (last_line
.empty())
36 std::vector
<std::string
> line_parts
;
37 base::SplitString(last_line
, ',', &line_parts
);
38 EXPECT_EQ(3u, line_parts
.size());
39 if (3u != line_parts
.size())
41 // The time (line_parts[0]) is the time when the info was written to the
42 // file which we don't know, so just verify that it's not empty.
43 EXPECT_FALSE(line_parts
[0].empty());
44 EXPECT_STREQ(kTestReportId
, line_parts
[1].c_str());
45 EXPECT_STREQ(kTestLocalId
, line_parts
[2].c_str());
49 bool VerifyLastLineHasLocalIdOnly() {
50 std::string last_line
= GetLastLineFromListFile();
51 if (last_line
.empty())
53 std::vector
<std::string
> line_parts
;
54 base::SplitString(last_line
, ',', &line_parts
);
55 EXPECT_EQ(3u, line_parts
.size());
56 if (3u != line_parts
.size())
58 EXPECT_TRUE(line_parts
[0].empty());
59 EXPECT_TRUE(line_parts
[1].empty());
60 EXPECT_STREQ(kTestLocalId
, line_parts
[2].c_str());
64 bool VerifyLastLineHasUploadTimeAndIdOnly() {
65 std::string last_line
= GetLastLineFromListFile();
66 if (last_line
.empty())
68 std::vector
<std::string
> line_parts
;
69 base::SplitString(last_line
, ',', &line_parts
);
70 EXPECT_EQ(3u, line_parts
.size());
71 if (3u != line_parts
.size())
73 EXPECT_FALSE(line_parts
[0].empty());
74 EXPECT_STREQ(kTestReportId
, line_parts
[1].c_str());
75 EXPECT_TRUE(line_parts
[2].empty());
79 bool AddLinesToTestFile(int number_of_lines
) {
80 base::File
test_list_file(test_list_path_
,
81 base::File::FLAG_OPEN
| base::File::FLAG_APPEND
);
82 EXPECT_TRUE(test_list_file
.IsValid());
83 if (!test_list_file
.IsValid())
86 for (int i
= 0; i
< number_of_lines
; ++i
) {
87 EXPECT_EQ(static_cast<int>(sizeof(kTestTime
)) - 1,
88 test_list_file
.WriteAtCurrentPos(kTestTime
,
89 sizeof(kTestTime
) - 1));
90 EXPECT_EQ(1, test_list_file
.WriteAtCurrentPos(",", 1));
91 EXPECT_EQ(static_cast<int>(sizeof(kTestReportId
)) - 1,
92 test_list_file
.WriteAtCurrentPos(kTestReportId
,
93 sizeof(kTestReportId
) - 1));
94 EXPECT_EQ(1, test_list_file
.WriteAtCurrentPos(",", 1));
95 EXPECT_EQ(static_cast<int>(sizeof(kTestLocalId
)) - 1,
96 test_list_file
.WriteAtCurrentPos(kTestLocalId
,
97 sizeof(kTestLocalId
) - 1));
98 EXPECT_EQ(1, test_list_file
.WriteAtCurrentPos("\n", 1));
103 std::vector
<std::string
> GetLinesFromListFile() {
104 std::string contents
;
105 int read
= base::ReadFileToString(test_list_path_
, &contents
);
108 return std::vector
<std::string
>();
109 // Since every line should end with '\n', the last line should be empty. So
110 // we expect at least two lines including the final empty. Remove the empty
111 // line before returning.
112 std::vector
<std::string
> lines
;
113 base::SplitString(contents
, '\n', &lines
);
114 EXPECT_GT(lines
.size(), 1u);
115 if (lines
.size() < 2)
116 return std::vector
<std::string
>();
117 EXPECT_TRUE(lines
[lines
.size() - 1].empty());
118 if (!lines
[lines
.size() - 1].empty())
119 return std::vector
<std::string
>();
124 std::string
GetLastLineFromListFile() {
125 std::vector
<std::string
> lines
= GetLinesFromListFile();
126 EXPECT_GT(lines
.size(), 0u);
128 return std::string();
129 return lines
[lines
.size() - 1];
132 void VerifyRtpDumpInMultipart(const std::string
& post_data
,
133 const std::string
& dump_name
,
134 const std::string
& dump_content
) {
135 std::vector
<std::string
> lines
;
136 base::SplitStringUsingSubstr(post_data
, "\r\n", &lines
);
138 std::string name_line
= "Content-Disposition: form-data; name=\"";
139 name_line
.append(dump_name
);
140 name_line
.append("\"");
141 name_line
.append("; filename=\"");
142 name_line
.append(dump_name
);
143 name_line
.append(".gz\"");
146 for (; i
< lines
.size(); ++i
) {
147 if (lines
[i
] == name_line
)
151 // The RTP dump takes 4 lines: content-disposition, content-type, empty
152 // line, dump content.
153 EXPECT_LT(i
, lines
.size() - 3);
155 EXPECT_EQ("Content-Type: application/gzip", lines
[i
+ 1]);
156 EXPECT_EQ("", lines
[i
+ 2]);
157 EXPECT_EQ(dump_content
, lines
[i
+ 3]);
160 base::FilePath test_list_path_
;
163 TEST_F(WebRtcLogUploaderTest
, AddLocallyStoredLogInfoToUploadListFile
) {
164 // Get a temporary filename. We don't want the file to exist to begin with
165 // since that's the normal use case, hence the delete.
166 ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_
));
167 EXPECT_TRUE(base::DeleteFile(test_list_path_
, false));
168 scoped_ptr
<WebRtcLogUploader
> webrtc_log_uploader(new WebRtcLogUploader());
170 webrtc_log_uploader
->AddLocallyStoredLogInfoToUploadListFile(test_list_path_
,
172 webrtc_log_uploader
->AddLocallyStoredLogInfoToUploadListFile(test_list_path_
,
174 ASSERT_TRUE(VerifyNumberOfLines(2));
175 ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
177 const int expected_line_limit
= 50;
178 ASSERT_TRUE(AddLinesToTestFile(expected_line_limit
- 2));
179 ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit
));
180 ASSERT_TRUE(VerifyLastLineHasAllInfo());
182 webrtc_log_uploader
->AddLocallyStoredLogInfoToUploadListFile(test_list_path_
,
184 ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit
));
185 ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
187 ASSERT_TRUE(AddLinesToTestFile(10));
188 ASSERT_TRUE(VerifyNumberOfLines(60));
189 ASSERT_TRUE(VerifyLastLineHasAllInfo());
191 webrtc_log_uploader
->AddLocallyStoredLogInfoToUploadListFile(test_list_path_
,
193 ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit
));
194 ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
196 webrtc_log_uploader
->StartShutdown();
199 TEST_F(WebRtcLogUploaderTest
, AddUploadedLogInfoToUploadListFile
) {
200 // Get a temporary filename. We don't want the file to exist to begin with
201 // since that's the normal use case, hence the delete.
202 ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_
));
203 EXPECT_TRUE(base::DeleteFile(test_list_path_
, false));
204 scoped_ptr
<WebRtcLogUploader
> webrtc_log_uploader(new WebRtcLogUploader());
206 webrtc_log_uploader
->AddLocallyStoredLogInfoToUploadListFile(test_list_path_
,
208 ASSERT_TRUE(VerifyNumberOfLines(1));
209 ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
211 webrtc_log_uploader
->AddUploadedLogInfoToUploadListFile(
212 test_list_path_
, kTestLocalId
, kTestReportId
);
213 ASSERT_TRUE(VerifyNumberOfLines(1));
214 ASSERT_TRUE(VerifyLastLineHasAllInfo());
216 // Use a local ID that should not be found in the list.
217 webrtc_log_uploader
->AddUploadedLogInfoToUploadListFile(
218 test_list_path_
, "dummy id", kTestReportId
);
219 ASSERT_TRUE(VerifyNumberOfLines(2));
220 ASSERT_TRUE(VerifyLastLineHasUploadTimeAndIdOnly());
222 webrtc_log_uploader
->StartShutdown();
225 TEST_F(WebRtcLogUploaderTest
, AddRtpDumpsToPostedData
) {
226 base::ScopedTempDir temp_dir
;
227 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
229 scoped_ptr
<WebRtcLogUploader
> webrtc_log_uploader(new WebRtcLogUploader());
231 std::string post_data
;
232 webrtc_log_uploader
->OverrideUploadWithBufferForTesting(&post_data
);
234 // Create the fake dump files.
235 const base::FilePath incoming_dump
= temp_dir
.path().AppendASCII("recv");
236 const base::FilePath outgoing_dump
= temp_dir
.path().AppendASCII("send");
237 const std::string incoming_dump_content
= "dummy incoming";
238 const std::string outgoing_dump_content
= "dummy outgoing";
240 base::WriteFile(incoming_dump
,
241 &incoming_dump_content
[0],
242 incoming_dump_content
.size());
243 base::WriteFile(outgoing_dump
,
244 &outgoing_dump_content
[0],
245 outgoing_dump_content
.size());
247 WebRtcLogUploadDoneData upload_done_data
;
248 upload_done_data
.log_path
= temp_dir
.path().AppendASCII("log");
250 upload_done_data
.incoming_rtp_dump
= incoming_dump
;
251 upload_done_data
.outgoing_rtp_dump
= outgoing_dump
;
253 scoped_ptr
<WebRtcLogBuffer
> log(new WebRtcLogBuffer());
255 webrtc_log_uploader
->LoggingStoppedDoUpload(
256 log
.Pass(), make_scoped_ptr(new MetaDataMap()), upload_done_data
);
258 VerifyRtpDumpInMultipart(post_data
, "rtpdump_recv", incoming_dump_content
);
259 VerifyRtpDumpInMultipart(post_data
, "rtpdump_send", outgoing_dump_content
);
261 webrtc_log_uploader
->StartShutdown();