| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | 2 |
| 3 #include "v8.h" | 3 #include "v8.h" |
| 4 #include "platform.h" | 4 #include "platform.h" |
| 5 #include "cctest.h" | 5 #include "cctest.h" |
| 6 | 6 |
| 7 | 7 |
| 8 using namespace ::v8::internal; | 8 using namespace ::v8::internal; |
| 9 | 9 |
| 10 | 10 |
| 11 static const char* kPort = "5858"; | |
| 12 static const char* kLocalhost = "localhost"; | |
| 13 | |
| 14 class SocketListenerThread : public Thread { | 11 class SocketListenerThread : public Thread { |
| 15 public: | 12 public: |
| 16 explicit SocketListenerThread(int data_size) | 13 explicit SocketListenerThread(int port, int data_size) |
| 17 : data_size_(data_size), server_(NULL), client_(NULL), | 14 : port_(port), data_size_(data_size), server_(NULL), client_(NULL), |
| 18 listening_(OS::CreateSemaphore(0)) { | 15 listening_(OS::CreateSemaphore(0)) { |
| 19 data_ = new char[data_size_]; | 16 data_ = new char[data_size_]; |
| 20 } | 17 } |
| 21 ~SocketListenerThread() { | 18 ~SocketListenerThread() { |
| 22 // Close both sockets. | 19 // Close both sockets. |
| 23 delete client_; | 20 delete client_; |
| 24 delete server_; | 21 delete server_; |
| 25 delete listening_; | 22 delete listening_; |
| 26 delete[] data_; | 23 delete[] data_; |
| 27 } | 24 } |
| 28 | 25 |
| 29 void Run(); | 26 void Run(); |
| 30 void WaitForListening() { listening_->Wait(); } | 27 void WaitForListening() { listening_->Wait(); } |
| 31 char* data() { return data_; } | 28 char* data() { return data_; } |
| 32 | 29 |
| 33 private: | 30 private: |
| 31 int port_; |
| 34 char* data_; | 32 char* data_; |
| 35 int data_size_; | 33 int data_size_; |
| 36 Socket* server_; // Server socket used for bind/accept. | 34 Socket* server_; // Server socket used for bind/accept. |
| 37 Socket* client_; // Single client connection used by the test. | 35 Socket* client_; // Single client connection used by the test. |
| 38 Semaphore* listening_; // Signalled when the server socket is in listen mode. | 36 Semaphore* listening_; // Signalled when the server socket is in listen mode. |
| 39 }; | 37 }; |
| 40 | 38 |
| 41 | 39 |
| 42 void SocketListenerThread::Run() { | 40 void SocketListenerThread::Run() { |
| 43 bool ok; | 41 bool ok; |
| 44 | 42 |
| 45 // Create the server socket and bind it to the requested port. | 43 // Create the server socket and bind it to the requested port. |
| 46 server_ = OS::CreateSocket(); | 44 server_ = OS::CreateSocket(); |
| 47 CHECK(server_ != NULL); | 45 CHECK(server_ != NULL); |
| 48 ok = server_->Bind(5858); | 46 ok = server_->Bind(port_); |
| 49 CHECK(ok); | 47 CHECK(ok); |
| 50 | 48 |
| 51 // Listen for new connections. | 49 // Listen for new connections. |
| 52 ok = server_->Listen(1); | 50 ok = server_->Listen(1); |
| 53 CHECK(ok); | 51 CHECK(ok); |
| 54 listening_->Signal(); | 52 listening_->Signal(); |
| 55 | 53 |
| 56 // Accept a connection. | 54 // Accept a connection. |
| 57 client_ = server_->Accept(); | 55 client_ = server_->Accept(); |
| 58 CHECK(client_ != NULL); | 56 CHECK(client_ != NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 int status = socket->Send(data, len); | 69 int status = socket->Send(data, len); |
| 72 if (status <= 0) { | 70 if (status <= 0) { |
| 73 return false; | 71 return false; |
| 74 } | 72 } |
| 75 sent_len += status; | 73 sent_len += status; |
| 76 } | 74 } |
| 77 return true; | 75 return true; |
| 78 } | 76 } |
| 79 | 77 |
| 80 | 78 |
| 81 static void SendAndReceive(char *data, int len) { | 79 static void SendAndReceive(int port, char *data, int len) { |
| 80 static const char* kLocalhost = "localhost"; |
| 81 |
| 82 bool ok; | 82 bool ok; |
| 83 | 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 |
| 84 // Create a socket listener. | 89 // Create a socket listener. |
| 85 SocketListenerThread* listener = new SocketListenerThread(len); | 90 SocketListenerThread* listener = new SocketListenerThread(port, len); |
| 86 listener->Start(); | 91 listener->Start(); |
| 87 listener->WaitForListening(); | 92 listener->WaitForListening(); |
| 88 | 93 |
| 89 // Connect and write some data. | 94 // Connect and write some data. |
| 90 Socket* client = OS::CreateSocket(); | 95 Socket* client = OS::CreateSocket(); |
| 91 CHECK(client != NULL); | 96 CHECK(client != NULL); |
| 92 ok = client->Connect(kLocalhost, kPort); | 97 ok = client->Connect(kLocalhost, port_str); |
| 93 CHECK(ok); | 98 CHECK(ok); |
| 94 | 99 |
| 95 // Send all the data. | 100 // Send all the data. |
| 96 ok = SendAll(client, data, len); | 101 ok = SendAll(client, data, len); |
| 97 CHECK(ok); | 102 CHECK(ok); |
| 98 | 103 |
| 99 // Wait until data is received. | 104 // Wait until data is received. |
| 100 listener->Join(); | 105 listener->Join(); |
| 101 | 106 |
| 102 // Check that data received is the same as data send. | 107 // Check that data received is the same as data send. |
| 103 for (int i = 0; i < len; i++) { | 108 for (int i = 0; i < len; i++) { |
| 104 CHECK(data[i] == listener->data()[i]); | 109 CHECK(data[i] == listener->data()[i]); |
| 105 } | 110 } |
| 106 | 111 |
| 107 // Close the client before the listener to avoid TIME_WAIT issues. | 112 // Close the client before the listener to avoid TIME_WAIT issues. |
| 108 client->Shutdown(); | 113 client->Shutdown(); |
| 109 delete client; | 114 delete client; |
| 110 delete listener; | 115 delete listener; |
| 111 } | 116 } |
| 112 | 117 |
| 113 | 118 |
| 114 TEST(Socket) { | 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 |
| 115 bool ok; | 124 bool ok; |
| 116 | 125 |
| 117 // Initialize socket support. | 126 // Initialize socket support. |
| 118 ok = Socket::Setup(); | 127 ok = Socket::Setup(); |
| 119 CHECK(ok); | 128 CHECK(ok); |
| 120 | 129 |
| 121 // Send and receive some data. | 130 // Send and receive some data. |
| 122 static const int kBufferSizeSmall = 20; | 131 static const int kBufferSizeSmall = 20; |
| 123 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij"; | 132 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij"; |
| 124 SendAndReceive(small_data, kBufferSizeSmall); | 133 SendAndReceive(kPort, small_data, kBufferSizeSmall); |
| 125 | 134 |
| 126 // Send and receive some more data. | 135 // Send and receive some more data. |
| 127 static const int kBufferSizeMedium = 10000; | 136 static const int kBufferSizeMedium = 10000; |
| 128 char* medium_data = new char[kBufferSizeMedium]; | 137 char* medium_data = new char[kBufferSizeMedium]; |
| 129 for (int i = 0; i < kBufferSizeMedium; i++) { | 138 for (int i = 0; i < kBufferSizeMedium; i++) { |
| 130 medium_data[i] = i % 256; | 139 medium_data[i] = i % 256; |
| 131 } | 140 } |
| 132 SendAndReceive(medium_data, kBufferSizeMedium); | 141 SendAndReceive(kPort, medium_data, kBufferSizeMedium); |
| 133 delete[] medium_data; | 142 delete[] medium_data; |
| 134 | 143 |
| 135 // Send and receive even more data. | 144 // Send and receive even more data. |
| 136 static const int kBufferSizeLarge = 1000000; | 145 static const int kBufferSizeLarge = 1000000; |
| 137 char* large_data = new char[kBufferSizeLarge]; | 146 char* large_data = new char[kBufferSizeLarge]; |
| 138 for (int i = 0; i < kBufferSizeLarge; i++) { | 147 for (int i = 0; i < kBufferSizeLarge; i++) { |
| 139 large_data[i] = i % 256; | 148 large_data[i] = i % 256; |
| 140 } | 149 } |
| 141 SendAndReceive(large_data, kBufferSizeLarge); | 150 SendAndReceive(kPort, large_data, kBufferSizeLarge); |
| 142 delete[] large_data; | 151 delete[] large_data; |
| 143 } | 152 } |
| 144 | 153 |
| 145 | 154 |
| 146 TEST(HToNNToH) { | 155 TEST(HToNNToH) { |
| 147 uint16_t x = 1234; | 156 uint16_t x = 1234; |
| 148 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); | 157 CHECK_EQ(x, Socket::NToH(Socket::HToN(x))); |
| 149 | 158 |
| 150 uint32_t y = 12345678; | 159 uint32_t y = 12345678; |
| 151 CHECK(y == Socket::NToH(Socket::HToN(y))); | 160 CHECK(y == Socket::NToH(Socket::HToN(y))); |
| 152 } | 161 } |
| OLD | NEW |