[M120 Migration][VD] Fix url crash in RequestCertificateConfirm
[platform/framework/web/chromium-efl.git] / dbus / end_to_end_sync_unittest.cc
1 // Copyright 2012 The Chromium Authors
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
7 #include "base/memory/raw_ptr.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/run_loop.h"
10 #include "base/test/task_environment.h"
11 #include "dbus/bus.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "dbus/test_service.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace dbus {
19
20 // The end-to-end test exercises the synchronous APIs in ObjectProxy and
21 // ExportedObject. The test will launch a thread for the service side
22 // operations (i.e. ExportedObject side).
23 class EndToEndSyncTest : public testing::Test {
24  public:
25   EndToEndSyncTest() = default;
26
27   void SetUp() override {
28     // Start the test service;
29     TestService::Options options;
30     test_service_ = std::make_unique<TestService>(options);
31     ASSERT_TRUE(test_service_->StartService());
32     test_service_->WaitUntilServiceIsStarted();
33     ASSERT_FALSE(test_service_->HasDBusThread());
34
35     // Create the client.
36     Bus::Options client_bus_options;
37     client_bus_options.bus_type = Bus::SESSION;
38     client_bus_options.connection_type = Bus::PRIVATE;
39     client_bus_ = new Bus(client_bus_options);
40     object_proxy_ = client_bus_->GetObjectProxy(
41         test_service_->service_name(),
42         ObjectPath("/org/chromium/TestObject"));
43     ASSERT_FALSE(client_bus_->HasDBusThread());
44   }
45
46   void TearDown() override {
47     test_service_->ShutdownAndBlock();
48     test_service_->Stop();
49     client_bus_->ShutdownAndBlock();
50   }
51
52  protected:
53   base::test::SingleThreadTaskEnvironment task_environment_{
54       base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
55   std::unique_ptr<TestService> test_service_;
56   scoped_refptr<Bus> client_bus_;
57   raw_ptr<ObjectProxy, DanglingUntriaged> object_proxy_;
58 };
59
60 TEST_F(EndToEndSyncTest, Echo) {
61   const std::string kHello = "hello";
62
63   // Create the method call.
64   MethodCall method_call("org.chromium.TestInterface", "Echo");
65   MessageWriter writer(&method_call);
66   writer.AppendString(kHello);
67
68   // Call the method.
69   const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
70   auto result = object_proxy_->CallMethodAndBlock(&method_call, timeout_ms);
71   ASSERT_TRUE(result.has_value());
72
73   // Check the response. kHello should be echoed back.
74   MessageReader reader(result->get());
75   std::string returned_message;
76   ASSERT_TRUE(reader.PopString(&returned_message));
77   EXPECT_EQ(kHello, returned_message);
78 }
79
80 TEST_F(EndToEndSyncTest, Timeout) {
81   const std::string kHello = "hello";
82
83   // Create the method call.
84   MethodCall method_call("org.chromium.TestInterface", "DelayedEcho");
85   MessageWriter writer(&method_call);
86   writer.AppendString(kHello);
87
88   // Call the method with timeout of 0ms.
89   const int timeout_ms = 0;
90   auto result = object_proxy_->CallMethodAndBlock(&method_call, timeout_ms);
91   // Should fail because of timeout.
92   ASSERT_FALSE(result.has_value());
93 }
94
95 TEST_F(EndToEndSyncTest, NonexistentMethod) {
96   MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
97
98   const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
99   auto result = object_proxy_->CallMethodAndBlock(&method_call, timeout_ms);
100   ASSERT_FALSE(result.has_value());
101 }
102
103 TEST_F(EndToEndSyncTest, BrokenMethod) {
104   MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
105
106   const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
107   auto result = object_proxy_->CallMethodAndBlock(&method_call, timeout_ms);
108   ASSERT_FALSE(result.has_value());
109 }
110
111 TEST_F(EndToEndSyncTest, InvalidServiceName) {
112   // Bus name cannot contain '/'.
113   const std::string invalid_service_name = ":1/2";
114
115   // Replace object proxy with new one.
116   object_proxy_ = client_bus_->GetObjectProxy(
117       invalid_service_name, ObjectPath("/org/chromium/TestObject"));
118
119   MethodCall method_call("org.chromium.TestInterface", "Echo");
120
121   const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
122   auto result = object_proxy_->CallMethodAndBlock(&method_call, timeout_ms);
123   ASSERT_FALSE(result.has_value());
124 }
125
126 TEST_F(EndToEndSyncTest, ConnectToSignalAndBlock) {
127   constexpr char kMessage[] = "hello";
128   base::RunLoop run_loop;
129   std::string test_signal_string;
130   EXPECT_TRUE(object_proxy_->ConnectToSignalAndBlock(
131       "org.chromium.TestInterface", "Test",
132       base::BindRepeating(
133           [](base::OnceClosure quit_closure, std::string* return_string,
134              Signal* signal) {
135             MessageReader reader(signal);
136             ASSERT_TRUE(reader.PopString(return_string));
137             std::move(quit_closure).Run();
138           },
139           run_loop.QuitClosure(), &test_signal_string)));
140   test_service_->SendTestSignal(kMessage);
141   run_loop.Run();
142   EXPECT_EQ(test_signal_string, kMessage);
143   // Ensure resources on the DBus thread are cleaned up.
144   task_environment_.RunUntilIdle();
145 }
146
147 }  // namespace dbus