2009-02-24 13:32:01 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-02-24 13:32:01 +00:00
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ::v8::internal;
|
|
|
|
|
|
|
|
|
|
|
|
class SocketListenerThread : public Thread {
|
|
|
|
public:
|
2011-06-10 09:54:04 +00:00
|
|
|
SocketListenerThread(int port, int data_size)
|
|
|
|
: Thread("SocketListenerThread"),
|
2011-03-21 15:04:17 +00:00
|
|
|
port_(port),
|
|
|
|
data_size_(data_size),
|
|
|
|
server_(NULL),
|
|
|
|
client_(NULL),
|
2013-09-02 12:26:06 +00:00
|
|
|
listening_(0) {
|
2009-02-24 13:32:01 +00:00
|
|
|
data_ = new char[data_size_];
|
|
|
|
}
|
|
|
|
~SocketListenerThread() {
|
|
|
|
// Close both sockets.
|
|
|
|
delete client_;
|
|
|
|
delete server_;
|
2009-03-18 13:32:22 +00:00
|
|
|
delete[] data_;
|
2009-02-24 13:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Run();
|
2013-09-02 12:26:06 +00:00
|
|
|
void WaitForListening() { listening_.Wait(); }
|
2009-02-24 13:32:01 +00:00
|
|
|
char* data() { return data_; }
|
|
|
|
|
|
|
|
private:
|
2009-03-19 21:07:07 +00:00
|
|
|
int port_;
|
2009-02-24 13:32:01 +00:00
|
|
|
char* data_;
|
|
|
|
int data_size_;
|
|
|
|
Socket* server_; // Server socket used for bind/accept.
|
|
|
|
Socket* client_; // Single client connection used by the test.
|
2013-09-02 12:26:06 +00:00
|
|
|
Semaphore listening_; // Signalled when the server socket is in listen mode.
|
2009-02-24 13:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void SocketListenerThread::Run() {
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Create the server socket and bind it to the requested port.
|
|
|
|
server_ = OS::CreateSocket();
|
2009-09-25 10:36:00 +00:00
|
|
|
server_->SetReuseAddress(true);
|
2009-02-24 13:32:01 +00:00
|
|
|
CHECK(server_ != NULL);
|
2009-03-19 21:07:07 +00:00
|
|
|
ok = server_->Bind(port_);
|
2009-02-24 13:32:01 +00:00
|
|
|
CHECK(ok);
|
|
|
|
|
|
|
|
// Listen for new connections.
|
|
|
|
ok = server_->Listen(1);
|
|
|
|
CHECK(ok);
|
2013-09-02 12:26:06 +00:00
|
|
|
listening_.Signal();
|
2009-02-24 14:22:42 +00:00
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
// Accept a connection.
|
|
|
|
client_ = server_->Accept();
|
|
|
|
CHECK(client_ != NULL);
|
|
|
|
|
|
|
|
// Read the expected niumber of bytes of data.
|
|
|
|
int bytes_read = 0;
|
|
|
|
while (bytes_read < data_size_) {
|
|
|
|
bytes_read += client_->Receive(data_ + bytes_read, data_size_ - bytes_read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 10:33:00 +00:00
|
|
|
static bool SendAll(Socket* socket, const char* data, int len) {
|
|
|
|
int sent_len = 0;
|
|
|
|
while (sent_len < len) {
|
|
|
|
int status = socket->Send(data, len);
|
|
|
|
if (status <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sent_len += status;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
static void SendAndReceive(int port, char *data, int len) {
|
|
|
|
static const char* kLocalhost = "localhost";
|
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
bool ok;
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
// Make a string with the port number.
|
|
|
|
const int kPortBuferLen = 6;
|
|
|
|
char port_str[kPortBuferLen];
|
|
|
|
OS::SNPrintF(Vector<char>(port_str, kPortBuferLen), "%d", port);
|
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
// Create a socket listener.
|
2011-06-10 09:54:04 +00:00
|
|
|
SocketListenerThread* listener = new SocketListenerThread(port, len);
|
2009-02-24 13:32:01 +00:00
|
|
|
listener->Start();
|
|
|
|
listener->WaitForListening();
|
2009-02-24 14:22:42 +00:00
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
// Connect and write some data.
|
|
|
|
Socket* client = OS::CreateSocket();
|
|
|
|
CHECK(client != NULL);
|
2009-03-19 21:07:07 +00:00
|
|
|
ok = client->Connect(kLocalhost, port_str);
|
2009-02-24 13:32:01 +00:00
|
|
|
CHECK(ok);
|
|
|
|
|
|
|
|
// Send all the data.
|
2009-03-04 10:33:00 +00:00
|
|
|
ok = SendAll(client, data, len);
|
2009-02-24 13:32:01 +00:00
|
|
|
CHECK(ok);
|
|
|
|
|
|
|
|
// Wait until data is received.
|
|
|
|
listener->Join();
|
|
|
|
|
|
|
|
// Check that data received is the same as data send.
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
CHECK(data[i] == listener->data()[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the client before the listener to avoid TIME_WAIT issues.
|
2009-03-19 11:55:09 +00:00
|
|
|
client->Shutdown();
|
2009-02-24 13:32:01 +00:00
|
|
|
delete client;
|
|
|
|
delete listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Socket) {
|
2009-03-19 21:07:07 +00:00
|
|
|
// Make sure this port is not used by other tests to allow tests to run in
|
|
|
|
// parallel.
|
2012-10-01 09:41:18 +00:00
|
|
|
static const int kPort = 5859 + FlagDependentPortOffset();
|
2009-03-19 21:07:07 +00:00
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Initialize socket support.
|
2012-01-13 13:09:52 +00:00
|
|
|
ok = Socket::SetUp();
|
2009-02-24 13:32:01 +00:00
|
|
|
CHECK(ok);
|
|
|
|
|
|
|
|
// Send and receive some data.
|
|
|
|
static const int kBufferSizeSmall = 20;
|
|
|
|
char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij";
|
2009-03-19 21:07:07 +00:00
|
|
|
SendAndReceive(kPort, small_data, kBufferSizeSmall);
|
2009-02-24 13:32:01 +00:00
|
|
|
|
|
|
|
// Send and receive some more data.
|
|
|
|
static const int kBufferSizeMedium = 10000;
|
|
|
|
char* medium_data = new char[kBufferSizeMedium];
|
|
|
|
for (int i = 0; i < kBufferSizeMedium; i++) {
|
|
|
|
medium_data[i] = i % 256;
|
|
|
|
}
|
2009-03-19 21:07:07 +00:00
|
|
|
SendAndReceive(kPort, medium_data, kBufferSizeMedium);
|
2009-03-18 13:32:22 +00:00
|
|
|
delete[] medium_data;
|
2009-02-24 13:32:01 +00:00
|
|
|
|
|
|
|
// Send and receive even more data.
|
|
|
|
static const int kBufferSizeLarge = 1000000;
|
|
|
|
char* large_data = new char[kBufferSizeLarge];
|
|
|
|
for (int i = 0; i < kBufferSizeLarge; i++) {
|
|
|
|
large_data[i] = i % 256;
|
|
|
|
}
|
2009-03-19 21:07:07 +00:00
|
|
|
SendAndReceive(kPort, large_data, kBufferSizeLarge);
|
2009-03-18 13:32:22 +00:00
|
|
|
delete[] large_data;
|
2009-02-24 13:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HToNNToH) {
|
|
|
|
uint16_t x = 1234;
|
|
|
|
CHECK_EQ(x, Socket::NToH(Socket::HToN(x)));
|
|
|
|
|
|
|
|
uint32_t y = 12345678;
|
|
|
|
CHECK(y == Socket::NToH(Socket::HToN(y)));
|
|
|
|
}
|