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

Side by Side Diff: dbus/message_unittest.cc

Issue 16012018: Cleanup: Put DBus unit tests in the dbus namespace, so one does not need to write dbus:: everywhere… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 6 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') | dbus/mock_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "dbus/message.h" 5 #include "dbus/message.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/posix/eintr_wrapper.h" 10 #include "base/posix/eintr_wrapper.h"
11 #include "dbus/object_path.h" 11 #include "dbus/object_path.h"
12 #include "dbus/test_proto.pb.h" 12 #include "dbus/test_proto.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace dbus {
16
15 // Test that a byte can be properly written and read. We only have this 17 // Test that a byte can be properly written and read. We only have this
16 // test for byte, as repeating this for other basic types is too redundant. 18 // test for byte, as repeating this for other basic types is too redundant.
17 TEST(MessageTest, AppendAndPopByte) { 19 TEST(MessageTest, AppendAndPopByte) {
18 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 20 scoped_ptr<Response> message(Response::CreateEmpty());
19 dbus::MessageWriter writer(message.get()); 21 MessageWriter writer(message.get());
20 writer.AppendByte(123); // The input is 123. 22 writer.AppendByte(123); // The input is 123.
21 23
22 dbus::MessageReader reader(message.get()); 24 MessageReader reader(message.get());
23 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read. 25 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read.
24 ASSERT_EQ(dbus::Message::BYTE, reader.GetDataType()); 26 ASSERT_EQ(Message::BYTE, reader.GetDataType());
25 27
26 bool bool_value = false; 28 bool bool_value = false;
27 // Should fail as the type is not bool here. 29 // Should fail as the type is not bool here.
28 ASSERT_FALSE(reader.PopBool(&bool_value)); 30 ASSERT_FALSE(reader.PopBool(&bool_value));
29 31
30 uint8 byte_value = 0; 32 uint8 byte_value = 0;
31 ASSERT_TRUE(reader.PopByte(&byte_value)); 33 ASSERT_TRUE(reader.PopByte(&byte_value));
32 EXPECT_EQ(123, byte_value); // Should match with the input. 34 EXPECT_EQ(123, byte_value); // Should match with the input.
33 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read. 35 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
34 36
35 // Try to get another byte. Should fail. 37 // Try to get another byte. Should fail.
36 ASSERT_FALSE(reader.PopByte(&byte_value)); 38 ASSERT_FALSE(reader.PopByte(&byte_value));
37 } 39 }
38 40
39 // Check all basic types can be properly written and read. 41 // Check all basic types can be properly written and read.
40 TEST(MessageTest, AppendAndPopBasicDataTypes) { 42 TEST(MessageTest, AppendAndPopBasicDataTypes) {
41 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 43 scoped_ptr<Response> message(Response::CreateEmpty());
42 dbus::MessageWriter writer(message.get()); 44 MessageWriter writer(message.get());
43 45
44 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path". 46 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
45 writer.AppendByte(0); 47 writer.AppendByte(0);
46 writer.AppendBool(true); 48 writer.AppendBool(true);
47 writer.AppendInt16(2); 49 writer.AppendInt16(2);
48 writer.AppendUint16(3); 50 writer.AppendUint16(3);
49 writer.AppendInt32(4); 51 writer.AppendInt32(4);
50 writer.AppendUint32(5); 52 writer.AppendUint32(5);
51 writer.AppendInt64(6); 53 writer.AppendInt64(6);
52 writer.AppendUint64(7); 54 writer.AppendUint64(7);
53 writer.AppendDouble(8.0); 55 writer.AppendDouble(8.0);
54 writer.AppendString("string"); 56 writer.AppendString("string");
55 writer.AppendObjectPath(dbus::ObjectPath("/object/path")); 57 writer.AppendObjectPath(ObjectPath("/object/path"));
56 58
57 uint8 byte_value = 0; 59 uint8 byte_value = 0;
58 bool bool_value = false; 60 bool bool_value = false;
59 int16 int16_value = 0; 61 int16 int16_value = 0;
60 uint16 uint16_value = 0; 62 uint16 uint16_value = 0;
61 int32 int32_value = 0; 63 int32 int32_value = 0;
62 uint32 uint32_value = 0; 64 uint32 uint32_value = 0;
63 int64 int64_value = 0; 65 int64 int64_value = 0;
64 uint64 uint64_value = 0; 66 uint64 uint64_value = 0;
65 double double_value = 0; 67 double double_value = 0;
66 std::string string_value; 68 std::string string_value;
67 dbus::ObjectPath object_path_value; 69 ObjectPath object_path_value;
68 70
69 dbus::MessageReader reader(message.get()); 71 MessageReader reader(message.get());
70 ASSERT_TRUE(reader.HasMoreData()); 72 ASSERT_TRUE(reader.HasMoreData());
71 ASSERT_TRUE(reader.PopByte(&byte_value)); 73 ASSERT_TRUE(reader.PopByte(&byte_value));
72 ASSERT_TRUE(reader.PopBool(&bool_value)); 74 ASSERT_TRUE(reader.PopBool(&bool_value));
73 ASSERT_TRUE(reader.PopInt16(&int16_value)); 75 ASSERT_TRUE(reader.PopInt16(&int16_value));
74 ASSERT_TRUE(reader.PopUint16(&uint16_value)); 76 ASSERT_TRUE(reader.PopUint16(&uint16_value));
75 ASSERT_TRUE(reader.PopInt32(&int32_value)); 77 ASSERT_TRUE(reader.PopInt32(&int32_value));
76 ASSERT_TRUE(reader.PopUint32(&uint32_value)); 78 ASSERT_TRUE(reader.PopUint32(&uint32_value));
77 ASSERT_TRUE(reader.PopInt64(&int64_value)); 79 ASSERT_TRUE(reader.PopInt64(&int64_value));
78 ASSERT_TRUE(reader.PopUint64(&uint64_value)); 80 ASSERT_TRUE(reader.PopUint64(&uint64_value));
79 ASSERT_TRUE(reader.PopDouble(&double_value)); 81 ASSERT_TRUE(reader.PopDouble(&double_value));
80 ASSERT_TRUE(reader.PopString(&string_value)); 82 ASSERT_TRUE(reader.PopString(&string_value));
81 ASSERT_TRUE(reader.PopObjectPath(&object_path_value)); 83 ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
82 ASSERT_FALSE(reader.HasMoreData()); 84 ASSERT_FALSE(reader.HasMoreData());
83 85
84 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned. 86 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
85 EXPECT_EQ(0, byte_value); 87 EXPECT_EQ(0, byte_value);
86 EXPECT_EQ(true, bool_value); 88 EXPECT_EQ(true, bool_value);
87 EXPECT_EQ(2, int16_value); 89 EXPECT_EQ(2, int16_value);
88 EXPECT_EQ(3U, uint16_value); 90 EXPECT_EQ(3U, uint16_value);
89 EXPECT_EQ(4, int32_value); 91 EXPECT_EQ(4, int32_value);
90 EXPECT_EQ(5U, uint32_value); 92 EXPECT_EQ(5U, uint32_value);
91 EXPECT_EQ(6, int64_value); 93 EXPECT_EQ(6, int64_value);
92 EXPECT_EQ(7U, uint64_value); 94 EXPECT_EQ(7U, uint64_value);
93 EXPECT_DOUBLE_EQ(8.0, double_value); 95 EXPECT_DOUBLE_EQ(8.0, double_value);
94 EXPECT_EQ("string", string_value); 96 EXPECT_EQ("string", string_value);
95 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value); 97 EXPECT_EQ(ObjectPath("/object/path"), object_path_value);
96 } 98 }
97 99
98 // Check all basic types can be properly written and read. 100 // Check all basic types can be properly written and read.
99 TEST(MessageTest, AppendAndPopFileDescriptor) { 101 TEST(MessageTest, AppendAndPopFileDescriptor) {
100 if (!dbus::IsDBusTypeUnixFdSupported()) { 102 if (!IsDBusTypeUnixFdSupported()) {
101 LOG(WARNING) << "FD passing is not supported"; 103 LOG(WARNING) << "FD passing is not supported";
102 return; 104 return;
103 } 105 }
104 106
105 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 107 scoped_ptr<Response> message(Response::CreateEmpty());
106 dbus::MessageWriter writer(message.get()); 108 MessageWriter writer(message.get());
107 109
108 // Append stdout. 110 // Append stdout.
109 dbus::FileDescriptor temp(1); 111 FileDescriptor temp(1);
110 // Descriptor should not be valid until checked. 112 // Descriptor should not be valid until checked.
111 ASSERT_FALSE(temp.is_valid()); 113 ASSERT_FALSE(temp.is_valid());
112 // NB: thread IO requirements not relevant for unit tests. 114 // NB: thread IO requirements not relevant for unit tests.
113 temp.CheckValidity(); 115 temp.CheckValidity();
114 ASSERT_TRUE(temp.is_valid()); 116 ASSERT_TRUE(temp.is_valid());
115 writer.AppendFileDescriptor(temp); 117 writer.AppendFileDescriptor(temp);
116 118
117 dbus::FileDescriptor fd_value; 119 FileDescriptor fd_value;
118 120
119 dbus::MessageReader reader(message.get()); 121 MessageReader reader(message.get());
120 ASSERT_TRUE(reader.HasMoreData()); 122 ASSERT_TRUE(reader.HasMoreData());
121 ASSERT_TRUE(reader.PopFileDescriptor(&fd_value)); 123 ASSERT_TRUE(reader.PopFileDescriptor(&fd_value));
122 ASSERT_FALSE(reader.HasMoreData()); 124 ASSERT_FALSE(reader.HasMoreData());
123 // Descriptor is not valid until explicitly checked. 125 // Descriptor is not valid until explicitly checked.
124 ASSERT_FALSE(fd_value.is_valid()); 126 ASSERT_FALSE(fd_value.is_valid());
125 fd_value.CheckValidity(); 127 fd_value.CheckValidity();
126 ASSERT_TRUE(fd_value.is_valid()); 128 ASSERT_TRUE(fd_value.is_valid());
127 129
128 // Stdout should be returned but we cannot check the descriptor 130 // Stdout should be returned but we cannot check the descriptor
129 // value because stdout will be dup'd. Instead check st_rdev 131 // value because stdout will be dup'd. Instead check st_rdev
130 // which should be identical. 132 // which should be identical.
131 struct stat sb_stdout; 133 struct stat sb_stdout;
132 int status_stdout = HANDLE_EINTR(fstat(1, &sb_stdout)); 134 int status_stdout = HANDLE_EINTR(fstat(1, &sb_stdout));
133 ASSERT_TRUE(status_stdout >= 0); 135 ASSERT_GE(status_stdout, 0);
134 struct stat sb_fd; 136 struct stat sb_fd;
135 int status_fd = HANDLE_EINTR(fstat(fd_value.value(), &sb_fd)); 137 int status_fd = HANDLE_EINTR(fstat(fd_value.value(), &sb_fd));
136 ASSERT_TRUE(status_fd >= 0); 138 ASSERT_GE(status_fd, 0);
137 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev); 139 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev);
138 } 140 }
139 141
140 // Check all variant types can be properly written and read. 142 // Check all variant types can be properly written and read.
141 TEST(MessageTest, AppendAndPopVariantDataTypes) { 143 TEST(MessageTest, AppendAndPopVariantDataTypes) {
142 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 144 scoped_ptr<Response> message(Response::CreateEmpty());
143 dbus::MessageWriter writer(message.get()); 145 MessageWriter writer(message.get());
144 146
145 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path". 147 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
146 writer.AppendVariantOfByte(0); 148 writer.AppendVariantOfByte(0);
147 writer.AppendVariantOfBool(true); 149 writer.AppendVariantOfBool(true);
148 writer.AppendVariantOfInt16(2); 150 writer.AppendVariantOfInt16(2);
149 writer.AppendVariantOfUint16(3); 151 writer.AppendVariantOfUint16(3);
150 writer.AppendVariantOfInt32(4); 152 writer.AppendVariantOfInt32(4);
151 writer.AppendVariantOfUint32(5); 153 writer.AppendVariantOfUint32(5);
152 writer.AppendVariantOfInt64(6); 154 writer.AppendVariantOfInt64(6);
153 writer.AppendVariantOfUint64(7); 155 writer.AppendVariantOfUint64(7);
154 writer.AppendVariantOfDouble(8.0); 156 writer.AppendVariantOfDouble(8.0);
155 writer.AppendVariantOfString("string"); 157 writer.AppendVariantOfString("string");
156 writer.AppendVariantOfObjectPath(dbus::ObjectPath("/object/path")); 158 writer.AppendVariantOfObjectPath(ObjectPath("/object/path"));
157 159
158 uint8 byte_value = 0; 160 uint8 byte_value = 0;
159 bool bool_value = false; 161 bool bool_value = false;
160 int16 int16_value = 0; 162 int16 int16_value = 0;
161 uint16 uint16_value = 0; 163 uint16 uint16_value = 0;
162 int32 int32_value = 0; 164 int32 int32_value = 0;
163 uint32 uint32_value = 0; 165 uint32 uint32_value = 0;
164 int64 int64_value = 0; 166 int64 int64_value = 0;
165 uint64 uint64_value = 0; 167 uint64 uint64_value = 0;
166 double double_value = 0; 168 double double_value = 0;
167 std::string string_value; 169 std::string string_value;
168 dbus::ObjectPath object_path_value; 170 ObjectPath object_path_value;
169 171
170 dbus::MessageReader reader(message.get()); 172 MessageReader reader(message.get());
171 ASSERT_TRUE(reader.HasMoreData()); 173 ASSERT_TRUE(reader.HasMoreData());
172 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value)); 174 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value));
173 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value)); 175 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value));
174 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value)); 176 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value));
175 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value)); 177 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value));
176 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value)); 178 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value));
177 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value)); 179 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value));
178 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value)); 180 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value));
179 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value)); 181 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value));
180 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value)); 182 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value));
181 ASSERT_TRUE(reader.PopVariantOfString(&string_value)); 183 ASSERT_TRUE(reader.PopVariantOfString(&string_value));
182 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value)); 184 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value));
183 ASSERT_FALSE(reader.HasMoreData()); 185 ASSERT_FALSE(reader.HasMoreData());
184 186
185 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned. 187 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
186 EXPECT_EQ(0, byte_value); 188 EXPECT_EQ(0, byte_value);
187 EXPECT_EQ(true, bool_value); 189 EXPECT_EQ(true, bool_value);
188 EXPECT_EQ(2, int16_value); 190 EXPECT_EQ(2, int16_value);
189 EXPECT_EQ(3U, uint16_value); 191 EXPECT_EQ(3U, uint16_value);
190 EXPECT_EQ(4, int32_value); 192 EXPECT_EQ(4, int32_value);
191 EXPECT_EQ(5U, uint32_value); 193 EXPECT_EQ(5U, uint32_value);
192 EXPECT_EQ(6, int64_value); 194 EXPECT_EQ(6, int64_value);
193 EXPECT_EQ(7U, uint64_value); 195 EXPECT_EQ(7U, uint64_value);
194 EXPECT_DOUBLE_EQ(8.0, double_value); 196 EXPECT_DOUBLE_EQ(8.0, double_value);
195 EXPECT_EQ("string", string_value); 197 EXPECT_EQ("string", string_value);
196 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value); 198 EXPECT_EQ(ObjectPath("/object/path"), object_path_value);
197 } 199 }
198 200
199 TEST(MessageTest, ArrayOfBytes) { 201 TEST(MessageTest, ArrayOfBytes) {
200 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 202 scoped_ptr<Response> message(Response::CreateEmpty());
201 dbus::MessageWriter writer(message.get()); 203 MessageWriter writer(message.get());
202 std::vector<uint8> bytes; 204 std::vector<uint8> bytes;
203 bytes.push_back(1); 205 bytes.push_back(1);
204 bytes.push_back(2); 206 bytes.push_back(2);
205 bytes.push_back(3); 207 bytes.push_back(3);
206 writer.AppendArrayOfBytes(bytes.data(), bytes.size()); 208 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
207 209
208 dbus::MessageReader reader(message.get()); 210 MessageReader reader(message.get());
209 uint8* output_bytes = NULL; 211 uint8* output_bytes = NULL;
210 size_t length = 0; 212 size_t length = 0;
211 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length)); 213 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
212 ASSERT_FALSE(reader.HasMoreData()); 214 ASSERT_FALSE(reader.HasMoreData());
213 ASSERT_EQ(3U, length); 215 ASSERT_EQ(3U, length);
214 EXPECT_EQ(1, output_bytes[0]); 216 EXPECT_EQ(1, output_bytes[0]);
215 EXPECT_EQ(2, output_bytes[1]); 217 EXPECT_EQ(2, output_bytes[1]);
216 EXPECT_EQ(3, output_bytes[2]); 218 EXPECT_EQ(3, output_bytes[2]);
217 } 219 }
218 220
219 TEST(MessageTest, ArrayOfBytes_Empty) { 221 TEST(MessageTest, ArrayOfBytes_Empty) {
220 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 222 scoped_ptr<Response> message(Response::CreateEmpty());
221 dbus::MessageWriter writer(message.get()); 223 MessageWriter writer(message.get());
222 std::vector<uint8> bytes; 224 std::vector<uint8> bytes;
223 writer.AppendArrayOfBytes(bytes.data(), bytes.size()); 225 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
224 226
225 dbus::MessageReader reader(message.get()); 227 MessageReader reader(message.get());
226 uint8* output_bytes = NULL; 228 uint8* output_bytes = NULL;
227 size_t length = 0; 229 size_t length = 0;
228 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length)); 230 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
229 ASSERT_FALSE(reader.HasMoreData()); 231 ASSERT_FALSE(reader.HasMoreData());
230 ASSERT_EQ(0U, length); 232 ASSERT_EQ(0U, length);
231 EXPECT_EQ(NULL, output_bytes); 233 EXPECT_EQ(NULL, output_bytes);
232 } 234 }
233 235
234 TEST(MessageTest, ArrayOfStrings) { 236 TEST(MessageTest, ArrayOfStrings) {
235 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 237 scoped_ptr<Response> message(Response::CreateEmpty());
236 dbus::MessageWriter writer(message.get()); 238 MessageWriter writer(message.get());
237 std::vector<std::string> strings; 239 std::vector<std::string> strings;
238 strings.push_back("fee"); 240 strings.push_back("fee");
239 strings.push_back("fie"); 241 strings.push_back("fie");
240 strings.push_back("foe"); 242 strings.push_back("foe");
241 strings.push_back("fum"); 243 strings.push_back("fum");
242 writer.AppendArrayOfStrings(strings); 244 writer.AppendArrayOfStrings(strings);
243 245
244 dbus::MessageReader reader(message.get()); 246 MessageReader reader(message.get());
245 std::vector<std::string> output_strings; 247 std::vector<std::string> output_strings;
246 ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings)); 248 ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings));
247 ASSERT_FALSE(reader.HasMoreData()); 249 ASSERT_FALSE(reader.HasMoreData());
248 ASSERT_EQ(4U, output_strings.size()); 250 ASSERT_EQ(4U, output_strings.size());
249 EXPECT_EQ("fee", output_strings[0]); 251 EXPECT_EQ("fee", output_strings[0]);
250 EXPECT_EQ("fie", output_strings[1]); 252 EXPECT_EQ("fie", output_strings[1]);
251 EXPECT_EQ("foe", output_strings[2]); 253 EXPECT_EQ("foe", output_strings[2]);
252 EXPECT_EQ("fum", output_strings[3]); 254 EXPECT_EQ("fum", output_strings[3]);
253 } 255 }
254 256
255 TEST(MessageTest, ArrayOfObjectPaths) { 257 TEST(MessageTest, ArrayOfObjectPaths) {
256 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 258 scoped_ptr<Response> message(Response::CreateEmpty());
257 dbus::MessageWriter writer(message.get()); 259 MessageWriter writer(message.get());
258 std::vector<dbus::ObjectPath> object_paths; 260 std::vector<ObjectPath> object_paths;
259 object_paths.push_back(dbus::ObjectPath("/object/path/1")); 261 object_paths.push_back(ObjectPath("/object/path/1"));
260 object_paths.push_back(dbus::ObjectPath("/object/path/2")); 262 object_paths.push_back(ObjectPath("/object/path/2"));
261 object_paths.push_back(dbus::ObjectPath("/object/path/3")); 263 object_paths.push_back(ObjectPath("/object/path/3"));
262 writer.AppendArrayOfObjectPaths(object_paths); 264 writer.AppendArrayOfObjectPaths(object_paths);
263 265
264 dbus::MessageReader reader(message.get()); 266 MessageReader reader(message.get());
265 std::vector<dbus::ObjectPath> output_object_paths; 267 std::vector<ObjectPath> output_object_paths;
266 ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths)); 268 ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths));
267 ASSERT_FALSE(reader.HasMoreData()); 269 ASSERT_FALSE(reader.HasMoreData());
268 ASSERT_EQ(3U, output_object_paths.size()); 270 ASSERT_EQ(3U, output_object_paths.size());
269 EXPECT_EQ(dbus::ObjectPath("/object/path/1"), output_object_paths[0]); 271 EXPECT_EQ(ObjectPath("/object/path/1"), output_object_paths[0]);
270 EXPECT_EQ(dbus::ObjectPath("/object/path/2"), output_object_paths[1]); 272 EXPECT_EQ(ObjectPath("/object/path/2"), output_object_paths[1]);
271 EXPECT_EQ(dbus::ObjectPath("/object/path/3"), output_object_paths[2]); 273 EXPECT_EQ(ObjectPath("/object/path/3"), output_object_paths[2]);
272 } 274 }
273 275
274 TEST(MessageTest, ProtoBuf) { 276 TEST(MessageTest, ProtoBuf) {
275 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 277 scoped_ptr<Response> message(Response::CreateEmpty());
276 dbus::MessageWriter writer(message.get()); 278 MessageWriter writer(message.get());
277 TestProto send_message; 279 TestProto send_message;
278 send_message.set_text("testing"); 280 send_message.set_text("testing");
279 send_message.set_number(123); 281 send_message.set_number(123);
280 writer.AppendProtoAsArrayOfBytes(send_message); 282 writer.AppendProtoAsArrayOfBytes(send_message);
281 283
282 dbus::MessageReader reader(message.get()); 284 MessageReader reader(message.get());
283 TestProto receive_message; 285 TestProto receive_message;
284 ASSERT_TRUE(reader.PopArrayOfBytesAsProto(&receive_message)); 286 ASSERT_TRUE(reader.PopArrayOfBytesAsProto(&receive_message));
285 EXPECT_EQ(receive_message.text(), send_message.text()); 287 EXPECT_EQ(receive_message.text(), send_message.text());
286 EXPECT_EQ(receive_message.number(), send_message.number()); 288 EXPECT_EQ(receive_message.number(), send_message.number());
287 } 289 }
288 290
289 291
290 // Test that an array can be properly written and read. We only have this 292 // Test that an array can be properly written and read. We only have this
291 // test for array, as repeating this for other container types is too 293 // test for array, as repeating this for other container types is too
292 // redundant. 294 // redundant.
293 TEST(MessageTest, OpenArrayAndPopArray) { 295 TEST(MessageTest, OpenArrayAndPopArray) {
294 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 296 scoped_ptr<Response> message(Response::CreateEmpty());
295 dbus::MessageWriter writer(message.get()); 297 MessageWriter writer(message.get());
296 dbus::MessageWriter array_writer(NULL); 298 MessageWriter array_writer(NULL);
297 writer.OpenArray("s", &array_writer); // Open an array of strings. 299 writer.OpenArray("s", &array_writer); // Open an array of strings.
298 array_writer.AppendString("foo"); 300 array_writer.AppendString("foo");
299 array_writer.AppendString("bar"); 301 array_writer.AppendString("bar");
300 array_writer.AppendString("baz"); 302 array_writer.AppendString("baz");
301 writer.CloseContainer(&array_writer); 303 writer.CloseContainer(&array_writer);
302 304
303 dbus::MessageReader reader(message.get()); 305 MessageReader reader(message.get());
304 ASSERT_EQ(dbus::Message::ARRAY, reader.GetDataType()); 306 ASSERT_EQ(Message::ARRAY, reader.GetDataType());
305 dbus::MessageReader array_reader(NULL); 307 MessageReader array_reader(NULL);
306 ASSERT_TRUE(reader.PopArray(&array_reader)); 308 ASSERT_TRUE(reader.PopArray(&array_reader));
307 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read. 309 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
308 310
309 std::string string_value; 311 std::string string_value;
310 ASSERT_TRUE(array_reader.PopString(&string_value)); 312 ASSERT_TRUE(array_reader.PopString(&string_value));
311 EXPECT_EQ("foo", string_value); 313 EXPECT_EQ("foo", string_value);
312 ASSERT_TRUE(array_reader.PopString(&string_value)); 314 ASSERT_TRUE(array_reader.PopString(&string_value));
313 EXPECT_EQ("bar", string_value); 315 EXPECT_EQ("bar", string_value);
314 ASSERT_TRUE(array_reader.PopString(&string_value)); 316 ASSERT_TRUE(array_reader.PopString(&string_value));
315 EXPECT_EQ("baz", string_value); 317 EXPECT_EQ("baz", string_value);
316 // Should not have more data to read. 318 // Should not have more data to read.
317 ASSERT_FALSE(array_reader.HasMoreData()); 319 ASSERT_FALSE(array_reader.HasMoreData());
318 } 320 }
319 321
320 // Create a complex message using array, struct, variant, dict entry, and 322 // Create a complex message using array, struct, variant, dict entry, and
321 // make sure it can be read properly. 323 // make sure it can be read properly.
322 TEST(MessageTest, CreateComplexMessageAndReadIt) { 324 TEST(MessageTest, CreateComplexMessageAndReadIt) {
323 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 325 scoped_ptr<Response> message(Response::CreateEmpty());
324 dbus::MessageWriter writer(message.get()); 326 MessageWriter writer(message.get());
325 { 327 {
326 dbus::MessageWriter array_writer(NULL); 328 MessageWriter array_writer(NULL);
327 // Open an array of variants. 329 // Open an array of variants.
328 writer.OpenArray("v", &array_writer); 330 writer.OpenArray("v", &array_writer);
329 { 331 {
330 // The first value in the array. 332 // The first value in the array.
331 { 333 {
332 dbus::MessageWriter variant_writer(NULL); 334 MessageWriter variant_writer(NULL);
333 // Open a variant of a boolean. 335 // Open a variant of a boolean.
334 array_writer.OpenVariant("b", &variant_writer); 336 array_writer.OpenVariant("b", &variant_writer);
335 variant_writer.AppendBool(true); 337 variant_writer.AppendBool(true);
336 array_writer.CloseContainer(&variant_writer); 338 array_writer.CloseContainer(&variant_writer);
337 } 339 }
338 340
339 // The second value in the array. 341 // The second value in the array.
340 { 342 {
341 dbus::MessageWriter variant_writer(NULL); 343 MessageWriter variant_writer(NULL);
342 // Open a variant of a struct that contains a string and an int32. 344 // Open a variant of a struct that contains a string and an int32.
343 array_writer.OpenVariant("(si)", &variant_writer); 345 array_writer.OpenVariant("(si)", &variant_writer);
344 { 346 {
345 dbus::MessageWriter struct_writer(NULL); 347 MessageWriter struct_writer(NULL);
346 variant_writer.OpenStruct(&struct_writer); 348 variant_writer.OpenStruct(&struct_writer);
347 struct_writer.AppendString("string"); 349 struct_writer.AppendString("string");
348 struct_writer.AppendInt32(123); 350 struct_writer.AppendInt32(123);
349 variant_writer.CloseContainer(&struct_writer); 351 variant_writer.CloseContainer(&struct_writer);
350 } 352 }
351 array_writer.CloseContainer(&variant_writer); 353 array_writer.CloseContainer(&variant_writer);
352 } 354 }
353 355
354 // The third value in the array. 356 // The third value in the array.
355 { 357 {
356 dbus::MessageWriter variant_writer(NULL); 358 MessageWriter variant_writer(NULL);
357 // Open a variant of an array of string-to-int64 dict entries. 359 // Open a variant of an array of string-to-int64 dict entries.
358 array_writer.OpenVariant("a{sx}", &variant_writer); 360 array_writer.OpenVariant("a{sx}", &variant_writer);
359 { 361 {
360 // Opens an array of string-to-int64 dict entries. 362 // Opens an array of string-to-int64 dict entries.
361 dbus::MessageWriter dict_array_writer(NULL); 363 MessageWriter dict_array_writer(NULL);
362 variant_writer.OpenArray("{sx}", &dict_array_writer); 364 variant_writer.OpenArray("{sx}", &dict_array_writer);
363 { 365 {
364 // Opens a string-to-int64 dict entries. 366 // Opens a string-to-int64 dict entries.
365 dbus::MessageWriter dict_entry_writer(NULL); 367 MessageWriter dict_entry_writer(NULL);
366 dict_array_writer.OpenDictEntry(&dict_entry_writer); 368 dict_array_writer.OpenDictEntry(&dict_entry_writer);
367 dict_entry_writer.AppendString("foo"); 369 dict_entry_writer.AppendString("foo");
368 dict_entry_writer.AppendInt64(GG_INT64_C(1234567890123456789)); 370 dict_entry_writer.AppendInt64(GG_INT64_C(1234567890123456789));
369 dict_array_writer.CloseContainer(&dict_entry_writer); 371 dict_array_writer.CloseContainer(&dict_entry_writer);
370 } 372 }
371 variant_writer.CloseContainer(&dict_array_writer); 373 variant_writer.CloseContainer(&dict_array_writer);
372 } 374 }
373 array_writer.CloseContainer(&variant_writer); 375 array_writer.CloseContainer(&variant_writer);
374 } 376 }
375 } 377 }
(...skipping 11 matching lines...) Expand all
387 " }\n" 389 " }\n"
388 " variant array [\n" 390 " variant array [\n"
389 " dict entry {\n" 391 " dict entry {\n"
390 " string \"foo\"\n" 392 " string \"foo\"\n"
391 " int64 1234567890123456789\n" 393 " int64 1234567890123456789\n"
392 " }\n" 394 " }\n"
393 " ]\n" 395 " ]\n"
394 "]\n", 396 "]\n",
395 message->ToString()); 397 message->ToString());
396 398
397 dbus::MessageReader reader(message.get()); 399 MessageReader reader(message.get());
398 dbus::MessageReader array_reader(NULL); 400 MessageReader array_reader(NULL);
399 ASSERT_TRUE(reader.PopArray(&array_reader)); 401 ASSERT_TRUE(reader.PopArray(&array_reader));
400 402
401 // The first value in the array. 403 // The first value in the array.
402 bool bool_value = false; 404 bool bool_value = false;
403 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value)); 405 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
404 EXPECT_EQ(true, bool_value); 406 EXPECT_EQ(true, bool_value);
405 407
406 // The second value in the array. 408 // The second value in the array.
407 { 409 {
408 dbus::MessageReader variant_reader(NULL); 410 MessageReader variant_reader(NULL);
409 ASSERT_TRUE(array_reader.PopVariant(&variant_reader)); 411 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
410 { 412 {
411 dbus::MessageReader struct_reader(NULL); 413 MessageReader struct_reader(NULL);
412 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader)); 414 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
413 std::string string_value; 415 std::string string_value;
414 ASSERT_TRUE(struct_reader.PopString(&string_value)); 416 ASSERT_TRUE(struct_reader.PopString(&string_value));
415 EXPECT_EQ("string", string_value); 417 EXPECT_EQ("string", string_value);
416 int32 int32_value = 0; 418 int32 int32_value = 0;
417 ASSERT_TRUE(struct_reader.PopInt32(&int32_value)); 419 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
418 EXPECT_EQ(123, int32_value); 420 EXPECT_EQ(123, int32_value);
419 ASSERT_FALSE(struct_reader.HasMoreData()); 421 ASSERT_FALSE(struct_reader.HasMoreData());
420 } 422 }
421 ASSERT_FALSE(variant_reader.HasMoreData()); 423 ASSERT_FALSE(variant_reader.HasMoreData());
422 } 424 }
423 425
424 // The third value in the array. 426 // The third value in the array.
425 { 427 {
426 dbus::MessageReader variant_reader(NULL); 428 MessageReader variant_reader(NULL);
427 ASSERT_TRUE(array_reader.PopVariant(&variant_reader)); 429 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
428 { 430 {
429 dbus::MessageReader dict_array_reader(NULL); 431 MessageReader dict_array_reader(NULL);
430 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader)); 432 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
431 { 433 {
432 dbus::MessageReader dict_entry_reader(NULL); 434 MessageReader dict_entry_reader(NULL);
433 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader)); 435 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
434 std::string string_value; 436 std::string string_value;
435 ASSERT_TRUE(dict_entry_reader.PopString(&string_value)); 437 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
436 EXPECT_EQ("foo", string_value); 438 EXPECT_EQ("foo", string_value);
437 int64 int64_value = 0; 439 int64 int64_value = 0;
438 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value)); 440 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
439 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value); 441 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value);
440 } 442 }
441 ASSERT_FALSE(dict_array_reader.HasMoreData()); 443 ASSERT_FALSE(dict_array_reader.HasMoreData());
442 } 444 }
443 ASSERT_FALSE(variant_reader.HasMoreData()); 445 ASSERT_FALSE(variant_reader.HasMoreData());
444 } 446 }
445 ASSERT_FALSE(array_reader.HasMoreData()); 447 ASSERT_FALSE(array_reader.HasMoreData());
446 ASSERT_FALSE(reader.HasMoreData()); 448 ASSERT_FALSE(reader.HasMoreData());
447 } 449 }
448 450
449 TEST(MessageTest, MethodCall) { 451 TEST(MessageTest, MethodCall) {
450 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); 452 MethodCall method_call("com.example.Interface", "SomeMethod");
451 EXPECT_TRUE(method_call.raw_message() != NULL); 453 EXPECT_TRUE(method_call.raw_message() != NULL);
452 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType()); 454 EXPECT_EQ(Message::MESSAGE_METHOD_CALL, method_call.GetMessageType());
453 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call.GetMessageTypeAsString()); 455 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call.GetMessageTypeAsString());
454 method_call.SetDestination("com.example.Service"); 456 method_call.SetDestination("com.example.Service");
455 method_call.SetPath(dbus::ObjectPath("/com/example/Object")); 457 method_call.SetPath(ObjectPath("/com/example/Object"));
456 458
457 dbus::MessageWriter writer(&method_call); 459 MessageWriter writer(&method_call);
458 writer.AppendString("payload"); 460 writer.AppendString("payload");
459 461
460 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n" 462 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
461 "destination: com.example.Service\n" 463 "destination: com.example.Service\n"
462 "path: /com/example/Object\n" 464 "path: /com/example/Object\n"
463 "interface: com.example.Interface\n" 465 "interface: com.example.Interface\n"
464 "member: SomeMethod\n" 466 "member: SomeMethod\n"
465 "signature: s\n" 467 "signature: s\n"
466 "\n" 468 "\n"
467 "string \"payload\"\n", 469 "string \"payload\"\n",
468 method_call.ToString()); 470 method_call.ToString());
469 } 471 }
470 472
471 TEST(MessageTest, MethodCall_FromRawMessage) { 473 TEST(MessageTest, MethodCall_FromRawMessage) {
472 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL); 474 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
473 dbus_message_set_interface(raw_message, "com.example.Interface"); 475 dbus_message_set_interface(raw_message, "com.example.Interface");
474 dbus_message_set_member(raw_message, "SomeMethod"); 476 dbus_message_set_member(raw_message, "SomeMethod");
475 477
476 scoped_ptr<dbus::MethodCall> method_call( 478 scoped_ptr<MethodCall> method_call(MethodCall::FromRawMessage(raw_message));
477 dbus::MethodCall::FromRawMessage(raw_message));
478 EXPECT_EQ("com.example.Interface", method_call->GetInterface()); 479 EXPECT_EQ("com.example.Interface", method_call->GetInterface());
479 EXPECT_EQ("SomeMethod", method_call->GetMember()); 480 EXPECT_EQ("SomeMethod", method_call->GetMember());
480 } 481 }
481 482
482 TEST(MessageTest, Signal) { 483 TEST(MessageTest, Signal) {
483 dbus::Signal signal("com.example.Interface", "SomeSignal"); 484 Signal signal("com.example.Interface", "SomeSignal");
484 EXPECT_TRUE(signal.raw_message() != NULL); 485 EXPECT_TRUE(signal.raw_message() != NULL);
485 EXPECT_EQ(dbus::Message::MESSAGE_SIGNAL, signal.GetMessageType()); 486 EXPECT_EQ(Message::MESSAGE_SIGNAL, signal.GetMessageType());
486 EXPECT_EQ("MESSAGE_SIGNAL", signal.GetMessageTypeAsString()); 487 EXPECT_EQ("MESSAGE_SIGNAL", signal.GetMessageTypeAsString());
487 signal.SetPath(dbus::ObjectPath("/com/example/Object")); 488 signal.SetPath(ObjectPath("/com/example/Object"));
488 489
489 dbus::MessageWriter writer(&signal); 490 MessageWriter writer(&signal);
490 writer.AppendString("payload"); 491 writer.AppendString("payload");
491 492
492 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n" 493 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
493 "path: /com/example/Object\n" 494 "path: /com/example/Object\n"
494 "interface: com.example.Interface\n" 495 "interface: com.example.Interface\n"
495 "member: SomeSignal\n" 496 "member: SomeSignal\n"
496 "signature: s\n" 497 "signature: s\n"
497 "\n" 498 "\n"
498 "string \"payload\"\n", 499 "string \"payload\"\n",
499 signal.ToString()); 500 signal.ToString());
500 } 501 }
501 502
502 TEST(MessageTest, Signal_FromRawMessage) { 503 TEST(MessageTest, Signal_FromRawMessage) {
503 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL); 504 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
504 dbus_message_set_interface(raw_message, "com.example.Interface"); 505 dbus_message_set_interface(raw_message, "com.example.Interface");
505 dbus_message_set_member(raw_message, "SomeSignal"); 506 dbus_message_set_member(raw_message, "SomeSignal");
506 507
507 scoped_ptr<dbus::Signal> signal( 508 scoped_ptr<Signal> signal(Signal::FromRawMessage(raw_message));
508 dbus::Signal::FromRawMessage(raw_message));
509 EXPECT_EQ("com.example.Interface", signal->GetInterface()); 509 EXPECT_EQ("com.example.Interface", signal->GetInterface());
510 EXPECT_EQ("SomeSignal", signal->GetMember()); 510 EXPECT_EQ("SomeSignal", signal->GetMember());
511 } 511 }
512 512
513 TEST(MessageTest, Response) { 513 TEST(MessageTest, Response) {
514 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); 514 scoped_ptr<Response> response(Response::CreateEmpty());
515 EXPECT_TRUE(response->raw_message()); 515 EXPECT_TRUE(response->raw_message());
516 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response->GetMessageType()); 516 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
517 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString()); 517 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
518 } 518 }
519 519
520 TEST(MessageTest, Response_FromMethodCall) { 520 TEST(MessageTest, Response_FromMethodCall) {
521 const uint32 kSerial = 123; 521 const uint32 kSerial = 123;
522 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); 522 MethodCall method_call("com.example.Interface", "SomeMethod");
523 method_call.SetSerial(kSerial); 523 method_call.SetSerial(kSerial);
524 524
525 scoped_ptr<dbus::Response> response( 525 scoped_ptr<Response> response(
526 dbus::Response::FromMethodCall(&method_call)); 526 Response::FromMethodCall(&method_call));
527 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response->GetMessageType()); 527 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
528 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString()); 528 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
529 // The serial should be copied to the reply serial. 529 // The serial should be copied to the reply serial.
530 EXPECT_EQ(kSerial, response->GetReplySerial()); 530 EXPECT_EQ(kSerial, response->GetReplySerial());
531 } 531 }
532 532
533 TEST(MessageTest, ErrorResponse_FromMethodCall) { 533 TEST(MessageTest, ErrorResponse_FromMethodCall) {
534 const uint32 kSerial = 123; 534 const uint32 kSerial = 123;
535 const char kErrorMessage[] = "error message"; 535 const char kErrorMessage[] = "error message";
536 536
537 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); 537 MethodCall method_call("com.example.Interface", "SomeMethod");
538 method_call.SetSerial(kSerial); 538 method_call.SetSerial(kSerial);
539 539
540 scoped_ptr<dbus::ErrorResponse> error_response( 540 scoped_ptr<ErrorResponse> error_response(
541 dbus::ErrorResponse::FromMethodCall(&method_call, 541 ErrorResponse::FromMethodCall(&method_call,
542 DBUS_ERROR_FAILED, 542 DBUS_ERROR_FAILED,
543 kErrorMessage)); 543 kErrorMessage));
544 EXPECT_EQ(dbus::Message::MESSAGE_ERROR, error_response->GetMessageType()); 544 EXPECT_EQ(Message::MESSAGE_ERROR, error_response->GetMessageType());
545 EXPECT_EQ("MESSAGE_ERROR", error_response->GetMessageTypeAsString()); 545 EXPECT_EQ("MESSAGE_ERROR", error_response->GetMessageTypeAsString());
546 // The serial should be copied to the reply serial. 546 // The serial should be copied to the reply serial.
547 EXPECT_EQ(kSerial, error_response->GetReplySerial()); 547 EXPECT_EQ(kSerial, error_response->GetReplySerial());
548 548
549 // Error message should be added to the payload. 549 // Error message should be added to the payload.
550 dbus::MessageReader reader(error_response.get()); 550 MessageReader reader(error_response.get());
551 std::string error_message; 551 std::string error_message;
552 ASSERT_TRUE(reader.PopString(&error_message)); 552 ASSERT_TRUE(reader.PopString(&error_message));
553 EXPECT_EQ(kErrorMessage, error_message); 553 EXPECT_EQ(kErrorMessage, error_message);
554 } 554 }
555 555
556 TEST(MessageTest, GetAndSetHeaders) { 556 TEST(MessageTest, GetAndSetHeaders) {
557 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 557 scoped_ptr<Response> message(Response::CreateEmpty());
558 558
559 EXPECT_EQ("", message->GetDestination()); 559 EXPECT_EQ("", message->GetDestination());
560 EXPECT_EQ(dbus::ObjectPath(std::string()), message->GetPath()); 560 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
561 EXPECT_EQ("", message->GetInterface()); 561 EXPECT_EQ("", message->GetInterface());
562 EXPECT_EQ("", message->GetMember()); 562 EXPECT_EQ("", message->GetMember());
563 EXPECT_EQ("", message->GetErrorName()); 563 EXPECT_EQ("", message->GetErrorName());
564 EXPECT_EQ("", message->GetSender()); 564 EXPECT_EQ("", message->GetSender());
565 EXPECT_EQ(0U, message->GetSerial()); 565 EXPECT_EQ(0U, message->GetSerial());
566 EXPECT_EQ(0U, message->GetReplySerial()); 566 EXPECT_EQ(0U, message->GetReplySerial());
567 567
568 EXPECT_TRUE(message->SetDestination("org.chromium.destination")); 568 EXPECT_TRUE(message->SetDestination("org.chromium.destination"));
569 EXPECT_TRUE(message->SetPath(dbus::ObjectPath("/org/chromium/path"))); 569 EXPECT_TRUE(message->SetPath(ObjectPath("/org/chromium/path")));
570 EXPECT_TRUE(message->SetInterface("org.chromium.interface")); 570 EXPECT_TRUE(message->SetInterface("org.chromium.interface"));
571 EXPECT_TRUE(message->SetMember("member")); 571 EXPECT_TRUE(message->SetMember("member"));
572 EXPECT_TRUE(message->SetErrorName("org.chromium.error")); 572 EXPECT_TRUE(message->SetErrorName("org.chromium.error"));
573 EXPECT_TRUE(message->SetSender(":1.2")); 573 EXPECT_TRUE(message->SetSender(":1.2"));
574 message->SetSerial(123); 574 message->SetSerial(123);
575 message->SetReplySerial(456); 575 message->SetReplySerial(456);
576 576
577 EXPECT_EQ("org.chromium.destination", message->GetDestination()); 577 EXPECT_EQ("org.chromium.destination", message->GetDestination());
578 EXPECT_EQ(dbus::ObjectPath("/org/chromium/path"), message->GetPath()); 578 EXPECT_EQ(ObjectPath("/org/chromium/path"), message->GetPath());
579 EXPECT_EQ("org.chromium.interface", message->GetInterface()); 579 EXPECT_EQ("org.chromium.interface", message->GetInterface());
580 EXPECT_EQ("member", message->GetMember()); 580 EXPECT_EQ("member", message->GetMember());
581 EXPECT_EQ("org.chromium.error", message->GetErrorName()); 581 EXPECT_EQ("org.chromium.error", message->GetErrorName());
582 EXPECT_EQ(":1.2", message->GetSender()); 582 EXPECT_EQ(":1.2", message->GetSender());
583 EXPECT_EQ(123U, message->GetSerial()); 583 EXPECT_EQ(123U, message->GetSerial());
584 EXPECT_EQ(456U, message->GetReplySerial()); 584 EXPECT_EQ(456U, message->GetReplySerial());
585 } 585 }
586 586
587 TEST(MessageTest, SetInvalidHeaders) { 587 TEST(MessageTest, SetInvalidHeaders) {
588 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 588 scoped_ptr<Response> message(Response::CreateEmpty());
589 EXPECT_EQ("", message->GetDestination()); 589 EXPECT_EQ("", message->GetDestination());
590 EXPECT_EQ(dbus::ObjectPath(std::string()), message->GetPath()); 590 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
591 EXPECT_EQ("", message->GetInterface()); 591 EXPECT_EQ("", message->GetInterface());
592 EXPECT_EQ("", message->GetMember()); 592 EXPECT_EQ("", message->GetMember());
593 EXPECT_EQ("", message->GetErrorName()); 593 EXPECT_EQ("", message->GetErrorName());
594 EXPECT_EQ("", message->GetSender()); 594 EXPECT_EQ("", message->GetSender());
595 595
596 // Empty element between periods. 596 // Empty element between periods.
597 EXPECT_FALSE(message->SetDestination("org..chromium")); 597 EXPECT_FALSE(message->SetDestination("org..chromium"));
598 // Trailing '/' is only allowed for the root path. 598 // Trailing '/' is only allowed for the root path.
599 EXPECT_FALSE(message->SetPath(dbus::ObjectPath("/org/chromium/"))); 599 EXPECT_FALSE(message->SetPath(ObjectPath("/org/chromium/")));
600 // Interface name cannot contain '/'. 600 // Interface name cannot contain '/'.
601 EXPECT_FALSE(message->SetInterface("org/chromium/interface")); 601 EXPECT_FALSE(message->SetInterface("org/chromium/interface"));
602 // Member name cannot begin with a digit. 602 // Member name cannot begin with a digit.
603 EXPECT_FALSE(message->SetMember("1member")); 603 EXPECT_FALSE(message->SetMember("1member"));
604 // Error name cannot begin with a period. 604 // Error name cannot begin with a period.
605 EXPECT_FALSE(message->SetErrorName(".org.chromium.error")); 605 EXPECT_FALSE(message->SetErrorName(".org.chromium.error"));
606 // Disallowed characters. 606 // Disallowed characters.
607 EXPECT_FALSE(message->SetSender("?!#*")); 607 EXPECT_FALSE(message->SetSender("?!#*"));
608 608
609 EXPECT_EQ("", message->GetDestination()); 609 EXPECT_EQ("", message->GetDestination());
610 EXPECT_EQ(dbus::ObjectPath(std::string()), message->GetPath()); 610 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
611 EXPECT_EQ("", message->GetInterface()); 611 EXPECT_EQ("", message->GetInterface());
612 EXPECT_EQ("", message->GetMember()); 612 EXPECT_EQ("", message->GetMember());
613 EXPECT_EQ("", message->GetErrorName()); 613 EXPECT_EQ("", message->GetErrorName());
614 EXPECT_EQ("", message->GetSender()); 614 EXPECT_EQ("", message->GetSender());
615 } 615 }
616 616
617 TEST(MessageTest, ToString_LongString) { 617 TEST(MessageTest, ToString_LongString) {
618 const std::string kLongString(1000, 'o'); 618 const std::string kLongString(1000, 'o');
619 619
620 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty()); 620 scoped_ptr<Response> message(Response::CreateEmpty());
621 dbus::MessageWriter writer(message.get()); 621 MessageWriter writer(message.get());
622 writer.AppendString(kLongString); 622 writer.AppendString(kLongString);
623 623
624 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n" 624 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
625 "signature: s\n\n" 625 "signature: s\n\n"
626 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo" 626 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo"
627 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... " 627 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... "
628 "(1000 bytes in total)\"\n", 628 "(1000 bytes in total)\"\n",
629 message->ToString()); 629 message->ToString());
630 } 630 }
631
632 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/message.cc ('k') | dbus/mock_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698