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

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: Created 9 years, 5 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
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 repating 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.HasMore()); // 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.HasMore()); // 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.HasMore());
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.HasMore());
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 // Test that an array can be properly written and read. We only have this
95 // test for array, as repating this for other container types is too
96 // redundant.
97 TEST(MessageTest, OpenArrayAndPopArray) {
98 dbus::Message message;
99 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
100 dbus::MessageWriter writer(&message);
101 dbus::MessageWriter array_writer(&message);
102 writer.OpenArray("s", &array_writer); // Open an array of strings.
103 array_writer.AppendString("foo");
104 array_writer.AppendString("bar");
105 array_writer.AppendString("baz");
106 writer.CloseContainer(&array_writer);
107
108 dbus::MessageReader reader(&message);
109 ASSERT_EQ(dbus::Message::ARRAY, reader.GetDataType());
110 dbus::MessageReader array_reader(&message);
111 ASSERT_TRUE(reader.PopArray(&array_reader));
112 ASSERT_FALSE(reader.HasMore()); // Should not have more data to read.
113
114 std::string string_value;
115 ASSERT_TRUE(array_reader.PopString(&string_value));
116 EXPECT_EQ("foo", string_value);
117 ASSERT_TRUE(array_reader.PopString(&string_value));
118 EXPECT_EQ("bar", string_value);
119 ASSERT_TRUE(array_reader.PopString(&string_value));
120 EXPECT_EQ("baz", string_value);
121 ASSERT_FALSE(array_reader.HasMore()); // Should not have more data to read.
122 }
123
124 // Create a complex message using array, struct, variant, dict entry, and
125 // make sure it can be read properly.
126 TEST(MessageTest, CreateComplexMessageAndReadIt) {
127 dbus::Message message;
128 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL));
129 dbus::MessageWriter writer(&message);
130 {
131 dbus::MessageWriter array_writer(&message);
132 // Open an array of variants.
133 writer.OpenArray("v", &array_writer);
134 {
135 // The first value in the array.
136 {
137 dbus::MessageWriter variant_writer(&message);
138 // Open a variant of a boolean.
139 array_writer.OpenVariant("b", &variant_writer);
140 variant_writer.AppendBool(true);
141 array_writer.CloseContainer(&variant_writer);
142 }
143
144 // The second value in the array.
145 {
146 dbus::MessageWriter variant_writer(&message);
147 // Open a variant of a struct that contains a string and an int32.
148 array_writer.OpenVariant("(si)", &variant_writer);
149 {
150 dbus::MessageWriter struct_writer(&message);
151 variant_writer.OpenStruct(&struct_writer);
152 struct_writer.AppendString("string");
153 struct_writer.AppendInt32(123);
154 variant_writer.CloseContainer(&struct_writer);
155 }
156 array_writer.CloseContainer(&variant_writer);
157 }
158
159 // The third value in the array.
160 {
161 dbus::MessageWriter variant_writer(&message);
162 // Open a variant of an array of string-to-int64 dict entries.
163 array_writer.OpenVariant("a{sx}", &variant_writer);
164 {
165 // Opens an array of string-to-int64 dict entries.
166 dbus::MessageWriter dict_array_writer(&message);
167 variant_writer.OpenArray("{sx}", &dict_array_writer);
168 {
169 // Opens a string-to-int64 dict entries.
170 dbus::MessageWriter dict_entry_writer(&message);
171 dict_array_writer.OpenDictEntry(&dict_entry_writer);
172 dict_entry_writer.AppendString("foo");
173 dict_entry_writer.AppendInt64(1234567890123456789);
174 dict_array_writer.CloseContainer(&dict_entry_writer);
175 }
176 variant_writer.CloseContainer(&dict_array_writer);
177 }
178 array_writer.CloseContainer(&variant_writer);
179 }
180 }
181 writer.CloseContainer(&array_writer);
182 }
183 // What we have created looks like this:
184 EXPECT_EQ("array [\n"
185 " variant bool true\n"
186 " variant struct {\n"
187 " string \"string\"\n"
188 " int32 123\n"
189 " }\n"
190 " variant array [\n"
191 " dict entry {\n"
192 " string \"foo\"\n"
193 " int64 1234567890123456789\n"
194 " }\n"
195 " ]\n"
196 "]\n",
197 message.ToString());
198
199 dbus::MessageReader reader(&message);
200 dbus::MessageReader array_reader(&message);
201 ASSERT_TRUE(reader.PopArray(&array_reader));
202
203 // The first value in the array.
204 bool bool_value = false;
205 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
206 EXPECT_EQ(true, bool_value);
207
208 // The second value in the array.
209 {
210 dbus::MessageReader variant_reader(&message);
211 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
212 {
213 dbus::MessageReader struct_reader(&message);
214 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
215 std::string string_value;
216 ASSERT_TRUE(struct_reader.PopString(&string_value));
217 EXPECT_EQ("string", string_value);
218 int32 int32_value = 0;
219 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
220 EXPECT_EQ(123, int32_value);
221 ASSERT_FALSE(struct_reader.HasMore());
222 }
223 ASSERT_FALSE(variant_reader.HasMore());
224 }
225
226 // The third value in the array.
227 {
228 dbus::MessageReader variant_reader(&message);
229 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
230 {
231 dbus::MessageReader dict_array_reader(&message);
232 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
233 {
234 dbus::MessageReader dict_entry_reader(&message);
235 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
236 std::string string_value;
237 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
238 EXPECT_EQ("foo", string_value);
239 int64 int64_value = 0;
240 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
241 EXPECT_EQ(1234567890123456789, int64_value);
242 }
243 ASSERT_FALSE(dict_array_reader.HasMore());
244 }
245 ASSERT_FALSE(variant_reader.HasMore());
246 }
247 ASSERT_FALSE(array_reader.HasMore());
248 ASSERT_FALSE(reader.HasMore());
249 }
OLDNEW
« no previous file with comments | « dbus/message.cc ('k') | dbus/run_all_unittests.cc » ('j') | dbus/run_all_unittests.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698