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" |
| 6 |
5 #include <stdint.h> | 7 #include <stdint.h> |
6 | 8 |
| 9 #include <memory> |
7 #include <string> | 10 #include <string> |
8 | 11 |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "remoting/proto/event.pb.h" | 15 #include "remoting/proto/event.pb.h" |
14 #include "remoting/proto/internal.pb.h" | 16 #include "remoting/proto/internal.pb.h" |
15 #include "remoting/protocol/message_decoder.h" | |
16 #include "remoting/protocol/message_serialization.h" | 17 #include "remoting/protocol/message_serialization.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace remoting { | 20 namespace remoting { |
20 namespace protocol { | 21 namespace protocol { |
21 | 22 |
22 static const unsigned int kTestKey = 142; | 23 static const unsigned int kTestKey = 142; |
23 | 24 |
24 static void AppendMessage(const EventMessage& msg, | 25 static void AppendMessage(const EventMessage& msg, |
25 std::string* buffer) { | 26 std::string* buffer) { |
(...skipping 20 matching lines...) Expand all Loading... |
46 *size = encoded_data.length(); | 47 *size = encoded_data.length(); |
47 *buffer = new uint8_t[*size]; | 48 *buffer = new uint8_t[*size]; |
48 memcpy(*buffer, encoded_data.c_str(), *size); | 49 memcpy(*buffer, encoded_data.c_str(), *size); |
49 } | 50 } |
50 | 51 |
51 void SimulateReadSequence(const int read_sequence[], int sequence_size) { | 52 void SimulateReadSequence(const int read_sequence[], int sequence_size) { |
52 // Prepare encoded data for testing. | 53 // Prepare encoded data for testing. |
53 int size; | 54 int size; |
54 uint8_t* test_data; | 55 uint8_t* test_data; |
55 PrepareData(&test_data, &size); | 56 PrepareData(&test_data, &size); |
56 scoped_ptr<uint8_t[]> memory_deleter(test_data); | 57 std::unique_ptr<uint8_t[]> memory_deleter(test_data); |
57 | 58 |
58 // Then simulate using MessageDecoder to decode variable | 59 // Then simulate using MessageDecoder to decode variable |
59 // size of encoded data. | 60 // size of encoded data. |
60 // 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 |
61 // by iterating the following array for read sizes. | 62 // by iterating the following array for read sizes. |
62 MessageDecoder decoder; | 63 MessageDecoder decoder; |
63 | 64 |
64 // 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 |
65 // read pattern. | 66 // read pattern. |
66 std::list<EventMessage*> message_list; | 67 std::list<EventMessage*> message_list; |
67 for (int pos = 0; pos < size;) { | 68 for (int pos = 0; pos < size;) { |
68 SCOPED_TRACE("Input position: " + base::IntToString(pos)); | 69 SCOPED_TRACE("Input position: " + base::IntToString(pos)); |
69 | 70 |
70 // First generate the amount to feed the decoder. | 71 // First generate the amount to feed the decoder. |
71 int read = std::min(size - pos, read_sequence[pos % sequence_size]); | 72 int read = std::min(size - pos, read_sequence[pos % sequence_size]); |
72 | 73 |
73 // And then prepare an IOBuffer for feeding it. | 74 // And then prepare an IOBuffer for feeding it. |
74 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); | 75 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); |
75 memcpy(buffer->data(), test_data + pos, read); | 76 memcpy(buffer->data(), test_data + pos, read); |
76 decoder.AddData(buffer, read); | 77 decoder.AddData(buffer, read); |
77 while (true) { | 78 while (true) { |
78 scoped_ptr<CompoundBuffer> message(decoder.GetNextMessage()); | 79 std::unique_ptr<CompoundBuffer> message(decoder.GetNextMessage()); |
79 if (!message.get()) | 80 if (!message.get()) |
80 break; | 81 break; |
81 | 82 |
82 EventMessage* event = new EventMessage(); | 83 EventMessage* event = new EventMessage(); |
83 CompoundBufferInputStream stream(message.get()); | 84 CompoundBufferInputStream stream(message.get()); |
84 ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream)); | 85 ASSERT_TRUE(event->ParseFromZeroCopyStream(&stream)); |
85 message_list.push_back(event); | 86 message_list.push_back(event); |
86 } | 87 } |
87 pos += read; | 88 pos += read; |
88 } | 89 } |
(...skipping 30 matching lines...) Expand all Loading... |
119 SimulateReadSequence(kReads, arraysize(kReads)); | 120 SimulateReadSequence(kReads, arraysize(kReads)); |
120 } | 121 } |
121 | 122 |
122 TEST(MessageDecoderTest, EmptyReads) { | 123 TEST(MessageDecoderTest, EmptyReads) { |
123 const int kReads[] = {4, 0, 50, 0}; | 124 const int kReads[] = {4, 0, 50, 0}; |
124 SimulateReadSequence(kReads, arraysize(kReads)); | 125 SimulateReadSequence(kReads, arraysize(kReads)); |
125 } | 126 } |
126 | 127 |
127 } // namespace protocol | 128 } // namespace protocol |
128 } // namespace remoting | 129 } // namespace remoting |
OLD | NEW |