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.
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"
9 #define IPC_MESSAGE_IMPL
10 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
12 // Generate ipc protobuf traits write methods.
13 #include "chrome/common/safe_browsing/protobuf_message_write_macros.h"
15 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
18 // Generate ipc protobuf traits read methods.
19 #include "chrome/common/safe_browsing/protobuf_message_read_macros.h"
21 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
24 // Generate ipc protobuf traits log methods.
25 #include "chrome/common/safe_browsing/protobuf_message_log_macros.h"
27 #include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
30 class IPCProtobufMessageTest
: public ::testing::TestWithParam
<bool> {
32 IPCProtobufMessageTest() : field_is_present_(GetParam()) {}
34 bool field_is_present_
;
37 // Tests writing and reading a message with an optional fundamental field.
38 TEST_P(IPCProtobufMessageTest
, FundamentalField
) {
41 if (field_is_present_
)
42 input
.set_fund_int(42);
44 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
45 IPC::WriteParam(&msg
, input
);
48 base::PickleIterator
iter(msg
);
49 ASSERT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
51 if (field_is_present_
) {
52 ASSERT_TRUE(output
.has_fund_int());
53 EXPECT_EQ(input
.fund_int(), output
.fund_int());
55 ASSERT_FALSE(output
.has_fund_int());
59 // Tests writing and reading a message with an optional string field.
60 TEST_P(IPCProtobufMessageTest
, StringField
) {
63 if (field_is_present_
)
64 input
.set_op_comp_string("some string");
66 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
67 IPC::WriteParam(&msg
, input
);
70 base::PickleIterator
iter(msg
);
71 ASSERT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
73 if (field_is_present_
) {
74 ASSERT_TRUE(output
.has_op_comp_string());
75 EXPECT_EQ(input
.op_comp_string(), output
.op_comp_string());
77 ASSERT_FALSE(output
.has_op_comp_string());
81 // Tests writing and reading a message with an optional bytes field.
82 TEST_P(IPCProtobufMessageTest
, BytesField
) {
85 if (field_is_present_
)
86 input
.set_op_comp_bytes("some string");
88 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
89 IPC::WriteParam(&msg
, input
);
92 base::PickleIterator
iter(msg
);
93 ASSERT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
95 if (field_is_present_
) {
96 ASSERT_TRUE(output
.has_op_comp_bytes());
97 EXPECT_EQ(input
.op_comp_bytes(), output
.op_comp_bytes());
99 ASSERT_FALSE(output
.has_op_comp_bytes());
103 // Tests writing and reading a message with an optional submessage field.
104 TEST_P(IPCProtobufMessageTest
, OptionalSubmessage
) {
107 if (field_is_present_
)
108 input
.mutable_op_comp_sub()->set_foo(47);
110 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
111 IPC::WriteParam(&msg
, input
);
114 base::PickleIterator
iter(msg
);
115 ASSERT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
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());
122 ASSERT_FALSE(output
.has_op_comp_sub());
126 // Tests writing and reading a message with a repeated submessage field.
127 TEST_P(IPCProtobufMessageTest
, RepeatedSubmessage
) {
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);
136 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
137 IPC::WriteParam(&msg
, input
);
140 base::PickleIterator
iter(msg
);
141 ASSERT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
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());
152 ASSERT_EQ(0, output
.rep_comp_sub_size());
156 INSTANTIATE_TEST_CASE_P(IPCProtobufMessage
,
157 IPCProtobufMessageTest
,