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