| 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 <stdint.h> |
| 6 |
| 5 #include <string> | 7 #include <string> |
| 6 | 8 |
| 9 #include "base/macros.h" |
| 7 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 10 #include "remoting/proto/event.pb.h" | 13 #include "remoting/proto/event.pb.h" |
| 11 #include "remoting/proto/internal.pb.h" | 14 #include "remoting/proto/internal.pb.h" |
| 12 #include "remoting/protocol/message_decoder.h" | 15 #include "remoting/protocol/message_decoder.h" |
| 13 #include "remoting/protocol/message_serialization.h" | 16 #include "remoting/protocol/message_serialization.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 18 |
| 16 namespace remoting { | 19 namespace remoting { |
| 17 namespace protocol { | 20 namespace protocol { |
| 18 | 21 |
| 19 static const unsigned int kTestKey = 142; | 22 static const unsigned int kTestKey = 142; |
| 20 | 23 |
| 21 static void AppendMessage(const EventMessage& msg, | 24 static void AppendMessage(const EventMessage& msg, |
| 22 std::string* buffer) { | 25 std::string* buffer) { |
| 23 // Contains one encoded message. | 26 // Contains one encoded message. |
| 24 scoped_refptr<net::IOBufferWithSize> encoded_msg; | 27 scoped_refptr<net::IOBufferWithSize> encoded_msg; |
| 25 encoded_msg = SerializeAndFrameMessage(msg); | 28 encoded_msg = SerializeAndFrameMessage(msg); |
| 26 buffer->append(encoded_msg->data(), encoded_msg->size()); | 29 buffer->append(encoded_msg->data(), encoded_msg->size()); |
| 27 } | 30 } |
| 28 | 31 |
| 29 // Construct and prepare data in the |output_stream|. | 32 // Construct and prepare data in the |output_stream|. |
| 30 static void PrepareData(uint8** buffer, int* size) { | 33 static void PrepareData(uint8_t** buffer, int* size) { |
| 31 // Contains all encoded messages. | 34 // Contains all encoded messages. |
| 32 std::string encoded_data; | 35 std::string encoded_data; |
| 33 | 36 |
| 34 // Then append 10 update sequences to the data. | 37 // Then append 10 update sequences to the data. |
| 35 for (int i = 0; i < 10; ++i) { | 38 for (int i = 0; i < 10; ++i) { |
| 36 EventMessage msg; | 39 EventMessage msg; |
| 37 msg.set_timestamp(i); | 40 msg.set_timestamp(i); |
| 38 msg.mutable_key_event()->set_usb_keycode(kTestKey + i); | 41 msg.mutable_key_event()->set_usb_keycode(kTestKey + i); |
| 39 msg.mutable_key_event()->set_pressed((i % 2) != 0); | 42 msg.mutable_key_event()->set_pressed((i % 2) != 0); |
| 40 AppendMessage(msg, &encoded_data); | 43 AppendMessage(msg, &encoded_data); |
| 41 } | 44 } |
| 42 | 45 |
| 43 *size = encoded_data.length(); | 46 *size = encoded_data.length(); |
| 44 *buffer = new uint8[*size]; | 47 *buffer = new uint8_t[*size]; |
| 45 memcpy(*buffer, encoded_data.c_str(), *size); | 48 memcpy(*buffer, encoded_data.c_str(), *size); |
| 46 } | 49 } |
| 47 | 50 |
| 48 void SimulateReadSequence(const int read_sequence[], int sequence_size) { | 51 void SimulateReadSequence(const int read_sequence[], int sequence_size) { |
| 49 // Prepare encoded data for testing. | 52 // Prepare encoded data for testing. |
| 50 int size; | 53 int size; |
| 51 uint8* test_data; | 54 uint8_t* test_data; |
| 52 PrepareData(&test_data, &size); | 55 PrepareData(&test_data, &size); |
| 53 scoped_ptr<uint8[]> memory_deleter(test_data); | 56 scoped_ptr<uint8_t[]> memory_deleter(test_data); |
| 54 | 57 |
| 55 // Then simulate using MessageDecoder to decode variable | 58 // Then simulate using MessageDecoder to decode variable |
| 56 // size of encoded data. | 59 // size of encoded data. |
| 57 // The first thing to do is to generate a variable size of data. This is done | 60 // The first thing to do is to generate a variable size of data. This is done |
| 58 // by iterating the following array for read sizes. | 61 // by iterating the following array for read sizes. |
| 59 MessageDecoder decoder; | 62 MessageDecoder decoder; |
| 60 | 63 |
| 61 // Then feed the protocol decoder using the above generated data and the | 64 // Then feed the protocol decoder using the above generated data and the |
| 62 // read pattern. | 65 // read pattern. |
| 63 std::list<EventMessage*> message_list; | 66 std::list<EventMessage*> message_list; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 SimulateReadSequence(kReads, arraysize(kReads)); | 119 SimulateReadSequence(kReads, arraysize(kReads)); |
| 117 } | 120 } |
| 118 | 121 |
| 119 TEST(MessageDecoderTest, EmptyReads) { | 122 TEST(MessageDecoderTest, EmptyReads) { |
| 120 const int kReads[] = {4, 0, 50, 0}; | 123 const int kReads[] = {4, 0, 50, 0}; |
| 121 SimulateReadSequence(kReads, arraysize(kReads)); | 124 SimulateReadSequence(kReads, arraysize(kReads)); |
| 122 } | 125 } |
| 123 | 126 |
| 124 } // namespace protocol | 127 } // namespace protocol |
| 125 } // namespace remoting | 128 } // namespace remoting |
| OLD | NEW |