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

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

Issue 17314012: Move PPB_TCPSocket out of dev. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months 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.h ('k') | ppapi/tests/test_udp_socket.cc » ('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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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.h" 5 #include "ppapi/tests/test_tcp_socket.h"
6 6
7 #include "ppapi/cpp/dev/tcp_socket_dev.h" 7 #include "ppapi/cpp/tcp_socket.h"
8 #include "ppapi/tests/test_utils.h" 8 #include "ppapi/tests/test_utils.h"
9 #include "ppapi/tests/testing_instance.h" 9 #include "ppapi/tests/testing_instance.h"
10 10
11 namespace { 11 namespace {
12 12
13 // Validates the first line of an HTTP response. 13 // Validates the first line of an HTTP response.
14 bool ValidateHttpResponse(const std::string& s) { 14 bool ValidateHttpResponse(const std::string& s) {
15 // Just check that it begins with "HTTP/" and ends with a "\r\n". 15 // Just check that it begins with "HTTP/" and ends with a "\r\n".
16 return s.size() >= 5 && 16 return s.size() >= 5 &&
17 s.substr(0, 5) == "HTTP/" && 17 s.substr(0, 5) == "HTTP/" &&
18 s.substr(s.size() - 2) == "\r\n"; 18 s.substr(s.size() - 2) == "\r\n";
19 } 19 }
20 20
21 } // namespace 21 } // namespace
22 22
23 REGISTER_TEST_CASE(TCPSocket); 23 REGISTER_TEST_CASE(TCPSocket);
24 24
25 TestTCPSocket::TestTCPSocket(TestingInstance* instance) : TestCase(instance) { 25 TestTCPSocket::TestTCPSocket(TestingInstance* instance) : TestCase(instance) {
26 } 26 }
27 27
28 bool TestTCPSocket::Init() { 28 bool TestTCPSocket::Init() {
29 if (!pp::TCPSocket_Dev::IsAvailable()) 29 if (!pp::TCPSocket::IsAvailable())
30 return false; 30 return false;
31 31
32 // We need something to connect to, so we connect to the HTTP server whence we 32 // We need something to connect to, so we connect to the HTTP server whence we
33 // came. Grab the host and port. 33 // came. Grab the host and port.
34 if (!EnsureRunningOverHTTP()) 34 if (!EnsureRunningOverHTTP())
35 return false; 35 return false;
36 36
37 std::string host; 37 std::string host;
38 uint16_t port = 0; 38 uint16_t port = 0;
39 if (!GetLocalHostPort(instance_->pp_instance(), &host, &port)) 39 if (!GetLocalHostPort(instance_->pp_instance(), &host, &port))
40 return false; 40 return false;
41 41
42 if (!ResolveHost(instance_->pp_instance(), host, port, &addr_)) 42 if (!ResolveHost(instance_->pp_instance(), host, port, &addr_))
43 return false; 43 return false;
44 44
45 return true; 45 return true;
46 } 46 }
47 47
48 void TestTCPSocket::RunTests(const std::string& filter) { 48 void TestTCPSocket::RunTests(const std::string& filter) {
49 RUN_CALLBACK_TEST(TestTCPSocket, Connect, filter); 49 RUN_CALLBACK_TEST(TestTCPSocket, Connect, filter);
50 RUN_CALLBACK_TEST(TestTCPSocket, ReadWrite, filter); 50 RUN_CALLBACK_TEST(TestTCPSocket, ReadWrite, filter);
51 RUN_CALLBACK_TEST(TestTCPSocket, SetOption, filter); 51 RUN_CALLBACK_TEST(TestTCPSocket, SetOption, filter);
52 } 52 }
53 53
54 std::string TestTCPSocket::TestConnect() { 54 std::string TestTCPSocket::TestConnect() {
55 pp::TCPSocket_Dev socket(instance_); 55 pp::TCPSocket socket(instance_);
56 TestCompletionCallback cb(instance_->pp_instance(), callback_type()); 56 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
57 57
58 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback())); 58 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
59 CHECK_CALLBACK_BEHAVIOR(cb); 59 CHECK_CALLBACK_BEHAVIOR(cb);
60 ASSERT_EQ(PP_OK, cb.result()); 60 ASSERT_EQ(PP_OK, cb.result());
61 61
62 pp::NetAddress local_addr, remote_addr; 62 pp::NetAddress local_addr, remote_addr;
63 local_addr = socket.GetLocalAddress(); 63 local_addr = socket.GetLocalAddress();
64 remote_addr = socket.GetRemoteAddress(); 64 remote_addr = socket.GetRemoteAddress();
65 65
66 ASSERT_NE(0, local_addr.pp_resource()); 66 ASSERT_NE(0, local_addr.pp_resource());
67 ASSERT_NE(0, remote_addr.pp_resource()); 67 ASSERT_NE(0, remote_addr.pp_resource());
68 ASSERT_TRUE(EqualNetAddress(addr_, remote_addr)); 68 ASSERT_TRUE(EqualNetAddress(addr_, remote_addr));
69 69
70 socket.Close(); 70 socket.Close();
71 71
72 PASS(); 72 PASS();
73 } 73 }
74 74
75 std::string TestTCPSocket::TestReadWrite() { 75 std::string TestTCPSocket::TestReadWrite() {
76 pp::TCPSocket_Dev socket(instance_); 76 pp::TCPSocket socket(instance_);
77 TestCompletionCallback cb(instance_->pp_instance(), callback_type()); 77 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
78 78
79 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback())); 79 cb.WaitForResult(socket.Connect(addr_, cb.GetCallback()));
80 CHECK_CALLBACK_BEHAVIOR(cb); 80 CHECK_CALLBACK_BEHAVIOR(cb);
81 ASSERT_EQ(PP_OK, cb.result()); 81 ASSERT_EQ(PP_OK, cb.result());
82 82
83 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n")); 83 ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));
84 84
85 // Read up to the first \n and check that it looks like valid HTTP response. 85 // Read up to the first \n and check that it looks like valid HTTP response.
86 std::string s; 86 std::string s;
87 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s)); 87 ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s));
88 ASSERT_TRUE(ValidateHttpResponse(s)); 88 ASSERT_TRUE(ValidateHttpResponse(s));
89 89
90 PASS(); 90 PASS();
91 } 91 }
92 92
93 std::string TestTCPSocket::TestSetOption() { 93 std::string TestTCPSocket::TestSetOption() {
94 pp::TCPSocket_Dev socket(instance_); 94 pp::TCPSocket socket(instance_);
95 TestCompletionCallback cb_1(instance_->pp_instance(), callback_type()); 95 TestCompletionCallback cb_1(instance_->pp_instance(), callback_type());
96 TestCompletionCallback cb_2(instance_->pp_instance(), callback_type()); 96 TestCompletionCallback cb_2(instance_->pp_instance(), callback_type());
97 TestCompletionCallback cb_3(instance_->pp_instance(), callback_type()); 97 TestCompletionCallback cb_3(instance_->pp_instance(), callback_type());
98 98
99 // These options cannot be set before the socket is connected. 99 // These options cannot be set before the socket is connected.
100 int32_t result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY, 100 int32_t result_1 = socket.SetOption(PP_TCPSOCKET_OPTION_NO_DELAY,
101 true, cb_1.GetCallback()); 101 true, cb_1.GetCallback());
102 int32_t result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE, 102 int32_t result_2 = socket.SetOption(PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE,
103 256, cb_2.GetCallback()); 103 256, cb_2.GetCallback());
104 int32_t result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE, 104 int32_t result_3 = socket.SetOption(PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE,
(...skipping 30 matching lines...) Expand all
135 CHECK_CALLBACK_BEHAVIOR(cb_2); 135 CHECK_CALLBACK_BEHAVIOR(cb_2);
136 ASSERT_EQ(PP_OK, cb_2.result()); 136 ASSERT_EQ(PP_OK, cb_2.result());
137 137
138 cb_3.WaitForResult(result_3); 138 cb_3.WaitForResult(result_3);
139 CHECK_CALLBACK_BEHAVIOR(cb_3); 139 CHECK_CALLBACK_BEHAVIOR(cb_3);
140 ASSERT_EQ(PP_OK, cb_3.result()); 140 ASSERT_EQ(PP_OK, cb_3.result());
141 141
142 PASS(); 142 PASS();
143 } 143 }
144 144
145 int32_t TestTCPSocket::ReadFirstLineFromSocket(pp::TCPSocket_Dev* socket, 145 int32_t TestTCPSocket::ReadFirstLineFromSocket(pp::TCPSocket* socket,
146 std::string* s) { 146 std::string* s) {
147 char buffer[1000]; 147 char buffer[1000];
148 148
149 s->clear(); 149 s->clear();
150 // Make sure we don't just hang if |Read()| spews. 150 // Make sure we don't just hang if |Read()| spews.
151 while (s->size() < 10000) { 151 while (s->size() < 10000) {
152 TestCompletionCallback cb(instance_->pp_instance(), callback_type()); 152 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
153 int32_t rv = socket->Read(buffer, sizeof(buffer), cb.GetCallback()); 153 int32_t rv = socket->Read(buffer, sizeof(buffer), cb.GetCallback());
154 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING) 154 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING)
155 return PP_ERROR_FAILED; 155 return PP_ERROR_FAILED;
156 cb.WaitForResult(rv); 156 cb.WaitForResult(rv);
157 if (cb.result() < 0) 157 if (cb.result() < 0)
158 return cb.result(); 158 return cb.result();
159 if (cb.result() == 0) 159 if (cb.result() == 0)
160 return PP_ERROR_FAILED; // Didn't get a \n-terminated line. 160 return PP_ERROR_FAILED; // Didn't get a \n-terminated line.
161 s->reserve(s->size() + cb.result()); 161 s->reserve(s->size() + cb.result());
162 for (int32_t i = 0; i < cb.result(); i++) { 162 for (int32_t i = 0; i < cb.result(); i++) {
163 s->push_back(buffer[i]); 163 s->push_back(buffer[i]);
164 if (buffer[i] == '\n') 164 if (buffer[i] == '\n')
165 return PP_OK; 165 return PP_OK;
166 } 166 }
167 } 167 }
168 return PP_ERROR_FAILED; 168 return PP_ERROR_FAILED;
169 } 169 }
170 170
171 int32_t TestTCPSocket::WriteStringToSocket(pp::TCPSocket_Dev* socket, 171 int32_t TestTCPSocket::WriteStringToSocket(pp::TCPSocket* socket,
172 const std::string& s) { 172 const std::string& s) {
173 const char* buffer = s.data(); 173 const char* buffer = s.data();
174 size_t written = 0; 174 size_t written = 0;
175 while (written < s.size()) { 175 while (written < s.size()) {
176 TestCompletionCallback cb(instance_->pp_instance(), callback_type()); 176 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
177 int32_t rv = socket->Write(buffer + written, s.size() - written, 177 int32_t rv = socket->Write(buffer + written, s.size() - written,
178 cb.GetCallback()); 178 cb.GetCallback());
179 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING) 179 if (callback_type() == PP_REQUIRED && rv != PP_OK_COMPLETIONPENDING)
180 return PP_ERROR_FAILED; 180 return PP_ERROR_FAILED;
181 cb.WaitForResult(rv); 181 cb.WaitForResult(rv);
182 if (cb.result() < 0) 182 if (cb.result() < 0)
183 return cb.result(); 183 return cb.result();
184 if (cb.result() == 0) 184 if (cb.result() == 0)
185 return PP_ERROR_FAILED; 185 return PP_ERROR_FAILED;
186 written += cb.result(); 186 written += cb.result();
187 } 187 }
188 if (written != s.size()) 188 if (written != s.size())
189 return PP_ERROR_FAILED; 189 return PP_ERROR_FAILED;
190 return PP_OK; 190 return PP_OK;
191 } 191 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_tcp_socket.h ('k') | ppapi/tests/test_udp_socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698