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