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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io_socket_test/socket_test.cc

Issue 22587003: [NaCl SDK] Add UDP and TCP Sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Running tests on package Created 7 years, 4 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
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <netinet/in.h>
9 #include <pthread.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/stat.h>
13
14 #include <map>
15 #include <string>
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19
20 #include "nacl_io/kernel_intercept.h"
21 #include "nacl_io/kernel_proxy.h"
22 #include "nacl_io/ossocket.h"
23 #include "nacl_io/ostypes.h"
24
25 #ifdef PROVIDES_SOCKET_API
26
27 using namespace nacl_io;
28 using namespace sdk_util;
29
30 // No error expected
31 #define ENONE 0
32
33 uint16_t htons(uint16_t val) {
binji 2013/08/09 19:28:06 why not use the functions provided by nacl_io?
noelallen1 2013/08/09 22:53:22 Has that landed? I'll merge it in if available.
Sam Clegg 2013/08/09 23:08:00 Yup, https://chromiumcodereview.appspot.com/226250
34 return (val >> 8) | (val << 8);
35 }
36
37 uint32_t htonl(uint32_t val) {
38 return (val >> 24) | ((val >> 8) & 0xFF00) |
39 ((val << 8) & 0xFF0000) | (val << 24);
40 }
41
42 namespace {
43 class SocketTest : public ::testing::Test {
44 public:
45 ~SocketTest() {
46 EXPECT_EQ(0, ki_close(sock1));
47 EXPECT_EQ(0, ki_close(sock2));
48 }
49
50 void IP4ToSockAddr(struct sockaddr_in* addr, uint32_t ip, uint16_t port) {
binji 2013/08/09 19:28:06 seems backward to have the out parameter first
noelallen1 2013/08/09 22:53:22 Done.
51 memset(addr, 0, sizeof(*addr));
52
53 addr->sin_family = AF_INET;
54 uint16_t rport = htons(port);
55 addr->sin_port = rport;
binji 2013/08/09 19:28:06 why not just addr->sin_port = htons(port);
noelallen1 2013/08/09 22:53:22 Done.
56 uint32_t rip = htonl(ip);
57 addr->sin_addr.s_addr = rip;
58 }
59
60 void Bind(int fd, uint32_t ip, uint16_t port, int expect) {
binji 2013/08/09 19:28:06 expect_errno?
noelallen1 2013/08/09 22:53:22 Done.
61 sockaddr_in addr;
62 socklen_t addrlen = sizeof(addr);
63
64 IP4ToSockAddr(&addr, ip, port);
65 int err = bind(fd, (sockaddr*) &addr, addrlen);
binji 2013/08/09 19:28:06 ki_bind?
noelallen1 2013/08/09 22:53:22 Whoops, removing ki_ this should be the end-to-en
66
67 if (expect != 0) {
binji 2013/08/09 19:28:06 ENONE
noelallen1 2013/08/09 22:53:22 Done.
68 EXPECT_EQ(-1, err);
69 EXPECT_EQ(errno, expect);
70 } else {
71 EXPECT_EQ(0, err);
72 }
73 }
74
75 void SetupPorts() {
76 Bind(sock1, 0x7F000001, 0, ENONE);
binji 2013/08/09 19:28:06 use const instead of magic number
noelallen1 2013/08/09 22:53:22 Done.
77 Bind(sock2, 0x7F000001, 0, ENONE);
78 }
79
80 public:
81 int sock1;
binji 2013/08/09 19:28:06 initialize member variables in constructor
noelallen1 2013/08/09 22:53:22 Done.
82 int sock2;
83 uint16_t port1;
84 uint16_t port2;
85 };
86
87 class SocketTestUDP : public SocketTest {
88 public:
89 SocketTestUDP() {
90 sock1 = ki_socket(AF_INET, SOCK_DGRAM, 0);
91 sock2 = ki_socket(AF_INET, SOCK_DGRAM, 0);
92
93 EXPECT_LT(-1, sock1);
94 EXPECT_LT(-1, sock2);
95 }
96 };
97
98 class SocketTestTCP : public SocketTest {
99 public:
100 SocketTestTCP() {
101 sock1 = ki_socket(AF_INET, SOCK_STREAM, 0);
102 sock2 = ki_socket(AF_INET, SOCK_STREAM, 0);
103
104 EXPECT_LT(-1, sock1);
105 EXPECT_LT(-1, sock2);
106 }
107 };
108
109 } // namespace
110
111
112 TEST(SocketTest, Socket) {
113 EXPECT_LT(ki_socket(AF_UNIX, SOCK_STREAM, 0), 0);
binji 2013/08/09 19:28:06 this looks a bit strange, maybe EXPECT_EQ(-1, ki_
noelallen1 2013/08/09 22:53:22 Done.
114 EXPECT_EQ(errno, EAFNOSUPPORT);
115 EXPECT_LT(ki_socket(AF_INET, SOCK_RAW, 0), 0);
116 EXPECT_EQ(errno, EPROTONOSUPPORT);
117
118 int sock1 = ki_socket(AF_INET, SOCK_DGRAM, 0);
binji 2013/08/09 19:28:06 shadows this->sock1, which has dummy value, and is
noelallen1 2013/08/09 22:53:22 This is not a TEST_F.
binji 2013/08/09 23:08:27 You're right, whoops!
119 EXPECT_NE(-1, sock1);
120
121 int sock2 = ki_socket(AF_INET6, SOCK_DGRAM, 0);
122 EXPECT_NE(-1, sock2);
123
124 int sock3 = ki_socket(AF_INET, SOCK_STREAM, 0);
125 EXPECT_NE(-1, sock3);
126
127 int sock4 = ki_socket(AF_INET6, SOCK_STREAM, 0);
128 EXPECT_NE(-1, sock4);
129
130 ki_close(sock1);
131 ki_close(sock2);
132 ki_close(sock3);
133 ki_close(sock4);
134 }
135
136 TEST_F(SocketTestUDP, Bind) {
137 // Bind away.
138 Bind(sock1, 0x7F000001, 4006, ENONE);
139
140 // Invalid to rebind a socket.
141 Bind(sock1, 0x7F000001, 4006, EINVAL);
142
143 // Addr in use.
144 Bind(sock2, 0x7F000001, 4006, EADDRINUSE);
145
146 // Bind with a wildcard.
147 Bind(sock2, 0x7F000001, 0, ENONE);
148
149 // Invalid to rebind after wildcard
150 Bind(sock2, 0x7F000001, 4007, EINVAL);
151
152 }
153
154 TEST_F(SocketTestUDP, SendRcv) {
155 char outbuf[256];
156 char inbuf[512];
binji 2013/08/09 19:28:06 why different sizes?
noelallen1 2013/08/09 22:53:22 To read > expected
157
158 memset(outbuf, 1, sizeof(outbuf));
159 memset(inbuf, 0, sizeof(inbuf));
160
161 Bind(sock1, 0x7F000001, 4006, ENONE);
162 Bind(sock2, 0x7F000001, 4007, ENONE);
163
164 sockaddr_in addr;
165 socklen_t addrlen = sizeof(addr);
166 IP4ToSockAddr(&addr, 0x7F000001, 4007);
167
168 int len1 =
169 sendto(sock1, outbuf, sizeof(outbuf), 0,(sockaddr *) &addr, addrlen);
binji 2013/08/09 19:28:06 nit: space after comma, remove extra space after c
binji 2013/08/09 19:28:06 ki_sendto?
noelallen1 2013/08/09 22:53:22 Done.
noelallen1 2013/08/09 22:53:22 no ki_*, end to end.
binji 2013/08/09 23:08:27 OK, I see.
170 EXPECT_EQ(sizeof(outbuf), len1);
171
172 // Ensure the buffers are different
173 EXPECT_NE(0, memcmp(outbuf, inbuf, sizeof(outbuf)));
174
175 // Try to receive the previously sent packet
176 int len2 =
177 ki_recvfrom(sock2, inbuf, sizeof(inbuf), 0, (sockaddr *) &addr, &addrlen);
178 EXPECT_EQ(sizeof(outbuf), len2);
179 EXPECT_EQ(sizeof(sockaddr_in), addrlen);
binji 2013/08/09 19:28:06 check addr?
noelallen1 2013/08/09 22:53:22 Done.
180
181 // Now they should be the same
182 EXPECT_EQ(0, memcmp(outbuf, inbuf, sizeof(outbuf)));
183 }
184
185 TEST_F(SocketTestTCP, Connect) {
186 int sock = socket(AF_INET, SOCK_STREAM, 0);
187 EXPECT_NE(-1, sock);
188
189 sockaddr_in addr;
190 socklen_t addrlen = sizeof(addr);
191
192 IP4ToSockAddr(&addr, 0x7F000001, 4006);
193 int err = connect(sock, (sockaddr*) &addr, addrlen);
194 EXPECT_EQ(0, err);
195
196 if (err) {
197 EXPECT_EQ(0, errno);
binji 2013/08/09 19:28:06 check doesn't make sense -- if err is non-zero, we
noelallen1 2013/08/09 22:53:22 Done.
198 }
199 }
200
201
202 #endif // PROVIDES_SOCKETPAIR_API
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698