1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/time/time.h"
#include "chrome/browser/media/webrtc_log_uploader.h"
#include "testing/gtest/include/gtest/gtest.h"
const char kTestTime[] = "time";
const char kTestReportId[] = "report-id";
const char kTestLocalId[] = "local-id";
class WebRtcLogUploaderTest : public testing::Test {
public:
WebRtcLogUploaderTest() {}
bool VerifyNumberOfLines(int expected_lines) {
std::vector<std::string> lines = GetLinesFromListFile();
EXPECT_EQ(expected_lines, static_cast<int>(lines.size()));
return expected_lines == static_cast<int>(lines.size());
}
bool VerifyLastLineHasAllInfo() {
std::string last_line = GetLastLineFromListFile();
if (last_line.empty())
return false;
std::vector<std::string> line_parts;
base::SplitString(last_line, ',', &line_parts);
EXPECT_EQ(3u, line_parts.size());
if (3u != line_parts.size())
return false;
// The time (line_parts[0]) is the time when the info was written to the
// file which we don't know, so just verify that it's not empty.
EXPECT_FALSE(line_parts[0].empty());
EXPECT_STREQ(kTestReportId, line_parts[1].c_str());
EXPECT_STREQ(kTestLocalId, line_parts[2].c_str());
return true;
}
bool VerifyLastLineHasLocalIdOnly() {
std::string last_line = GetLastLineFromListFile();
if (last_line.empty())
return false;
std::vector<std::string> line_parts;
base::SplitString(last_line, ',', &line_parts);
EXPECT_EQ(3u, line_parts.size());
if (3u != line_parts.size())
return false;
EXPECT_TRUE(line_parts[0].empty());
EXPECT_TRUE(line_parts[1].empty());
EXPECT_STREQ(kTestLocalId, line_parts[2].c_str());
return true;
}
bool VerifyLastLineHasUploadTimeAndIdOnly() {
std::string last_line = GetLastLineFromListFile();
if (last_line.empty())
return false;
std::vector<std::string> line_parts;
base::SplitString(last_line, ',', &line_parts);
EXPECT_EQ(3u, line_parts.size());
if (3u != line_parts.size())
return false;
EXPECT_FALSE(line_parts[0].empty());
EXPECT_STREQ(kTestReportId, line_parts[1].c_str());
EXPECT_TRUE(line_parts[2].empty());
return true;
}
bool AddLinesToTestFile(int number_of_lines) {
base::File test_list_file(test_list_path_,
base::File::FLAG_OPEN | base::File::FLAG_APPEND);
EXPECT_TRUE(test_list_file.IsValid());
if (!test_list_file.IsValid())
return false;
for (int i = 0; i < number_of_lines; ++i) {
EXPECT_EQ(static_cast<int>(sizeof(kTestTime)) - 1,
test_list_file.WriteAtCurrentPos(kTestTime,
sizeof(kTestTime) - 1));
EXPECT_EQ(1, test_list_file.WriteAtCurrentPos(",", 1));
EXPECT_EQ(static_cast<int>(sizeof(kTestReportId)) - 1,
test_list_file.WriteAtCurrentPos(kTestReportId,
sizeof(kTestReportId) - 1));
EXPECT_EQ(1, test_list_file.WriteAtCurrentPos(",", 1));
EXPECT_EQ(static_cast<int>(sizeof(kTestLocalId)) - 1,
test_list_file.WriteAtCurrentPos(kTestLocalId,
sizeof(kTestLocalId) - 1));
EXPECT_EQ(1, test_list_file.WriteAtCurrentPos("\n", 1));
}
return true;
}
std::vector<std::string> GetLinesFromListFile() {
std::string contents;
int read = base::ReadFileToString(test_list_path_, &contents);
EXPECT_GT(read, 0);
if (read == 0)
return std::vector<std::string>();
// Since every line should end with '\n', the last line should be empty. So
// we expect at least two lines including the final empty. Remove the empty
// line before returning.
std::vector<std::string> lines;
base::SplitString(contents, '\n', &lines);
EXPECT_GT(lines.size(), 1u);
if (lines.size() < 2)
return std::vector<std::string>();
EXPECT_TRUE(lines[lines.size() - 1].empty());
if (!lines[lines.size() - 1].empty())
return std::vector<std::string>();
lines.pop_back();
return lines;
}
std::string GetLastLineFromListFile() {
std::vector<std::string> lines = GetLinesFromListFile();
EXPECT_GT(lines.size(), 0u);
if (lines.empty())
return std::string();
return lines[lines.size() - 1];
}
void VerifyRtpDumpInMultipart(const std::string& post_data,
const std::string& dump_name,
const std::string& dump_content) {
std::vector<std::string> lines;
base::SplitStringUsingSubstr(post_data, "\r\n", &lines);
std::string name_line = "Content-Disposition: form-data; name=\"";
name_line.append(dump_name);
name_line.append("\"");
name_line.append("; filename=\"");
name_line.append(dump_name);
name_line.append(".gz\"");
size_t i = 0;
for (; i < lines.size(); ++i) {
if (lines[i] == name_line)
break;
}
// The RTP dump takes 4 lines: content-disposition, content-type, empty
// line, dump content.
EXPECT_LT(i, lines.size() - 3);
EXPECT_EQ("Content-Type: application/gzip", lines[i + 1]);
EXPECT_EQ("", lines[i + 2]);
EXPECT_EQ(dump_content, lines[i + 3]);
}
base::FilePath test_list_path_;
};
TEST_F(WebRtcLogUploaderTest, AddLocallyStoredLogInfoToUploadListFile) {
// Get a temporary filename. We don't want the file to exist to begin with
// since that's the normal use case, hence the delete.
ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_));
EXPECT_TRUE(base::DeleteFile(test_list_path_, false));
scoped_ptr<WebRtcLogUploader> webrtc_log_uploader(new WebRtcLogUploader());
webrtc_log_uploader->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
kTestLocalId);
webrtc_log_uploader->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
kTestLocalId);
ASSERT_TRUE(VerifyNumberOfLines(2));
ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
const int expected_line_limit = 50;
ASSERT_TRUE(AddLinesToTestFile(expected_line_limit - 2));
ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
ASSERT_TRUE(VerifyLastLineHasAllInfo());
webrtc_log_uploader->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
kTestLocalId);
ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
ASSERT_TRUE(AddLinesToTestFile(10));
ASSERT_TRUE(VerifyNumberOfLines(60));
ASSERT_TRUE(VerifyLastLineHasAllInfo());
webrtc_log_uploader->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
kTestLocalId);
ASSERT_TRUE(VerifyNumberOfLines(expected_line_limit));
ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
webrtc_log_uploader->StartShutdown();
}
TEST_F(WebRtcLogUploaderTest, AddUploadedLogInfoToUploadListFile) {
// Get a temporary filename. We don't want the file to exist to begin with
// since that's the normal use case, hence the delete.
ASSERT_TRUE(base::CreateTemporaryFile(&test_list_path_));
EXPECT_TRUE(base::DeleteFile(test_list_path_, false));
scoped_ptr<WebRtcLogUploader> webrtc_log_uploader(new WebRtcLogUploader());
webrtc_log_uploader->AddLocallyStoredLogInfoToUploadListFile(test_list_path_,
kTestLocalId);
ASSERT_TRUE(VerifyNumberOfLines(1));
ASSERT_TRUE(VerifyLastLineHasLocalIdOnly());
webrtc_log_uploader->AddUploadedLogInfoToUploadListFile(
test_list_path_, kTestLocalId, kTestReportId);
ASSERT_TRUE(VerifyNumberOfLines(1));
ASSERT_TRUE(VerifyLastLineHasAllInfo());
// Use a local ID that should not be found in the list.
webrtc_log_uploader->AddUploadedLogInfoToUploadListFile(
test_list_path_, "dummy id", kTestReportId);
ASSERT_TRUE(VerifyNumberOfLines(2));
ASSERT_TRUE(VerifyLastLineHasUploadTimeAndIdOnly());
webrtc_log_uploader->StartShutdown();
}
TEST_F(WebRtcLogUploaderTest, AddRtpDumpsToPostedData) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
scoped_ptr<WebRtcLogUploader> webrtc_log_uploader(new WebRtcLogUploader());
std::string post_data;
webrtc_log_uploader->OverrideUploadWithBufferForTesting(&post_data);
// Create the fake dump files.
const base::FilePath incoming_dump = temp_dir.path().AppendASCII("recv");
const base::FilePath outgoing_dump = temp_dir.path().AppendASCII("send");
const std::string incoming_dump_content = "dummy incoming";
const std::string outgoing_dump_content = "dummy outgoing";
base::WriteFile(incoming_dump,
&incoming_dump_content[0],
incoming_dump_content.size());
base::WriteFile(outgoing_dump,
&outgoing_dump_content[0],
outgoing_dump_content.size());
WebRtcLogUploadDoneData upload_done_data;
upload_done_data.log_path = temp_dir.path().AppendASCII("log");
upload_done_data.incoming_rtp_dump = incoming_dump;
upload_done_data.outgoing_rtp_dump = outgoing_dump;
const size_t log_length = 100;
scoped_ptr<unsigned char[]> log(new unsigned char[log_length]);
memset(log.get(), 0, log_length);
webrtc_log_uploader->LoggingStoppedDoUpload(
log.Pass(),
log_length,
std::map<std::string, std::string>(),
upload_done_data);
VerifyRtpDumpInMultipart(post_data, "rtpdump_recv", incoming_dump_content);
VerifyRtpDumpInMultipart(post_data, "rtpdump_send", outgoing_dump_content);
webrtc_log_uploader->StartShutdown();
}
|