Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: test/cctest/test-sockets.cc

Issue 27085: Add socket support to platform code.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/SConscript ('k') | tools/visual_studio/d8.vcproj » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ native
OLDNEW
(Empty)
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 static const char* kPort = "5858";
12 static const char* kLocalhost = "localhost";
13
14 class SocketListenerThread : public Thread {
15 public:
16 SocketListenerThread(int data_size) : data_size_(data_size),
17 server_(NULL), client_(NULL),
18 listening_(OS::CreateSemaphore(0)) {
19 data_ = new char[data_size_];
20 }
21 ~SocketListenerThread() {
22 // Close both sockets.
23 delete client_;
24 delete server_;
25 }
26
27 void Run();
28 void WaitForListening() { listening_->Wait(); }
29 char* data() { return data_; }
30
31 private:
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(5858);
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 void SendAndReceive(char *data, int len) {
67 bool ok;
68
69 // Create a socket listener.
70 SocketListenerThread* listener = new SocketListenerThread(len);
71 listener->Start();
72 listener->WaitForListening();
73
74 // Connect and write some data.
75 Socket* client = OS::CreateSocket();
76 CHECK(client != NULL);
77 ok = client->Connect(kLocalhost, kPort);
78 CHECK(ok);
79
80 // Send all the data.
81 ok = client->SendAll(data, len);
82 CHECK(ok);
83
84 // Wait until data is received.
85 listener->Join();
86
87 // Check that data received is the same as data send.
88 for (int i = 0; i < len; i++) {
89 CHECK(data[i] == listener->data()[i]);
90 }
91
92 // Close the client before the listener to avoid TIME_WAIT issues.
93 delete client;
94 delete listener;
95 }
96
97
98 TEST(Socket) {
99 bool ok;
100
101 // Initialize socket support.
102 ok = Socket::Setup();
103 CHECK(ok);
104
105 // Send and receive some data.
106 static const int kBufferSizeSmall = 20;
107 char small_data[kBufferSizeSmall + 1] = "1234567890abcdefghij";
108 SendAndReceive(small_data, kBufferSizeSmall);
109
110 // Send and receive some more data.
111 static const int kBufferSizeMedium = 10000;
112 char* medium_data = new char[kBufferSizeMedium];
113 for (int i = 0; i < kBufferSizeMedium; i++) {
114 medium_data[i] = i % 256;
115 }
116 SendAndReceive(medium_data, kBufferSizeMedium);
117
118 // Send and receive even more data.
119 static const int kBufferSizeLarge = 1000000;
120 char* large_data = new char[kBufferSizeLarge];
121 for (int i = 0; i < kBufferSizeLarge; i++) {
122 large_data[i] = i % 256;
123 }
124 SendAndReceive(large_data, kBufferSizeLarge);
125 }
126
127
128 TEST(HToNNToH) {
129 uint16_t x = 1234;
130 CHECK_EQ(x, Socket::NToH(Socket::HToN(x)));
131
132 uint32_t y = 12345678;
133 CHECK(y == Socket::NToH(Socket::HToN(y)));
134 }
OLDNEW
« no previous file with comments | « test/cctest/SConscript ('k') | tools/visual_studio/d8.vcproj » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698