summaryrefslogtreecommitdiff
path: root/chromium/chrome/common/safe_browsing/ipc_protobuf_message_unittest.cc
blob: b66377511fc5f579ff80886e2ebebd829734b7b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/safe_browsing/ipc_protobuf_message_test.pb.h"
#include "ipc/ipc_message.h"
#include "testing/gtest/include/gtest/gtest.h"

#define IPC_MESSAGE_IMPL
#undef SAFE_BROWSING_IPC_PROTOBUF_MESSAGE_TEST_MESSAGES_H_
#include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"

// Generate ipc protobuf traits write methods.
#include "chrome/common/safe_browsing/protobuf_message_write_macros.h"
namespace IPC {
#undef SAFE_BROWSING_IPC_PROTOBUF_MESSAGE_TEST_MESSAGES_H_
#include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
}  // namespace IPC

// Generate ipc protobuf traits read methods.
#include "chrome/common/safe_browsing/protobuf_message_read_macros.h"
namespace IPC {
#undef SAFE_BROWSING_IPC_PROTOBUF_MESSAGE_TEST_MESSAGES_H_
#include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
}  // namespace IPC

// Generate ipc protobuf traits log methods.
#include "chrome/common/safe_browsing/protobuf_message_log_macros.h"
namespace IPC {
#undef SAFE_BROWSING_IPC_PROTOBUF_MESSAGE_TEST_MESSAGES_H_
#include "chrome/common/safe_browsing/ipc_protobuf_message_test_messages.h"
}  // namespace IPC

class IPCProtobufMessageTest : public ::testing::TestWithParam<bool> {
 protected:
  IPCProtobufMessageTest() : field_is_present_(GetParam()) {}

  bool field_is_present_;
};

// Tests writing and reading a message with an optional fundamental field.
TEST_P(IPCProtobufMessageTest, FundamentalField) {
  TestMessage input;

  if (field_is_present_)
    input.set_fund_int(42);

  IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  IPC::WriteParam(&msg, input);

  TestMessage output;
  base::PickleIterator iter(msg);
  ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output));

  if (field_is_present_) {
    ASSERT_TRUE(output.has_fund_int());
    EXPECT_EQ(input.fund_int(), output.fund_int());
  } else {
    ASSERT_FALSE(output.has_fund_int());
  }
}

// Tests writing and reading a message with an optional string field.
TEST_P(IPCProtobufMessageTest, StringField) {
  TestMessage input;

  if (field_is_present_)
    input.set_op_comp_string("some string");

  IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  IPC::WriteParam(&msg, input);

  TestMessage output;
  base::PickleIterator iter(msg);
  ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output));

  if (field_is_present_) {
    ASSERT_TRUE(output.has_op_comp_string());
    EXPECT_EQ(input.op_comp_string(), output.op_comp_string());
  } else {
    ASSERT_FALSE(output.has_op_comp_string());
  }
}

// Tests writing and reading a message with an optional bytes field.
TEST_P(IPCProtobufMessageTest, BytesField) {
  TestMessage input;

  if (field_is_present_)
    input.set_op_comp_bytes("some string");

  IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  IPC::WriteParam(&msg, input);

  TestMessage output;
  base::PickleIterator iter(msg);
  ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output));

  if (field_is_present_) {
    ASSERT_TRUE(output.has_op_comp_bytes());
    EXPECT_EQ(input.op_comp_bytes(), output.op_comp_bytes());
  } else {
    ASSERT_FALSE(output.has_op_comp_bytes());
  }
}

// Tests writing and reading a message with an optional submessage field.
TEST_P(IPCProtobufMessageTest, OptionalSubmessage) {
  TestMessage input;

  if (field_is_present_)
    input.mutable_op_comp_sub()->set_foo(47);

  IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  IPC::WriteParam(&msg, input);

  TestMessage output;
  base::PickleIterator iter(msg);
  ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output));

  if (field_is_present_) {
    ASSERT_TRUE(output.has_op_comp_sub());
    ASSERT_TRUE(output.op_comp_sub().has_foo());
    EXPECT_EQ(input.op_comp_sub().foo(), output.op_comp_sub().foo());
  } else {
    ASSERT_FALSE(output.has_op_comp_sub());
  }
}

// Tests writing and reading a message with a repeated submessage field.
TEST_P(IPCProtobufMessageTest, RepeatedSubmessage) {
  TestMessage input;

  if (field_is_present_) {
    input.add_rep_comp_sub()->set_foo(0);
    input.add_rep_comp_sub()->set_foo(1);
    input.add_rep_comp_sub()->set_foo(2);
  }

  IPC::Message msg(1, 2, IPC::Message::PRIORITY_NORMAL);
  IPC::WriteParam(&msg, input);

  TestMessage output;
  base::PickleIterator iter(msg);
  ASSERT_TRUE(IPC::ReadParam(&msg, &iter, &output));

  if (field_is_present_) {
    ASSERT_EQ(3, output.rep_comp_sub_size());
    ASSERT_TRUE(output.rep_comp_sub(0).has_foo());
    EXPECT_EQ(input.rep_comp_sub(0).foo(), output.rep_comp_sub(0).foo());
    ASSERT_TRUE(output.rep_comp_sub(1).has_foo());
    EXPECT_EQ(input.rep_comp_sub(1).foo(), output.rep_comp_sub(1).foo());
    ASSERT_TRUE(output.rep_comp_sub(2).has_foo());
    EXPECT_EQ(input.rep_comp_sub(2).foo(), output.rep_comp_sub(2).foo());
  } else {
    ASSERT_EQ(0, output.rep_comp_sub_size());
  }
}

INSTANTIATE_TEST_SUITE_P(IPCProtobufMessage,
                         IPCProtobufMessageTest,
                         ::testing::Bool());