Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: dbus/message_unittest.cc

Issue 7492029: Implement classes used for manipulating D-Bus messages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix all.gyp Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dbus/message.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium 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 "base/logging.h"
6 #include "dbus/message.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 // Test that a byte can be properly written and read. We only have this
10 // test for byte, as repeating this for other basic types is too redundant.
11 TEST(MessageTest, AppendAndPopByte) {
12 dbus::Message message;
13 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
14 dbus::MessageWriter writer(&message);
15 writer.AppendByte(123); // The input is 123.
16
17 dbus::MessageReader reader(&message);
18 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read.
19 ASSERT_EQ(dbus::Message::BYTE, reader.GetDataType());
20
21 bool bool_value = false;
22 // Should fail as the type is not bool here.
23 ASSERT_FALSE(reader.PopBool(&bool_value));
24
25 uint8 byte_value = 0;
26 ASSERT_TRUE(reader.PopByte(&byte_value));
27 EXPECT_EQ(123, byte_value); // Should match with the input.
28 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
29
30 // Try to get another byte. Should fail.
31 ASSERT_FALSE(reader.PopByte(&byte_value));
32 }
33
34 // Check all basic types can be properly written and read.
35 TEST(MessageTest, AppendAndPopBasicDataTypes) {
36 dbus::Message message;
37 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
38 dbus::MessageWriter writer(&message);
39
40 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
41 writer.AppendByte(0);
42 writer.AppendBool(true);
43 writer.AppendInt16(2);
44 writer.AppendUint16(3);
45 writer.AppendInt32(4);
46 writer.AppendUint32(5);
47 writer.AppendInt64(6);
48 writer.AppendUint64(7);
49 writer.AppendDouble(8.0);
50 writer.AppendString("string");
51 writer.AppendObjectPath("/object/path");
52
53 uint8 byte_value = 0;
54 bool bool_value = false;
55 int16 int16_value = 0;
56 uint16 uint16_value = 0;
57 int32 int32_value = 0;
58 uint32 uint32_value = 0;
59 int64 int64_value = 0;
60 uint64 uint64_value = 0;
61 double double_value = 0;
62 std::string string_value;
63 std::string object_path_value;
64
65 dbus::MessageReader reader(&message);
66 ASSERT_TRUE(reader.HasMoreData());
67 ASSERT_TRUE(reader.PopByte(&byte_value));
68 ASSERT_TRUE(reader.PopBool(&bool_value));
69 ASSERT_TRUE(reader.PopInt16(&int16_value));
70 ASSERT_TRUE(reader.PopUint16(&uint16_value));
71 ASSERT_TRUE(reader.PopInt32(&int32_value));
72 ASSERT_TRUE(reader.PopUint32(&uint32_value));
73 ASSERT_TRUE(reader.PopInt64(&int64_value));
74 ASSERT_TRUE(reader.PopUint64(&uint64_value));
75 ASSERT_TRUE(reader.PopDouble(&double_value));
76 ASSERT_TRUE(reader.PopString(&string_value));
77 ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
78 ASSERT_FALSE(reader.HasMoreData());
79
80 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
81 EXPECT_EQ(0, byte_value);
82 EXPECT_EQ(true, bool_value);
83 EXPECT_EQ(2, int16_value);
84 EXPECT_EQ(3U, uint16_value);
85 EXPECT_EQ(4, int32_value);
86 EXPECT_EQ(5U, uint32_value);
87 EXPECT_EQ(6, int64_value);
88 EXPECT_EQ(7U, uint64_value);
89 EXPECT_DOUBLE_EQ(8.0, double_value);
90 EXPECT_EQ("string", string_value);
91 EXPECT_EQ("/object/path", object_path_value);
92 }
93
94 // Check all variant types can be properly written and read.
95 TEST(MessageTest, AppendAndPopVariantDataTypes) {
96 dbus::Message message;
97 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
98 dbus::MessageWriter writer(&message);
99
100 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
101 writer.AppendVariantOfByte(0);
102 writer.AppendVariantOfBool(true);
103 writer.AppendVariantOfInt16(2);
104 writer.AppendVariantOfUint16(3);
105 writer.AppendVariantOfInt32(4);
106 writer.AppendVariantOfUint32(5);
107 writer.AppendVariantOfInt64(6);
108 writer.AppendVariantOfUint64(7);
109 writer.AppendVariantOfDouble(8.0);
110 writer.AppendVariantOfString("string");
111 writer.AppendVariantOfObjectPath("/object/path");
112
113 uint8 byte_value = 0;
114 bool bool_value = false;
115 int16 int16_value = 0;
116 uint16 uint16_value = 0;
117 int32 int32_value = 0;
118 uint32 uint32_value = 0;
119 int64 int64_value = 0;
120 uint64 uint64_value = 0;
121 double double_value = 0;
122 std::string string_value;
123 std::string object_path_value;
124
125 dbus::MessageReader reader(&message);
126 ASSERT_TRUE(reader.HasMoreData());
127 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value));
128 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value));
129 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value));
130 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value));
131 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value));
132 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value));
133 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value));
134 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value));
135 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value));
136 ASSERT_TRUE(reader.PopVariantOfString(&string_value));
137 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value));
138 ASSERT_FALSE(reader.HasMoreData());
139
140 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
141 EXPECT_EQ(0, byte_value);
142 EXPECT_EQ(true, bool_value);
143 EXPECT_EQ(2, int16_value);
144 EXPECT_EQ(3U, uint16_value);
145 EXPECT_EQ(4, int32_value);
146 EXPECT_EQ(5U, uint32_value);
147 EXPECT_EQ(6, int64_value);
148 EXPECT_EQ(7U, uint64_value);
149 EXPECT_DOUBLE_EQ(8.0, double_value);
150 EXPECT_EQ("string", string_value);
151 EXPECT_EQ("/object/path", object_path_value);
152 }
153
154 TEST(MessageTest, ArrayOfBytes) {
155 dbus::Message message;
156 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
157 dbus::MessageWriter writer(&message);
158 std::vector<uint8> bytes;
159 bytes.push_back(1);
160 bytes.push_back(2);
161 bytes.push_back(3);
162 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
163
164 dbus::MessageReader reader(&message);
165 uint8* output_bytes = NULL;
166 size_t length = 0;
167 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
168 ASSERT_FALSE(reader.HasMoreData());
169 ASSERT_EQ(3U, length);
170 ASSERT_EQ(1, output_bytes[0]);
171 ASSERT_EQ(2, output_bytes[1]);
172 ASSERT_EQ(3, output_bytes[2]);
173 }
174
175 // Test that an array can be properly written and read. We only have this
176 // test for array, as repeating this for other container types is too
177 // redundant.
178 TEST(MessageTest, OpenArrayAndPopArray) {
179 dbus::Message message;
180 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
181 dbus::MessageWriter writer(&message);
182 dbus::MessageWriter array_writer(&message);
183 writer.OpenArray("s", &array_writer); // Open an array of strings.
184 array_writer.AppendString("foo");
185 array_writer.AppendString("bar");
186 array_writer.AppendString("baz");
187 writer.CloseContainer(&array_writer);
188
189 dbus::MessageReader reader(&message);
190 ASSERT_EQ(dbus::Message::ARRAY, reader.GetDataType());
191 dbus::MessageReader array_reader(&message);
192 ASSERT_TRUE(reader.PopArray(&array_reader));
193 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
194
195 std::string string_value;
196 ASSERT_TRUE(array_reader.PopString(&string_value));
197 EXPECT_EQ("foo", string_value);
198 ASSERT_TRUE(array_reader.PopString(&string_value));
199 EXPECT_EQ("bar", string_value);
200 ASSERT_TRUE(array_reader.PopString(&string_value));
201 EXPECT_EQ("baz", string_value);
202 // Should not have more data to read.
203 ASSERT_FALSE(array_reader.HasMoreData());
204 }
205
206 // Create a complex message using array, struct, variant, dict entry, and
207 // make sure it can be read properly.
208 TEST(MessageTest, CreateComplexMessageAndReadIt) {
209 dbus::Message message;
210 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
211 dbus::MessageWriter writer(&message);
212 {
213 dbus::MessageWriter array_writer(&message);
214 // Open an array of variants.
215 writer.OpenArray("v", &array_writer);
216 {
217 // The first value in the array.
218 {
219 dbus::MessageWriter variant_writer(&message);
220 // Open a variant of a boolean.
221 array_writer.OpenVariant("b", &variant_writer);
222 variant_writer.AppendBool(true);
223 array_writer.CloseContainer(&variant_writer);
224 }
225
226 // The second value in the array.
227 {
228 dbus::MessageWriter variant_writer(&message);
229 // Open a variant of a struct that contains a string and an int32.
230 array_writer.OpenVariant("(si)", &variant_writer);
231 {
232 dbus::MessageWriter struct_writer(&message);
233 variant_writer.OpenStruct(&struct_writer);
234 struct_writer.AppendString("string");
235 struct_writer.AppendInt32(123);
236 variant_writer.CloseContainer(&struct_writer);
237 }
238 array_writer.CloseContainer(&variant_writer);
239 }
240
241 // The third value in the array.
242 {
243 dbus::MessageWriter variant_writer(&message);
244 // Open a variant of an array of string-to-int64 dict entries.
245 array_writer.OpenVariant("a{sx}", &variant_writer);
246 {
247 // Opens an array of string-to-int64 dict entries.
248 dbus::MessageWriter dict_array_writer(&message);
249 variant_writer.OpenArray("{sx}", &dict_array_writer);
250 {
251 // Opens a string-to-int64 dict entries.
252 dbus::MessageWriter dict_entry_writer(&message);
253 dict_array_writer.OpenDictEntry(&dict_entry_writer);
254 dict_entry_writer.AppendString("foo");
255 dict_entry_writer.AppendInt64(1234567890123456789);
Mark Mentovai 2011/07/30 19:41:14 Use GG_INT64_C(1234567890123456789) (base/port.h,
256 dict_array_writer.CloseContainer(&dict_entry_writer);
257 }
258 variant_writer.CloseContainer(&dict_array_writer);
259 }
260 array_writer.CloseContainer(&variant_writer);
261 }
262 }
263 writer.CloseContainer(&array_writer);
264 }
265 // What we have created looks like this:
266 EXPECT_EQ("signature: av\n"
267 "\n"
268 "array [\n"
269 " variant bool true\n"
270 " variant struct {\n"
271 " string \"string\"\n"
272 " int32 123\n"
273 " }\n"
274 " variant array [\n"
275 " dict entry {\n"
276 " string \"foo\"\n"
277 " int64 1234567890123456789\n"
278 " }\n"
279 " ]\n"
280 "]\n",
281 message.ToString());
282
283 dbus::MessageReader reader(&message);
284 dbus::MessageReader array_reader(&message);
285 ASSERT_TRUE(reader.PopArray(&array_reader));
286
287 // The first value in the array.
288 bool bool_value = false;
289 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
290 EXPECT_EQ(true, bool_value);
291
292 // The second value in the array.
293 {
294 dbus::MessageReader variant_reader(&message);
295 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
296 {
297 dbus::MessageReader struct_reader(&message);
298 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
299 std::string string_value;
300 ASSERT_TRUE(struct_reader.PopString(&string_value));
301 EXPECT_EQ("string", string_value);
302 int32 int32_value = 0;
303 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
304 EXPECT_EQ(123, int32_value);
305 ASSERT_FALSE(struct_reader.HasMoreData());
306 }
307 ASSERT_FALSE(variant_reader.HasMoreData());
308 }
309
310 // The third value in the array.
311 {
312 dbus::MessageReader variant_reader(&message);
313 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
314 {
315 dbus::MessageReader dict_array_reader(&message);
316 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
317 {
318 dbus::MessageReader dict_entry_reader(&message);
319 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
320 std::string string_value;
321 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
322 EXPECT_EQ("foo", string_value);
323 int64 int64_value = 0;
324 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
325 EXPECT_EQ(1234567890123456789, int64_value);
Mark Mentovai 2011/07/30 19:41:14 Same.
326 }
327 ASSERT_FALSE(dict_array_reader.HasMoreData());
328 }
329 ASSERT_FALSE(variant_reader.HasMoreData());
330 }
331 ASSERT_FALSE(array_reader.HasMoreData());
332 ASSERT_FALSE(reader.HasMoreData());
333 }
334
335 TEST(MessageTest, Message) {
336 dbus::Message message;
337 EXPECT_TRUE(message.raw_message() == NULL);
338 EXPECT_EQ(dbus::Message::MESSAGE_INVALID, message.GetMessageType());
339 }
340
341 TEST(MessageTest, MethodCall) {
342 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
343 EXPECT_TRUE(method_call.raw_message() != NULL);
344 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType());
345 method_call.SetServiceName("com.example.Service");
346 method_call.SetObjectPath("/com/example/Object");
347
348 dbus::MessageWriter writer(&method_call);
349 writer.AppendString("payload");
350
351 EXPECT_EQ("destination: com.example.Service\n"
352 "path: /com/example/Object\n"
353 "interface: com.example.Interface\n"
354 "member: SomeMethod\n"
355 "signature: s\n"
356 "\n"
357 "string \"payload\"\n",
358 method_call.ToString());
359 }
360
361 TEST(MessageTest, Response) {
362 dbus::Response response;
363 EXPECT_TRUE(response.raw_message() == NULL);
364 response.reset_raw_message(
365 dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN));
366 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response.GetMessageType());
367 }
368
369 TEST(MessageTest, ToString_EmptyMessage) {
370 dbus::Message message;
371 EXPECT_EQ("", message.ToString());
372 }
OLDNEW
« no previous file with comments | « dbus/message.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698