| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 "chrome/common/safe_browsing/ipc_protobuf_message_test.pb.h" |
| 6 #include "ipc/ipc_message.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 #define IPC_MESSAGE_IMPL |
| 10 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h" |
| 11 |
| 12 // Generate ipc protobuf traits write methods. |
| 13 #include "chrome/common/safe_browsing/protobuf_message_write_macros.h" |
| 14 namespace IPC { |
| 15 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h" |
| 16 } // namespace IPC |
| 17 |
| 18 // Generate ipc protobuf traits read methods. |
| 19 #include "chrome/common/safe_browsing/protobuf_message_read_macros.h" |
| 20 namespace IPC { |
| 21 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h" |
| 22 } // namespace IPC |
| 23 |
| 24 // Generate ipc protobuf traits log methods. |
| 25 #include "chrome/common/safe_browsing/protobuf_message_log_macros.h" |
| 26 namespace IPC { |
| 27 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h" |
| 28 } // namespace IPC |
| 29 |
| 30 class IPCProtobufMessageTest : public ::testing::TestWithParam<bool> { |
| 31 protected: |
| 32 IPCProtobufMessageTest() : field_is_present_(GetParam()) {} |
| 33 |
| 34 bool field_is_present_; |
| 35 }; |
| 36 |
| 37 // Tests writing and reading a message with an optional fundamental field. |
| 38 TEST_P(IPCProtobufMessageTest, FundamentalField) { |
| 39 TestMessage input; |
| 40 |
| 41 if (field_is_present_) |
| 42 input.set_fund_int(42); |
| 43 |
| 44 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 45 IPC::WriteParam(&msg, input); |
| 46 |
| 47 TestMessage output; |
| 48 PickleIterator iter(msg); |
| 49 ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 50 |
| 51 if (field_is_present_) { |
| 52 ASSERT_TRUE(output.has_fund_int()); |
| 53 EXPECT_EQ(input.fund_int(), output.fund_int()); |
| 54 } else { |
| 55 ASSERT_FALSE(output.has_fund_int()); |
| 56 } |
| 57 } |
| 58 |
| 59 // Tests writing and reading a message with an optional string field. |
| 60 TEST_P(IPCProtobufMessageTest, StringField) { |
| 61 TestMessage input; |
| 62 |
| 63 if (field_is_present_) |
| 64 input.set_op_comp_string("some string"); |
| 65 |
| 66 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 67 IPC::WriteParam(&msg, input); |
| 68 |
| 69 TestMessage output; |
| 70 PickleIterator iter(msg); |
| 71 ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 72 |
| 73 if (field_is_present_) { |
| 74 ASSERT_TRUE(output.has_op_comp_string()); |
| 75 EXPECT_EQ(input.op_comp_string(), output.op_comp_string()); |
| 76 } else { |
| 77 ASSERT_FALSE(output.has_op_comp_string()); |
| 78 } |
| 79 } |
| 80 |
| 81 // Tests writing and reading a message with an optional bytes field. |
| 82 TEST_P(IPCProtobufMessageTest, BytesField) { |
| 83 TestMessage input; |
| 84 |
| 85 if (field_is_present_) |
| 86 input.set_op_comp_bytes("some string"); |
| 87 |
| 88 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 89 IPC::WriteParam(&msg, input); |
| 90 |
| 91 TestMessage output; |
| 92 PickleIterator iter(msg); |
| 93 ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 94 |
| 95 if (field_is_present_) { |
| 96 ASSERT_TRUE(output.has_op_comp_bytes()); |
| 97 EXPECT_EQ(input.op_comp_bytes(), output.op_comp_bytes()); |
| 98 } else { |
| 99 ASSERT_FALSE(output.has_op_comp_bytes()); |
| 100 } |
| 101 } |
| 102 |
| 103 // Tests writing and reading a message with an optional submessage field. |
| 104 TEST_P(IPCProtobufMessageTest, OptionalSubmessage) { |
| 105 TestMessage input; |
| 106 |
| 107 if (field_is_present_) |
| 108 input.mutable_op_comp_sub()->set_foo(47); |
| 109 |
| 110 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 111 IPC::WriteParam(&msg, input); |
| 112 |
| 113 TestMessage output; |
| 114 PickleIterator iter(msg); |
| 115 ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 116 |
| 117 if (field_is_present_) { |
| 118 ASSERT_TRUE(output.has_op_comp_sub()); |
| 119 ASSERT_TRUE(output.op_comp_sub().has_foo()); |
| 120 EXPECT_EQ(input.op_comp_sub().foo(), output.op_comp_sub().foo()); |
| 121 } else { |
| 122 ASSERT_FALSE(output.has_op_comp_sub()); |
| 123 } |
| 124 } |
| 125 |
| 126 // Tests writing and reading a message with a repeated submessage field. |
| 127 TEST_P(IPCProtobufMessageTest, RepeatedSubmessage) { |
| 128 TestMessage input; |
| 129 |
| 130 if (field_is_present_) { |
| 131 input.add_rep_comp_sub()->set_foo(0); |
| 132 input.add_rep_comp_sub()->set_foo(1); |
| 133 input.add_rep_comp_sub()->set_foo(2); |
| 134 } |
| 135 |
| 136 IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL); |
| 137 IPC::WriteParam(&msg, input); |
| 138 |
| 139 TestMessage output; |
| 140 PickleIterator iter(msg); |
| 141 ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output)); |
| 142 |
| 143 if (field_is_present_) { |
| 144 ASSERT_EQ(3, output.rep_comp_sub_size()); |
| 145 ASSERT_TRUE(output.rep_comp_sub(0).has_foo()); |
| 146 EXPECT_EQ(input.rep_comp_sub(0).foo(), output.rep_comp_sub(0).foo()); |
| 147 ASSERT_TRUE(output.rep_comp_sub(1).has_foo()); |
| 148 EXPECT_EQ(input.rep_comp_sub(1).foo(), output.rep_comp_sub(1).foo()); |
| 149 ASSERT_TRUE(output.rep_comp_sub(2).has_foo()); |
| 150 EXPECT_EQ(input.rep_comp_sub(2).foo(), output.rep_comp_sub(2).foo()); |
| 151 } else { |
| 152 ASSERT_EQ(0, output.rep_comp_sub_size()); |
| 153 } |
| 154 } |
| 155 |
| 156 INSTANTIATE_TEST_CASE_P(IPCProtobufMessage, |
| 157 IPCProtobufMessageTest, |
| 158 ::testing::Bool()); |
| OLD | NEW |