blob: d4671f5307975d174d8bfd52e6b3a732eae14e6b [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cb3b1f9312010-06-07 19:58:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng0765c492016-04-06 22:41:535#include "remoting/protocol/message_decoder.h"
6
avi5a080f012015-12-22 23:15:437#include <stdint.h>
8
Brett Wilson55ff1475e2017-09-26 00:28:489#include <list>
dcheng0765c492016-04-06 22:41:5310#include <memory>
[email protected]cb3b1f9312010-06-07 19:58:2311#include <string>
12
avi5a080f012015-12-22 23:15:4313#include "base/macros.h"
[email protected]eaf92532013-06-11 07:39:1914#include "base/strings/string_number_conversions.h"
[email protected]61e12a2c2010-12-08 20:45:4215#include "remoting/proto/event.pb.h"
[email protected]6852d7d2011-01-22 02:34:5616#include "remoting/proto/internal.pb.h"
[email protected]b76a2742014-04-10 05:38:2617#include "remoting/protocol/message_serialization.h"
[email protected]cb3b1f9312010-06-07 19:58:2318#include "testing/gtest/include/gtest/gtest.h"
19
20namespace remoting {
[email protected]2c229682010-12-02 20:23:3521namespace protocol {
[email protected]cb3b1f9312010-06-07 19:58:2322
[email protected]f5e7c882012-09-11 01:52:1423static const unsigned int kTestKey = 142;
[email protected]cb3b1f9312010-06-07 19:58:2324
[email protected]61e12a2c2010-12-08 20:45:4225static void AppendMessage(const EventMessage& msg,
[email protected]cb3b1f9312010-06-07 19:58:2326 std::string* buffer) {
27 // Contains one encoded message.
[email protected]c3af26f332010-10-06 22:46:0028 scoped_refptr<net::IOBufferWithSize> encoded_msg;
[email protected]cb3b1f9312010-06-07 19:58:2329 encoded_msg = SerializeAndFrameMessage(msg);
[email protected]c3af26f332010-10-06 22:46:0030 buffer->append(encoded_msg->data(), encoded_msg->size());
[email protected]cb3b1f9312010-06-07 19:58:2331}
32
33// Construct and prepare data in the |output_stream|.
avi5a080f012015-12-22 23:15:4334static void PrepareData(uint8_t** buffer, int* size) {
[email protected]cb3b1f9312010-06-07 19:58:2335 // Contains all encoded messages.
36 std::string encoded_data;
37
[email protected]cb3b1f9312010-06-07 19:58:2338 // Then append 10 update sequences to the data.
39 for (int i = 0; i < 10; ++i) {
[email protected]6852d7d2011-01-22 02:34:5640 EventMessage msg;
sergeyuc258bd4b2015-01-08 20:57:5041 msg.set_timestamp(i);
[email protected]f5e7c882012-09-11 01:52:1442 msg.mutable_key_event()->set_usb_keycode(kTestKey + i);
[email protected]6852d7d2011-01-22 02:34:5643 msg.mutable_key_event()->set_pressed((i % 2) != 0);
[email protected]cb3b1f9312010-06-07 19:58:2344 AppendMessage(msg, &encoded_data);
[email protected]cb3b1f9312010-06-07 19:58:2345 }
46
47 *size = encoded_data.length();
avi5a080f012015-12-22 23:15:4348 *buffer = new uint8_t[*size];
[email protected]cb3b1f9312010-06-07 19:58:2349 memcpy(*buffer, encoded_data.c_str(), *size);
50}
51
[email protected]4d10ede2010-10-28 18:43:3752void SimulateReadSequence(const int read_sequence[], int sequence_size) {
[email protected]cb3b1f9312010-06-07 19:58:2353 // Prepare encoded data for testing.
54 int size;
avi5a080f012015-12-22 23:15:4355 uint8_t* test_data;
[email protected]cb3b1f9312010-06-07 19:58:2356 PrepareData(&test_data, &size);
dcheng0765c492016-04-06 22:41:5357 std::unique_ptr<uint8_t[]> memory_deleter(test_data);
[email protected]cb3b1f9312010-06-07 19:58:2358
[email protected]4d10ede2010-10-28 18:43:3759 // Then simulate using MessageDecoder to decode variable
[email protected]cb3b1f9312010-06-07 19:58:2360 // size of encoded data.
61 // The first thing to do is to generate a variable size of data. This is done
62 // by iterating the following array for read sizes.
[email protected]4d10ede2010-10-28 18:43:3763 MessageDecoder decoder;
[email protected]cb3b1f9312010-06-07 19:58:2364
65 // Then feed the protocol decoder using the above generated data and the
66 // read pattern.
avic58e7852016-10-01 05:58:0067 std::list<std::unique_ptr<EventMessage>> message_list;
[email protected]6852d7d2011-01-22 02:34:5668 for (int pos = 0; pos < size;) {
69 SCOPED_TRACE("Input position: " + base::IntToString(pos));
70
[email protected]cb3b1f9312010-06-07 19:58:2371 // First generate the amount to feed the decoder.
[email protected]6852d7d2011-01-22 02:34:5672 int read = std::min(size - pos, read_sequence[pos % sequence_size]);
[email protected]cb3b1f9312010-06-07 19:58:2373
[email protected]4d10ede2010-10-28 18:43:3774 // And then prepare an IOBuffer for feeding it.
[email protected]ad8e04a2010-11-01 04:16:2775 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read));
[email protected]6852d7d2011-01-22 02:34:5676 memcpy(buffer->data(), test_data + pos, read);
[email protected]051916e62011-01-14 21:58:0177 decoder.AddData(buffer, read);
78 while (true) {
dcheng0765c492016-04-06 22:41:5379 std::unique_ptr<CompoundBuffer> message(decoder.GetNextMessage());
[email protected]6852d7d2011-01-22 02:34:5680 if (!message.get())
[email protected]051916e62011-01-14 21:58:0181 break;
82
Jinho Bang138fde32018-01-18 23:13:4283 std::unique_ptr<EventMessage> event = std::make_unique<EventMessage>();
[email protected]6852d7d2011-01-22 02:34:5684 CompoundBufferInputStream stream(message.get());
[email protected]051916e62011-01-14 21:58:0185 ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream));
avic58e7852016-10-01 05:58:0086 message_list.push_back(std::move(event));
[email protected]051916e62011-01-14 21:58:0187 }
[email protected]6852d7d2011-01-22 02:34:5688 pos += read;
[email protected]cb3b1f9312010-06-07 19:58:2389 }
90
91 // Then verify the decoded messages.
[email protected]3adf1b22010-11-09 23:22:2092 EXPECT_EQ(10u, message_list.size());
[email protected]cb3b1f9312010-06-07 19:58:2393
[email protected]f5e7c882012-09-11 01:52:1494 unsigned int index = 0;
avic58e7852016-10-01 05:58:0095 for (const auto& message : message_list) {
riceaac430822015-09-20 06:25:4296 SCOPED_TRACE("Message " + base::UintToString(index));
[email protected]6852d7d2011-01-22 02:34:5697
[email protected]04b36142010-11-02 01:08:1998 // Partial update stream.
[email protected]6852d7d2011-01-22 02:34:5699 EXPECT_TRUE(message->has_key_event());
[email protected]3adf1b22010-11-09 23:22:20100
101 // TODO(sergeyu): Don't use index here. Instead store the expected values
102 // in an array.
[email protected]f5e7c882012-09-11 01:52:14103 EXPECT_EQ(kTestKey + index, message->key_event().usb_keycode());
[email protected]6852d7d2011-01-22 02:34:56104 EXPECT_EQ((index % 2) != 0, message->key_event().pressed());
[email protected]3adf1b22010-11-09 23:22:20105 ++index;
[email protected]cb3b1f9312010-06-07 19:58:23106 }
[email protected]cb3b1f9312010-06-07 19:58:23107}
108
[email protected]4d10ede2010-10-28 18:43:37109TEST(MessageDecoderTest, SmallReads) {
110 const int kReads[] = {1, 2, 3, 1};
111 SimulateReadSequence(kReads, arraysize(kReads));
112}
113
114TEST(MessageDecoderTest, LargeReads) {
115 const int kReads[] = {50, 50, 5};
116 SimulateReadSequence(kReads, arraysize(kReads));
117}
118
119TEST(MessageDecoderTest, EmptyReads) {
120 const int kReads[] = {4, 0, 50, 0};
121 SimulateReadSequence(kReads, arraysize(kReads));
122}
123
[email protected]2c229682010-12-02 20:23:35124} // namespace protocol
[email protected]cb3b1f9312010-06-07 19:58:23125} // namespace remoting