Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: ppapi/tests/test_tcp_socket_private.cc

Issue 8506016: Remove 'Flash' from TCP/UDP Pepper interfaces. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: resolved last comments Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_tcp_socket_private.h ('k') | ppapi/thunk/interfaces_ppb_private.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ppapi/tests/test_tcp_socket_private.h" 5 #include "ppapi/tests/test_tcp_socket_private.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include "base/string_split.h" 9 #include "base/string_split.h"
10 #include "ppapi/c/dev/ppb_url_util_dev.h" 10 #include "ppapi/c/dev/ppb_url_util_dev.h"
11 #include "ppapi/cpp/dev/url_util_dev.h" 11 #include "ppapi/cpp/dev/url_util_dev.h"
12 #include "ppapi/cpp/private/flash_tcp_socket.h" 12 #include "ppapi/cpp/private/tcp_socket_private.h"
13 #include "ppapi/cpp/var.h" 13 #include "ppapi/cpp/var.h"
14 #include "ppapi/tests/testing_instance.h" 14 #include "ppapi/tests/testing_instance.h"
15 #include "ppapi/tests/test_utils.h" 15 #include "ppapi/tests/test_utils.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Validates the first line of an HTTP response. 19 // Validates the first line of an HTTP response.
20 bool ValidateHttpResponse(const std::string& s) { 20 bool ValidateHttpResponse(const std::string& s) {
21 // Just check that it begins with "HTTP/" and ends with a "\r\n". 21 // Just check that it begins with "HTTP/" and ends with a "\r\n".
22 return s.size() >= 5 && 22 return s.size() >= 5 &&
23 s.substr(0, 5) == "HTTP/" && 23 s.substr(0, 5) == "HTTP/" &&
24 s.substr(s.size() - 2) == "\r\n"; 24 s.substr(s.size() - 2) == "\r\n";
25 } 25 }
26 26
27 } // namespace 27 } // namespace
28 28
29 REGISTER_TEST_CASE(TCPSocketPrivate); 29 REGISTER_TEST_CASE(TCPSocketPrivate);
30 30
31 TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance) 31 TestTCPSocketPrivate::TestTCPSocketPrivate(TestingInstance* instance)
32 : TestCase(instance) { 32 : TestCase(instance) {
33 } 33 }
34 34
35 bool TestTCPSocketPrivate::Init() { 35 bool TestTCPSocketPrivate::Init() {
36 if (!TCPSocketPrivate::IsAvailable()) 36 if (!pp::TCPSocketPrivate::IsAvailable())
37 return false; 37 return false;
38 38
39 // This test currently only works out-of-process (since the API is really only 39 // This test currently only works out-of-process (since the API is really only
40 // implemented in that case). 40 // implemented in that case).
41 const PPB_Testing_Dev* testing = GetTestingInterface(); 41 const PPB_Testing_Dev* testing = GetTestingInterface();
42 if (!testing) 42 if (!testing)
43 return false; 43 return false;
44 if (!testing->IsOutOfProcess()) 44 if (!testing->IsOutOfProcess())
45 return false; 45 return false;
46 46
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 return true; 80 return true;
81 } 81 }
82 82
83 void TestTCPSocketPrivate::RunTests(const std::string& filter) { 83 void TestTCPSocketPrivate::RunTests(const std::string& filter) {
84 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter); 84 RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter);
85 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter); 85 RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter);
86 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter); 86 RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter);
87 } 87 }
88 88
89 std::string TestTCPSocketPrivate::TestBasic() { 89 std::string TestTCPSocketPrivate::TestBasic() {
90 TCPSocketPrivate socket(instance_); 90 pp::TCPSocketPrivate socket(instance_);
91 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 91 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
92 92
93 int32_t rv = socket.Connect(host_.c_str(), port_, cb); 93 int32_t rv = socket.Connect(host_.c_str(), port_, cb);
94 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); 94 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
95 if (rv == PP_OK_COMPLETIONPENDING) 95 if (rv == PP_OK_COMPLETIONPENDING)
96 rv = cb.WaitForResult(); 96 rv = cb.WaitForResult();
97 ASSERT_EQ(PP_OK, rv); 97 ASSERT_EQ(PP_OK, rv);
98 98
99 PP_NetAddress_Private unused; 99 PP_NetAddress_Private unused;
100 // TODO(viettrungluu): check the values somehow. 100 // TODO(viettrungluu): check the values somehow.
101 ASSERT_TRUE(socket.GetLocalAddress(&unused)); 101 ASSERT_TRUE(socket.GetLocalAddress(&unused));
102 ASSERT_TRUE(socket.GetRemoteAddress(&unused)); 102 ASSERT_TRUE(socket.GetRemoteAddress(&unused));
103 103
104 socket.Disconnect(); 104 socket.Disconnect();
105 105
106 PASS(); 106 PASS();
107 } 107 }
108 108
109 std::string TestTCPSocketPrivate::TestReadWrite() { 109 std::string TestTCPSocketPrivate::TestReadWrite() {
110 TCPSocketPrivate socket(instance_); 110 pp::TCPSocketPrivate socket(instance_);
111 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 111 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
112 112
113 int32_t rv = socket.Connect(host_.c_str(), port_, cb); 113 int32_t rv = socket.Connect(host_.c_str(), port_, cb);
114 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); 114 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
115 if (rv == PP_OK_COMPLETIONPENDING) 115 if (rv == PP_OK_COMPLETIONPENDING)
116 rv = cb.WaitForResult(); 116 rv = cb.WaitForResult();
117 ASSERT_EQ(PP_OK, rv); 117 ASSERT_EQ(PP_OK, rv);
118 118
119 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); 119 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));
120 120
121 // Read up to the first \n and check that it looks like valid HTTP response. 121 // Read up to the first \n and check that it looks like valid HTTP response.
122 std::string s; 122 std::string s;
123 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); 123 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s));
124 ASSERT_TRUE(ValidateHttpResponse(s)); 124 ASSERT_TRUE(ValidateHttpResponse(s));
125 125
126 socket.Disconnect(); 126 socket.Disconnect();
127 127
128 PASS(); 128 PASS();
129 } 129 }
130 130
131 std::string TestTCPSocketPrivate::TestConnectAddress() { 131 std::string TestTCPSocketPrivate::TestConnectAddress() {
132 PP_NetAddress_Private address; 132 PP_NetAddress_Private address;
133 133
134 // First, bring up a connection and grab the address. 134 // First, bring up a connection and grab the address.
135 { 135 {
136 TCPSocketPrivate socket(instance_); 136 pp::TCPSocketPrivate socket(instance_);
137 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 137 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
138 int32_t rv = socket.Connect(host_.c_str(), port_, cb); 138 int32_t rv = socket.Connect(host_.c_str(), port_, cb);
139 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); 139 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
140 if (rv == PP_OK_COMPLETIONPENDING) 140 if (rv == PP_OK_COMPLETIONPENDING)
141 rv = cb.WaitForResult(); 141 rv = cb.WaitForResult();
142 ASSERT_EQ(PP_OK, rv); 142 ASSERT_EQ(PP_OK, rv);
143 ASSERT_TRUE(socket.GetRemoteAddress(&address)); 143 ASSERT_TRUE(socket.GetRemoteAddress(&address));
144 // Omit the |Disconnect()| here to make sure we don't crash if we just let 144 // Omit the |Disconnect()| here to make sure we don't crash if we just let
145 // the resource be destroyed. 145 // the resource be destroyed.
146 } 146 }
147 147
148 // Connect to that address. 148 // Connect to that address.
149 TCPSocketPrivate socket(instance_); 149 pp::TCPSocketPrivate socket(instance_);
150 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 150 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
151 int32_t rv = socket.ConnectWithNetAddress(&address, cb); 151 int32_t rv = socket.ConnectWithNetAddress(&address, cb);
152 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING); 152 ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
153 if (rv == PP_OK_COMPLETIONPENDING) 153 if (rv == PP_OK_COMPLETIONPENDING)
154 rv = cb.WaitForResult(); 154 rv = cb.WaitForResult();
155 ASSERT_EQ(PP_OK, rv); 155 ASSERT_EQ(PP_OK, rv);
156 156
157 // Make sure we can read/write to it properly (see |TestReadWrite()|). 157 // Make sure we can read/write to it properly (see |TestReadWrite()|).
158 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); 158 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));
159 std::string s; 159 std::string s;
160 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); 160 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s));
161 ASSERT_TRUE(ValidateHttpResponse(s)); 161 ASSERT_TRUE(ValidateHttpResponse(s));
162 162
163 socket.Disconnect(); 163 socket.Disconnect();
164 164
165 PASS(); 165 PASS();
166 } 166 }
167 167
168 // TODO(viettrungluu): Try testing SSL somehow. 168 // TODO(viettrungluu): Try testing SSL somehow.
169 169
170 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket(TCPSocketPrivate* socket, 170 int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket(
171 std::string* s) { 171 pp::TCPSocketPrivate* socket,
172 std::string* s) {
172 char buffer[10000]; 173 char buffer[10000];
173 174
174 s->clear(); 175 s->clear();
175 // Make sure we don't just hang if |Read()| spews. 176 // Make sure we don't just hang if |Read()| spews.
176 while (s->size() < 1000000) { 177 while (s->size() < 1000000) {
177 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 178 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
178 int32_t rv = socket->Read(buffer, sizeof(buffer), cb); 179 int32_t rv = socket->Read(buffer, sizeof(buffer), cb);
179 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 180 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
180 return PP_ERROR_FAILED; 181 return PP_ERROR_FAILED;
181 if (rv == PP_OK_COMPLETIONPENDING) 182 if (rv == PP_OK_COMPLETIONPENDING)
182 rv = cb.WaitForResult(); 183 rv = cb.WaitForResult();
183 if (rv < 0) 184 if (rv < 0)
184 return rv; 185 return rv;
185 if (rv == 0) 186 if (rv == 0)
186 return PP_ERROR_FAILED; // Didn't get a \n-terminated line. 187 return PP_ERROR_FAILED; // Didn't get a \n-terminated line.
187 s->reserve(s->size() + rv); 188 s->reserve(s->size() + rv);
188 for (int32_t i = 0; i < rv; i++) { 189 for (int32_t i = 0; i < rv; i++) {
189 s->push_back(buffer[i]); 190 s->push_back(buffer[i]);
190 if (buffer[i] == '\n') 191 if (buffer[i] == '\n')
191 return PP_OK; 192 return PP_OK;
192 } 193 }
193 } 194 }
194 return PP_ERROR_FAILED; 195 return PP_ERROR_FAILED;
195 } 196 }
196 197
197 int32_t TestTCPSocketPrivate::WriteStringToSocket(TCPSocketPrivate* socket, 198 int32_t TestTCPSocketPrivate::WriteStringToSocket(pp::TCPSocketPrivate* socket,
198 const std::string& s) { 199 const std::string& s) {
199 const char* buffer = s.data(); 200 const char* buffer = s.data();
200 size_t written = 0; 201 size_t written = 0;
201 while (written < s.size()) { 202 while (written < s.size()) {
202 TestCompletionCallback cb(instance_->pp_instance(), force_async_); 203 TestCompletionCallback cb(instance_->pp_instance(), force_async_);
203 int32_t rv = socket->Write(buffer + written, s.size() - written, cb); 204 int32_t rv = socket->Write(buffer + written, s.size() - written, cb);
204 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 205 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
205 return PP_ERROR_FAILED; 206 return PP_ERROR_FAILED;
206 if (rv == PP_OK_COMPLETIONPENDING) 207 if (rv == PP_OK_COMPLETIONPENDING)
207 rv = cb.WaitForResult(); 208 rv = cb.WaitForResult();
208 if (rv < 0) 209 if (rv < 0)
209 return rv; 210 return rv;
210 if (rv == 0) 211 if (rv == 0)
211 return PP_ERROR_FAILED; 212 return PP_ERROR_FAILED;
212 written += rv; 213 written += rv;
213 } 214 }
214 if (written != s.size()) 215 if (written != s.size())
215 return PP_ERROR_FAILED; 216 return PP_ERROR_FAILED;
216 return PP_OK; 217 return PP_OK;
217 } 218 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_tcp_socket_private.h ('k') | ppapi/thunk/interfaces_ppb_private.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698