Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_framer_unittest.cc
blobc45bd319a75c98e1b4925d160df94de7b350e815
1 // Copyright 2014 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 "extensions/browser/api/cast_channel/cast_framer.h"
7 #include <algorithm>
8 #include <string>
10 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions {
14 namespace core_api {
15 namespace cast_channel {
16 class CastFramerTest : public testing::Test {
17 public:
18 CastFramerTest() {}
19 ~CastFramerTest() override {}
21 void SetUp() override {
22 cast_message_.set_protocol_version(CastMessage::CASTV2_1_0);
23 cast_message_.set_source_id("source");
24 cast_message_.set_destination_id("destination");
25 cast_message_.set_namespace_("namespace");
26 cast_message_.set_payload_type(CastMessage::STRING);
27 cast_message_.set_payload_utf8("payload");
28 ASSERT_TRUE(MessageFramer::Serialize(cast_message_, &cast_message_str_));
30 buffer_ = new net::GrowableIOBuffer;
31 buffer_->SetCapacity(MessageFramer::MessageHeader::max_message_size());
32 framer_.reset(new MessageFramer(buffer_.get()));
35 void WriteToBuffer(const std::string& data) {
36 memcpy(buffer_->StartOfBuffer(), data.data(), data.size());
39 protected:
40 CastMessage cast_message_;
41 std::string cast_message_str_;
42 scoped_refptr<net::GrowableIOBuffer> buffer_;
43 scoped_ptr<MessageFramer> framer_;
46 TEST_F(CastFramerTest, TestMessageFramerCompleteMessage) {
47 ChannelError error;
48 size_t message_length;
50 WriteToBuffer(cast_message_str_);
52 // Receive 1 byte of the header, framer demands 3 more bytes.
53 EXPECT_EQ(4u, framer_->BytesRequested());
54 EXPECT_EQ(nullptr, framer_->Ingest(1, &message_length, &error).get());
55 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE, error);
56 EXPECT_EQ(3u, framer_->BytesRequested());
58 // Ingest remaining 3, expect that the framer has moved on to requesting the
59 // body contents.
60 EXPECT_EQ(nullptr, framer_->Ingest(3, &message_length, &error).get());
61 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE, error);
62 EXPECT_EQ(
63 cast_message_str_.size() - MessageFramer::MessageHeader::header_size(),
64 framer_->BytesRequested());
66 // Remainder of packet sent over the wire.
67 scoped_ptr<CastMessage> message;
68 message = framer_->Ingest(framer_->BytesRequested(), &message_length, &error);
69 EXPECT_NE(static_cast<CastMessage*>(nullptr), message.get());
70 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE, error);
71 EXPECT_EQ(message->SerializeAsString(), cast_message_.SerializeAsString());
72 EXPECT_EQ(4u, framer_->BytesRequested());
73 EXPECT_EQ(message->SerializeAsString().size(), message_length);
76 TEST_F(CastFramerTest, TestSerializeErrorMessageTooLarge) {
77 std::string serialized;
78 CastMessage big_message;
79 big_message.CopyFrom(cast_message_);
80 std::string payload;
81 payload.append(MessageFramer::MessageHeader::max_message_size() + 1, 'x');
82 big_message.set_payload_utf8(payload);
83 EXPECT_FALSE(MessageFramer::Serialize(big_message, &serialized));
86 TEST_F(CastFramerTest, TestIngestIllegalLargeMessage) {
87 std::string mangled_cast_message = cast_message_str_;
88 mangled_cast_message[0] = 88;
89 mangled_cast_message[1] = 88;
90 mangled_cast_message[2] = 88;
91 mangled_cast_message[3] = 88;
92 WriteToBuffer(mangled_cast_message);
94 size_t bytes_ingested;
95 ChannelError error;
96 EXPECT_EQ(4u, framer_->BytesRequested());
97 EXPECT_EQ(nullptr, framer_->Ingest(4, &bytes_ingested, &error).get());
98 EXPECT_EQ(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE, error);
99 EXPECT_EQ(0u, framer_->BytesRequested());
101 // Test that the parser enters a terminal error state.
102 WriteToBuffer(cast_message_str_);
103 EXPECT_EQ(0u, framer_->BytesRequested());
104 EXPECT_EQ(nullptr, framer_->Ingest(4, &bytes_ingested, &error).get());
105 EXPECT_EQ(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE, error);
106 EXPECT_EQ(0u, framer_->BytesRequested());
109 TEST_F(CastFramerTest, TestUnparsableBodyProto) {
110 // Message header is OK, but the body is replaced with "x"en.
111 std::string mangled_cast_message = cast_message_str_;
112 for (size_t i = MessageFramer::MessageHeader::header_size();
113 i < mangled_cast_message.size();
114 ++i) {
115 std::fill(mangled_cast_message.begin() +
116 MessageFramer::MessageHeader::header_size(),
117 mangled_cast_message.end(),
118 'x');
120 WriteToBuffer(mangled_cast_message);
122 // Send header.
123 size_t message_length;
124 ChannelError error;
125 EXPECT_EQ(4u, framer_->BytesRequested());
126 EXPECT_EQ(nullptr, framer_->Ingest(4, &message_length, &error).get());
127 EXPECT_EQ(cast_channel::CHANNEL_ERROR_NONE, error);
128 EXPECT_EQ(cast_message_str_.size() - 4, framer_->BytesRequested());
130 // Send body, expect an error.
131 scoped_ptr<CastMessage> message;
132 EXPECT_EQ(nullptr, framer_->Ingest(framer_->BytesRequested(), &message_length,
133 &error).get());
134 EXPECT_EQ(cast_channel::CHANNEL_ERROR_INVALID_MESSAGE, error);
136 } // namespace cast_channel
137 } // namespace core_api
138 } // namespace extensions