[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 5 | #include "remoting/protocol/message_decoder.h" |
| 6 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Brett Wilson | 55ff1475e | 2017-09-26 00:28:48 | [diff] [blame] | 9 | #include <list> |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 10 | #include <memory> |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 11 | #include <string> |
| 12 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | eaf9253 | 2013-06-11 07:39:19 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
[email protected] | 61e12a2c | 2010-12-08 20:45:42 | [diff] [blame] | 15 | #include "remoting/proto/event.pb.h" |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 16 | #include "remoting/proto/internal.pb.h" |
[email protected] | b76a274 | 2014-04-10 05:38:26 | [diff] [blame] | 17 | #include "remoting/protocol/message_serialization.h" |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace remoting { |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 21 | namespace protocol { |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 22 | |
[email protected] | f5e7c88 | 2012-09-11 01:52:14 | [diff] [blame] | 23 | static const unsigned int kTestKey = 142; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 24 | |
[email protected] | 61e12a2c | 2010-12-08 20:45:42 | [diff] [blame] | 25 | static void AppendMessage(const EventMessage& msg, |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 26 | std::string* buffer) { |
| 27 | // Contains one encoded message. |
[email protected] | c3af26f33 | 2010-10-06 22:46:00 | [diff] [blame] | 28 | scoped_refptr<net::IOBufferWithSize> encoded_msg; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 29 | encoded_msg = SerializeAndFrameMessage(msg); |
[email protected] | c3af26f33 | 2010-10-06 22:46:00 | [diff] [blame] | 30 | buffer->append(encoded_msg->data(), encoded_msg->size()); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Construct and prepare data in the |output_stream|. |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 34 | static void PrepareData(uint8_t** buffer, int* size) { |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 35 | // Contains all encoded messages. |
| 36 | std::string encoded_data; |
| 37 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 38 | // Then append 10 update sequences to the data. |
| 39 | for (int i = 0; i < 10; ++i) { |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 40 | EventMessage msg; |
sergeyu | c258bd4b | 2015-01-08 20:57:50 | [diff] [blame] | 41 | msg.set_timestamp(i); |
[email protected] | f5e7c88 | 2012-09-11 01:52:14 | [diff] [blame] | 42 | msg.mutable_key_event()->set_usb_keycode(kTestKey + i); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 43 | msg.mutable_key_event()->set_pressed((i % 2) != 0); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 44 | AppendMessage(msg, &encoded_data); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | *size = encoded_data.length(); |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 48 | *buffer = new uint8_t[*size]; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 49 | memcpy(*buffer, encoded_data.c_str(), *size); |
| 50 | } |
| 51 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 52 | void SimulateReadSequence(const int read_sequence[], int sequence_size) { |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 53 | // Prepare encoded data for testing. |
| 54 | int size; |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 55 | uint8_t* test_data; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 56 | PrepareData(&test_data, &size); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 57 | std::unique_ptr<uint8_t[]> memory_deleter(test_data); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 58 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 59 | // Then simulate using MessageDecoder to decode variable |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 60 | // 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] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 63 | MessageDecoder decoder; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 64 | |
| 65 | // Then feed the protocol decoder using the above generated data and the |
| 66 | // read pattern. |
avi | c58e785 | 2016-10-01 05:58:00 | [diff] [blame] | 67 | std::list<std::unique_ptr<EventMessage>> message_list; |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 68 | for (int pos = 0; pos < size;) { |
| 69 | SCOPED_TRACE("Input position: " + base::IntToString(pos)); |
| 70 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 71 | // First generate the amount to feed the decoder. |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 72 | int read = std::min(size - pos, read_sequence[pos % sequence_size]); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 73 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 74 | // And then prepare an IOBuffer for feeding it. |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 75 | scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 76 | memcpy(buffer->data(), test_data + pos, read); |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 77 | decoder.AddData(buffer, read); |
| 78 | while (true) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 79 | std::unique_ptr<CompoundBuffer> message(decoder.GetNextMessage()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 80 | if (!message.get()) |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 81 | break; |
| 82 | |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 83 | std::unique_ptr<EventMessage> event = std::make_unique<EventMessage>(); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 84 | CompoundBufferInputStream stream(message.get()); |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 85 | ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream)); |
avi | c58e785 | 2016-10-01 05:58:00 | [diff] [blame] | 86 | message_list.push_back(std::move(event)); |
[email protected] | 051916e6 | 2011-01-14 21:58:01 | [diff] [blame] | 87 | } |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 88 | pos += read; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Then verify the decoded messages. |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 92 | EXPECT_EQ(10u, message_list.size()); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 93 | |
[email protected] | f5e7c88 | 2012-09-11 01:52:14 | [diff] [blame] | 94 | unsigned int index = 0; |
avi | c58e785 | 2016-10-01 05:58:00 | [diff] [blame] | 95 | for (const auto& message : message_list) { |
ricea | ac43082 | 2015-09-20 06:25:42 | [diff] [blame] | 96 | SCOPED_TRACE("Message " + base::UintToString(index)); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 97 | |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 98 | // Partial update stream. |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 99 | EXPECT_TRUE(message->has_key_event()); |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 100 | |
| 101 | // TODO(sergeyu): Don't use index here. Instead store the expected values |
| 102 | // in an array. |
[email protected] | f5e7c88 | 2012-09-11 01:52:14 | [diff] [blame] | 103 | EXPECT_EQ(kTestKey + index, message->key_event().usb_keycode()); |
[email protected] | 6852d7d | 2011-01-22 02:34:56 | [diff] [blame] | 104 | EXPECT_EQ((index % 2) != 0, message->key_event().pressed()); |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 105 | ++index; |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 106 | } |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 4d10ede | 2010-10-28 18:43:37 | [diff] [blame] | 109 | TEST(MessageDecoderTest, SmallReads) { |
| 110 | const int kReads[] = {1, 2, 3, 1}; |
| 111 | SimulateReadSequence(kReads, arraysize(kReads)); |
| 112 | } |
| 113 | |
| 114 | TEST(MessageDecoderTest, LargeReads) { |
| 115 | const int kReads[] = {50, 50, 5}; |
| 116 | SimulateReadSequence(kReads, arraysize(kReads)); |
| 117 | } |
| 118 | |
| 119 | TEST(MessageDecoderTest, EmptyReads) { |
| 120 | const int kReads[] = {4, 0, 50, 0}; |
| 121 | SimulateReadSequence(kReads, arraysize(kReads)); |
| 122 | } |
| 123 | |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 124 | } // namespace protocol |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 125 | } // namespace remoting |