Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 // Unit test for AudioOutputAuthorizationHandlerTest. | |
| 6 | |
| 7 #include "content/browser/renderer_host/media/audio_output_authorization_handler .h" | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "content/public/test/mock_render_process_host.h" | |
| 13 #include "content/public/test/test_browser_context.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "media/audio/audio_device_description.h" | |
| 16 #include "media/audio/fake_audio_manager.h" | |
| 17 #include "media/base/media_switches.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "url/gurl.h" | |
| 21 #include "url/origin.h" | |
| 22 | |
| 23 using ::testing::_; | |
| 24 | |
| 25 const int kRenderProcessId = 1; | |
|
o1ka
2016/11/03 10:07:22
hide statics in an unnamed namespace
Max Morin
2016/11/10 14:59:52
Done.
| |
| 26 const int kRenderFrameId = 2; | |
| 27 const char kSecurityOriginString[] = "http://localhost"; | |
| 28 const GURL kSecurityOriginGURL(kSecurityOriginString); | |
|
o1ka
2016/11/03 10:07:22
only PODs can be static: https://google.github.io/
Max Morin
2016/11/10 14:59:52
Done.
| |
| 29 const url::Origin kSecurityOrigin(kSecurityOriginGURL); | |
| 30 const char kBadSecurityOriginString[] = "about:about"; | |
| 31 const GURL kBadSecurityOriginGURL(kBadSecurityOriginString); | |
| 32 const url::Origin kBadSecurityOrigin(kBadSecurityOriginGURL); | |
| 33 const char kDefaultDeviceId[] = "default"; | |
| 34 const char kEmptyDeviceId[] = ""; | |
| 35 const char kInvalidDeviceId[] = "invalid-device-id"; | |
| 36 const char kSalt[] = "salt"; | |
| 37 | |
| 38 namespace content { | |
| 39 | |
| 40 class AudioOutputAuthorizationHandlerTest : public testing::Test { | |
| 41 public: | |
| 42 AudioOutputAuthorizationHandlerTest() { | |
| 43 audio_manager_ = media::FakeAudioManager::CreateForTesting( | |
| 44 base::ThreadTaskRunnerHandle::Get()); | |
| 45 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 46 switches::kUseFakeDeviceForMediaStream); | |
| 47 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | |
| 48 raw_nondefault_id = GetRawNondefaultFakeId(); | |
| 49 hashed_nondefault_id = MediaStreamManager::GetHMACForMediaDeviceID( | |
| 50 kSalt, kSecurityOrigin, raw_nondefault_id); | |
| 51 } | |
| 52 | |
| 53 ~AudioOutputAuthorizationHandlerTest() override {} | |
| 54 | |
| 55 protected: | |
| 56 void SetUp() override {} | |
| 57 | |
| 58 void TearDown() override {} | |
| 59 | |
| 60 std::unique_ptr<MediaStreamManager> media_stream_manager_; | |
| 61 TestBrowserThreadBundle thread_bundle_; | |
|
o1ka
2016/11/03 10:07:22
All the browser threads are emulated with a single
Max Morin
2016/11/10 14:59:52
Done.
| |
| 62 media::ScopedAudioManagerPtr audio_manager_; | |
| 63 std::string raw_nondefault_id; | |
| 64 std::string hashed_nondefault_id; | |
| 65 | |
| 66 private: | |
| 67 std::string GetRawNondefaultFakeId() { | |
| 68 MediaDeviceInfoArray devices; | |
| 69 MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; | |
| 70 devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; | |
| 71 | |
| 72 media_stream_manager_->media_devices_manager()->EnumerateDevices( | |
| 73 devices_to_enumerate, | |
| 74 base::Bind( | |
| 75 [](MediaDeviceInfoArray* out, | |
| 76 const MediaDeviceEnumeration& result) { | |
| 77 *out = result[MediaDeviceType::MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]; | |
| 78 }, | |
| 79 &devices)); | |
| 80 base::RunLoop().RunUntilIdle(); | |
| 81 return devices[1].device_id; // devices[0] is default. | |
| 82 } | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(AudioOutputAuthorizationHandlerTest); | |
| 85 }; | |
| 86 | |
| 87 struct MockListener { | |
| 88 MOCK_METHOD3(MockAuthorizationCallback, | |
| 89 void(media::OutputDeviceStatus status, | |
| 90 const media::AudioParameters& params, | |
| 91 const std::string& id)); | |
| 92 AudioOutputAuthorizationHandler::AuthorizationCompletedCallback | |
| 93 GetCallback() { | |
| 94 return base::Bind(&MockListener::MockAuthorizationCallback, | |
| 95 base::Unretained(this)); | |
| 96 } | |
| 97 }; | |
| 98 | |
| 99 TEST_F(AudioOutputAuthorizationHandlerTest, AuthorizeDefaultDevice_Ok) { | |
| 100 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 101 kRenderProcessId, kSalt); | |
| 102 | |
| 103 MockListener listener; | |
| 104 EXPECT_CALL(listener, | |
| 105 MockAuthorizationCallback(media::OUTPUT_DEVICE_STATUS_OK, _, | |
| 106 kDefaultDeviceId)) | |
| 107 .Times(1); | |
| 108 | |
| 109 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, | |
| 110 kDefaultDeviceId, kSecurityOrigin, | |
| 111 listener.GetCallback()); | |
| 112 base::RunLoop().RunUntilIdle(); | |
| 113 } | |
| 114 | |
| 115 TEST_F(AudioOutputAuthorizationHandlerTest, | |
| 116 AuthorizeDefaultDeviceByEmptyId_Ok) { | |
| 117 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 118 kRenderProcessId, kSalt); | |
| 119 | |
| 120 MockListener listener; | |
| 121 EXPECT_CALL(listener, | |
| 122 MockAuthorizationCallback(media::OUTPUT_DEVICE_STATUS_OK, _, | |
| 123 kDefaultDeviceId)) | |
| 124 .Times(1); | |
| 125 | |
| 126 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, | |
| 127 kEmptyDeviceId, kSecurityOrigin, | |
| 128 listener.GetCallback()); | |
| 129 base::RunLoop().RunUntilIdle(); | |
| 130 } | |
| 131 | |
| 132 TEST_F(AudioOutputAuthorizationHandlerTest, | |
| 133 AuthorizeNonDefaultDeviceIdWithoutPermission_NotAuthorized) { | |
| 134 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 135 kRenderProcessId, kSalt); | |
| 136 std::string id = MediaStreamManager::GetHMACForMediaDeviceID( | |
| 137 kSalt, kSecurityOrigin, raw_nondefault_id); | |
| 138 | |
| 139 MockListener listener; | |
| 140 EXPECT_CALL(listener, | |
| 141 MockAuthorizationCallback( | |
| 142 media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, _, _)) | |
| 143 .Times(1); | |
| 144 | |
| 145 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, id, | |
| 146 kSecurityOrigin, listener.GetCallback()); | |
| 147 base::RunLoop().RunUntilIdle(); | |
| 148 } | |
| 149 | |
| 150 TEST_F(AudioOutputAuthorizationHandlerTest, | |
| 151 AuthorizeNonDefaultDeviceIdWithPermission_Ok) { | |
| 152 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 153 kRenderProcessId, kSalt); | |
| 154 // Override permissions check. | |
| 155 handler.GetMediaDevicesPermissionCheckerForTesting() | |
| 156 .OverridePermissionsForTesting(true); | |
| 157 | |
| 158 MockListener listener; | |
| 159 EXPECT_CALL(listener, | |
| 160 MockAuthorizationCallback(media::OUTPUT_DEVICE_STATUS_OK, _, | |
| 161 raw_nondefault_id)) | |
| 162 .Times(1); | |
| 163 | |
| 164 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, | |
| 165 hashed_nondefault_id, kSecurityOrigin, | |
| 166 listener.GetCallback()); | |
| 167 base::RunLoop().RunUntilIdle(); | |
| 168 } | |
| 169 | |
| 170 TEST_F(AudioOutputAuthorizationHandlerTest, | |
| 171 AuthorizeInvalidDeviceId_BadMessage) { | |
| 172 TestBrowserContext context; | |
| 173 MockRenderProcessHost RPH(&context); | |
| 174 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 175 RPH.GetID(), kSalt); | |
| 176 | |
| 177 MockListener listener; | |
| 178 EXPECT_CALL(listener, MockAuthorizationCallback(_, _, _)).Times(0); | |
| 179 | |
| 180 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, | |
| 181 kInvalidDeviceId, kSecurityOrigin, | |
| 182 listener.GetCallback()); | |
| 183 base::RunLoop().RunUntilIdle(); | |
| 184 | |
| 185 EXPECT_EQ(RPH.bad_msg_count(), 1); | |
|
o1ka
2016/11/03 10:07:22
nit: expect it to be 0 at the beginning of the tes
Max Morin
2016/11/10 14:59:52
Done.
| |
| 186 } | |
| 187 | |
| 188 TEST_F(AudioOutputAuthorizationHandlerTest, | |
| 189 AuthorizeNondefaultDeviceIdWithBadOrigin_BadMessage) { | |
| 190 TestBrowserContext context; | |
| 191 MockRenderProcessHost RPH(&context); | |
| 192 AudioOutputAuthorizationHandler handler(media_stream_manager_.get(), | |
| 193 RPH.GetID(), kSalt); | |
| 194 | |
| 195 MockListener listener; | |
| 196 EXPECT_CALL(listener, MockAuthorizationCallback(_, _, _)).Times(0); | |
| 197 | |
| 198 handler.RequestDeviceAuthorization(kRenderFrameId, /*session_id*/ 0, | |
| 199 hashed_nondefault_id, kBadSecurityOrigin, | |
| 200 listener.GetCallback()); | |
| 201 base::RunLoop().RunUntilIdle(); | |
| 202 | |
| 203 EXPECT_EQ(RPH.bad_msg_count(), 1); | |
| 204 } | |
| 205 | |
| 206 } // namespace content | |
| OLD | NEW |