File: webrtc_logging_private_apitest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (329 lines) | stat: -rw-r--r-- 13,079 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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 "base/json/json_writer.h"
#include "base/strings/string_split.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_private_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/media/webrtc_log_uploader.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/test_util.h"

using extensions::Extension;

namespace utils = extension_function_test_utils;

namespace {

static const char kTestLoggingSessionId[] = "0123456789abcdef";
static const char kTestLoggingUrl[] = "dummy url string";

class WebrtcLoggingPrivateApiTest : public ExtensionApiTest {
};

}  // namespace

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopDiscard) {
  scoped_refptr<Extension> empty_extension(
      extensions::test_util::CreateEmptyExtension());

  // Tell the uploader to save the multipart to a buffer instead of uploading.
  std::string multipart;
  g_browser_process->webrtc_log_uploader()->
      OverrideUploadWithBufferForTesting(&multipart);

  // Start

  scoped_refptr<extensions::WebrtcLoggingPrivateStartFunction>
      start_function(new extensions::WebrtcLoggingPrivateStartFunction());
  start_function->set_extension(empty_extension.get());
  start_function->set_has_callback(true);

  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  base::ListValue parameters;
  base::DictionaryValue* request_info = new base::DictionaryValue();
  request_info->SetInteger("tabId",
                           extensions::ExtensionTabUtil::GetTabId(contents));
  parameters.Append(request_info);
  parameters.AppendString(contents->GetURL().GetOrigin().spec());
  std::string parameter_string;
  base::JSONWriter::Write(&parameters, &parameter_string);

  // TODO(grunell): MaybeRunFunction is suitable for those calls not returning
  // anything.
  scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
      start_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Stop

  scoped_refptr<extensions::WebrtcLoggingPrivateStopFunction>
      stop_function(new extensions::WebrtcLoggingPrivateStopFunction());
  stop_function->set_extension(empty_extension.get());
  stop_function->set_has_callback(true);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      stop_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Discard

  scoped_refptr<extensions::WebrtcLoggingPrivateDiscardFunction>
      discard_function(new extensions::WebrtcLoggingPrivateDiscardFunction());
  discard_function->set_extension(empty_extension.get());
  discard_function->set_has_callback(true);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      discard_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  ASSERT_TRUE(multipart.empty());

  g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting(
      NULL);
}

