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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include <memory>
#include <optional>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/shill_client_unittest_base.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/values_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using testing::_;
using testing::ByRef;
namespace ash {
namespace {
const char kExampleDevicePath[] = "/foo/bar";
// Expects the reader to have a string and a bool.
void ExpectStringAndBoolArguments(const std::string& expected_string,
bool expected_bool,
dbus::MessageReader* reader) {
std::string arg1;
ASSERT_TRUE(reader->PopString(&arg1));
EXPECT_EQ(expected_string, arg1);
bool arg2 = false;
ASSERT_TRUE(reader->PopBool(&arg2));
EXPECT_EQ(expected_bool, arg2);
EXPECT_FALSE(reader->HasMoreData());
}
// Expects the reader to have two strings.
void ExpectTwoStringArguments(const std::string& expected_string1,
const std::string& expected_string2,
dbus::MessageReader* reader) {
std::string arg1;
ASSERT_TRUE(reader->PopString(&arg1));
EXPECT_EQ(expected_string1, arg1);
std::string arg2;
ASSERT_TRUE(reader->PopString(&arg2));
EXPECT_EQ(expected_string2, arg2);
EXPECT_FALSE(reader->HasMoreData());
}
} // namespace
class ShillDeviceClientTest : public ShillClientUnittestBase {
public:
ShillDeviceClientTest()
: ShillClientUnittestBase(shill::kFlimflamDeviceInterface,
dbus::ObjectPath(kExampleDevicePath)) {}
void SetUp() override {
ShillClientUnittestBase::SetUp();
// Create a client with the mock bus.
ShillDeviceClient::Initialize(mock_bus_.get());
client_ = ShillDeviceClient::Get();
// Run the message loop to run the signal connection result callback.
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
ShillDeviceClient::Shutdown();
ShillClientUnittestBase::TearDown();
}
protected:
raw_ptr<ShillDeviceClient, DanglingUntriaged> client_ =
nullptr; // Unowned convenience pointer.
};
TEST_F(ShillDeviceClientTest, PropertyChanged) {
const bool kValue = true;
// Create a signal.
dbus::Signal signal(shill::kFlimflamDeviceInterface,
shill::kMonitorPropertyChanged);
dbus::MessageWriter writer(&signal);
writer.AppendString(shill::kCellularPolicyAllowRoamingProperty);
writer.AppendVariantOfBool(kValue);
// Set expectations.
const base::Value value(kValue);
MockPropertyChangeObserver observer;
EXPECT_CALL(observer,
OnPropertyChanged(shill::kCellularPolicyAllowRoamingProperty,
ValueEq(ByRef(value))))
.Times(1);
// Add the observer
client_->AddPropertyChangedObserver(dbus::ObjectPath(kExampleDevicePath),
&observer);
// Run the signal callback.
SendPropertyChangedSignal(&signal);
// Remove the observer.
client_->RemovePropertyChangedObserver(dbus::ObjectPath(kExampleDevicePath),
&observer);
EXPECT_CALL(observer,
OnPropertyChanged(shill::kCellularPolicyAllowRoamingProperty,
ValueEq(ByRef(value))))
.Times(0);
// Run the signal callback again and make sure the observer isn't called.
SendPropertyChangedSignal(&signal);
}
TEST_F(ShillDeviceClientTest, GetProperties) {
const bool kValue = true;
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
dbus::MessageWriter writer(response.get());
dbus::MessageWriter array_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
dbus::MessageWriter entry_writer(NULL);
array_writer.OpenDictEntry(&entry_writer);
entry_writer.AppendString(shill::kCellularPolicyAllowRoamingProperty);
entry_writer.AppendVariantOfBool(kValue);
array_writer.CloseContainer(&entry_writer);
writer.CloseContainer(&array_writer);
// Set expectations.
base::Value::Dict expected_value;
expected_value.Set(shill::kCellularPolicyAllowRoamingProperty, kValue);
PrepareForMethodCall(shill::kGetPropertiesFunction,
base::BindRepeating(&ExpectNoArgument), response.get());
// Call GetProperties.
base::test::TestFuture<std::optional<base::Value::Dict>>
properties_result_future;
client_->GetProperties(dbus::ObjectPath(kExampleDevicePath),
properties_result_future.GetCallback());
std::optional<base::Value::Dict> result = properties_result_future.Take();
EXPECT_TRUE(result.has_value());
const base::Value::Dict& result_value = result.value();
EXPECT_EQ(expected_value, result_value);
}
TEST_F(ShillDeviceClientTest, SetProperty) {
const bool kValue = true;
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
const base::Value value(kValue);
PrepareForMethodCall(
shill::kSetPropertyFunction,
base::BindRepeating(&ExpectStringAndValueArguments,
shill::kCellularPolicyAllowRoamingProperty, &value),
response.get());
// Call SetProperty.
base::test::TestFuture<void> set_property_result;
base::test::TestFuture<std::string, std::string> error_result;
client_->SetProperty(
dbus::ObjectPath(kExampleDevicePath),
shill::kCellularPolicyAllowRoamingProperty, value,
set_property_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(set_property_result.Wait());
// The SetProperty() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, ClearProperty) {
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
PrepareForMethodCall(
shill::kClearPropertyFunction,
base::BindRepeating(&ExpectStringArgument,
shill::kCellularPolicyAllowRoamingProperty),
response.get());
// Call ClearProperty.
base::test::TestFuture<bool> clear_property_result;
client_->ClearProperty(dbus::ObjectPath(kExampleDevicePath),
shill::kCellularPolicyAllowRoamingProperty,
clear_property_result.GetCallback());
EXPECT_TRUE(clear_property_result.Get());
}
TEST_F(ShillDeviceClientTest, RequirePin) {
const char kPin[] = "123456";
const bool kRequired = true;
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
PrepareForMethodCall(
shill::kRequirePinFunction,
base::BindRepeating(&ExpectStringAndBoolArguments, kPin, kRequired),
response.get());
base::test::TestFuture<void> require_pin_result;
base::test::TestFuture<std::string, std::string> error_result;
// Call RequirePin.
client_->RequirePin(
dbus::ObjectPath(kExampleDevicePath), kPin, kRequired,
require_pin_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(require_pin_result.Wait());
// The RequirePin() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, EnterPin) {
const char kPin[] = "123456";
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
base::test::TestFuture<void> enter_pin_result;
base::test::TestFuture<std::string, std::string> error_result;
PrepareForMethodCall(shill::kEnterPinFunction,
base::BindRepeating(&ExpectStringArgument, kPin),
response.get());
// Call EnterPin.
client_->EnterPin(
dbus::ObjectPath(kExampleDevicePath), kPin,
enter_pin_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(enter_pin_result.Wait());
// The EnterPin() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, UnblockPin) {
const char kPuk[] = "987654";
const char kPin[] = "123456";
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
base::test::TestFuture<void> unblock_pin_result;
base::test::TestFuture<std::string, std::string> error_result;
PrepareForMethodCall(
shill::kUnblockPinFunction,
base::BindRepeating(&ExpectTwoStringArguments, kPuk, kPin),
response.get());
// Call UnblockPin.
client_->UnblockPin(
dbus::ObjectPath(kExampleDevicePath), kPuk, kPin,
unblock_pin_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(unblock_pin_result.Wait());
// The UnblockPin() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, ChangePin) {
const char kOldPin[] = "123456";
const char kNewPin[] = "234567";
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
PrepareForMethodCall(
shill::kChangePinFunction,
base::BindRepeating(&ExpectTwoStringArguments, kOldPin, kNewPin),
response.get());
base::test::TestFuture<void> change_pin_result;
base::test::TestFuture<std::string, std::string> error_result;
// Call ChangePin.
client_->ChangePin(
dbus::ObjectPath(kExampleDevicePath), kOldPin, kNewPin,
change_pin_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(change_pin_result.Wait());
// The ChangePin() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, Register) {
const char kNetworkId[] = "networkid";
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
PrepareForMethodCall(shill::kRegisterFunction,
base::BindRepeating(&ExpectStringArgument, kNetworkId),
response.get());
base::test::TestFuture<void> register_result;
base::test::TestFuture<std::string, std::string> error_result;
// Call Register.
client_->Register(
dbus::ObjectPath(kExampleDevicePath), kNetworkId,
register_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(register_result.Wait());
// The Register() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
TEST_F(ShillDeviceClientTest, Reset) {
// Create response.
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
// Set expectations.
PrepareForMethodCall(shill::kResetFunction,
base::BindRepeating(&ExpectNoArgument), response.get());
base::test::TestFuture<void> reset_result;
base::test::TestFuture<std::string, std::string> error_result;
// Call Reset.
client_->Reset(
dbus::ObjectPath(kExampleDevicePath), reset_result.GetCallback(),
error_result.GetCallback<const std::string&, const std::string&>());
EXPECT_TRUE(reset_result.Wait());
// The Reset() error callback should not be invoked after successful
// completion.
EXPECT_FALSE(error_result.IsReady());
}
} // namespace ash
|