OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 <string.h> |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/common/common_param_traits.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 #include "ipc/ipc_message.h" |
| 12 #include "ipc/ipc_message_utils.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 |
| 16 // Tests that serialize/deserialize correctly understand each other |
| 17 TEST(IPCMessageTest, Serialize) { |
| 18 const char* serialize_cases[] = { |
| 19 "http://www.google.com/", |
| 20 "http://user:pass@host.com:888/foo;bar?baz#nop", |
| 21 "#inva://idurl/", |
| 22 }; |
| 23 |
| 24 for (size_t i = 0; i < arraysize(serialize_cases); i++) { |
| 25 GURL input(serialize_cases[i]); |
| 26 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 27 IPC::ParamTraits<GURL>::Write(&msg, input); |
| 28 |
| 29 GURL output; |
| 30 void* iter = NULL; |
| 31 EXPECT_TRUE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
| 32 |
| 33 // We want to test each component individually to make sure its range was |
| 34 // correctly serialized and deserialized, not just the spec. |
| 35 EXPECT_EQ(input.possibly_invalid_spec(), output.possibly_invalid_spec()); |
| 36 EXPECT_EQ(input.is_valid(), output.is_valid()); |
| 37 EXPECT_EQ(input.scheme(), output.scheme()); |
| 38 EXPECT_EQ(input.username(), output.username()); |
| 39 EXPECT_EQ(input.password(), output.password()); |
| 40 EXPECT_EQ(input.host(), output.host()); |
| 41 EXPECT_EQ(input.port(), output.port()); |
| 42 EXPECT_EQ(input.path(), output.path()); |
| 43 EXPECT_EQ(input.query(), output.query()); |
| 44 EXPECT_EQ(input.ref(), output.ref()); |
| 45 } |
| 46 |
| 47 // Also test the corrupt case. |
| 48 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 49 msg.WriteInt(99); |
| 50 GURL output; |
| 51 void* iter = NULL; |
| 52 EXPECT_FALSE(IPC::ParamTraits<GURL>::Read(&msg, &iter, &output)); |
| 53 } |
| 54 |
| 55 // Tests bitmap serialization. |
| 56 TEST(IPCMessageTest, Bitmap) { |
| 57 SkBitmap bitmap; |
| 58 |
| 59 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 5); |
| 60 bitmap.allocPixels(); |
| 61 memset(bitmap.getPixels(), 'A', bitmap.getSize()); |
| 62 |
| 63 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 64 IPC::ParamTraits<SkBitmap>::Write(&msg, bitmap); |
| 65 |
| 66 SkBitmap output; |
| 67 void* iter = NULL; |
| 68 EXPECT_TRUE(IPC::ParamTraits<SkBitmap>::Read(&msg, &iter, &output)); |
| 69 |
| 70 EXPECT_EQ(bitmap.config(), output.config()); |
| 71 EXPECT_EQ(bitmap.width(), output.width()); |
| 72 EXPECT_EQ(bitmap.height(), output.height()); |
| 73 EXPECT_EQ(bitmap.rowBytes(), output.rowBytes()); |
| 74 EXPECT_EQ(bitmap.getSize(), output.getSize()); |
| 75 EXPECT_EQ(memcmp(bitmap.getPixels(), output.getPixels(), bitmap.getSize()), |
| 76 0); |
| 77 |
| 78 // Also test the corrupt case. |
| 79 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 80 // Copy the first message block over to |bad_msg|. |
| 81 const char* fixed_data; |
| 82 int fixed_data_size; |
| 83 iter = NULL; |
| 84 msg.ReadData(&iter, &fixed_data, &fixed_data_size); |
| 85 bad_msg.WriteData(fixed_data, fixed_data_size); |
| 86 // Add some bogus pixel data. |
| 87 const size_t bogus_pixels_size = bitmap.getSize() * 2; |
| 88 scoped_array<char> bogus_pixels(new char[bogus_pixels_size]); |
| 89 memset(bogus_pixels.get(), 'B', bogus_pixels_size); |
| 90 bad_msg.WriteData(bogus_pixels.get(), bogus_pixels_size); |
| 91 // Make sure we don't read out the bitmap! |
| 92 SkBitmap bad_output; |
| 93 iter = NULL; |
| 94 EXPECT_FALSE(IPC::ParamTraits<SkBitmap>::Read(&bad_msg, &iter, &bad_output)); |
| 95 } |
| 96 |
| 97 TEST(IPCMessageTest, ListValue) { |
| 98 ListValue input; |
| 99 input.Set(0, Value::CreateRealValue(42.42)); |
| 100 input.Set(1, Value::CreateStringValue("forty")); |
| 101 input.Set(2, Value::CreateNullValue()); |
| 102 |
| 103 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 104 IPC::WriteParam(&msg, input); |
| 105 |
| 106 ListValue output; |
| 107 void* iter = NULL; |
| 108 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 109 |
| 110 EXPECT_TRUE(input.Equals(&output)); |
| 111 |
| 112 // Also test the corrupt case. |
| 113 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 114 bad_msg.WriteInt(99); |
| 115 iter = NULL; |
| 116 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 117 } |
| 118 |
| 119 TEST(IPCMessageTest, DictionaryValue) { |
| 120 DictionaryValue input; |
| 121 input.Set(L"null", Value::CreateNullValue()); |
| 122 input.Set(L"bool", Value::CreateBooleanValue(true)); |
| 123 input.Set(L"int", Value::CreateIntegerValue(42)); |
| 124 |
| 125 scoped_ptr<DictionaryValue> subdict(new DictionaryValue()); |
| 126 subdict->Set(L"str", Value::CreateStringValue("forty two")); |
| 127 subdict->Set(L"bool", Value::CreateBooleanValue(false)); |
| 128 |
| 129 scoped_ptr<ListValue> sublist(new ListValue()); |
| 130 sublist->Set(0, Value::CreateRealValue(42.42)); |
| 131 sublist->Set(1, Value::CreateStringValue("forty")); |
| 132 sublist->Set(2, Value::CreateStringValue("two")); |
| 133 subdict->Set(L"list", sublist.release()); |
| 134 |
| 135 input.Set(L"dict", subdict.release()); |
| 136 |
| 137 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 138 IPC::WriteParam(&msg, input); |
| 139 |
| 140 DictionaryValue output; |
| 141 void* iter = NULL; |
| 142 EXPECT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 143 |
| 144 EXPECT_TRUE(input.Equals(&output)); |
| 145 |
| 146 // Also test the corrupt case. |
| 147 IPC::Message bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 148 bad_msg.WriteInt(99); |
| 149 iter = NULL; |
| 150 EXPECT_FALSE(IPC::ReadParam(&bad_msg, &iter, &output)); |
| 151 } |
OLD | NEW |