chromeos: bluetooth: add BluetoothInputClient
[chromium-blink-merge.git] / dbus / message_unittest.cc
bloba40ba0aa740f5538ec5b0c784020fa6587096616
1 // Copyright (c) 2012 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.
5 #include "dbus/message.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "dbus/object_path.h"
11 #include "dbus/test_proto.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // Test that a byte can be properly written and read. We only have this
15 // test for byte, as repeating this for other basic types is too redundant.
16 TEST(MessageTest, AppendAndPopByte) {
17 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
18 dbus::MessageWriter writer(message.get());
19 writer.AppendByte(123); // The input is 123.
21 dbus::MessageReader reader(message.get());
22 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read.
23 ASSERT_EQ(dbus::Message::BYTE, reader.GetDataType());
25 bool bool_value = false;
26 // Should fail as the type is not bool here.
27 ASSERT_FALSE(reader.PopBool(&bool_value));
29 uint8 byte_value = 0;
30 ASSERT_TRUE(reader.PopByte(&byte_value));
31 EXPECT_EQ(123, byte_value); // Should match with the input.
32 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
34 // Try to get another byte. Should fail.
35 ASSERT_FALSE(reader.PopByte(&byte_value));
38 // Check all basic types can be properly written and read.
39 TEST(MessageTest, AppendAndPopBasicDataTypes) {
40 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
41 dbus::MessageWriter writer(message.get());
43 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
44 writer.AppendByte(0);
45 writer.AppendBool(true);
46 writer.AppendInt16(2);
47 writer.AppendUint16(3);
48 writer.AppendInt32(4);
49 writer.AppendUint32(5);
50 writer.AppendInt64(6);
51 writer.AppendUint64(7);
52 writer.AppendDouble(8.0);
53 writer.AppendString("string");
54 writer.AppendObjectPath(dbus::ObjectPath("/object/path"));
56 uint8 byte_value = 0;
57 bool bool_value = false;
58 int16 int16_value = 0;
59 uint16 uint16_value = 0;
60 int32 int32_value = 0;
61 uint32 uint32_value = 0;
62 int64 int64_value = 0;
63 uint64 uint64_value = 0;
64 double double_value = 0;
65 std::string string_value;
66 dbus::ObjectPath object_path_value;
68 dbus::MessageReader reader(message.get());
69 ASSERT_TRUE(reader.HasMoreData());
70 ASSERT_TRUE(reader.PopByte(&byte_value));
71 ASSERT_TRUE(reader.PopBool(&bool_value));
72 ASSERT_TRUE(reader.PopInt16(&int16_value));
73 ASSERT_TRUE(reader.PopUint16(&uint16_value));
74 ASSERT_TRUE(reader.PopInt32(&int32_value));
75 ASSERT_TRUE(reader.PopUint32(&uint32_value));
76 ASSERT_TRUE(reader.PopInt64(&int64_value));
77 ASSERT_TRUE(reader.PopUint64(&uint64_value));
78 ASSERT_TRUE(reader.PopDouble(&double_value));
79 ASSERT_TRUE(reader.PopString(&string_value));
80 ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
81 ASSERT_FALSE(reader.HasMoreData());
83 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
84 EXPECT_EQ(0, byte_value);
85 EXPECT_EQ(true, bool_value);
86 EXPECT_EQ(2, int16_value);
87 EXPECT_EQ(3U, uint16_value);
88 EXPECT_EQ(4, int32_value);
89 EXPECT_EQ(5U, uint32_value);
90 EXPECT_EQ(6, int64_value);
91 EXPECT_EQ(7U, uint64_value);
92 EXPECT_DOUBLE_EQ(8.0, double_value);
93 EXPECT_EQ("string", string_value);
94 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value);
97 // Check all variant types can be properly written and read.
98 TEST(MessageTest, AppendAndPopVariantDataTypes) {
99 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
100 dbus::MessageWriter writer(message.get());
102 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
103 writer.AppendVariantOfByte(0);
104 writer.AppendVariantOfBool(true);
105 writer.AppendVariantOfInt16(2);
106 writer.AppendVariantOfUint16(3);
107 writer.AppendVariantOfInt32(4);
108 writer.AppendVariantOfUint32(5);
109 writer.AppendVariantOfInt64(6);
110 writer.AppendVariantOfUint64(7);
111 writer.AppendVariantOfDouble(8.0);
112 writer.AppendVariantOfString("string");
113 writer.AppendVariantOfObjectPath(dbus::ObjectPath("/object/path"));
115 uint8 byte_value = 0;
116 bool bool_value = false;
117 int16 int16_value = 0;
118 uint16 uint16_value = 0;
119 int32 int32_value = 0;
120 uint32 uint32_value = 0;
121 int64 int64_value = 0;
122 uint64 uint64_value = 0;
123 double double_value = 0;
124 std::string string_value;
125 dbus::ObjectPath object_path_value;
127 dbus::MessageReader reader(message.get());
128 ASSERT_TRUE(reader.HasMoreData());
129 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value));
130 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value));
131 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value));
132 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value));
133 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value));
134 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value));
135 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value));
136 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value));
137 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value));
138 ASSERT_TRUE(reader.PopVariantOfString(&string_value));
139 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value));
140 ASSERT_FALSE(reader.HasMoreData());
142 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
143 EXPECT_EQ(0, byte_value);
144 EXPECT_EQ(true, bool_value);
145 EXPECT_EQ(2, int16_value);
146 EXPECT_EQ(3U, uint16_value);
147 EXPECT_EQ(4, int32_value);
148 EXPECT_EQ(5U, uint32_value);
149 EXPECT_EQ(6, int64_value);
150 EXPECT_EQ(7U, uint64_value);
151 EXPECT_DOUBLE_EQ(8.0, double_value);
152 EXPECT_EQ("string", string_value);
153 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value);
156 TEST(MessageTest, ArrayOfBytes) {
157 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
158 dbus::MessageWriter writer(message.get());
159 std::vector<uint8> bytes;
160 bytes.push_back(1);
161 bytes.push_back(2);
162 bytes.push_back(3);
163 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
165 dbus::MessageReader reader(message.get());
166 uint8* output_bytes = NULL;
167 size_t length = 0;
168 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
169 ASSERT_FALSE(reader.HasMoreData());
170 ASSERT_EQ(3U, length);
171 EXPECT_EQ(1, output_bytes[0]);
172 EXPECT_EQ(2, output_bytes[1]);
173 EXPECT_EQ(3, output_bytes[2]);
176 TEST(MessageTest, ArrayOfBytes_Empty) {
177 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
178 dbus::MessageWriter writer(message.get());
179 std::vector<uint8> bytes;
180 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
182 dbus::MessageReader reader(message.get());
183 uint8* output_bytes = NULL;
184 size_t length = 0;
185 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
186 ASSERT_FALSE(reader.HasMoreData());
187 ASSERT_EQ(0U, length);
188 EXPECT_EQ(NULL, output_bytes);
191 TEST(MessageTest, ArrayOfStrings) {
192 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
193 dbus::MessageWriter writer(message.get());
194 std::vector<std::string> strings;
195 strings.push_back("fee");
196 strings.push_back("fie");
197 strings.push_back("foe");
198 strings.push_back("fum");
199 writer.AppendArrayOfStrings(strings);
201 dbus::MessageReader reader(message.get());
202 std::vector<std::string> output_strings;
203 ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings));
204 ASSERT_FALSE(reader.HasMoreData());
205 ASSERT_EQ(4U, output_strings.size());
206 EXPECT_EQ("fee", output_strings[0]);
207 EXPECT_EQ("fie", output_strings[1]);
208 EXPECT_EQ("foe", output_strings[2]);
209 EXPECT_EQ("fum", output_strings[3]);
212 TEST(MessageTest, ArrayOfObjectPaths) {
213 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
214 dbus::MessageWriter writer(message.get());
215 std::vector<dbus::ObjectPath> object_paths;
216 object_paths.push_back(dbus::ObjectPath("/object/path/1"));
217 object_paths.push_back(dbus::ObjectPath("/object/path/2"));
218 object_paths.push_back(dbus::ObjectPath("/object/path/3"));
219 writer.AppendArrayOfObjectPaths(object_paths);
221 dbus::MessageReader reader(message.get());
222 std::vector<dbus::ObjectPath> output_object_paths;
223 ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths));
224 ASSERT_FALSE(reader.HasMoreData());
225 ASSERT_EQ(3U, output_object_paths.size());
226 EXPECT_EQ(dbus::ObjectPath("/object/path/1"), output_object_paths[0]);
227 EXPECT_EQ(dbus::ObjectPath("/object/path/2"), output_object_paths[1]);
228 EXPECT_EQ(dbus::ObjectPath("/object/path/3"), output_object_paths[2]);
231 TEST(MessageTest, ProtoBuf) {
232 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
233 dbus::MessageWriter writer(message.get());
234 TestProto send_message;
235 send_message.set_text("testing");
236 send_message.set_number(123);
237 writer.AppendProtoAsArrayOfBytes(send_message);
239 dbus::MessageReader reader(message.get());
240 TestProto receive_message;
241 ASSERT_TRUE(reader.PopArrayOfBytesAsProto(&receive_message));
242 EXPECT_EQ(receive_message.text(), send_message.text());
243 EXPECT_EQ(receive_message.number(), send_message.number());
247 // Test that an array can be properly written and read. We only have this
248 // test for array, as repeating this for other container types is too
249 // redundant.
250 TEST(MessageTest, OpenArrayAndPopArray) {
251 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
252 dbus::MessageWriter writer(message.get());
253 dbus::MessageWriter array_writer(NULL);
254 writer.OpenArray("s", &array_writer); // Open an array of strings.
255 array_writer.AppendString("foo");
256 array_writer.AppendString("bar");
257 array_writer.AppendString("baz");
258 writer.CloseContainer(&array_writer);
260 dbus::MessageReader reader(message.get());
261 ASSERT_EQ(dbus::Message::ARRAY, reader.GetDataType());
262 dbus::MessageReader array_reader(NULL);
263 ASSERT_TRUE(reader.PopArray(&array_reader));
264 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
266 std::string string_value;
267 ASSERT_TRUE(array_reader.PopString(&string_value));
268 EXPECT_EQ("foo", string_value);
269 ASSERT_TRUE(array_reader.PopString(&string_value));
270 EXPECT_EQ("bar", string_value);
271 ASSERT_TRUE(array_reader.PopString(&string_value));
272 EXPECT_EQ("baz", string_value);
273 // Should not have more data to read.
274 ASSERT_FALSE(array_reader.HasMoreData());
277 // Create a complex message using array, struct, variant, dict entry, and
278 // make sure it can be read properly.
279 TEST(MessageTest, CreateComplexMessageAndReadIt) {
280 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
281 dbus::MessageWriter writer(message.get());
283 dbus::MessageWriter array_writer(NULL);
284 // Open an array of variants.
285 writer.OpenArray("v", &array_writer);
287 // The first value in the array.
289 dbus::MessageWriter variant_writer(NULL);
290 // Open a variant of a boolean.
291 array_writer.OpenVariant("b", &variant_writer);
292 variant_writer.AppendBool(true);
293 array_writer.CloseContainer(&variant_writer);
296 // The second value in the array.
298 dbus::MessageWriter variant_writer(NULL);
299 // Open a variant of a struct that contains a string and an int32.
300 array_writer.OpenVariant("(si)", &variant_writer);
302 dbus::MessageWriter struct_writer(NULL);
303 variant_writer.OpenStruct(&struct_writer);
304 struct_writer.AppendString("string");
305 struct_writer.AppendInt32(123);
306 variant_writer.CloseContainer(&struct_writer);
308 array_writer.CloseContainer(&variant_writer);
311 // The third value in the array.
313 dbus::MessageWriter variant_writer(NULL);
314 // Open a variant of an array of string-to-int64 dict entries.
315 array_writer.OpenVariant("a{sx}", &variant_writer);
317 // Opens an array of string-to-int64 dict entries.
318 dbus::MessageWriter dict_array_writer(NULL);
319 variant_writer.OpenArray("{sx}", &dict_array_writer);
321 // Opens a string-to-int64 dict entries.
322 dbus::MessageWriter dict_entry_writer(NULL);
323 dict_array_writer.OpenDictEntry(&dict_entry_writer);
324 dict_entry_writer.AppendString("foo");
325 dict_entry_writer.AppendInt64(GG_INT64_C(1234567890123456789));
326 dict_array_writer.CloseContainer(&dict_entry_writer);
328 variant_writer.CloseContainer(&dict_array_writer);
330 array_writer.CloseContainer(&variant_writer);
333 writer.CloseContainer(&array_writer);
335 // What we have created looks like this:
336 EXPECT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
337 "signature: av\n"
338 "\n"
339 "array [\n"
340 " variant bool true\n"
341 " variant struct {\n"
342 " string \"string\"\n"
343 " int32 123\n"
344 " }\n"
345 " variant array [\n"
346 " dict entry {\n"
347 " string \"foo\"\n"
348 " int64 1234567890123456789\n"
349 " }\n"
350 " ]\n"
351 "]\n",
352 message->ToString());
354 dbus::MessageReader reader(message.get());
355 dbus::MessageReader array_reader(NULL);
356 ASSERT_TRUE(reader.PopArray(&array_reader));
358 // The first value in the array.
359 bool bool_value = false;
360 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
361 EXPECT_EQ(true, bool_value);
363 // The second value in the array.
365 dbus::MessageReader variant_reader(NULL);
366 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
368 dbus::MessageReader struct_reader(NULL);
369 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
370 std::string string_value;
371 ASSERT_TRUE(struct_reader.PopString(&string_value));
372 EXPECT_EQ("string", string_value);
373 int32 int32_value = 0;
374 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
375 EXPECT_EQ(123, int32_value);
376 ASSERT_FALSE(struct_reader.HasMoreData());
378 ASSERT_FALSE(variant_reader.HasMoreData());
381 // The third value in the array.
383 dbus::MessageReader variant_reader(NULL);
384 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
386 dbus::MessageReader dict_array_reader(NULL);
387 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
389 dbus::MessageReader dict_entry_reader(NULL);
390 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
391 std::string string_value;
392 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
393 EXPECT_EQ("foo", string_value);
394 int64 int64_value = 0;
395 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
396 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value);
398 ASSERT_FALSE(dict_array_reader.HasMoreData());
400 ASSERT_FALSE(variant_reader.HasMoreData());
402 ASSERT_FALSE(array_reader.HasMoreData());
403 ASSERT_FALSE(reader.HasMoreData());
406 TEST(MessageTest, MethodCall) {
407 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
408 EXPECT_TRUE(method_call.raw_message() != NULL);
409 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType());
410 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call.GetMessageTypeAsString());
411 method_call.SetDestination("com.example.Service");
412 method_call.SetPath(dbus::ObjectPath("/com/example/Object"));
414 dbus::MessageWriter writer(&method_call);
415 writer.AppendString("payload");
417 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
418 "destination: com.example.Service\n"
419 "path: /com/example/Object\n"
420 "interface: com.example.Interface\n"
421 "member: SomeMethod\n"
422 "signature: s\n"
423 "\n"
424 "string \"payload\"\n",
425 method_call.ToString());
428 TEST(MessageTest, MethodCall_FromRawMessage) {
429 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
430 dbus_message_set_interface(raw_message, "com.example.Interface");
431 dbus_message_set_member(raw_message, "SomeMethod");
433 scoped_ptr<dbus::MethodCall> method_call(
434 dbus::MethodCall::FromRawMessage(raw_message));
435 EXPECT_EQ("com.example.Interface", method_call->GetInterface());
436 EXPECT_EQ("SomeMethod", method_call->GetMember());
439 TEST(MessageTest, Signal) {
440 dbus::Signal signal("com.example.Interface", "SomeSignal");
441 EXPECT_TRUE(signal.raw_message() != NULL);
442 EXPECT_EQ(dbus::Message::MESSAGE_SIGNAL, signal.GetMessageType());
443 EXPECT_EQ("MESSAGE_SIGNAL", signal.GetMessageTypeAsString());
444 signal.SetPath(dbus::ObjectPath("/com/example/Object"));
446 dbus::MessageWriter writer(&signal);
447 writer.AppendString("payload");
449 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
450 "path: /com/example/Object\n"
451 "interface: com.example.Interface\n"
452 "member: SomeSignal\n"
453 "signature: s\n"
454 "\n"
455 "string \"payload\"\n",
456 signal.ToString());
459 TEST(MessageTest, Signal_FromRawMessage) {
460 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
461 dbus_message_set_interface(raw_message, "com.example.Interface");
462 dbus_message_set_member(raw_message, "SomeSignal");
464 scoped_ptr<dbus::Signal> signal(
465 dbus::Signal::FromRawMessage(raw_message));
466 EXPECT_EQ("com.example.Interface", signal->GetInterface());
467 EXPECT_EQ("SomeSignal", signal->GetMember());
470 TEST(MessageTest, Response) {
471 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
472 EXPECT_TRUE(response->raw_message());
473 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
474 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
477 TEST(MessageTest, Response_FromMethodCall) {
478 const uint32 kSerial = 123;
479 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
480 method_call.SetSerial(kSerial);
482 scoped_ptr<dbus::Response> response(
483 dbus::Response::FromMethodCall(&method_call));
484 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
485 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
486 // The serial should be copied to the reply serial.
487 EXPECT_EQ(kSerial, response->GetReplySerial());
490 TEST(MessageTest, ErrorResponse_FromMethodCall) {
491 const uint32 kSerial = 123;
492 const char kErrorMessage[] = "error message";
494 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
495 method_call.SetSerial(kSerial);
497 scoped_ptr<dbus::ErrorResponse> error_response(
498 dbus::ErrorResponse::FromMethodCall(&method_call,
499 DBUS_ERROR_FAILED,
500 kErrorMessage));
501 EXPECT_EQ(dbus::Message::MESSAGE_ERROR, error_response->GetMessageType());
502 EXPECT_EQ("MESSAGE_ERROR", error_response->GetMessageTypeAsString());
503 // The serial should be copied to the reply serial.
504 EXPECT_EQ(kSerial, error_response->GetReplySerial());
506 // Error message should be added to the payload.
507 dbus::MessageReader reader(error_response.get());
508 std::string error_message;
509 ASSERT_TRUE(reader.PopString(&error_message));
510 EXPECT_EQ(kErrorMessage, error_message);
513 TEST(MessageTest, GetAndSetHeaders) {
514 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
516 EXPECT_EQ("", message->GetDestination());
517 EXPECT_EQ(dbus::ObjectPath(""), message->GetPath());
518 EXPECT_EQ("", message->GetInterface());
519 EXPECT_EQ("", message->GetMember());
520 EXPECT_EQ("", message->GetErrorName());
521 EXPECT_EQ("", message->GetSender());
522 EXPECT_EQ(0U, message->GetSerial());
523 EXPECT_EQ(0U, message->GetReplySerial());
525 message->SetDestination("org.chromium.destination");
526 message->SetPath(dbus::ObjectPath("/org/chromium/path"));
527 message->SetInterface("org.chromium.interface");
528 message->SetMember("member");
529 message->SetErrorName("org.chromium.error");
530 message->SetSender(":1.2");
531 message->SetSerial(123);
532 message->SetReplySerial(456);
534 EXPECT_EQ("org.chromium.destination", message->GetDestination());
535 EXPECT_EQ(dbus::ObjectPath("/org/chromium/path"), message->GetPath());
536 EXPECT_EQ("org.chromium.interface", message->GetInterface());
537 EXPECT_EQ("member", message->GetMember());
538 EXPECT_EQ("org.chromium.error", message->GetErrorName());
539 EXPECT_EQ(":1.2", message->GetSender());
540 EXPECT_EQ(123U, message->GetSerial());
541 EXPECT_EQ(456U, message->GetReplySerial());