OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "remoting/protocol/messages_decoder.h" | 10 #include "remoting/base/multiple_array_input_stream.h" |
| 11 #include "remoting/proto/internal.pb.h" |
| 12 #include "remoting/protocol/message_decoder.h" |
11 #include "remoting/protocol/util.h" | 13 #include "remoting/protocol/util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace remoting { | 16 namespace remoting { |
15 | 17 |
16 static const int kWidth = 640; | 18 static const int kWidth = 640; |
17 static const int kHeight = 480; | 19 static const int kHeight = 480; |
18 static const std::string kTestData = "Chromoting rockz"; | 20 static const std::string kTestData = "Chromoting rockz"; |
19 | 21 |
20 static void AppendMessage(const ChromotingHostMessage& msg, | 22 static void AppendMessage(const ChromotingHostMessage& msg, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 msg.mutable_end_update_stream(); | 55 msg.mutable_end_update_stream(); |
54 AppendMessage(msg, &encoded_data); | 56 AppendMessage(msg, &encoded_data); |
55 msg.Clear(); | 57 msg.Clear(); |
56 } | 58 } |
57 | 59 |
58 *size = encoded_data.length(); | 60 *size = encoded_data.length(); |
59 *buffer = new uint8[*size]; | 61 *buffer = new uint8[*size]; |
60 memcpy(*buffer, encoded_data.c_str(), *size); | 62 memcpy(*buffer, encoded_data.c_str(), *size); |
61 } | 63 } |
62 | 64 |
63 TEST(MessagesDecoderTest, BasicOperations) { | 65 void SimulateReadSequence(const int read_sequence[], int sequence_size) { |
64 // Prepare encoded data for testing. | 66 // Prepare encoded data for testing. |
65 int size; | 67 int size; |
66 uint8* test_data; | 68 uint8* test_data; |
67 PrepareData(&test_data, &size); | 69 PrepareData(&test_data, &size); |
68 scoped_array<uint8> memory_deleter(test_data); | 70 scoped_array<uint8> memory_deleter(test_data); |
69 | 71 |
70 // Then simulate using MessagesDecoder to decode variable | 72 // Then simulate using MessageDecoder to decode variable |
71 // size of encoded data. | 73 // size of encoded data. |
72 // The first thing to do is to generate a variable size of data. This is done | 74 // The first thing to do is to generate a variable size of data. This is done |
73 // by iterating the following array for read sizes. | 75 // by iterating the following array for read sizes. |
74 const int kReadSizes[] = {1, 2, 3, 1}; | 76 MessageDecoder decoder; |
75 | |
76 MessagesDecoder decoder; | |
77 | 77 |
78 // Then feed the protocol decoder using the above generated data and the | 78 // Then feed the protocol decoder using the above generated data and the |
79 // read pattern. | 79 // read pattern. |
80 HostMessageList message_list; | 80 std::list<ChromotingHostMessage*> message_list; |
81 for (int i = 0; i < size;) { | 81 for (int i = 0; i < size;) { |
82 // First generate the amount to feed the decoder. | 82 // First generate the amount to feed the decoder. |
83 int read = std::min(size - i, kReadSizes[i % arraysize(kReadSizes)]); | 83 int read = std::min(size - i, read_sequence[i % sequence_size]); |
84 | 84 |
85 // And then prepare a DataBuffer for feeding it. | 85 // And then prepare an IOBuffer for feeding it. |
86 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(read); | 86 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(read); |
87 memcpy(buffer->data(), test_data + i, read); | 87 memcpy(buffer->data(), test_data + i, read); |
88 decoder.ParseHostMessages(buffer, read, &message_list); | 88 decoder.ParseMessages(buffer, read, &message_list); |
89 i += read; | 89 i += read; |
90 } | 90 } |
91 | 91 |
92 // Then verify the decoded messages. | 92 // Then verify the decoded messages. |
93 EXPECT_EQ(31u, message_list.size()); | 93 EXPECT_EQ(31u, message_list.size()); |
94 ASSERT_TRUE(message_list.size() > 0); | |
95 EXPECT_TRUE(message_list.front()->has_init_client()); | 94 EXPECT_TRUE(message_list.front()->has_init_client()); |
96 delete message_list.front(); | 95 delete message_list.front(); |
97 message_list.pop_front(); | 96 message_list.pop_front(); |
98 | 97 |
99 int index = 0; | 98 int index = 0; |
100 for (HostMessageList::iterator it = message_list.begin(); | 99 for (std::list<ChromotingHostMessage*>::iterator it = |
| 100 message_list.begin(); |
101 it != message_list.end(); ++it) { | 101 it != message_list.end(); ++it) { |
102 ChromotingHostMessage* message = *it; | 102 ChromotingHostMessage* message = *it; |
103 int type = index % 3; | 103 int type = index % 3; |
104 ++index; | 104 ++index; |
105 if (type == 0) { | 105 if (type == 0) { |
106 // Begin update stream. | 106 // Begin update stream. |
107 EXPECT_TRUE(message->has_begin_update_stream()); | 107 EXPECT_TRUE(message->has_begin_update_stream()); |
108 } else if (type == 1) { | 108 } else if (type == 1) { |
109 // Partial update stream. | 109 // Partial update stream. |
110 EXPECT_TRUE(message->has_update_stream_packet()); | 110 EXPECT_TRUE(message->has_update_stream_packet()); |
111 EXPECT_EQ(kTestData, | 111 EXPECT_EQ(kTestData, |
112 message->update_stream_packet().rect_data().data()); | 112 message->update_stream_packet().rect_data().data()); |
113 } else if (type == 2) { | 113 } else if (type == 2) { |
114 // End update stream. | 114 // End update stream. |
115 EXPECT_TRUE(message->has_end_update_stream()); | 115 EXPECT_TRUE(message->has_end_update_stream()); |
116 } | 116 } |
117 } | 117 } |
118 STLDeleteElements(&message_list); | 118 STLDeleteElements(&message_list); |
119 } | 119 } |
120 | 120 |
| 121 TEST(MessageDecoderTest, SmallReads) { |
| 122 const int kReads[] = {1, 2, 3, 1}; |
| 123 SimulateReadSequence(kReads, arraysize(kReads)); |
| 124 } |
| 125 |
| 126 TEST(MessageDecoderTest, LargeReads) { |
| 127 const int kReads[] = {50, 50, 5}; |
| 128 SimulateReadSequence(kReads, arraysize(kReads)); |
| 129 } |
| 130 |
| 131 TEST(MessageDecoderTest, EmptyReads) { |
| 132 const int kReads[] = {4, 0, 50, 0}; |
| 133 SimulateReadSequence(kReads, arraysize(kReads)); |
| 134 } |
| 135 |
121 } // namespace remoting | 136 } // namespace remoting |
OLD | NEW |