2009-02-24 13:32:01 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ::v8::internal;
|
|
|
|
|
|
|
|
|
|
|
|
class SocketListenerThread : public Thread {
|
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit SocketListenerThread(Isolate* isolate, int port, int data_size)
|
2011-03-21 15:04:17 +00:00
|
|
|
: Thread(isolate, "SocketListenerThread"),
|
|
|
|
port_(port),
|
|
|
|
data_size_(data_size),
|
|
|
|
server_(NULL),
|
|
|
|
client_(NULL),
|
|
|
|
listening_(OS::CreateSemaphore(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 listening_;
|
|
|
|
delete[] data_;
|
2009-02-24 13:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Run();
|
|
|
|
void WaitForListening() { listening_->Wait(); }
|
|
|
|
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.
|
|
|
|
Semaphore* listening_; // Signalled when the server socket is in listen mode.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
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-03-18 20:35:07 +00:00
|
|
|
SocketListenerThread* listener = new SocketListenerThread(Isolate::Current(),
|
|
|
|
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.
|
|
|
|
static const int kPort = 5859;
|
|
|
|
|
2009-02-24 13:32:01 +00:00
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Initialize socket support.
|
|
|
|
ok = Socket::Setup();
|
|
|
|
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)));
|
|
|
|
}
|