| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 #include "platform.h" | 29 #include "platform.h" |
| 30 #include "platform/socket.h" |
| 30 #include "cctest.h" | 31 #include "cctest.h" |
| 31 | 32 |
| 32 | 33 |
| 33 using namespace ::v8::internal; | 34 using namespace ::v8::internal; |
| 34 | 35 |
| 35 | 36 |
| 36 class SocketListenerThread : public Thread { | 37 class SocketListenerThread : public Thread { |
| 37 public: | 38 public: |
| 38 SocketListenerThread(int port, int data_size) | 39 SocketListenerThread(int port, int data_size) |
| 39 : Thread("SocketListenerThread"), | 40 : Thread("SocketListenerThread"), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 62 Socket* server_; // Server socket used for bind/accept. | 63 Socket* server_; // Server socket used for bind/accept. |
| 63 Socket* client_; // Single client connection used by the test. | 64 Socket* client_; // Single client connection used by the test. |
| 64 Semaphore listening_; // Signalled when the server socket is in listen mode. | 65 Semaphore listening_; // Signalled when the server socket is in listen mode. |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 | 68 |
| 68 void SocketListenerThread::Run() { | 69 void SocketListenerThread::Run() { |
| 69 bool ok; | 70 bool ok; |
| 70 | 71 |
| 71 // Create the server socket and bind it to the requested port. | 72 // Create the server socket and bind it to the requested port. |
| 72 server_ = OS::CreateSocket(); | 73 server_ = new Socket; |
| 73 server_->SetReuseAddress(true); | 74 server_->SetReuseAddress(true); |
| 74 CHECK(server_ != NULL); | 75 CHECK(server_ != NULL); |
| 75 ok = server_->Bind(port_); | 76 ok = server_->Bind(port_); |
| 76 CHECK(ok); | 77 CHECK(ok); |
| 77 | 78 |
| 78 // Listen for new connections. | 79 // Listen for new connections. |
| 79 ok = server_->Listen(1); | 80 ok = server_->Listen(1); |
| 80 CHECK(ok); | 81 CHECK(ok); |
| 81 listening_.Signal(); | 82 listening_.Signal(); |
| 82 | 83 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 const int kPortBuferLen = 6; | 115 const int kPortBuferLen = 6; |
| 115 char port_str[kPortBuferLen]; | 116 char port_str[kPortBuferLen]; |
| 116 OS::SNPrintF(Vector<char>(port_str, kPortBuferLen), "%d", port); | 117 OS::SNPrintF(Vector<char>(port_str, kPortBuferLen), "%d", port); |
| 117 | 118 |
| 118 // Create a socket listener. | 119 // Create a socket listener. |
| 119 SocketListenerThread* listener = new SocketListenerThread(port, len); | 120 SocketListenerThread* listener = new SocketListenerThread(port, len); |
| 120 listener->Start(); | 121 listener->Start(); |
| 121 listener->WaitForListening(); | 122 listener->WaitForListening(); |
| 122 | 123 |
| 123 // Connect and write some data. | 124 // Connect and write some data. |
| 124 Socket* client = OS::CreateSocket(); | 125 Socket* client = new Socket; |
| 125 CHECK(client != NULL); | 126 CHECK(client != NULL); |
| 126 ok = client->Connect(kLocalhost, port_str); | 127 ok = client->Connect(kLocalhost, port_str); |
| 127 CHECK(ok); | 128 CHECK(ok); |
| 128 | 129 |
| 129 // Send all the data. | 130 // Send all the data. |
| 130 ok = SendAll(client, data, len); | 131 ok = SendAll(client, data, len); |
| 131 CHECK(ok); | 132 CHECK(ok); |
| 132 | 133 |
| 133 // Wait until data is received. | 134 // Wait until data is received. |
| 134 listener->Join(); | 135 listener->Join(); |
| 135 | 136 |
| 136 // Check that data received is the same as data send. | 137 // Check that data received is the same as data send. |
| 137 for (int i = 0; i < len; i++) { | 138 for (int i = 0; i < len; i++) { |
| 138 CHECK(data[i] == listener->data()[i]); | 139 CHECK(data[i] == listener->data()[i]); |
| 139 } | 140 } |
| 140 | 141 |
| 141 // Close the client before the listener to avoid TIME_WAIT issues. | 142 // Close the client before the listener to avoid TIME_WAIT issues. |
| 142 client->Shutdown(); | 143 client->Shutdown(); |
| 143 delete client; | 144 delete client; |
| 144 delete listener; | 145 delete listener; |
| 145 } | 146 } |
| 146 | 147 |
| 147 | 148 |
| 148 TEST(Socket) { | 149 TEST(Socket) { |
| 149 // Make sure this port is not used by other tests to allow tests to run in | 150 // Make sure this port is not used by other tests to allow tests to run in |
| 150 // parallel. | 151 // parallel. |
| 151 static const int kPort = 5859 + FlagDependentPortOffset(); | 152 static const int kPort = 5859 + FlagDependentPortOffset(); |
| 152 | 153 |
| 153 bool ok; | |
| 154 | |
| 155 // Initialize socket support. | |
| 156 ok = Socket::SetUp(); | |
| 157 CHECK(ok); | |
| 158 | |
| 159 // Send and receive some data. | 154 // Send and receive some data. |
| 160 static const int kBufferSizeSmall = 20; | 155 static const int kBufferSizeSmall = 20; |
| 161 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij"; | 156 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij"; |
| 162 SendAndReceive(kPort, small_data, kBufferSizeSmall); | 157 SendAndReceive(kPort, small_data, kBufferSizeSmall); |
| 163 | 158 |
| 164 // Send and receive some more data. | 159 // Send and receive some more data. |
| 165 static const int kBufferSizeMedium = 10000; | 160 static const int kBufferSizeMedium = 10000; |
| 166 char* medium_data = new char[kBufferSizeMedium]; | 161 char* medium_data = new char[kBufferSizeMedium]; |
| 167 for (int i = 0; i < kBufferSizeMedium; i++) { | 162 for (int i = 0; i < kBufferSizeMedium; i++) { |
| 168 medium_data[i] = i % 256; | 163 medium_data[i] = i % 256; |
| 169 } | 164 } |
| 170 SendAndReceive(kPort, medium_data, kBufferSizeMedium); | 165 SendAndReceive(kPort, medium_data, kBufferSizeMedium); |
| 171 delete[] medium_data; | 166 delete[] medium_data; |
| 172 | 167 |
| 173 // Send and receive even more data. | 168 // Send and receive even more data. |
| 174 static const int kBufferSizeLarge = 1000000; | 169 static const int kBufferSizeLarge = 1000000; |
| 175 char* large_data = new char[kBufferSizeLarge]; | 170 char* large_data = new char[kBufferSizeLarge]; |
| 176 for (int i = 0; i < kBufferSizeLarge; i++) { | 171 for (int i = 0; i < kBufferSizeLarge; i++) { |
| 177 large_data[i] = i % 256; | 172 large_data[i] = i % 256; |
| 178 } | 173 } |
| 179 SendAndReceive(kPort, large_data, kBufferSizeLarge); | 174 SendAndReceive(kPort, large_data, kBufferSizeLarge); |
| 180 delete[] large_data; | 175 delete[] large_data; |
| 181 } | 176 } |
| 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 } | |
| OLD | NEW |