1 // Copyright 2014 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 <brillo/dbus/dbus_param_writer.h>
6
7 #include <string>
8
9 #include <brillo/any.h>
10 #include <gtest/gtest.h>
11
12 using dbus::MessageReader;
13 using dbus::MessageWriter;
14 using dbus::ObjectPath;
15 using dbus::Response;
16
17 namespace brillo {
18 namespace dbus_utils {
19
TEST(DBusParamWriter,Append_NoArgs)20 TEST(DBusParamWriter, Append_NoArgs) {
21 std::unique_ptr<Response> message = Response::CreateEmpty();
22 MessageWriter writer(message.get());
23 DBusParamWriter::Append(&writer);
24 EXPECT_EQ("", message->GetSignature());
25 }
26
TEST(DBusParamWriter,Append_OneArg)27 TEST(DBusParamWriter, Append_OneArg) {
28 std::unique_ptr<Response> message = Response::CreateEmpty();
29 MessageWriter writer(message.get());
30 DBusParamWriter::Append(&writer, int32_t{2});
31 EXPECT_EQ("i", message->GetSignature());
32 DBusParamWriter::Append(&writer, std::string{"foo"});
33 EXPECT_EQ("is", message->GetSignature());
34 DBusParamWriter::Append(&writer, ObjectPath{"/o"});
35 EXPECT_EQ("iso", message->GetSignature());
36
37 int32_t int_value = 0;
38 std::string string_value;
39 ObjectPath path_value;
40
41 MessageReader reader(message.get());
42 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
43 EXPECT_TRUE(PopValueFromReader(&reader, &string_value));
44 EXPECT_TRUE(PopValueFromReader(&reader, &path_value));
45
46 EXPECT_EQ(2, int_value);
47 EXPECT_EQ("foo", string_value);
48 EXPECT_EQ(ObjectPath{"/o"}, path_value);
49 }
50
TEST(DBusParamWriter,Append_ManyArgs)51 TEST(DBusParamWriter, Append_ManyArgs) {
52 std::unique_ptr<Response> message = Response::CreateEmpty();
53 MessageWriter writer(message.get());
54 DBusParamWriter::Append(&writer, int32_t{9}, Any{7.5}, true);
55 EXPECT_EQ("ivb", message->GetSignature());
56
57 int32_t int_value = 0;
58 Any variant_value;
59 bool bool_value = false;
60
61 MessageReader reader(message.get());
62 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
63 EXPECT_TRUE(PopValueFromReader(&reader, &variant_value));
64 EXPECT_TRUE(PopValueFromReader(&reader, &bool_value));
65
66 EXPECT_EQ(9, int_value);
67 EXPECT_DOUBLE_EQ(7.5, variant_value.Get<double>());
68 EXPECT_TRUE(bool_value);
69 }
70
TEST(DBusParamWriter,AppendDBusOutParams_NoArgs)71 TEST(DBusParamWriter, AppendDBusOutParams_NoArgs) {
72 std::unique_ptr<Response> message = Response::CreateEmpty();
73 MessageWriter writer(message.get());
74 DBusParamWriter::AppendDBusOutParams(&writer);
75 EXPECT_EQ("", message->GetSignature());
76 }
77
TEST(DBusParamWriter,AppendDBusOutParams_OneArg)78 TEST(DBusParamWriter, AppendDBusOutParams_OneArg) {
79 std::unique_ptr<Response> message = Response::CreateEmpty();
80 MessageWriter writer(message.get());
81 int32_t int_value_in{5};
82 std::string string_value_in{"bar"};
83 ObjectPath path_value_in{"/obj/path"};
84
85 DBusParamWriter::AppendDBusOutParams(&writer, &int_value_in);
86 EXPECT_EQ("i", message->GetSignature());
87 DBusParamWriter::AppendDBusOutParams(&writer, &string_value_in);
88 EXPECT_EQ("is", message->GetSignature());
89 DBusParamWriter::AppendDBusOutParams(&writer, &path_value_in);
90 EXPECT_EQ("iso", message->GetSignature());
91
92 int32_t int_value = 0;
93 std::string string_value;
94 ObjectPath path_value;
95
96 MessageReader reader(message.get());
97 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
98 EXPECT_TRUE(PopValueFromReader(&reader, &string_value));
99 EXPECT_TRUE(PopValueFromReader(&reader, &path_value));
100
101 EXPECT_EQ(5, int_value);
102 EXPECT_EQ("bar", string_value);
103 EXPECT_EQ(ObjectPath{"/obj/path"}, path_value);
104 }
105
TEST(DBusParamWriter,AppendDBusOutParams_ManyArgs)106 TEST(DBusParamWriter, AppendDBusOutParams_ManyArgs) {
107 std::unique_ptr<Response> message = Response::CreateEmpty();
108 MessageWriter writer(message.get());
109 int32_t int_value_in{8};
110 Any variant_value_in{8.5};
111 bool bool_value_in{true};
112 DBusParamWriter::AppendDBusOutParams(
113 &writer, &int_value_in, &variant_value_in, &bool_value_in);
114 EXPECT_EQ("ivb", message->GetSignature());
115
116 int32_t int_value = 0;
117 Any variant_value;
118 bool bool_value = false;
119
120 MessageReader reader(message.get());
121 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
122 EXPECT_TRUE(PopValueFromReader(&reader, &variant_value));
123 EXPECT_TRUE(PopValueFromReader(&reader, &bool_value));
124
125 EXPECT_EQ(8, int_value);
126 EXPECT_DOUBLE_EQ(8.5, variant_value.Get<double>());
127 EXPECT_TRUE(bool_value);
128 }
129
TEST(DBusParamWriter,AppendDBusOutParams_Mixed_NoArgs)130 TEST(DBusParamWriter, AppendDBusOutParams_Mixed_NoArgs) {
131 std::unique_ptr<Response> message = Response::CreateEmpty();
132 MessageWriter writer(message.get());
133 DBusParamWriter::AppendDBusOutParams(&writer, 3, 5);
134 EXPECT_EQ("", message->GetSignature());
135 }
136
TEST(DBusParamWriter,AppendDBusOutParams_Mixed_OneArg)137 TEST(DBusParamWriter, AppendDBusOutParams_Mixed_OneArg) {
138 std::unique_ptr<Response> message = Response::CreateEmpty();
139 MessageWriter writer(message.get());
140 int32_t int_value_in{5};
141 std::string str_value_in{"bar"};
142 ObjectPath path_value_in{"/obj"};
143
144 DBusParamWriter::AppendDBusOutParams(&writer, 2, &int_value_in);
145 EXPECT_EQ("i", message->GetSignature());
146 DBusParamWriter::AppendDBusOutParams(&writer, &str_value_in, 0);
147 EXPECT_EQ("is", message->GetSignature());
148 DBusParamWriter::AppendDBusOutParams(&writer, 1, &path_value_in, 2);
149 EXPECT_EQ("iso", message->GetSignature());
150
151 int32_t int_value = 0;
152 std::string string_value;
153 ObjectPath path_value;
154
155 MessageReader reader(message.get());
156 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
157 EXPECT_TRUE(PopValueFromReader(&reader, &string_value));
158 EXPECT_TRUE(PopValueFromReader(&reader, &path_value));
159
160 EXPECT_EQ(5, int_value);
161 EXPECT_EQ("bar", string_value);
162 EXPECT_EQ(ObjectPath{"/obj"}, path_value);
163 }
164
TEST(DBusParamWriter,AppendDBusOutParams_Mixed_ManyArgs)165 TEST(DBusParamWriter, AppendDBusOutParams_Mixed_ManyArgs) {
166 std::unique_ptr<Response> message = Response::CreateEmpty();
167 MessageWriter writer(message.get());
168 int32_t int_value_in{8};
169 Any variant_value_in{7.5};
170 bool bool_value_in{true};
171 DBusParamWriter::AppendDBusOutParams(
172 &writer, 0, &int_value_in, 1, &variant_value_in, 2, &bool_value_in, 3);
173 EXPECT_EQ("ivb", message->GetSignature());
174
175 int32_t int_value = 0;
176 Any variant_value;
177 bool bool_value = false;
178
179 MessageReader reader(message.get());
180 EXPECT_TRUE(PopValueFromReader(&reader, &int_value));
181 EXPECT_TRUE(PopValueFromReader(&reader, &variant_value));
182 EXPECT_TRUE(PopValueFromReader(&reader, &bool_value));
183
184 EXPECT_EQ(8, int_value);
185 EXPECT_DOUBLE_EQ(7.5, variant_value.Get<double>());
186 EXPECT_TRUE(bool_value);
187 }
188 } // namespace dbus_utils
189 } // namespace brillo
190