The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / test / cctest / test-sockets.cc @ 40c0f755
History | View | Annotate | Download (3.86 KB)
1 |
// Copyright 2009 the V8 project authors. All rights reserved.
|
---|---|
2 |
|
3 |
#include "v8.h" |
4 |
#include "platform.h" |
5 |
#include "cctest.h" |
6 |
|
7 |
|
8 |
using namespace ::v8::internal; |
9 |
|
10 |
|
11 |
class SocketListenerThread : public Thread { |
12 |
public:
|
13 |
explicit SocketListenerThread(int port, int data_size) |
14 |
: port_(port), data_size_(data_size), server_(NULL), client_(NULL), |
15 |
listening_(OS::CreateSemaphore(0)) {
|
16 |
data_ = new char[data_size_]; |
17 |
} |
18 |
~SocketListenerThread() { |
19 |
// Close both sockets.
|
20 |
delete client_;
|
21 |
delete server_;
|
22 |
delete listening_;
|
23 |
delete[] data_;
|
24 |
} |
25 |
|
26 |
void Run();
|
27 |
void WaitForListening() { listening_->Wait(); }
|
28 |
char* data() { return data_; } |
29 |
|
30 |
private:
|
31 |
int port_;
|
32 |
char* data_;
|
33 |
int data_size_;
|
34 |
Socket* server_; // Server socket used for bind/accept.
|
35 |
Socket* client_; // Single client connection used by the test.
|
36 |
Semaphore* listening_; // Signalled when the server socket is in listen mode.
|
37 |
}; |
38 |
|
39 |
|
40 |
void SocketListenerThread::Run() {
|
41 |
bool ok;
|
42 |
|
43 |
// Create the server socket and bind it to the requested port.
|
44 |
server_ = OS::CreateSocket(); |
45 |
CHECK(server_ != NULL);
|
46 |
ok = server_->Bind(port_); |
47 |
CHECK(ok); |
48 |
|
49 |
// Listen for new connections.
|
50 |
ok = server_->Listen(1);
|
51 |
CHECK(ok); |
52 |
listening_->Signal(); |
53 |
|
54 |
// Accept a connection.
|
55 |
client_ = server_->Accept(); |
56 |
CHECK(client_ != NULL);
|
57 |
|
58 |
// Read the expected niumber of bytes of data.
|
59 |
int bytes_read = 0; |
60 |
while (bytes_read < data_size_) {
|
61 |
bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read); |
62 |
} |
63 |
} |
64 |
|
65 |
|
66 |
static bool SendAll(Socket* socket, const char* data, int len) { |
67 |
int sent_len = 0; |
68 |
while (sent_len < len) {
|
69 |
int status = socket->Send(data, len);
|
70 |
if (status <= 0) { |
71 |
return false; |
72 |
} |
73 |
sent_len += status; |
74 |
} |
75 |
return true; |
76 |
} |
77 |
|
78 |
|
79 |
static void SendAndReceive(int port, char *data, int len) { |
80 |
static const char* kLocalhost = "localhost"; |
81 |
|
82 |
bool ok;
|
83 |
|
84 |
// Make a string with the port number.
|
85 |
const int kPortBuferLen = 6; |
86 |
char port_str[kPortBuferLen];
|
87 |
OS::SNPrintF(Vector<char>(port_str, kPortBuferLen), "%d", port); |
88 |
|
89 |
// Create a socket listener.
|
90 |
SocketListenerThread* listener = new SocketListenerThread(port, len);
|
91 |
listener->Start(); |
92 |
listener->WaitForListening(); |
93 |
|
94 |
// Connect and write some data.
|
95 |
Socket* client = OS::CreateSocket(); |
96 |
CHECK(client != NULL);
|
97 |
ok = client->Connect(kLocalhost, port_str); |
98 |
CHECK(ok); |
99 |
|
100 |
// Send all the data.
|
101 |
ok = SendAll(client, data, len); |
102 |
CHECK(ok); |
103 |
|
104 |
// Wait until data is received.
|
105 |
listener->Join(); |
106 |
|
107 |
// Check that data received is the same as data send.
|
108 |
for (int i = 0; i < len; i++) { |
109 |
CHECK(data[i] == listener->data()[i]); |
110 |
} |
111 |
|
112 |
// Close the client before the listener to avoid TIME_WAIT issues.
|
113 |
client->Shutdown(); |
114 |
delete client;
|
115 |
delete listener;
|
116 |
} |
117 |
|
118 |
|
119 |
TEST(Socket) { |
120 |
// Make sure this port is not used by other tests to allow tests to run in
|
121 |
// parallel.
|
122 |
static const int kPort = 5859; |
123 |
|
124 |
bool ok;
|
125 |
|
126 |
// Initialize socket support.
|
127 |
ok = Socket::Setup(); |
128 |
CHECK(ok); |
129 |
|
130 |
// Send and receive some data.
|
131 |
static const int kBufferSizeSmall = 20; |
132 |
char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij"; |
133 |
SendAndReceive(kPort, small_data, kBufferSizeSmall); |
134 |
|
135 |
// Send and receive some more data.
|
136 |
static const int kBufferSizeMedium = 10000; |
137 |
char* medium_data = new char[kBufferSizeMedium]; |
138 |
for (int i = 0; i < kBufferSizeMedium; i++) { |
139 |
medium_data[i] = i % 256;
|
140 |
} |
141 |
SendAndReceive(kPort, medium_data, kBufferSizeMedium); |
142 |
delete[] medium_data;
|
143 |
|
144 |
// Send and receive even more data.
|
145 |
static const int kBufferSizeLarge = 1000000; |
146 |
char* large_data = new char[kBufferSizeLarge]; |
147 |
for (int i = 0; i < kBufferSizeLarge; i++) { |
148 |
large_data[i] = i % 256;
|
149 |
} |
150 |
SendAndReceive(kPort, large_data, kBufferSizeLarge); |
151 |
delete[] large_data;
|
152 |
} |
153 |
|
154 |
|
155 |
TEST(HToNNToH) { |
156 |
uint16_t x = 1234;
|
157 |
CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); |
158 |
|
159 |
uint32_t y = 12345678;
|
160 |
CHECK(y == Socket::NToH(Socket::HToN(y))); |
161 |
} |