| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "remoting/protocol/message_decoder.h" | 5 #include "remoting/protocol/message_decoder.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "remoting/proto/event.pb.h" | 15 #include "remoting/proto/event.pb.h" |
| 16 #include "remoting/proto/internal.pb.h" | 16 #include "remoting/proto/internal.pb.h" |
| 17 #include "remoting/protocol/message_serialization.h" | 17 #include "remoting/protocol/message_serialization.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace remoting { | 20 namespace remoting { |
| 21 namespace protocol { | 21 namespace protocol { |
| 22 | 22 |
| 23 static const unsigned int kTestKey = 142; | 23 static const unsigned int kTestKey = 142; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 std::unique_ptr<uint8_t[]> memory_deleter(test_data); | 57 std::unique_ptr<uint8_t[]> memory_deleter(test_data); |
| 58 | 58 |
| 59 // Then simulate using MessageDecoder to decode variable | 59 // Then simulate using MessageDecoder to decode variable |
| 60 // size of encoded data. | 60 // size of encoded data. |
| 61 // The first thing to do is to generate a variable size of data. This is done | 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. | 62 // by iterating the following array for read sizes. |
| 63 MessageDecoder decoder; | 63 MessageDecoder decoder; |
| 64 | 64 |
| 65 // Then feed the protocol decoder using the above generated data and the | 65 // Then feed the protocol decoder using the above generated data and the |
| 66 // read pattern. | 66 // read pattern. |
| 67 std::list<EventMessage*> message_list; | 67 std::list<std::unique_ptr<EventMessage>> message_list; |
| 68 for (int pos = 0; pos < size;) { | 68 for (int pos = 0; pos < size;) { |
| 69 SCOPED_TRACE("Input position: " + base::IntToString(pos)); | 69 SCOPED_TRACE("Input position: " + base::IntToString(pos)); |
| 70 | 70 |
| 71 // First generate the amount to feed the decoder. | 71 // First generate the amount to feed the decoder. |
| 72 int read = std::min(size - pos, read_sequence[pos % sequence_size]); | 72 int read = std::min(size - pos, read_sequence[pos % sequence_size]); |
| 73 | 73 |
| 74 // And then prepare an IOBuffer for feeding it. | 74 // And then prepare an IOBuffer for feeding it. |
| 75 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); | 75 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); |
| 76 memcpy(buffer->data(), test_data + pos, read); | 76 memcpy(buffer->data(), test_data + pos, read); |
| 77 decoder.AddData(buffer, read); | 77 decoder.AddData(buffer, read); |
| 78 while (true) { | 78 while (true) { |
| 79 std::unique_ptr<CompoundBuffer> message(decoder.GetNextMessage()); | 79 std::unique_ptr<CompoundBuffer> message(decoder.GetNextMessage()); |
| 80 if (!message.get()) | 80 if (!message.get()) |
| 81 break; | 81 break; |
| 82 | 82 |
| 83 EventMessage* event = new EventMessage(); | 83 std::unique_ptr<EventMessage> event = base::MakeUnique<EventMessage>(); |
| 84 CompoundBufferInputStream stream(message.get()); | 84 CompoundBufferInputStream stream(message.get()); |
| 85 ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream)); | 85 ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream)); |
| 86 message_list.push_back(event); | 86 message_list.push_back(std::move(event)); |
| 87 } | 87 } |
| 88 pos += read; | 88 pos += read; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Then verify the decoded messages. | 91 // Then verify the decoded messages. |
| 92 EXPECT_EQ(10u, message_list.size()); | 92 EXPECT_EQ(10u, message_list.size()); |
| 93 | 93 |
| 94 unsigned int index = 0; | 94 unsigned int index = 0; |
| 95 for (std::list<EventMessage*>::iterator it = | 95 for (const auto& message : message_list) { |
| 96 message_list.begin(); | |
| 97 it != message_list.end(); ++it) { | |
| 98 SCOPED_TRACE("Message " + base::UintToString(index)); | 96 SCOPED_TRACE("Message " + base::UintToString(index)); |
| 99 | 97 |
| 100 EventMessage* message = *it; | |
| 101 // Partial update stream. | 98 // Partial update stream. |
| 102 EXPECT_TRUE(message->has_key_event()); | 99 EXPECT_TRUE(message->has_key_event()); |
| 103 | 100 |
| 104 // TODO(sergeyu): Don't use index here. Instead store the expected values | 101 // TODO(sergeyu): Don't use index here. Instead store the expected values |
| 105 // in an array. | 102 // in an array. |
| 106 EXPECT_EQ(kTestKey + index, message->key_event().usb_keycode()); | 103 EXPECT_EQ(kTestKey + index, message->key_event().usb_keycode()); |
| 107 EXPECT_EQ((index % 2) != 0, message->key_event().pressed()); | 104 EXPECT_EQ((index % 2) != 0, message->key_event().pressed()); |
| 108 ++index; | 105 ++index; |
| 109 } | 106 } |
| 110 base::STLDeleteElements(&message_list); | |
| 111 } | 107 } |
| 112 | 108 |
| 113 TEST(MessageDecoderTest, SmallReads) { | 109 TEST(MessageDecoderTest, SmallReads) { |
| 114 const int kReads[] = {1, 2, 3, 1}; | 110 const int kReads[] = {1, 2, 3, 1}; |
| 115 SimulateReadSequence(kReads, arraysize(kReads)); | 111 SimulateReadSequence(kReads, arraysize(kReads)); |
| 116 } | 112 } |
| 117 | 113 |
| 118 TEST(MessageDecoderTest, LargeReads) { | 114 TEST(MessageDecoderTest, LargeReads) { |
| 119 const int kReads[] = {50, 50, 5}; | 115 const int kReads[] = {50, 50, 5}; |
| 120 SimulateReadSequence(kReads, arraysize(kReads)); | 116 SimulateReadSequence(kReads, arraysize(kReads)); |
| 121 } | 117 } |
| 122 | 118 |
| 123 TEST(MessageDecoderTest, EmptyReads) { | 119 TEST(MessageDecoderTest, EmptyReads) { |
| 124 const int kReads[] = {4, 0, 50, 0}; | 120 const int kReads[] = {4, 0, 50, 0}; |
| 125 SimulateReadSequence(kReads, arraysize(kReads)); | 121 SimulateReadSequence(kReads, arraysize(kReads)); |
| 126 } | 122 } |
| 127 | 123 |
| 128 } // namespace protocol | 124 } // namespace protocol |
| 129 } // namespace remoting | 125 } // namespace remoting |
| OLD | NEW |