| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 #include "base/strings/string_split.h" | 6 #include "base/strings/string_split.h" |
| 7 #include "base/strings/stringprintf.h" |
| 7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_pr
ivate_api.h" | 9 #include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_pr
ivate_api.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_function_test_utils.h" | 11 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/media/webrtc_log_uploader.h" | 13 #include "chrome/browser/media/webrtc_log_uploader.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "components/compression/compression_utils.h" |
| 14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 16 #include "extensions/common/test_util.h" | 18 #include "extensions/common/test_util.h" |
| 17 | 19 |
| 20 using compression::GzipUncompress; |
| 18 using extensions::Extension; | 21 using extensions::Extension; |
| 19 using extensions::WebrtcLoggingPrivateDiscardFunction; | 22 using extensions::WebrtcLoggingPrivateDiscardFunction; |
| 20 using extensions::WebrtcLoggingPrivateSetMetaDataFunction; | 23 using extensions::WebrtcLoggingPrivateSetMetaDataFunction; |
| 21 using extensions::WebrtcLoggingPrivateStartFunction; | 24 using extensions::WebrtcLoggingPrivateStartFunction; |
| 22 using extensions::WebrtcLoggingPrivateStartRtpDumpFunction; | 25 using extensions::WebrtcLoggingPrivateStartRtpDumpFunction; |
| 23 using extensions::WebrtcLoggingPrivateStopFunction; | 26 using extensions::WebrtcLoggingPrivateStopFunction; |
| 24 using extensions::WebrtcLoggingPrivateStopRtpDumpFunction; | 27 using extensions::WebrtcLoggingPrivateStopRtpDumpFunction; |
| 25 using extensions::WebrtcLoggingPrivateStoreFunction; | 28 using extensions::WebrtcLoggingPrivateStoreFunction; |
| 26 using extensions::WebrtcLoggingPrivateUploadFunction; | 29 using extensions::WebrtcLoggingPrivateUploadFunction; |
| 27 using extensions::WebrtcLoggingPrivateUploadStoredFunction; | 30 using extensions::WebrtcLoggingPrivateUploadStoredFunction; |
| 28 | 31 |
| 29 namespace utils = extension_function_test_utils; | 32 namespace utils = extension_function_test_utils; |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 | 35 |
| 33 static const char kTestLoggingSessionId[] = "0123456789abcdef"; | 36 static const char kTestLoggingSessionIdKey[] = "app_session_id"; |
| 37 static const char kTestLoggingSessionIdValue[] = "0123456789abcdef"; |
| 34 static const char kTestLoggingUrl[] = "dummy url string"; | 38 static const char kTestLoggingUrl[] = "dummy url string"; |
| 35 | 39 |
| 36 std::string ParamsToString(const base::ListValue& parameters) { | 40 std::string ParamsToString(const base::ListValue& parameters) { |
| 37 std::string parameter_string; | 41 std::string parameter_string; |
| 38 EXPECT_TRUE(base::JSONWriter::Write(parameters, ¶meter_string)); | 42 EXPECT_TRUE(base::JSONWriter::Write(parameters, ¶meter_string)); |
| 39 return parameter_string; | 43 return parameter_string; |
| 40 } | 44 } |
| 41 | 45 |
| 42 void InitializeTestMetaData(base::ListValue* parameters) { | 46 void InitializeTestMetaData(base::ListValue* parameters) { |
| 43 base::DictionaryValue* meta_data_entry = new base::DictionaryValue(); | 47 base::DictionaryValue* meta_data_entry = new base::DictionaryValue(); |
| 44 meta_data_entry->SetString("key", "app_session_id"); | 48 meta_data_entry->SetString("key", kTestLoggingSessionIdKey); |
| 45 meta_data_entry->SetString("value", kTestLoggingSessionId); | 49 meta_data_entry->SetString("value", kTestLoggingSessionIdValue); |
| 46 base::ListValue* meta_data = new base::ListValue(); | 50 base::ListValue* meta_data = new base::ListValue(); |
| 47 meta_data->Append(meta_data_entry); | 51 meta_data->Append(meta_data_entry); |
| 48 meta_data_entry = new base::DictionaryValue(); | 52 meta_data_entry = new base::DictionaryValue(); |
| 49 meta_data_entry->SetString("key", "url"); | 53 meta_data_entry->SetString("key", "url"); |
| 50 meta_data_entry->SetString("value", kTestLoggingUrl); | 54 meta_data_entry->SetString("value", kTestLoggingUrl); |
| 51 meta_data->Append(meta_data_entry); | 55 meta_data->Append(meta_data_entry); |
| 52 parameters->Append(meta_data); | 56 parameters->Append(meta_data); |
| 53 } | 57 } |
| 54 | 58 |
| 55 class WebrtcLoggingPrivateApiTest : public ExtensionApiTest { | 59 class WebrtcLoggingPrivateApiTest : public ExtensionApiTest { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 // ------**--yradnuoBgoLtrapitluMklaTelgooG--**------ | 233 // ------**--yradnuoBgoLtrapitluMklaTelgooG--**------ |
| 230 // | 234 // |
| 231 IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopUpload) { | 235 IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopUpload) { |
| 232 ScopedOverrideUploadBuffer buffer_override; | 236 ScopedOverrideUploadBuffer buffer_override; |
| 233 | 237 |
| 234 base::ListValue parameters; | 238 base::ListValue parameters; |
| 235 AppendTabIdAndUrl(¶meters); | 239 AppendTabIdAndUrl(¶meters); |
| 236 InitializeTestMetaData(¶meters); | 240 InitializeTestMetaData(¶meters); |
| 237 | 241 |
| 238 SetMetaData(parameters); | 242 SetMetaData(parameters); |
| 239 | |
| 240 StartLogging(); | 243 StartLogging(); |
| 241 StopLogging(); | 244 StopLogging(); |
| 242 UploadLog(); | 245 UploadLog(); |
| 243 | 246 |
| 244 std::string multipart = buffer_override.multipart(); | 247 std::string multipart = buffer_override.multipart(); |
| 245 ASSERT_FALSE(multipart.empty()); | 248 ASSERT_FALSE(multipart.empty()); |
| 246 | 249 |
| 247 // Check multipart data. | 250 // Check multipart data. |
| 248 | 251 |
| 249 const char boundary[] = "------**--yradnuoBgoLtrapitluMklaTelgooG--**----"; | 252 const char boundary[] = "------**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
| 250 | 253 |
| 251 // Remove the compressed data, it may contain "\r\n". Just verify that its | 254 // Move the compressed data to its own string, since it may contain "\r\n" and |
| 252 // size is > 0. | 255 // it makes the tests below easier. |
| 253 const char zip_content_type[] = "Content-Type: application/gzip"; | 256 const char zip_content_type[] = "Content-Type: application/gzip"; |
| 254 size_t zip_pos = multipart.find(&zip_content_type[0]); | 257 size_t zip_pos = multipart.find(&zip_content_type[0]); |
| 255 ASSERT_NE(std::string::npos, zip_pos); | 258 ASSERT_NE(std::string::npos, zip_pos); |
| 256 // Move pos to where the zip begins. - 1 to remove '\0', + 4 for two "\r\n". | 259 // Move pos to where the zip begins. - 1 to remove '\0', + 4 for two "\r\n". |
| 257 zip_pos += sizeof(zip_content_type) + 3; | 260 zip_pos += sizeof(zip_content_type) + 3; |
| 258 size_t zip_length = multipart.find(boundary, zip_pos); | 261 size_t zip_length = multipart.find(boundary, zip_pos); |
| 259 ASSERT_NE(std::string::npos, zip_length); | 262 ASSERT_NE(std::string::npos, zip_length); |
| 260 // Calculate length, adjust for a "\r\n". | 263 // Calculate length, adjust for a "\r\n". |
| 261 zip_length -= zip_pos + 2; | 264 zip_length -= zip_pos + 2; |
| 262 ASSERT_GT(zip_length, 0u); | 265 ASSERT_GT(zip_length, 0u); |
| 266 std::string log_part = multipart.substr(zip_pos, zip_length); |
| 263 multipart.erase(zip_pos, zip_length); | 267 multipart.erase(zip_pos, zip_length); |
| 264 | 268 |
| 269 // Uncompress log and verify contents. |
| 270 EXPECT_TRUE(GzipUncompress(log_part, &log_part)); |
| 271 EXPECT_GT(log_part.length(), 0u); |
| 272 // Verify that meta data exists. |
| 273 EXPECT_NE(std::string::npos, log_part.find(base::StringPrintf("%s: %s", |
| 274 kTestLoggingSessionIdKey, kTestLoggingSessionIdValue))); |
| 275 // Verify that the basic info generated at logging startup exists. |
| 276 EXPECT_NE(std::string::npos, log_part.find("Chrome version:")); |
| 277 EXPECT_NE(std::string::npos, log_part.find("Cpu brand:")); |
| 278 |
| 265 // Check the multipart contents. | 279 // Check the multipart contents. |
| 266 std::vector<std::string> multipart_lines; | 280 std::vector<std::string> multipart_lines; |
| 267 base::SplitStringUsingSubstr(multipart, "\r\n", &multipart_lines); | 281 base::SplitStringUsingSubstr(multipart, "\r\n", &multipart_lines); |
| 268 ASSERT_EQ(31, static_cast<int>(multipart_lines.size())); | 282 ASSERT_EQ(31, static_cast<int>(multipart_lines.size())); |
| 269 | 283 |
| 270 EXPECT_STREQ(&boundary[0], multipart_lines[0].c_str()); | 284 EXPECT_STREQ(&boundary[0], multipart_lines[0].c_str()); |
| 271 EXPECT_STREQ("Content-Disposition: form-data; name=\"prod\"", | 285 EXPECT_STREQ("Content-Disposition: form-data; name=\"prod\"", |
| 272 multipart_lines[1].c_str()); | 286 multipart_lines[1].c_str()); |
| 273 EXPECT_TRUE(multipart_lines[2].empty()); | 287 EXPECT_TRUE(multipart_lines[2].empty()); |
| 274 EXPECT_NE(std::string::npos, multipart_lines[3].find("Chrome")); | 288 EXPECT_NE(std::string::npos, multipart_lines[3].find("Chrome")); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 289 EXPECT_STREQ(&boundary[0], multipart_lines[12].c_str()); | 303 EXPECT_STREQ(&boundary[0], multipart_lines[12].c_str()); |
| 290 EXPECT_STREQ("Content-Disposition: form-data; name=\"type\"", | 304 EXPECT_STREQ("Content-Disposition: form-data; name=\"type\"", |
| 291 multipart_lines[13].c_str()); | 305 multipart_lines[13].c_str()); |
| 292 EXPECT_TRUE(multipart_lines[14].empty()); | 306 EXPECT_TRUE(multipart_lines[14].empty()); |
| 293 EXPECT_STREQ("webrtc_log", multipart_lines[15].c_str()); | 307 EXPECT_STREQ("webrtc_log", multipart_lines[15].c_str()); |
| 294 | 308 |
| 295 EXPECT_STREQ(&boundary[0], multipart_lines[16].c_str()); | 309 EXPECT_STREQ(&boundary[0], multipart_lines[16].c_str()); |
| 296 EXPECT_STREQ("Content-Disposition: form-data; name=\"app_session_id\"", | 310 EXPECT_STREQ("Content-Disposition: form-data; name=\"app_session_id\"", |
| 297 multipart_lines[17].c_str()); | 311 multipart_lines[17].c_str()); |
| 298 EXPECT_TRUE(multipart_lines[18].empty()); | 312 EXPECT_TRUE(multipart_lines[18].empty()); |
| 299 EXPECT_STREQ(kTestLoggingSessionId, multipart_lines[19].c_str()); | 313 EXPECT_STREQ(kTestLoggingSessionIdValue, multipart_lines[19].c_str()); |
| 300 | 314 |
| 301 EXPECT_STREQ(&boundary[0], multipart_lines[20].c_str()); | 315 EXPECT_STREQ(&boundary[0], multipart_lines[20].c_str()); |
| 302 EXPECT_STREQ("Content-Disposition: form-data; name=\"url\"", | 316 EXPECT_STREQ("Content-Disposition: form-data; name=\"url\"", |
| 303 multipart_lines[21].c_str()); | 317 multipart_lines[21].c_str()); |
| 304 EXPECT_TRUE(multipart_lines[22].empty()); | 318 EXPECT_TRUE(multipart_lines[22].empty()); |
| 305 EXPECT_STREQ(kTestLoggingUrl, multipart_lines[23].c_str()); | 319 EXPECT_STREQ(kTestLoggingUrl, multipart_lines[23].c_str()); |
| 306 | 320 |
| 307 EXPECT_STREQ(&boundary[0], multipart_lines[24].c_str()); | 321 EXPECT_STREQ(&boundary[0], multipart_lines[24].c_str()); |
| 308 EXPECT_STREQ("Content-Disposition: form-data; name=\"webrtc_log\";" | 322 EXPECT_STREQ("Content-Disposition: form-data; name=\"webrtc_log\";" |
| 309 " filename=\"webrtc_log.gz\"", | 323 " filename=\"webrtc_log.gz\"", |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 ASSERT_TRUE(StoreLog(kLogId)); | 400 ASSERT_TRUE(StoreLog(kLogId)); |
| 387 | 401 |
| 388 ScopedOverrideUploadBuffer buffer_override; | 402 ScopedOverrideUploadBuffer buffer_override; |
| 389 EXPECT_TRUE(UploadStoredLog(kLogId)); | 403 EXPECT_TRUE(UploadStoredLog(kLogId)); |
| 390 EXPECT_NE(std::string::npos, | 404 EXPECT_NE(std::string::npos, |
| 391 buffer_override.multipart().find("filename=\"webrtc_log.gz\"")); | 405 buffer_override.multipart().find("filename=\"webrtc_log.gz\"")); |
| 392 EXPECT_NE(std::string::npos, | 406 EXPECT_NE(std::string::npos, |
| 393 buffer_override.multipart().find(kTestLoggingUrl)); | 407 buffer_override.multipart().find(kTestLoggingUrl)); |
| 394 } | 408 } |
| 395 | 409 |
| OLD | NEW |