| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/feedback/feedback_common.h" | 5 #include "components/feedback/feedback_common.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "components/feedback/proto/common.pb.h" | 8 #include "components/feedback/proto/common.pb.h" |
| 9 #include "components/feedback/proto/dom.pb.h" | 9 #include "components/feedback/proto/dom.pb.h" |
| 10 #include "components/feedback/proto/extension.pb.h" | 10 #include "components/feedback/proto/extension.pb.h" |
| 11 #include "components/feedback/proto/math.pb.h" | 11 #include "components/feedback/proto/math.pb.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/zlib/google/zip_reader.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const char kOne[] = "one"; | 17 const char kOne[] = "one"; |
| 17 const char kTwo[] = "two"; | 18 const char kTwo[] = "two"; |
| 18 const char kThree[] = "three"; | 19 const char kThree[] = "three"; |
| 19 const char kFour[] = "four"; | 20 const char kFour[] = "four"; |
| 20 #define TEN_LINES "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" | 21 #define TEN_LINES "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" |
| 21 const char kLongLog[] = TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES; | 22 const char kLongLog[] = TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES; |
| 22 const char kLogsAttachmentName[] = "system_logs.zip"; | 23 const char kLogsAttachmentName[] = "system_logs.zip"; |
| 24 const char kLogsFilename[] = "system_logs.txt"; |
| 23 } // namespace | 25 } // namespace |
| 24 | 26 |
| 25 class FeedbackCommonTest : public testing::Test { | 27 class FeedbackCommonTest : public testing::Test { |
| 26 protected: | 28 protected: |
| 27 FeedbackCommonTest() { | 29 FeedbackCommonTest() { |
| 28 feedback = scoped_refptr<FeedbackCommon>(new FeedbackCommon()); | 30 feedback_ = scoped_refptr<FeedbackCommon>(new FeedbackCommon()); |
| 29 } | 31 } |
| 30 | 32 |
| 31 ~FeedbackCommonTest() override {} | 33 ~FeedbackCommonTest() override {} |
| 32 | 34 |
| 33 scoped_refptr<FeedbackCommon> feedback; | 35 scoped_refptr<FeedbackCommon> feedback_; |
| 34 userfeedback::ExtensionSubmit report; | 36 userfeedback::ExtensionSubmit report_; |
| 35 }; | 37 }; |
| 36 | 38 |
| 37 TEST_F(FeedbackCommonTest, TestBasicData) { | 39 TEST_F(FeedbackCommonTest, TestBasicData) { |
| 38 // Test that basic data can be set and propagates to the request. | 40 // Test that basic data can be set and propagates to the request. |
| 39 feedback->set_category_tag(kOne); | 41 feedback_->set_category_tag(kOne); |
| 40 feedback->set_description(kTwo); | 42 feedback_->set_description(kTwo); |
| 41 feedback->set_page_url(kThree); | 43 feedback_->set_page_url(kThree); |
| 42 feedback->set_user_email(kFour); | 44 feedback_->set_user_email(kFour); |
| 43 feedback->PrepareReport(&report); | 45 feedback_->PrepareReport(&report_); |
| 44 | 46 |
| 45 EXPECT_EQ(kOne, report.bucket()); | 47 EXPECT_EQ(kOne, report_.bucket()); |
| 46 EXPECT_EQ(kTwo, report.common_data().description()); | 48 EXPECT_EQ(kTwo, report_.common_data().description()); |
| 47 EXPECT_EQ(kThree, report.web_data().url()); | 49 EXPECT_EQ(kThree, report_.web_data().url()); |
| 48 EXPECT_EQ(kFour, report.common_data().user_email()); | 50 EXPECT_EQ(kFour, report_.common_data().user_email()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 TEST_F(FeedbackCommonTest, TestAddLogs) { | 53 TEST_F(FeedbackCommonTest, TestAddLogs) { |
| 52 feedback->AddLog(kOne, kTwo); | 54 feedback_->AddLog(kOne, kTwo); |
| 53 feedback->AddLog(kThree, kFour); | 55 feedback_->AddLog(kThree, kFour); |
| 54 | 56 |
| 55 EXPECT_EQ(2U, feedback->sys_info()->size()); | 57 EXPECT_EQ(2U, feedback_->sys_info()->size()); |
| 56 } | 58 } |
| 57 | 59 |
| 58 TEST_F(FeedbackCommonTest, TestCompressionThreshold) { | 60 TEST_F(FeedbackCommonTest, TestCompressionThreshold) { |
| 59 // Add a large and small log, verify that only the small log gets | 61 // Add a large and small log, verify that only the small log gets |
| 60 // included in the report. | 62 // included in the report. |
| 61 feedback->AddLog(kOne, kTwo); | 63 feedback_->AddLog(kOne, kTwo); |
| 62 feedback->AddLog(kThree, kLongLog); | 64 feedback_->AddLog(kThree, kLongLog); |
| 63 feedback->PrepareReport(&report); | 65 feedback_->PrepareReport(&report_); |
| 64 | 66 |
| 65 EXPECT_EQ(1, report.web_data().product_specific_data_size()); | 67 EXPECT_EQ(1, report_.web_data().product_specific_data_size()); |
| 66 EXPECT_EQ(kOne, report.web_data().product_specific_data(0).key()); | 68 EXPECT_EQ(kOne, report_.web_data().product_specific_data(0).key()); |
| 67 } | 69 } |
| 68 | 70 |
| 69 TEST_F(FeedbackCommonTest, TestCompression) { | 71 TEST_F(FeedbackCommonTest, TestCompression) { |
| 70 // Add a large and small log, verify that an attachment has been | 72 // Add a large and small log, verify that an attachment has been |
| 71 // added with the right name. | 73 // added with the right name. |
| 72 feedback->AddLog(kOne, kTwo); | 74 feedback_->AddLog(kOne, kTwo); |
| 73 feedback->AddLog(kThree, kLongLog); | 75 feedback_->AddLog(kThree, kLongLog); |
| 74 feedback->CompressLogs(); | 76 feedback_->CompressLogs(); |
| 75 feedback->PrepareReport(&report); | 77 feedback_->PrepareReport(&report_); |
| 76 | 78 |
| 77 EXPECT_EQ(1, report.product_specific_binary_data_size()); | 79 EXPECT_EQ(1, report_.product_specific_binary_data_size()); |
| 78 EXPECT_EQ(kLogsAttachmentName, report.product_specific_binary_data(0).name()); | 80 EXPECT_EQ(kLogsAttachmentName, |
| 81 report_.product_specific_binary_data(0).name()); |
| 79 } | 82 } |
| 83 |
| 84 TEST_F(FeedbackCommonTest, TestAddLogsAnonymization) { |
| 85 std::string mac_address = "Some text 00:11:22:33:44:55 text"; |
| 86 std::string anonymized_mac_address = "Some text 00:11:22:00:00:01 text"; |
| 87 feedback_->AddLog(kOne, mac_address); |
| 88 feedback_->AddLog(kThree, mac_address + kLongLog); |
| 89 feedback_->CompressLogs(); |
| 90 feedback_->PrepareReport(&report_); |
| 91 |
| 92 // Log entry kOne and kThree are treated differently because of their size. |
| 93 // kOne is stored uncompressed whereas kThree is large enough to be exported |
| 94 // into a compressed file. Therefore, both need to be tested separately. |
| 95 |
| 96 // Verify that uncompressed data is anonymized. |
| 97 ASSERT_EQ(1, report_.web_data().product_specific_data_size()); |
| 98 EXPECT_EQ(anonymized_mac_address, |
| 99 report_.web_data().product_specific_data(0).value()); |
| 100 |
| 101 // Verify that compressed data is anonymized. |
| 102 ASSERT_EQ(1, report_.product_specific_binary_data_size()); |
| 103 const ::userfeedback::ProductSpecificBinaryData& binary_data = |
| 104 report_.product_specific_binary_data(0); |
| 105 |
| 106 zip::ZipReader reader; |
| 107 std::string content; |
| 108 ASSERT_TRUE(reader.OpenFromString(binary_data.data())); |
| 109 ASSERT_TRUE(reader.LocateAndOpenEntry(base::FilePath(kLogsFilename))); |
| 110 ASSERT_TRUE(reader.ExtractCurrentEntryToString(1000u, &content)); |
| 111 |
| 112 EXPECT_TRUE(content.find(anonymized_mac_address) != std::string::npos); |
| 113 EXPECT_TRUE(content.find(mac_address) == std::string::npos); |
| 114 } |
| OLD | NEW |