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

Side by Side Diff: test/cctest/test-sockets.cc

Issue 23484014: Cleanup Socket class and remove it from the platform files. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Cleanup leftover junk, and rename test-sockets to test-socket. Created 7 years, 3 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
OLDNEW
(Empty)
1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29 #include "platform.h"
30 #include "cctest.h"
31
32
33 using namespace ::v8::internal;
34
35
36 class SocketListenerThread : public Thread {
37 public:
38 SocketListenerThread(int port, int data_size)
39 : Thread("SocketListenerThread"),
40 port_(port),
41 data_size_(data_size),
42 server_(NULL),
43 client_(NULL),
44 listening_(0) {
45 data_ = new char[data_size_];
46 }
47 ~SocketListenerThread() {
48 // Close both sockets.
49 delete client_;
50 delete server_;
51 delete[] data_;
52 }
53
54 void Run();
55 void WaitForListening() { listening_.Wait(); }
56 char* data() { return data_; }
57
58 private:
59 int port_;
60 char* data_;
61 int data_size_;
62 Socket* server_; // Server socket used for bind/accept.
63 Socket* client_; // Single client connection used by the test.
64 Semaphore listening_; // Signalled when the server socket is in listen mode.
65 };
66
67
68 void SocketListenerThread::Run() {
69 bool ok;
70
71 // Create the server socket and bind it to the requested port.
72 server_ = OS::CreateSocket();
73 server_->SetReuseAddress(true);
74 CHECK(server_ != NULL);
75 ok = server_->Bind(port_);
76 CHECK(ok);
77
78 // Listen for new connections.
79 ok = server_->Listen(1);
80 CHECK(ok);
81 listening_.Signal();
82
83 // Accept a connection.
84 client_ = server_->Accept();
85 CHECK(client_ != NULL);
86
87 // Read the expected niumber of bytes of data.
88 int bytes_read = 0;
89 while (bytes_read < data_size_) {
90 bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read);
91 }
92 }
93
94
95 static bool SendAll(Socket* socket, const char* data, int len) {
96 int sent_len = 0;
97 while (sent_len < len) {
98 int status = socket->Send(data, len);
99 if (status <= 0) {
100 return false;
101 }
102 sent_len += status;
103 }
104 return true;
105 }
106
107
108 static void SendAndReceive(int port, char *data, int len) {
109 static const char* kLocalhost = "localhost";
110
111 bool ok;
112
113 // Make a string with the port number.
114 const int kPortBuferLen = 6;
115 char port_str[kPortBuferLen];
116 OS::SNPrintF(Vector<char>(port_str, kPortBuferLen), "%d", port);
117
118 // Create a socket listener.
119 SocketListenerThread* listener = new SocketListenerThread(port, len);
120 listener->Start();
121 listener->WaitForListening();
122
123 // Connect and write some data.
124 Socket* client = OS::CreateSocket();
125 CHECK(client != NULL);
126 ok = client->Connect(kLocalhost, port_str);
127 CHECK(ok);
128
129 // Send all the data.
130 ok = SendAll(client, data, len);
131 CHECK(ok);
132
133 // Wait until data is received.
134 listener->Join();
135
136 // Check that data received is the same as data send.
137 for (int i = 0; i < len; i++) {
138 CHECK(data[i] == listener->data()[i]);
139 }
140
141 // Close the client before the listener to avoid TIME_WAIT issues.
142 client->Shutdown();
143 delete client;
144 delete listener;
145 }
146
147
148 TEST(Socket) {
149 // Make sure this port is not used by other tests to allow tests to run in
150 // parallel.
151 static const int kPort = 5859 + FlagDependentPortOffset();
152
153 bool ok;
154
155 // Initialize socket support.
156 ok = Socket::SetUp();
157 CHECK(ok);
158
159 // Send and receive some data.
160 static const int kBufferSizeSmall = 20;
161 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij";
162 SendAndReceive(kPort, small_data, kBufferSizeSmall);
163
164 // Send and receive some more data.
165 static const int kBufferSizeMedium = 10000;
166 char* medium_data = new char[kBufferSizeMedium];
167 for (int i = 0; i < kBufferSizeMedium; i++) {
168 medium_data[i] = i % 256;
169 }
170 SendAndReceive(kPort, medium_data, kBufferSizeMedium);
171 delete[] medium_data;
172
173 // Send and receive even more data.
174 static const int kBufferSizeLarge = 1000000;
175 char* large_data = new char[kBufferSizeLarge];
176 for (int i = 0; i < kBufferSizeLarge; i++) {
177 large_data[i] = i % 256;
178 }
179 SendAndReceive(kPort, large_data, kBufferSizeLarge);
180 delete[] large_data;
181 }
182
183
184 TEST(HToNNToH) {
185 uint16_t x = 1234;
186 CHECK_EQ(x, Socket::NToH(Socket::HToN(x)));
187
188 uint32_t y = 12345678;
189 CHECK(y == Socket::NToH(Socket::HToN(y)));
190 }
OLDNEW
« src/platform.h ('K') | « test/cctest/test-socket.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698