blob: 25fcd25f86c560f1d9a13b1baba2d06edcff1894 [file] [log] [blame]
Charles Zhaod4fb7b62020-08-25 17:21:58 +10001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6#include <utility>
7
8#include <base/bind.h>
hscham4ce3c992021-02-19 16:37:23 +09009#include <base/callback_helpers.h>
Charles Zhaod4fb7b62020-08-25 17:21:58 +100010#include <base/run_loop.h>
11#include <dbus/dlcservice/dbus-constants.h>
12#include <dbus/message.h>
13#include <dbus/mock_bus.h>
14#include <dbus/mock_object_proxy.h>
15#include <dbus/object_proxy.h>
16#include <dlcservice/proto_bindings/dlcservice.pb.h>
17#include <gtest/gtest.h>
18
19#include "ml/dlcservice_client.h"
20
21namespace ml {
22
23// A random string returned as a root_path.
24constexpr char kDlcValidFakePath[] = "/valid/fake-path";
25constexpr char kRandomDlcId[] = "random-dlc-id";
26
27class DlcserviceClientTest : public ::testing::Test {
28 public:
29 void SetUp() override {
30 method_call_ = std::make_unique<dbus::MethodCall>(
31 dlcservice::kDlcServiceInterface, dlcservice::kGetDlcStateMethod);
32 method_call_->SetSerial(123);
33 err_response_ = dbus::ErrorResponse::FromMethodCall(
34 method_call_.get(), "org.ErrorName", "Random error message");
35 response_ = dbus::Response::FromMethodCall(method_call_.get());
36 response_writer_ = std::make_unique<dbus::MessageWriter>(response_.get());
37 }
38
39 // Expect correct path is extract from the `response`.
40 void ExpectRootPathFromResponse(const std::string& expected_path,
41 dbus::Response* response,
42 dbus::ErrorResponse* err_response) {
43 bool get_path_callback_done = false;
44 DlcserviceClient::OnGetDlcStateComplete(
45 base::BindOnce(
46 [](bool* const get_path_callback_done,
47 const std::string& expected_path, const std::string& root_path) {
48 *get_path_callback_done = true;
49 EXPECT_EQ(expected_path, root_path);
50 },
51 &get_path_callback_done, expected_path),
52 response, err_response);
53
54 base::RunLoop().RunUntilIdle();
55 EXPECT_TRUE(get_path_callback_done);
56 }
57
58 protected:
59 std::unique_ptr<dbus::MethodCall> method_call_;
60 std::unique_ptr<dbus::ErrorResponse> err_response_;
61 std::unique_ptr<dbus::Response> response_;
62 std::unique_ptr<dbus::MessageWriter> response_writer_;
63};
64
65// root_path_ should be empty if the response is empty.
66TEST_F(DlcserviceClientTest, ShouldReturnEmptyForNoResponse) {
67 ExpectRootPathFromResponse("", nullptr, err_response_.get());
68}
69
70// root_path_ should be empty if the response is invalid.
71TEST_F(DlcserviceClientTest, ShouldReturnEmptyOnInvalidResponse) {
72 response_writer_->AppendString("random_string");
73 ExpectRootPathFromResponse("", response_.get(), err_response_.get());
74}
75
76// root_path_ should be empty if the dlc state is not installed.
77TEST_F(DlcserviceClientTest, ShouldReturnEmptyIfNotInstalled) {
78 dlcservice::DlcState dlc_state;
79 dlc_state.set_state(dlcservice::DlcState::INSTALLING);
80 dlc_state.set_root_path(kDlcValidFakePath);
81 response_writer_->AppendProtoAsArrayOfBytes(dlc_state);
82 ExpectRootPathFromResponse("", response_.get(), err_response_.get());
83}
84
85// root_path_ should be valid if the dlc state is installed.
86TEST_F(DlcserviceClientTest, ShouldReturnRootPathIfInstalled) {
87 dlcservice::DlcState dlc_state;
88 dlc_state.set_state(dlcservice::DlcState::INSTALLED);
89 dlc_state.set_root_path(kDlcValidFakePath);
90 response_writer_->AppendProtoAsArrayOfBytes(dlc_state);
91 ExpectRootPathFromResponse(kDlcValidFakePath, response_.get(),
92 err_response_.get());
93}
94
95// Test GetDlcRootPath call is passed correctly.
96TEST_F(DlcserviceClientTest, ShouldInitializeAndCallWithCorrectDbusInterface) {
97 scoped_refptr<dbus::MockBus> bus = new dbus::MockBus(dbus::Bus::Options());
98 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy =
99 new dbus::MockObjectProxy(
100 bus.get(), dlcservice::kDlcServiceServiceName,
101 dbus::ObjectPath(dlcservice::kDlcServiceServicePath));
102
103 // Mock the GetObjectProxy for the `bus_`.
104 EXPECT_CALL(*bus, GetObjectProxy(
105 dlcservice::kDlcServiceServiceName,
106 dbus::ObjectPath(dlcservice::kDlcServiceServicePath)))
107 .WillOnce(testing::Return(mock_object_proxy.get()));
108
109 // Mock the CallMethodWithErrorResponse for the `mock_object_proxy_`.
110 EXPECT_CALL(*mock_object_proxy,
111 DoCallMethodWithErrorResponse(testing::_, testing::_, testing::_))
112 .WillOnce(testing::WithArgs<0>(
113 testing::Invoke([](dbus::MethodCall* method_call) {
114 EXPECT_EQ(method_call->GetInterface(),
115 dlcservice::kDlcServiceInterface);
116 EXPECT_EQ(method_call->GetMember(), dlcservice::kGetDlcStateMethod);
117
118 // Get dlc_id
119 std::string dlc_id;
120 EXPECT_TRUE(dbus::MessageReader(method_call).PopString(&dlc_id));
121 EXPECT_TRUE(dlc_id.find(kRandomDlcId) != std::string::npos);
122 })));
123
124 DlcserviceClient(bus.get()).GetDlcRootPath(kRandomDlcId,
125 base::NullCallback());
126 base::RunLoop().RunUntilIdle();
127}
128
129} // namespace ml