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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/dbus/chrome_features_service_provider.h"
#include "base/feature_list.h"
#include "base/test/scoped_feature_list.h"
#include "dbus/message.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
void ResponseSenderCallback(const std::string& expected_message,
std::unique_ptr<dbus::Response> response) {
EXPECT_EQ(expected_message, response->ToString());
}
} // namespace
class ChromeFeaturesServiceProviderTest : public testing::Test {
protected:
void GetFeatureParams(dbus::MethodCall* method_call, std::string expected) {
provider_->GetFeatureParams(
method_call, base::BindOnce(&ResponseSenderCallback, expected));
}
void IsFeatureEnabled(dbus::MethodCall* method_call, std::string expected) {
provider_->IsFeatureEnabled(
method_call, base::BindOnce(&ResponseSenderCallback, expected));
}
std::unique_ptr<ChromeFeaturesServiceProvider> provider_;
};
TEST_F(ChromeFeaturesServiceProviderTest, IsFeatureEnabled_Success) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
const char enabled[] = "CrOSLateBootA";
const char disabled[] = "";
feature_list->InitFromCommandLine(enabled, disabled);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
const char kExpectedMessage[] =
R"--(message_type: MESSAGE_METHOD_RETURN
signature: b
reply_serial: 123
bool true
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
writer.AppendString("CrOSLateBootA");
// Not setting the serial causes a crash.
method_call.SetSerial(123);
IsFeatureEnabled(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, IsFeatureEnabled_UnknownFeature) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
const char enabled[] = "CrOSLateBootA";
const char disabled[] = "";
feature_list->InitFromCommandLine(enabled, disabled);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
const char kExpectedMessage[] =
R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Chrome can't get state for 'CrOSLateBootB'; feature_library will decide"
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
writer.AppendString("CrOSLateBootB");
// Not setting the serial causes a crash.
method_call.SetSerial(123);
IsFeatureEnabled(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, IsFeatureEnabled_InvalidPrefix) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
const char enabled[] = "CrOSLateBootA";
const char disabled[] = "";
feature_list->InitFromCommandLine(enabled, disabled);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
const char kExpectedMessage[] =
R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Invalid prefix for feature name: 'B'. Want CrOSLateBoot"
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
writer.AppendString("B");
// Not setting the serial causes a crash.
method_call.SetSerial(123);
IsFeatureEnabled(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, IsFeatureEnabled_InvalidInput) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
const char enabled[] = "";
const char disabled[] = "";
feature_list->InitFromCommandLine(enabled, disabled);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
const char kExpectedMessage[] =
R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Missing or invalid feature_name string arg."
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
writer.AppendBool(true);
// Not setting the serial causes a crash.
method_call.SetSerial(123);
IsFeatureEnabled(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, GetFeatureParams_Success) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
const char enabled[] = "CrOSLateBootA:key1/value1/key2/value2,CrOSLateBootB";
const char disabled[] = "CrOSLateBootC";
feature_list->InitFromCommandLine(enabled, disabled);
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
const char kExpectedMessage[] =
R"--(message_type: MESSAGE_METHOD_RETURN
signature: a{s(bba{ss})}
reply_serial: 123
array [
dict entry {
string "CrOSLateBootA"
struct {
bool true
bool true
array [
dict entry {
string "key1"
string "value1"
}
dict entry {
string "key2"
string "value2"
}
]
}
}
dict entry {
string "CrOSLateBootB"
struct {
bool true
bool true
array [
]
}
}
dict entry {
string "CrOSLateBootC"
struct {
bool true
bool false
array [
]
}
}
dict entry {
string "CrOSLateBootD"
struct {
bool false
bool false
array [
]
}
}
]
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("s", &array_writer);
array_writer.AppendString("CrOSLateBootA");
array_writer.AppendString("CrOSLateBootB");
array_writer.AppendString("CrOSLateBootC");
array_writer.AppendString("CrOSLateBootD");
writer.CloseContainer(&array_writer);
// Not setting the serial causes a crash.
method_call.SetSerial(123);
GetFeatureParams(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, GetFeatureParams_NoInput) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
feature_list->InitFromCommandLine("", "");
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
constexpr char kExpectedMessage[] = R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Could not pop string array of feature names"
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
// Not setting the serial causes a crash.
method_call.SetSerial(123);
GetFeatureParams(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, GetFeatureParams_BadInput) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
feature_list->InitFromCommandLine("", "");
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
constexpr char kExpectedMessage[] = R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Could not pop string array of feature names"
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
writer.AppendString("CrOSLateBootA"); // not in an array!
// Not setting the serial causes a crash.
method_call.SetSerial(123);
GetFeatureParams(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, GetFeatureParams_BadArrayEntry) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
feature_list->InitFromCommandLine("", "");
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
constexpr char kExpectedMessage[] = R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Missing or invalid feature_name string arg in array."
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("b", &array_writer);
array_writer.AppendBool(true); // wrong type
writer.CloseContainer(&array_writer);
// Not setting the serial causes a crash.
method_call.SetSerial(123);
GetFeatureParams(&method_call, kExpectedMessage);
}
TEST_F(ChromeFeaturesServiceProviderTest, GetFeatureParams_BadNameFormat) {
auto feature_list = std::make_unique<base::FeatureList>();
auto feature_list_accessor = feature_list->ConstructAccessor();
feature_list->InitFromCommandLine("", "");
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatureList(std::move(feature_list));
provider_ = std::make_unique<ChromeFeaturesServiceProvider>(
std::move(feature_list_accessor));
constexpr char kExpectedMessage[] = R"--(message_type: MESSAGE_ERROR
error_name: org.freedesktop.DBus.Error.InvalidArgs
signature: s
reply_serial: 123
string "Invalid prefix for feature name: 'B'. Want CrOSLateBoot"
)--";
dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
dbus::MessageWriter writer(&method_call);
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("s", &array_writer);
array_writer.AppendString("CrOSLateBootA");
array_writer.AppendString("B"); // missing prefix!
writer.CloseContainer(&array_writer);
// Not setting the serial causes a crash.
method_call.SetSerial(123);
GetFeatureParams(&method_call, kExpectedMessage);
}
} // namespace ash
|