Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: ipc/ipc_channel_reader_unittest.cc

Issue 1345353004: Resize IPC input buffer to fit the next message. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix overflow test to actually overflow on 32-bit Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include <limits>
7 #include <set> 8 #include <set>
8 9
9 #include "ipc/attachment_broker.h" 10 #include "ipc/attachment_broker.h"
10 #include "ipc/brokerable_attachment.h" 11 #include "ipc/brokerable_attachment.h"
11 #include "ipc/ipc_channel_reader.h" 12 #include "ipc/ipc_channel_reader.h"
12 #include "ipc/placeholder_brokerable_attachment.h" 13 #include "ipc/placeholder_brokerable_attachment.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 #if USE_ATTACHMENT_BROKER
16 namespace IPC { 16 namespace IPC {
17 namespace internal { 17 namespace internal {
18 18
19 namespace { 19 namespace {
20 20
21 #if USE_ATTACHMENT_BROKER
22
21 class MockAttachment : public BrokerableAttachment { 23 class MockAttachment : public BrokerableAttachment {
22 public: 24 public:
23 MockAttachment() {} 25 MockAttachment() {}
24 MockAttachment(BrokerableAttachment::AttachmentId id) 26 MockAttachment(BrokerableAttachment::AttachmentId id)
25 : BrokerableAttachment(id) {} 27 : BrokerableAttachment(id) {}
26 28
27 #if defined(OS_POSIX) 29 #if defined(OS_POSIX)
28 base::PlatformFile TakePlatformFile() override { 30 base::PlatformFile TakePlatformFile() override {
29 return base::PlatformFile(); 31 return base::PlatformFile();
30 } 32 }
(...skipping 15 matching lines...) Expand all
46 } 48 }
47 49
48 bool OnMessageReceived(const Message& message) override { return false; } 50 bool OnMessageReceived(const Message& message) override { return false; }
49 51
50 void AddAttachment(scoped_refptr<BrokerableAttachment> attachment) { 52 void AddAttachment(scoped_refptr<BrokerableAttachment> attachment) {
51 get_attachments()->push_back(attachment); 53 get_attachments()->push_back(attachment);
52 NotifyObservers(attachment->GetIdentifier()); 54 NotifyObservers(attachment->GetIdentifier());
53 } 55 }
54 }; 56 };
55 57
58 #endif // USE_ATTACHMENT_BROKER
59
56 class MockChannelReader : public ChannelReader { 60 class MockChannelReader : public ChannelReader {
57 public: 61 public:
58 MockChannelReader() 62 MockChannelReader()
59 : ChannelReader(nullptr), last_dispatched_message_(nullptr) {} 63 : ChannelReader(nullptr), last_dispatched_message_(nullptr) {}
60 64
61 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override { 65 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override {
62 return READ_FAILED; 66 return READ_FAILED;
63 } 67 }
64 68
65 bool ShouldDispatchInputMessage(Message* msg) override { return true; } 69 bool ShouldDispatchInputMessage(Message* msg) override { return true; }
(...skipping 19 matching lines...) Expand all
85 89
86 Message* get_last_dispatched_message() { return last_dispatched_message_; } 90 Message* get_last_dispatched_message() { return last_dispatched_message_; }
87 91
88 void set_broker(AttachmentBroker* broker) { broker_ = broker; } 92 void set_broker(AttachmentBroker* broker) { broker_ = broker; }
89 93
90 private: 94 private:
91 Message* last_dispatched_message_; 95 Message* last_dispatched_message_;
92 AttachmentBroker* broker_; 96 AttachmentBroker* broker_;
93 }; 97 };
94 98
99 class ExposedMessage: public Message {
100 public:
101 using Message::Header;
102 using Message::header;
103 };
104
95 } // namespace 105 } // namespace
96 106
107 #if USE_ATTACHMENT_BROKER
108
97 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) { 109 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) {
98 MockAttachmentBroker broker; 110 MockAttachmentBroker broker;
99 MockChannelReader reader; 111 MockChannelReader reader;
100 reader.set_broker(&broker); 112 reader.set_broker(&broker);
101 scoped_refptr<MockAttachment> attachment(new MockAttachment); 113 scoped_refptr<MockAttachment> attachment(new MockAttachment);
102 broker.AddAttachment(attachment); 114 broker.AddAttachment(attachment);
103 115
104 Message* m = new Message; 116 Message* m = new Message;
105 PlaceholderBrokerableAttachment* needs_brokering_attachment = 117 PlaceholderBrokerableAttachment* needs_brokering_attachment =
106 new PlaceholderBrokerableAttachment(attachment->GetIdentifier()); 118 new PlaceholderBrokerableAttachment(attachment->GetIdentifier());
(...skipping 15 matching lines...) Expand all
122 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); 134 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment));
123 reader.AddMessageForDispatch(m); 135 reader.AddMessageForDispatch(m);
124 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER, 136 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER,
125 reader.DispatchMessages()); 137 reader.DispatchMessages());
126 EXPECT_EQ(nullptr, reader.get_last_dispatched_message()); 138 EXPECT_EQ(nullptr, reader.get_last_dispatched_message());
127 139
128 broker.AddAttachment(attachment); 140 broker.AddAttachment(attachment);
129 EXPECT_EQ(m, reader.get_last_dispatched_message()); 141 EXPECT_EQ(m, reader.get_last_dispatched_message());
130 } 142 }
131 143
144 #endif // USE_ATTACHMENT_BROKER
145
146 #if !USE_ATTACHMENT_BROKER
Maria 2015/09/25 22:23:00 should we just do #else here? instead of another i
Dmitry Skiba 2015/09/28 17:37:18 The effect of #else will be the same, but it's jus
147
148 // We can determine message size from its header (and hence resize the buffer)
149 // only when attachment broker is not used, see IPC::Message::FindNext().
150
151 TEST(ChannelReaderTest, ResizeOverflowBuffer) {
152 MockChannelReader reader;
153
154 ExposedMessage::Header header = {};
155
156 header.payload_size = 128 * 1024;
157 EXPECT_LT(reader.input_overflow_buf_.capacity(), header.payload_size);
158 EXPECT_TRUE(reader.TranslateInputData(
159 reinterpret_cast<const char*>(&header), sizeof(header)));
160
161 // Once message header is available we resize overflow buffer to
162 // fit the entire message.
163 EXPECT_GE(reader.input_overflow_buf_.capacity(), header.payload_size);
164 }
165
166 TEST(ChannelReaderTest, InvalidMessageSize) {
167 MockChannelReader reader;
168
169 ExposedMessage::Header header = {};
170
171 size_t capacity_before = reader.input_overflow_buf_.capacity();
172
173 // Message is slightly larger than maximum allowed size
174 header.payload_size = Channel::kMaximumMessageSize + 1;
175 EXPECT_FALSE(reader.TranslateInputData(
176 reinterpret_cast<const char*>(&header), sizeof(header)));
177 EXPECT_LE(reader.input_overflow_buf_.capacity(), capacity_before);
178
179 // Payload size is negative, overflow is detected by Pickle::PeekNext()
180 header.payload_size = static_cast<uint32_t>(-1);
181 EXPECT_FALSE(reader.TranslateInputData(
182 reinterpret_cast<const char*>(&header), sizeof(header)));
183 EXPECT_LE(reader.input_overflow_buf_.capacity(), capacity_before);
184
185 // Payload size is maximum int32 value
186 header.payload_size = std::numeric_limits<int32_t>::max();
187 EXPECT_FALSE(reader.TranslateInputData(
188 reinterpret_cast<const char*>(&header), sizeof(header)));
189 EXPECT_LE(reader.input_overflow_buf_.capacity(), capacity_before);
190 }
191
192 #endif // !USE_ATTACHMENT_BROKER
193
132 } // namespace internal 194 } // namespace internal
133 } // namespace IPC 195 } // namespace IPC
134 #endif // USE_ATTACHMENT_BROKER
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698