// Tests WebRTC diagnostic logging. Sets up the browser to save the multipart
// contents to a buffer instead of uploading it, then verifies it after a calls.
// Example of multipart contents:
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="prod"
//
// Chrome_Linux
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="ver"
//
// 30.0.1554.0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="guid"
//
// 0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="type"
//
// webrtc_log
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="app_session_id"
//
// 0123456789abcdef
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="url"
//
// http://127.0.0.1:43213/webrtc/webrtc_jsep01_test.html
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="webrtc_log"; filename="webrtc_log.gz"
// Content-Type: application/gzip
//
// <compressed data (zip)>
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**------
//
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopUpload) {
  scoped_refptr<Extension> empty_extension(
      extensions::test_util::CreateEmptyExtension());

  // Tell the uploader to save the multipart to a buffer instead of uploading.
  std::string multipart;
  g_browser_process->webrtc_log_uploader()->
      OverrideUploadWithBufferForTesting(&multipart);

  // SetMetaData.

  scoped_refptr<extensions::WebrtcLoggingPrivateSetMetaDataFunction>
      set_meta_data_function(
          new extensions::WebrtcLoggingPrivateSetMetaDataFunction());
  set_meta_data_function->set_extension(empty_extension.get());
  set_meta_data_function->set_has_callback(true);

  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  base::ListValue parameters;
  base::DictionaryValue* request_info = new base::DictionaryValue();
  request_info->SetInteger("tabId",
                           extensions::ExtensionTabUtil::GetTabId(contents));
  parameters.Append(request_info);
  parameters.AppendString(contents->GetURL().GetOrigin().spec());
  base::DictionaryValue* meta_data_entry = new base::DictionaryValue();
  meta_data_entry->SetString("key", "app_session_id");
  meta_data_entry->SetString("value", kTestLoggingSessionId);
  base::ListValue* meta_data = new base::ListValue();
  meta_data->Append(meta_data_entry);
  meta_data_entry = new base::DictionaryValue();
  meta_data_entry->SetString("key", "url");
  meta_data_entry->SetString("value", kTestLoggingUrl);
  meta_data->Append(meta_data_entry);
  parameters.Append(meta_data);

  std::string parameter_string;
  base::JSONWriter::Write(&parameters, &parameter_string);

  // TODO(grunell): MaybeRunFunction is suitable for those calls not returning
  // anything.
  scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
      set_meta_data_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Start.

  scoped_refptr<extensions::WebrtcLoggingPrivateStartFunction>
      start_function(new extensions::WebrtcLoggingPrivateStartFunction());
  start_function->set_extension(empty_extension.get());
  start_function->set_has_callback(true);

  parameters.Clear();
  request_info = new base::DictionaryValue();
  request_info->SetInteger("tabId",
                           extensions::ExtensionTabUtil::GetTabId(contents));
  parameters.Append(request_info);
  parameters.AppendString(contents->GetURL().GetOrigin().spec());
  base::JSONWriter::Write(&parameters, &parameter_string);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      start_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Stop.

  scoped_refptr<extensions::WebrtcLoggingPrivateStopFunction>
      stop_function(new extensions::WebrtcLoggingPrivateStopFunction());
  stop_function->set_extension(empty_extension.get());
  stop_function->set_has_callback(true);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      stop_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Upload.

  scoped_refptr<extensions::WebrtcLoggingPrivateUploadFunction>
      upload_function(new extensions::WebrtcLoggingPrivateUploadFunction());
  upload_function->set_extension(empty_extension.get());
  upload_function->set_has_callback(true);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      upload_function.get(), parameter_string, browser()));
  ASSERT_TRUE(result.get());

  ASSERT_FALSE(multipart.empty());

  // Check multipart data.

  const char boundary[] = "------**--yradnuoBgoLtrapitluMklaTelgooG--**----";

  // Remove the compressed data, it may contain "\r\n". Just verify that its
  // size is > 0.
  const char zip_content_type[] = "Content-Type: application/gzip";
  size_t zip_pos = multipart.find(&zip_content_type[0]);
  ASSERT_NE(std::string::npos, zip_pos);
  // Move pos to where the zip begins. - 1 to remove '\0', + 4 for two "\r\n".
  zip_pos += sizeof(zip_content_type) + 3;
  size_t zip_length = multipart.find(boundary, zip_pos);
  ASSERT_NE(std::string::npos, zip_length);
  // Calculate length, adjust for a "\r\n".
  zip_length -= zip_pos + 2;
  ASSERT_GT(zip_length, 0u);
  multipart.erase(zip_pos, zip_length);

  // Check the multipart contents.
  std::vector<std::string> multipart_lines;
  base::SplitStringUsingSubstr(multipart, "\r\n", &multipart_lines);
  ASSERT_EQ(31, static_cast<int>(multipart_lines.size()));

  EXPECT_STREQ(&boundary[0], multipart_lines[0].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"prod\"",
               multipart_lines[1].c_str());
  EXPECT_TRUE(multipart_lines[2].empty());
  EXPECT_NE(std::string::npos, multipart_lines[3].find("Chrome"));

  EXPECT_STREQ(&boundary[0], multipart_lines[4].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"ver\"",
               multipart_lines[5].c_str());
  EXPECT_TRUE(multipart_lines[6].empty());
  // Just check that the version contains a dot.
  EXPECT_NE(std::string::npos, multipart_lines[7].find('.'));

  EXPECT_STREQ(&boundary[0], multipart_lines[8].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"guid\"",
               multipart_lines[9].c_str());
  EXPECT_TRUE(multipart_lines[10].empty());
  EXPECT_STREQ("0", multipart_lines[11].c_str());

  EXPECT_STREQ(&boundary[0], multipart_lines[12].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"type\"",
               multipart_lines[13].c_str());
  EXPECT_TRUE(multipart_lines[14].empty());
  EXPECT_STREQ("webrtc_log", multipart_lines[15].c_str());

  EXPECT_STREQ(&boundary[0], multipart_lines[16].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"app_session_id\"",
               multipart_lines[17].c_str());
  EXPECT_TRUE(multipart_lines[18].empty());
  EXPECT_STREQ(kTestLoggingSessionId, multipart_lines[19].c_str());

  EXPECT_STREQ(&boundary[0], multipart_lines[20].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"url\"",
               multipart_lines[21].c_str());
  EXPECT_TRUE(multipart_lines[22].empty());
  EXPECT_STREQ(kTestLoggingUrl, multipart_lines[23].c_str());

  EXPECT_STREQ(&boundary[0], multipart_lines[24].c_str());
  EXPECT_STREQ("Content-Disposition: form-data; name=\"webrtc_log\";"
               " filename=\"webrtc_log.gz\"",
               multipart_lines[25].c_str());
  EXPECT_STREQ("Content-Type: application/gzip",
               multipart_lines[26].c_str());
  EXPECT_TRUE(multipart_lines[27].empty());
  EXPECT_TRUE(multipart_lines[28].empty());  // The removed zip part.
  std::string final_delimiter = boundary;
  final_delimiter += "--";
  EXPECT_STREQ(final_delimiter.c_str(), multipart_lines[29].c_str());
  EXPECT_TRUE(multipart_lines[30].empty());

  g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting(
      NULL);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopRtpDump) {
  scoped_refptr<Extension> empty_extension(
      extensions::test_util::CreateEmptyExtension());

  // Start RTP dump.
  scoped_refptr<extensions::WebrtcLoggingPrivateStartRtpDumpFunction>
      start_function(
          new extensions::WebrtcLoggingPrivateStartRtpDumpFunction());
  start_function->set_extension(empty_extension.get());
  start_function->set_has_callback(true);

  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  base::ListValue parameters;
  base::DictionaryValue* request_info = new base::DictionaryValue();
  request_info->SetInteger("tabId",
                           extensions::ExtensionTabUtil::GetTabId(contents));
  parameters.Append(request_info);
  parameters.AppendString(contents->GetURL().GetOrigin().spec());
  parameters.AppendBoolean(true);
  parameters.AppendBoolean(true);
  std::string parameter_string;
  base::JSONWriter::Write(&parameters, &parameter_string);

  scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
      start_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());

  // Stop RTP dump.
  scoped_refptr<extensions::WebrtcLoggingPrivateStopRtpDumpFunction>
      stop_function(new extensions::WebrtcLoggingPrivateStopRtpDumpFunction());
  stop_function->set_extension(empty_extension.get());
  stop_function->set_has_callback(true);

  result.reset(utils::RunFunctionAndReturnSingleResult(
      stop_function.get(), parameter_string, browser()));
  ASSERT_FALSE(result.get());
}