| OLD | NEW |
| 1 // Copyright (c) 2011 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 "ipc/ipc_message.h" | 5 #include "ipc/ipc_message.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "ipc/ipc_message_utils.h" | 11 #include "ipc/ipc_message_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 TEST(IPCMessageTest, ListValue) { | 14 TEST(IPCMessageTest, ListValue) { |
| 15 ListValue input; | 15 ListValue input; |
| 16 input.Set(0, Value::CreateDoubleValue(42.42)); | 16 input.Set(0, Value::CreateDoubleValue(42.42)); |
| 17 input.Set(1, Value::CreateStringValue("forty")); | 17 input.Set(1, Value::CreateStringValue("forty")); |
| 18 input.Set(2, Value::CreateNullValue()); | 18 input.Set(2, Value::CreateNullValue()); |
| 19 | 19 |
| 20 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 20 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 21 IPC::WriteParam(&msg, input); | 21 IPC::WriteParam(&msg, input); |
| 22 | 22 |
| 23 ListValue output; | 23 ListValue output; |
| 24 void* iter = NULL; | 24 PickleIterator iter(msg); |
| 25 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); | 25 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 26 | 26 |
| 27 EXPECT_TRUE(input.Equals(&output)); | 27 EXPECT_TRUE(input.Equals(&output)); |
| 28 | 28 |
| 29 // Also test the corrupt case. | 29 // Also test the corrupt case. |
| 30 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 30 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 31 bad_msg.WriteInt(99); | 31 bad_msg.WriteInt(99); |
| 32 iter = NULL; | 32 iter = PickleIterator(bad_msg); |
| 33 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); | 33 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 TEST(IPCMessageTest, DictionaryValue) { | 36 TEST(IPCMessageTest, DictionaryValue) { |
| 37 DictionaryValue input; | 37 DictionaryValue input; |
| 38 input.Set("null", Value::CreateNullValue()); | 38 input.Set("null", Value::CreateNullValue()); |
| 39 input.Set("bool", Value::CreateBooleanValue(true)); | 39 input.Set("bool", Value::CreateBooleanValue(true)); |
| 40 input.Set("int", Value::CreateIntegerValue(42)); | 40 input.Set("int", Value::CreateIntegerValue(42)); |
| 41 input.SetWithoutPathExpansion("int.with.dot", Value::CreateIntegerValue(43)); | 41 input.SetWithoutPathExpansion("int.with.dot", Value::CreateIntegerValue(43)); |
| 42 | 42 |
| 43 scoped_ptr<DictionaryValue> subdict(new DictionaryValue()); | 43 scoped_ptr<DictionaryValue> subdict(new DictionaryValue()); |
| 44 subdict->Set("str", Value::CreateStringValue("forty two")); | 44 subdict->Set("str", Value::CreateStringValue("forty two")); |
| 45 subdict->Set("bool", Value::CreateBooleanValue(false)); | 45 subdict->Set("bool", Value::CreateBooleanValue(false)); |
| 46 | 46 |
| 47 scoped_ptr<ListValue> sublist(new ListValue()); | 47 scoped_ptr<ListValue> sublist(new ListValue()); |
| 48 sublist->Set(0, Value::CreateDoubleValue(42.42)); | 48 sublist->Set(0, Value::CreateDoubleValue(42.42)); |
| 49 sublist->Set(1, Value::CreateStringValue("forty")); | 49 sublist->Set(1, Value::CreateStringValue("forty")); |
| 50 sublist->Set(2, Value::CreateStringValue("two")); | 50 sublist->Set(2, Value::CreateStringValue("two")); |
| 51 subdict->Set("list", sublist.release()); | 51 subdict->Set("list", sublist.release()); |
| 52 | 52 |
| 53 input.Set("dict", subdict.release()); | 53 input.Set("dict", subdict.release()); |
| 54 | 54 |
| 55 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 55 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 56 IPC::WriteParam(&msg, input); | 56 IPC::WriteParam(&msg, input); |
| 57 | 57 |
| 58 DictionaryValue output; | 58 DictionaryValue output; |
| 59 void* iter = NULL; | 59 PickleIterator iter(msg); |
| 60 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); | 60 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 61 | 61 |
| 62 EXPECT_TRUE(input.Equals(&output)); | 62 EXPECT_TRUE(input.Equals(&output)); |
| 63 | 63 |
| 64 // Also test the corrupt case. | 64 // Also test the corrupt case. |
| 65 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); | 65 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 66 bad_msg.WriteInt(99); | 66 bad_msg.WriteInt(99); |
| 67 iter = NULL; | 67 iter = PickleIterator(bad_msg); |
| 68 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); | 68 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 69 } | 69 } |
| OLD | NEW |