1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/feedback/feedback_common.h"
#include <cstdint>
#include <string>
#include <string_view>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "components/feedback/feedback_report.h"
#include "components/feedback/proto/common.pb.h"
#include "components/feedback/proto/dom.pb.h"
#include "components/feedback/proto/extension.pb.h"
#include "components/feedback/proto/math.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kOne[] = "one";
constexpr char kTwo[] = "two";
constexpr char kThree[] = "three";
constexpr char kFour[] = "four";
#define TEN_LINES "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n"
constexpr char kLongLog[] = TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES;
constexpr char kLogsAttachmentName[] = "system_logs.zip";
constexpr int kTestProductId = 3490;
constexpr uint8_t kPngBytes[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A,
0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
0x49, 0x48, 0x44, 0x52};
constexpr uint8_t kJpegBytes[] = {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10,
0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x00, 0x00, 0x01};
#if BUILDFLAG(IS_CHROMEOS)
constexpr int kDefaultProductId = 208; // ChromeOS default product ID.
#else
constexpr int kDefaultProductId = 237; // Chrome default product ID.
#endif // BUILDFLAG(IS_CHROMEOS)
} // namespace
class FeedbackCommonTest : public testing::Test {
protected:
FeedbackCommonTest() : feedback_(new FeedbackCommon()) {}
~FeedbackCommonTest() override = default;
void CompressLogs() { feedback_->CompressLogs(); }
bool FeedbackHasProductId() const { return feedback_->HasProductId(); }
scoped_refptr<FeedbackCommon> feedback_;
userfeedback::ExtensionSubmit report_;
};
TEST_F(FeedbackCommonTest, TestBasicData) {
// Test that basic data can be set and propagates to the request.
feedback_->set_category_tag(kOne);
feedback_->set_description(kTwo);
feedback_->set_page_url(kThree);
feedback_->set_user_email(kFour);
EXPECT_FALSE(FeedbackHasProductId());
feedback_->set_product_id(kTestProductId);
EXPECT_TRUE(FeedbackHasProductId());
feedback_->PrepareReport(&report_);
EXPECT_EQ(kOne, report_.bucket());
EXPECT_EQ(kTwo, report_.common_data().description());
EXPECT_EQ(kThree, report_.web_data().url());
EXPECT_EQ(kFour, report_.common_data().user_email());
EXPECT_EQ(kTestProductId, report_.product_id());
}
// If an feedback requester doesn't set the product ID, the report will be sent
// with the default product ID for Chrome/ChromeOS depending on the platform.
TEST_F(FeedbackCommonTest, TestDefaultProductId) {
EXPECT_FALSE(FeedbackHasProductId());
feedback_->PrepareReport(&report_);
EXPECT_EQ(kDefaultProductId, report_.product_id());
}
TEST_F(FeedbackCommonTest, TestAddLogs) {
feedback_->AddLog(kOne, kTwo);
feedback_->AddLog(kThree, kFour);
EXPECT_EQ(2U, feedback_->sys_info()->size());
}
TEST_F(FeedbackCommonTest, TestCompressionThreshold) {
// Add a large and small log, verify that only the small log gets
// included in the report.
feedback_->AddLog(kOne, kTwo);
feedback_->AddLog(kThree, kLongLog);
feedback_->PrepareReport(&report_);
EXPECT_EQ(1, report_.web_data().product_specific_data_size());
EXPECT_EQ(kOne, report_.web_data().product_specific_data(0).key());
}
TEST_F(FeedbackCommonTest, TestCompression) {
// Add a large and small log, verify that an attachment has been
// added with the right name.
feedback_->AddLog(kOne, kTwo);
feedback_->AddLog(kThree, kLongLog);
CompressLogs();
feedback_->PrepareReport(&report_);
EXPECT_EQ(1, report_.product_specific_binary_data_size());
EXPECT_EQ(kLogsAttachmentName,
report_.product_specific_binary_data(0).name());
}
TEST_F(FeedbackCommonTest, TestAllCrashIdsRemoval) {
feedback_->AddLog(feedback::FeedbackReport::kAllCrashReportIdsKey, kOne);
feedback_->set_user_email("nobody@example.com");
feedback_->PrepareReport(&report_);
EXPECT_EQ(0, report_.web_data().product_specific_data_size());
}
TEST_F(FeedbackCommonTest, TestAllCrashIdsRetention) {
feedback_->AddLog(feedback::FeedbackReport::kAllCrashReportIdsKey, kOne);
feedback_->set_user_email("nobody@google.com");
feedback_->PrepareReport(&report_);
EXPECT_EQ(1, report_.web_data().product_specific_data_size());
}
TEST_F(FeedbackCommonTest, IncludeInSystemLogs) {
bool google_email = true;
EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(kOne, google_email));
EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(
feedback::FeedbackReport::kAllCrashReportIdsKey, google_email));
google_email = false;
EXPECT_TRUE(FeedbackCommon::IncludeInSystemLogs(kOne, google_email));
EXPECT_FALSE(FeedbackCommon::IncludeInSystemLogs(
feedback::FeedbackReport::kAllCrashReportIdsKey, google_email));
}
TEST_F(FeedbackCommonTest, IsOffensiveOrUnsafe) {
feedback_->set_is_offensive_or_unsafe(true);
feedback_->PrepareReport(&report_);
EXPECT_EQ(1, report_.web_data().product_specific_data_size());
EXPECT_EQ("is_offensive_or_unsafe",
report_.web_data().product_specific_data(0).key());
EXPECT_EQ("true", report_.web_data().product_specific_data(0).value());
}
TEST_F(FeedbackCommonTest, AiMetadata) {
feedback_->set_ai_metadata("{\"log_id\":\"TEST_ID\"}");
feedback_->PrepareReport(&report_);
EXPECT_EQ(1, report_.web_data().product_specific_data_size());
EXPECT_EQ("log_id", report_.web_data().product_specific_data(0).key());
EXPECT_EQ("TEST_ID", report_.web_data().product_specific_data(0).value());
}
TEST_F(FeedbackCommonTest, ImageMimeTypeNoImage) {
feedback_->PrepareReport(&report_);
EXPECT_FALSE(report_.screenshot().has_mime_type());
}
TEST_F(FeedbackCommonTest, ImageMimeTypeNotSpecified) {
feedback_->set_image(std::string(base::as_string_view(kPngBytes)));
feedback_->PrepareReport(&report_);
EXPECT_EQ(report_.screenshot().mime_type(), "image/png");
}
TEST_F(FeedbackCommonTest, ImageMimeTypeSpecified) {
constexpr std::string_view kMimeType = "image/jpeg";
feedback_->set_image(std::string(base::as_string_view(kJpegBytes)));
feedback_->set_image_mime_type(std::string(kMimeType));
feedback_->PrepareReport(&report_);
EXPECT_EQ(report_.screenshot().mime_type(), kMimeType);
}
|