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