summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_private_apitest.cc
blob: c878060ff8f665cd58c131f78340858062fd82b4 (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// 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 <stddef.h>

#include <memory>
#include <utility>

#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_command_line.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/webrtc_event_log_manager_common.h"
#include "chrome/browser/media/webrtc/webrtc_log_uploader.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/webrtc_event_logger.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/extension_builder.h"
#include "third_party/zlib/google/compression_utils.h"

using compression::GzipUncompress;
using extensions::Extension;
using extensions::WebrtcLoggingPrivateDiscardFunction;
using extensions::WebrtcLoggingPrivateSetMetaDataFunction;
using extensions::WebrtcLoggingPrivateStartFunction;
using extensions::WebrtcLoggingPrivateStartRtpDumpFunction;
using extensions::WebrtcLoggingPrivateStopFunction;
using extensions::WebrtcLoggingPrivateStopRtpDumpFunction;
using extensions::WebrtcLoggingPrivateStoreFunction;
using extensions::WebrtcLoggingPrivateUploadFunction;
using extensions::WebrtcLoggingPrivateUploadStoredFunction;
using extensions::WebrtcLoggingPrivateStartAudioDebugRecordingsFunction;
using extensions::WebrtcLoggingPrivateStopAudioDebugRecordingsFunction;
using extensions::WebrtcLoggingPrivateStartEventLoggingFunction;

namespace utils = extension_function_test_utils;

namespace {

static const char kTestLoggingSessionIdKey[] = "app_session_id";
static const char kTestLoggingSessionIdValue[] = "0123456789abcdef";
static const char kTestLoggingUrl[] = "dummy url string";

std::string ParamsToString(const base::ListValue& parameters) {
  std::string parameter_string;
  EXPECT_TRUE(base::JSONWriter::Write(parameters, &parameter_string));
  return parameter_string;
}

void InitializeTestMetaData(base::ListValue* parameters) {
  std::unique_ptr<base::DictionaryValue> meta_data_entry(
      new base::DictionaryValue());
  meta_data_entry->SetString("key", kTestLoggingSessionIdKey);
  meta_data_entry->SetString("value", kTestLoggingSessionIdValue);
  std::unique_ptr<base::ListValue> meta_data(new base::ListValue());
  meta_data->Append(std::move(meta_data_entry));
  meta_data_entry.reset(new base::DictionaryValue());
  meta_data_entry->SetString("key", "url");
  meta_data_entry->SetString("value", kTestLoggingUrl);
  meta_data->Append(std::move(meta_data_entry));
  parameters->Append(std::move(meta_data));
}

class WebrtcLoggingPrivateApiTest : public extensions::ExtensionApiTest {
 protected:
  void SetUp() override {
    auto* cl = scoped_command_line_.GetProcessCommandLine();
    cl->AppendSwitchASCII(::switches::kWebRtcRemoteEventLog, "enabled");
    extensions::ExtensionApiTest::SetUp();
    extension_ = extensions::ExtensionBuilder("Test").Build();
  }

  template<typename T>
  scoped_refptr<T> CreateFunction() {
    scoped_refptr<T> function(new T());
    function->set_extension(extension_.get());
    function->set_has_callback(true);
    return function;
  }

  content::WebContents* web_contents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  void AppendTabIdAndUrl(base::ListValue* parameters) {
    std::unique_ptr<base::DictionaryValue> request_info(
        new base::DictionaryValue());
    request_info->SetInteger(
        "tabId", extensions::ExtensionTabUtil::GetTabId(web_contents()));
    parameters->Append(std::move(request_info));
    parameters->AppendString(web_contents()->GetURL().GetOrigin().spec());
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value (NOT whether
  // it had succeeded or failed).
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool RunFunction(UIThreadExtensionFunction* function,
                   const base::ListValue& parameters) {
    std::unique_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
        function, ParamsToString(parameters), browser()));
    return (result != nullptr);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value (NOT whether
  // it had succeeded or failed).
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  template <typename Function>
  bool RunFunction(const base::ListValue& parameters) {
    scoped_refptr<Function> function(CreateFunction<Function>());
    return RunFunction(function.get(), parameters);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value (NOT whether
  // it had succeeded or failed).
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  template <typename Function>
  bool RunNoArgsFunction() {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    scoped_refptr<Function> function(CreateFunction<Function>());
    return RunFunction(function.get(), params);
  }

  template <typename Function>
  void RunFunctionAndExpectError(const base::ListValue& parameters,
                                 const std::string& expected_error) {
    DCHECK(!expected_error.empty());
    scoped_refptr<Function> function(CreateFunction<Function>());
    const std::string error_message = utils::RunFunctionAndReturnError(
        function.get(), ParamsToString(parameters), browser());
    EXPECT_EQ(error_message, expected_error);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StartLogging() {
    constexpr bool result_expected = false;
    const bool result_returned =
        RunNoArgsFunction<WebrtcLoggingPrivateStartFunction>();
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StopLogging() {
    constexpr bool result_expected = false;
    const bool result_returned =
        RunNoArgsFunction<WebrtcLoggingPrivateStopFunction>();
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool DiscardLog() {
    constexpr bool result_expected = false;
    const bool result_returned =
        RunNoArgsFunction<WebrtcLoggingPrivateDiscardFunction>();
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool UploadLog() {
    constexpr bool result_expected = true;
    const bool result_returned =
        RunNoArgsFunction<WebrtcLoggingPrivateUploadFunction>();
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool SetMetaData(const base::ListValue& data) {
    constexpr bool result_expected = false;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateSetMetaDataFunction>(data);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StartRtpDump(bool incoming, bool outgoing) {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendBoolean(incoming);
    params.AppendBoolean(outgoing);
    constexpr bool result_expected = false;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateStartRtpDumpFunction>(params);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StopRtpDump(bool incoming, bool outgoing) {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendBoolean(incoming);
    params.AppendBoolean(outgoing);
    constexpr bool result_expected = false;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateStopRtpDumpFunction>(params);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StoreLog(const std::string& log_id) {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendString(log_id);
    constexpr bool result_expected = false;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateStoreFunction>(params);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool UploadStoredLog(const std::string& log_id) {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendString(log_id);
    constexpr bool result_expected = true;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateUploadStoredFunction>(params);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StartAudioDebugRecordings(int seconds) {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendInteger(seconds);
    constexpr bool result_expected = true;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateStartAudioDebugRecordingsFunction>(
            params);
    return (result_expected == result_returned);
  }

  // This function implicitly expects the function to succeed (test failure
  // initiated otherwise).
  // Returns whether the function that was run returned a value, or avoided
  // returning a value, according to expectation.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  bool StopAudioDebugRecordings() {
    base::ListValue params;
    AppendTabIdAndUrl(&params);
    constexpr bool result_expected = true;
    const bool result_returned =
        RunFunction<WebrtcLoggingPrivateStopAudioDebugRecordingsFunction>(
            params);
    return (result_expected == result_returned);
  }

  // This function expects the function to succeed or fail according to
  // |expect_success| (test failure initiated otherwise). It also implicitly
  // expects that no value would be returned.
  // TODO(crbug.com/829419): Return success/failure of the executed function.
  void StartEventLogging(const std::string& peerConnectionId,
                         int maxLogSizeBytes,
                         const std::string& metadata,
                         bool expect_success,
                         const std::string& expected_error = std::string()) {
    DCHECK_EQ(expect_success, expected_error.empty());

    base::ListValue params;
    AppendTabIdAndUrl(&params);
    params.AppendString(peerConnectionId);
    params.AppendInteger(maxLogSizeBytes);
    params.AppendString(metadata);

    if (expect_success) {
      const bool result_returned =
          RunFunction<WebrtcLoggingPrivateStartEventLoggingFunction>(params);
      EXPECT_FALSE(result_returned);  // Should never return a value.
    } else {
      RunFunctionAndExpectError<WebrtcLoggingPrivateStartEventLoggingFunction>(
          params, expected_error);
    }
  }

  void SetUpPeerConnection(const std::string& peer_connection_id) {
    auto* manager = content::WebRtcEventLogger::Get();
    auto* rph = web_contents()->GetRenderViewHost()->GetProcess();

    const int render_process_id = rph->GetID();
    const int lid = 0;

    manager->PeerConnectionAdded(render_process_id, lid, peer_connection_id);
  }

  base::test::ScopedCommandLine scoped_command_line_;
  scoped_refptr<Extension> extension_;
};

class WebrtcLoggingPrivateApiTestDisabledRemoteLogging
    : public WebrtcLoggingPrivateApiTest {
 protected:
  void SetUp() override {
    auto* cl = scoped_command_line_.GetProcessCommandLine();
    cl->AppendSwitchASCII(::switches::kWebRtcRemoteEventLog, "disabled");
    extensions::ExtensionApiTest::SetUp();
    extension_ = extensions::ExtensionBuilder("Test").Build();
  }
};

// Helper class to temporarily tell the uploader to save the multipart buffer to
// a test string instead of uploading.
class ScopedOverrideUploadBuffer {
 public:
  ScopedOverrideUploadBuffer() {
    g_browser_process->webrtc_log_uploader()->
        OverrideUploadWithBufferForTesting(&multipart_);
  }

  ~ScopedOverrideUploadBuffer() {
    g_browser_process->webrtc_log_uploader()->
        OverrideUploadWithBufferForTesting(nullptr);
  }

  const std::string& multipart() const { return multipart_; }

 private:
  std::string multipart_;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopDiscard) {
  ScopedOverrideUploadBuffer buffer_override;

  EXPECT_TRUE(StartLogging());
  EXPECT_TRUE(StopLogging());
  EXPECT_TRUE(DiscardLog());

  EXPECT_TRUE(buffer_override.multipart().empty());
}

// 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) {
  ScopedOverrideUploadBuffer buffer_override;

  base::ListValue parameters;
  AppendTabIdAndUrl(&parameters);
  InitializeTestMetaData(&parameters);

  SetMetaData(parameters);
  StartLogging();
  StopLogging();
  UploadLog();

  std::string multipart = buffer_override.multipart();
  ASSERT_FALSE(multipart.empty());

  // Check multipart data.

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

  // Move the compressed data to its own string, since it may contain "\r\n" and
  // it makes the tests below easier.
  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);
  std::string log_part = multipart.substr(zip_pos, zip_length);
  multipart.erase(zip_pos, zip_length);

  // Uncompress log and verify contents.
  EXPECT_TRUE(GzipUncompress(log_part, &log_part));
  EXPECT_GT(log_part.length(), 0u);
  // Verify that meta data exists.
  EXPECT_NE(std::string::npos, log_part.find(base::StringPrintf("%s: %s",
      kTestLoggingSessionIdKey, kTestLoggingSessionIdValue)));
  // Verify that the basic info generated at logging startup exists.
  EXPECT_NE(std::string::npos, log_part.find("Chrome version:"));
  EXPECT_NE(std::string::npos, log_part.find("Cpu brand:"));

  // Check the multipart contents.
  std::vector<std::string> multipart_lines = base::SplitStringUsingSubstr(
      multipart, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  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(kTestLoggingSessionIdValue, 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());
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopRtpDump) {
  // TODO(tommi): As is, these tests are missing verification of the actual
  // RTP dump data.  We should fix that, e.g. use SetDumpWriterForTesting.
  StartRtpDump(true, true);
  StopRtpDump(true, true);
}

// Tests trying to store a log when a log is not being captured.
// We should get a failure callback in this case.
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStoreWithoutLog) {
  base::ListValue parameters;
  AppendTabIdAndUrl(&parameters);
  parameters.AppendString("MyLogId");
  scoped_refptr<WebrtcLoggingPrivateStoreFunction> store(
      CreateFunction<WebrtcLoggingPrivateStoreFunction>());
  const std::string error = utils::RunFunctionAndReturnError(
      store.get(), ParamsToString(parameters), browser());
  ASSERT_FALSE(error.empty());
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopStore) {
  ASSERT_TRUE(StartLogging());
  ASSERT_TRUE(StopLogging());
  EXPECT_TRUE(StoreLog("MyLogID"));
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       TestStartStopStoreAndUpload) {
  static const char kLogId[] = "TestStartStopStoreAndUpload";
  ASSERT_TRUE(StartLogging());
  ASSERT_TRUE(StopLogging());
  ASSERT_TRUE(StoreLog(kLogId));

  ScopedOverrideUploadBuffer buffer_override;
  EXPECT_TRUE(UploadStoredLog(kLogId));
  EXPECT_NE(std::string::npos,
            buffer_override.multipart().find("filename=\"webrtc_log.gz\""));
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       TestStartStopStoreAndUploadWithRtp) {
  static const char kLogId[] = "TestStartStopStoreAndUploadWithRtp";
  ASSERT_TRUE(StartLogging());
  ASSERT_TRUE(StartRtpDump(true, true));
  ASSERT_TRUE(StopLogging());
  ASSERT_TRUE(StopRtpDump(true, true));
  ASSERT_TRUE(StoreLog(kLogId));

  ScopedOverrideUploadBuffer buffer_override;
  EXPECT_TRUE(UploadStoredLog(kLogId));
  EXPECT_NE(std::string::npos,
            buffer_override.multipart().find("filename=\"webrtc_log.gz\""));
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       TestStartStopStoreAndUploadWithMetaData) {
  static const char kLogId[] = "TestStartStopStoreAndUploadWithRtp";
  ASSERT_TRUE(StartLogging());

  base::ListValue parameters;
  AppendTabIdAndUrl(&parameters);
  InitializeTestMetaData(&parameters);
  SetMetaData(parameters);

  ASSERT_TRUE(StopLogging());
  ASSERT_TRUE(StoreLog(kLogId));

  ScopedOverrideUploadBuffer buffer_override;
  EXPECT_TRUE(UploadStoredLog(kLogId));
  EXPECT_NE(std::string::npos,
            buffer_override.multipart().find("filename=\"webrtc_log.gz\""));
  EXPECT_NE(std::string::npos,
            buffer_override.multipart().find(kTestLoggingUrl));
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       TestStartStopAudioDebugRecordings) {
  // TODO(guidou): These tests are missing verification of the actual AEC dump
  // data. This will be fixed with a separate browser test.
  // See crbug.com/569957.
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kEnableAudioDebugRecordingsFromExtension);
  ASSERT_TRUE(StartAudioDebugRecordings(0));
  ASSERT_TRUE(StopAudioDebugRecordings());
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       TestStartTimedAudioDebugRecordings) {
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kEnableAudioDebugRecordingsFromExtension);
  ASSERT_TRUE(StartAudioDebugRecordings(1));
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingForKnownPeerConnectionSucceeds) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata = "metadata";
  constexpr bool expect_success = true;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingWithUnlimitedSizeFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const int max_size_bytes = kWebRtcEventLogManagerUnlimitedFileSize;
  const std::string metadata = "metadata";
  constexpr bool expect_success = false;
  const std::string error_message =
      kStartRemoteLoggingFailureUnlimitedSizeDisallowed;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingWithExcessiveMaxSizeFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes + 1;
  const std::string metadata = "metadata";
  constexpr bool expect_success = false;
  const std::string error_message = kStartRemoteLoggingFailureMaxSizeTooLarge;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingWithExcessivelyLongMetadataFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata(kMaxRemoteLogFileMetadataSizeBytes + 1, 'X');
  constexpr bool expect_success = false;
  const std::string error_message = kStartRemoteLoggingFailureMetadaTooLong;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingWithMaxSizeTooSmallFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const std::string metadata = "metadata";
  const size_t max_size_bytes =
      kRemoteBoundLogFileHeaderSizeBytes + metadata.length();
  constexpr bool expect_success = false;
  const std::string error_message = kStartRemoteLoggingFailureMaxSizeTooSmall;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingForNeverAddedPeerConnectionFails) {
  // Note that manager->PeerConnectionAdded() is not called.
  const std::string peer_connection_id = "id";
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata = "metadata";
  constexpr bool expect_success = false;
  const std::string error_message =
      kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingForWrongPeerConnectionIdFails) {
  const std::string peer_connection_id_1 = "id1";
  const std::string peer_connection_id_2 = "id2";

  SetUpPeerConnection(peer_connection_id_1);
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata = "metadata";
  constexpr bool expect_success = false;
  const std::string error_message =
      kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
  StartEventLogging(peer_connection_id_2, max_size_bytes, metadata,
                    expect_success, error_message);
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
                       StartEventLoggingForAlreadyLoggedPeerConnectionFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);

  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata = "metadata";

  // First call succeeds.
  {
    constexpr bool expect_success = true;
    StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                      expect_success);
  }

  // Second call fails.
  {
    constexpr bool expect_success = false;
    const std::string error_message = kStartRemoteLoggingFailureAlreadyLogging;
    StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                      expect_success, error_message);
  }
}

IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTestDisabledRemoteLogging,
                       StartEventLoggingFails) {
  const std::string peer_connection_id = "id";
  SetUpPeerConnection(peer_connection_id);
  const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
  const std::string metadata = "metadata";
  constexpr bool expect_success = false;
  const std::string error_message = kStartRemoteLoggingFailureFeatureDisabled;
  StartEventLogging(peer_connection_id, max_size_bytes, metadata,
                    expect_success, error_message);
}