1 // Copyright 2014 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 "base/command_line.h"
6 #include "base/macros.h"
7 #include "build/build_config.h"
8 #include "chrome/browser/media/webrtc/webrtc_browsertest_base.h"
9 #include "chrome/browser/media/webrtc/webrtc_browsertest_common.h"
10 #include "chrome/common/channel_info.h"
11 #include "components/version_info/version_info.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/test/browser_test.h"
14 #include "media/base/media_switches.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 
17 static const char kMainWebrtcTestHtmlPage[] =
18     "/webrtc/webrtc_jsep01_test.html";
19 
20 // This tests the --disable-webrtc-encryption command line flag. Disabling
21 // encryption should only be possible on certain channels.
22 
23 // NOTE: The test case for each channel will only be exercised when the browser
24 // is actually built for that channel. This is not ideal. One can test manually
25 // by e.g. faking the channel returned in chrome::GetChannel(). It's likely good
26 // to have the test anyway, even though a failure might not be detected until a
27 // branch has been promoted to another channel. The unit test for
28 // ChromeContentBrowserClient::MaybeCopyDisableWebRtcEncryptionSwitch tests for
29 // all channels however.
30 // TODO(grunell): Test the different channel cases for any build.
31 class WebRtcDisableEncryptionFlagBrowserTest : public WebRtcTestBase {
32  public:
WebRtcDisableEncryptionFlagBrowserTest()33   WebRtcDisableEncryptionFlagBrowserTest() {}
~WebRtcDisableEncryptionFlagBrowserTest()34   ~WebRtcDisableEncryptionFlagBrowserTest() override {}
35 
SetUpInProcessBrowserTestFixture()36   void SetUpInProcessBrowserTestFixture() override {
37     DetectErrorsInJavaScript();  // Look for errors in our rather complex js.
38   }
39 
SetUpCommandLine(base::CommandLine * command_line)40   void SetUpCommandLine(base::CommandLine* command_line) override {
41     // Disable encryption with the command line flag.
42     command_line->AppendSwitch(switches::kDisableWebRtcEncryption);
43   }
44 
45  private:
46   DISALLOW_COPY_AND_ASSIGN(WebRtcDisableEncryptionFlagBrowserTest);
47 };
48 
49 // Makes a call and checks that there's encryption or not in the SDP offer.
50 // TODO(crbug.com/910216): De-flake this for ChromeOs.
51 // TODO(crbug.com/984879): De-flake this for ASAN/MSAN Linux.
52 #if defined(OS_CHROMEOS) ||                                        \
53     (defined(OS_LINUX) &&                                          \
54      (defined(MEMORY_SANITIZER) || defined(ADDRESS_SANITIZER))) || \
55     (defined(OS_WIN) && defined(ADDRESS_SANITIZER))
56 #define MAYBE_VerifyEncryption DISABLED_VerifyEncryption
57 #else
58 #define MAYBE_VerifyEncryption VerifyEncryption
59 #endif
IN_PROC_BROWSER_TEST_F(WebRtcDisableEncryptionFlagBrowserTest,MAYBE_VerifyEncryption)60 IN_PROC_BROWSER_TEST_F(WebRtcDisableEncryptionFlagBrowserTest,
61                        MAYBE_VerifyEncryption) {
62   ASSERT_TRUE(embedded_test_server()->Start());
63 
64   content::WebContents* left_tab =
65       OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
66   content::WebContents* right_tab =
67       OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage);
68 
69   SetupPeerconnectionWithLocalStream(left_tab);
70   SetupPeerconnectionWithLocalStream(right_tab);
71 
72   NegotiateCall(left_tab, right_tab);
73 
74   StartDetectingVideo(left_tab, "remote-view");
75   StartDetectingVideo(right_tab, "remote-view");
76 
77   WaitForVideoToPlay(left_tab);
78   WaitForVideoToPlay(right_tab);
79 
80   bool should_detect_encryption = true;
81   version_info::Channel channel = chrome::GetChannel();
82   if (channel == version_info::Channel::UNKNOWN ||
83       channel == version_info::Channel::CANARY ||
84       channel == version_info::Channel::DEV) {
85     should_detect_encryption = false;
86   }
87 #if defined(OS_ANDROID)
88   if (channel == version_info::Channel::BETA)
89     should_detect_encryption = false;
90 #endif
91 
92   std::string expected_string = should_detect_encryption ?
93     "crypto-seen" : "no-crypto-seen";
94 
95   ASSERT_EQ(expected_string,
96             ExecuteJavascript("hasSeenCryptoInSdp()", left_tab));
97 
98   HangUp(left_tab);
99   HangUp(right_tab);
100 }
101