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 #include "content/browser/media/webrtc/webrtc_eventlog_host.h" | |
| 6 | |
| 7 #include <tuple> | |
| 8 | |
| 9 #include "base/files/file.h" | |
| 10 #include "base/files/file_util.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "content/browser/renderer_host/render_process_host_impl.h" | |
| 14 #include "content/common/media/peer_connection_tracker_messages.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/test/mock_render_process_host.h" | |
| 17 #include "content/public/test/test_browser_context.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Get the expected Rtc eventlog file name. The name will be | |
| 26 // <temporary path>.<render process id>.<peer connection local id> | |
| 27 base::FilePath GetExpectedEventLogFileName(const base::FilePath& base_file, | |
| 28 int render_process_id, | |
| 29 int peer_connection_local_id) { | |
| 30 return base_file.AddExtension(base::IntToString(render_process_id)) | |
| 31 .AddExtension(base::IntToString(peer_connection_local_id)); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 class WebRtcEventlogHostTest : public testing::Test { | |
| 37 public: | |
| 38 WebRtcEventlogHostTest() | |
| 39 : mock_render_process_host_(static_cast<MockRenderProcessHost*>( | |
| 40 mock_render_process_factory_.CreateRenderProcessHost( | |
| 41 &test_browser_context_, | |
| 42 nullptr))), | |
| 43 render_id_(mock_render_process_host_->GetID()), | |
| 44 event_log_host_(render_id_) {} | |
| 45 TestBrowserThreadBundle thread_bundle_; | |
| 46 MockRenderProcessHostFactory mock_render_process_factory_; | |
| 47 TestBrowserContext test_browser_context_; | |
| 48 std::unique_ptr<MockRenderProcessHost> mock_render_process_host_; | |
| 49 const int render_id_; | |
| 50 WebRTCEventLogHost event_log_host_; | |
| 51 base::FilePath base_file_; | |
| 52 | |
| 53 void StartLogging() { | |
| 54 ASSERT_TRUE(CreateTemporaryFile(&base_file_)); | |
| 55 EXPECT_TRUE(DeleteFile(base_file_, false)); | |
| 56 EXPECT_FALSE(PathExists(base_file_)); | |
| 57 EXPECT_TRUE(event_log_host_.StartWebRTCEventLog(base_file_)); | |
| 58 base::RunLoop().RunUntilIdle(); | |
| 59 } | |
| 60 | |
| 61 void StopLogging() { | |
| 62 EXPECT_TRUE(event_log_host_.StopWebRTCEventLog()); | |
| 63 base::RunLoop().RunUntilIdle(); | |
| 64 } | |
| 65 | |
| 66 void ValidateStartIPCMessageAndCloseFile(const IPC::Message* msg, | |
| 67 const int peer_connection_id) { | |
| 68 ASSERT_TRUE(msg); | |
| 69 std::tuple<int, IPC::PlatformFileForTransit> start_params; | |
| 70 PeerConnectionTracker_StartEventLog::Read(msg, &start_params); | |
| 71 EXPECT_EQ(peer_connection_id, std::get<0>(start_params)); | |
| 72 ASSERT_NE(IPC::InvalidPlatformFileForTransit(), std::get<1>(start_params)); | |
| 73 IPC::PlatformFileForTransitToFile(std::get<1>(start_params)).Close(); | |
| 74 } | |
| 75 | |
| 76 void ValidateStopIPCMessage(const IPC::Message* msg, | |
| 77 const int peer_connection_id) { | |
| 78 ASSERT_TRUE(msg); | |
| 79 std::tuple<int> stop_params; | |
| 80 PeerConnectionTracker_StopEventLog::Read(msg, &stop_params); | |
| 81 EXPECT_EQ(peer_connection_id, std::get<0>(stop_params)); | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() without having | |
| 86 // added any PeerConnections. It is expected that no IPC messages will be sent. | |
| 87 TEST_F(WebRtcEventlogHostTest, NoPeerConnectionTest) { | |
| 88 mock_render_process_host_->sink().ClearMessages(); | |
| 89 | |
| 90 // Start logging and check that no IPC messages were sent. | |
| 91 StartLogging(); | |
| 92 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 93 | |
| 94 // Stop logging and check that no IPC messages were sent. | |
| 95 StopLogging(); | |
| 96 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 97 } | |
| 98 | |
| 99 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after adding a | |
| 100 // single PeerConnection. It is expected that one IPC message will be sent for | |
| 101 // each of the Start and Stop calls, and that a logfile is created. | |
| 102 TEST_F(WebRtcEventlogHostTest, OnePeerConnectionTest) { | |
| 103 const int kTestPeerConnectionId = 123; | |
| 104 mock_render_process_host_->sink().ClearMessages(); | |
| 105 | |
| 106 // Add a PeerConnection and start logging. | |
| 107 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId); | |
| 108 StartLogging(); | |
| 109 | |
| 110 // Check that the correct IPC message was sent. | |
| 111 EXPECT_EQ(size_t(1), mock_render_process_host_->sink().message_count()); | |
| 112 const IPC::Message* start_msg = | |
| 113 mock_render_process_host_->sink().GetMessageAt(0); | |
| 114 ValidateStartIPCMessageAndCloseFile(start_msg, kTestPeerConnectionId); | |
| 115 | |
| 116 // Stop logging. | |
| 117 mock_render_process_host_->sink().ClearMessages(); | |
| 118 StopLogging(); | |
| 119 | |
| 120 // Check that the correct IPC message was sent. | |
| 121 EXPECT_EQ(size_t(1), mock_render_process_host_->sink().message_count()); | |
| 122 const IPC::Message* stop_msg = | |
| 123 mock_render_process_host_->sink().GetMessageAt(0); | |
| 124 ValidateStopIPCMessage(stop_msg, kTestPeerConnectionId); | |
| 125 | |
| 126 // Clean up the logfile. | |
| 127 base::FilePath expected_file = GetExpectedEventLogFileName( | |
| 128 base_file_, render_id_, kTestPeerConnectionId); | |
| 129 ASSERT_TRUE(PathExists(expected_file)); | |
| 130 EXPECT_TRUE(DeleteFile(expected_file, false)); | |
| 131 } | |
| 132 | |
| 133 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after adding | |
| 134 // two PeerConnections. It is expected that two IPC messages will be sent for | |
| 135 // each of the Start and Stop calls, and that a file is created for both | |
| 136 // PeerConnections. | |
| 137 TEST_F(WebRtcEventlogHostTest, TwoPeerConnectionsTest) { | |
| 138 const int kTestPeerConnectionId1 = 123; | |
| 139 const int kTestPeerConnectionId2 = 321; | |
| 140 mock_render_process_host_->sink().ClearMessages(); | |
| 141 | |
| 142 // Add two PeerConnections and start logging. | |
| 143 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId1); | |
| 144 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId2); | |
| 145 StartLogging(); | |
| 146 | |
| 147 // Check that the correct IPC messages were sent. | |
| 148 EXPECT_EQ(size_t(2), mock_render_process_host_->sink().message_count()); | |
| 149 const IPC::Message* start_msg1 = | |
| 150 mock_render_process_host_->sink().GetMessageAt(0); | |
| 151 ValidateStartIPCMessageAndCloseFile(start_msg1, kTestPeerConnectionId1); | |
| 152 const IPC::Message* start_msg2 = | |
| 153 mock_render_process_host_->sink().GetMessageAt(1); | |
| 154 ValidateStartIPCMessageAndCloseFile(start_msg2, kTestPeerConnectionId2); | |
| 155 | |
| 156 // Stop logging. | |
| 157 mock_render_process_host_->sink().ClearMessages(); | |
| 158 StopLogging(); | |
| 159 | |
| 160 // Check that the correct IPC messages were sent. | |
| 161 EXPECT_EQ(size_t(2), mock_render_process_host_->sink().message_count()); | |
| 162 const IPC::Message* stop_msg1 = | |
| 163 mock_render_process_host_->sink().GetMessageAt(0); | |
| 164 ValidateStopIPCMessage(stop_msg1, kTestPeerConnectionId1); | |
| 165 const IPC::Message* stop_msg2 = | |
| 166 mock_render_process_host_->sink().GetMessageAt(1); | |
| 167 ValidateStopIPCMessage(stop_msg2, kTestPeerConnectionId2); | |
| 168 | |
| 169 // Clean up the logfiles. | |
| 170 base::FilePath expected_file1 = GetExpectedEventLogFileName( | |
| 171 base_file_, render_id_, kTestPeerConnectionId1); | |
| 172 base::FilePath expected_file2 = GetExpectedEventLogFileName( | |
| 173 base_file_, render_id_, kTestPeerConnectionId2); | |
| 174 ASSERT_TRUE(PathExists(expected_file1)); | |
| 175 EXPECT_TRUE(DeleteFile(expected_file1, false)); | |
| 176 ASSERT_TRUE(PathExists(expected_file2)); | |
| 177 EXPECT_TRUE(DeleteFile(expected_file2, false)); | |
| 178 } | |
| 179 | |
| 180 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after adding | |
| 181 // more PeerConnections than the maximum allowed. It is expected that only the | |
| 182 // maximum allowed number of IPC messages and log files will be opened, but we | |
| 183 // expect the number of stop IPC messages to be equal to the actual number of | |
| 184 // PeerConnections. | |
| 185 TEST_F(WebRtcEventlogHostTest, ExceedMaxPeerConnectionsTest) { | |
| 186 #if defined(OS_ANDROID) | |
| 187 const int kMaxNumberLogFiles = 3; | |
| 188 #else | |
| 189 const int kMaxNumberLogFiles = 5; | |
| 190 #endif | |
| 191 const int kNumberOfPeerConnections = kMaxNumberLogFiles + 1; | |
| 192 mock_render_process_host_->sink().ClearMessages(); | |
| 193 | |
| 194 // Add the maximum number + 1 PeerConnections and start logging. | |
| 195 for (int i = 0; i < kNumberOfPeerConnections; ++i) { | |
|
Henrik Grunell
2016/06/09 10:28:22
Nit: No {}.
Ivo-OOO until feb 6
2016/06/09 12:59:40
Done.
| |
| 196 event_log_host_.PeerConnectionAdded(i); | |
| 197 } | |
| 198 StartLogging(); | |
| 199 | |
| 200 // Check that the correct IPC messages were sent. | |
| 201 ASSERT_EQ(size_t(kMaxNumberLogFiles), | |
| 202 mock_render_process_host_->sink().message_count()); | |
| 203 for (int i = 0; i < kMaxNumberLogFiles; ++i) { | |
| 204 const IPC::Message* start_msg = | |
| 205 mock_render_process_host_->sink().GetMessageAt(i); | |
| 206 ValidateStartIPCMessageAndCloseFile(start_msg, i); | |
| 207 } | |
| 208 | |
| 209 // Stop logging. | |
| 210 mock_render_process_host_->sink().ClearMessages(); | |
| 211 StopLogging(); | |
| 212 | |
| 213 // Check that the correct IPC messages were sent. | |
| 214 ASSERT_EQ(size_t(kNumberOfPeerConnections), | |
| 215 mock_render_process_host_->sink().message_count()); | |
| 216 for (int i = 0; i < kNumberOfPeerConnections; ++i) { | |
| 217 const IPC::Message* stop_msg = | |
| 218 mock_render_process_host_->sink().GetMessageAt(i); | |
| 219 ValidateStopIPCMessage(stop_msg, i); | |
| 220 } | |
| 221 | |
| 222 // Clean up the logfiles. | |
| 223 for (int i = 0; i < kMaxNumberLogFiles; ++i) { | |
| 224 base::FilePath expected_file = | |
| 225 GetExpectedEventLogFileName(base_file_, render_id_, i); | |
| 226 ASSERT_TRUE(PathExists(expected_file)); | |
| 227 EXPECT_TRUE(DeleteFile(expected_file, false)); | |
| 228 } | |
| 229 | |
| 230 // Check that not too many files were created. | |
| 231 for (int i = kMaxNumberLogFiles; i < kNumberOfPeerConnections; ++i) { | |
| 232 base::FilePath expected_file = | |
| 233 GetExpectedEventLogFileName(base_file_, render_id_, i); | |
| 234 EXPECT_FALSE(PathExists(expected_file)); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 // This test calls StartWebRTCEventLog() and StopWebRTCEventLog() after first | |
| 239 // adding and then removing a single PeerConnection. It is expected that no IPC | |
| 240 // message will be sent. | |
| 241 TEST_F(WebRtcEventlogHostTest, AddRemovePeerConnectionTest) { | |
| 242 const int kTestPeerConnectionId = 123; | |
| 243 mock_render_process_host_->sink().ClearMessages(); | |
| 244 | |
| 245 // Add and immediately remove a PeerConnection. | |
| 246 event_log_host_.PeerConnectionAdded(kTestPeerConnectionId); | |
| 247 event_log_host_.PeerConnectionRemoved(kTestPeerConnectionId); | |
| 248 | |
| 249 // Start logging and check that no IPC messages were sent. | |
| 250 StartLogging(); | |
| 251 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 252 | |
| 253 // Stop logging and check that no IPC messages were sent. | |
| 254 StopLogging(); | |
| 255 EXPECT_EQ(size_t(0), mock_render_process_host_->sink().message_count()); | |
| 256 | |
| 257 // Check that no logfile was created. | |
| 258 base::FilePath expected_file = GetExpectedEventLogFileName( | |
| 259 base_file_, render_id_, kTestPeerConnectionId); | |
| 260 ASSERT_FALSE(PathExists(expected_file)); | |
| 261 } | |
| 262 | |
| 263 } // namespace content | |
| OLD | NEW |