PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/minix/tests/common-socket.c

http://www.minix3.org/
C | 2122 lines | 1541 code | 458 blank | 123 comment | 544 complexity | 50a270436d585eaf1c891599c09db552 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. #include <assert.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include "common.h"
  15. #include "common-socket.h"
  16. #define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
  17. /* timestamps for debug and error logs */
  18. static char *get_timestamp(void)
  19. {
  20. struct tm *tm;
  21. time_t t;
  22. size_t len;
  23. char *s;
  24. len = sizeof(char) * 32;
  25. t = time(NULL);
  26. if (t == -1) {
  27. return NULL;
  28. }
  29. tm = gmtime(&t);
  30. if (tm == NULL) {
  31. return NULL;
  32. }
  33. s = (char *) malloc(len);
  34. if (!s) {
  35. perror("malloc");
  36. return NULL;
  37. }
  38. memset(s, '\0', len);
  39. strftime(s, len - 1, ISO8601_FORMAT, tm);
  40. return s;
  41. }
  42. void test_fail_fl(char *msg, char *file, int line)
  43. {
  44. char *timestamp;
  45. timestamp = get_timestamp();
  46. if (errct == 0) fprintf(stderr, "\n");
  47. fprintf(stderr, "[ERROR][%s] (%s Line %d) %s [pid=%d:errno=%d:%s]\n",
  48. timestamp, file, line, msg, getpid(),
  49. errno, strerror(errno));
  50. fflush(stderr);
  51. if (timestamp != NULL) {
  52. free(timestamp);
  53. timestamp = NULL;
  54. }
  55. e(7);
  56. }
  57. #if DEBUG == 1
  58. void debug_fl(char *msg, char *file, int line)
  59. {
  60. char *timestamp;
  61. timestamp = get_timestamp();
  62. fprintf(stdout,"[DEBUG][%s] (%s:%d) %s [pid=%d]\n",
  63. timestamp, __FILE__, __LINE__, msg, getpid());
  64. fflush(stdout);
  65. if (timestamp != NULL) {
  66. free(timestamp);
  67. timestamp = NULL;
  68. }
  69. }
  70. #endif
  71. void test_socket(const struct socket_test_info *info)
  72. {
  73. struct stat statbuf, statbuf2;
  74. int sd, sd2;
  75. int rc;
  76. int i;
  77. debug("entering test_socket()");
  78. debug("Test socket() with an unsupported address family");
  79. errno = 0;
  80. sd = socket(-1, info->type, 0);
  81. if (!(sd == -1 && errno == EAFNOSUPPORT)) {
  82. test_fail("socket");
  83. if (sd != -1) {
  84. CLOSE(sd);
  85. }
  86. }
  87. debug("Test socket() with all available FDs open by this process");
  88. for (i = 3; i < getdtablesize(); i++) {
  89. rc = open("/dev/null", O_RDONLY);
  90. if (rc == -1) {
  91. test_fail("we couldn't open /dev/null for read");
  92. }
  93. }
  94. errno = 0;
  95. sd = socket(info->domain, info->type, 0);
  96. if (!(sd == -1 && errno == EMFILE)) {
  97. test_fail("socket() call with all fds open should fail");
  98. if (sd != -1) {
  99. CLOSE(sd);
  100. }
  101. }
  102. for (i = 3; i < getdtablesize(); i++) {
  103. CLOSE(i);
  104. }
  105. debug("Test socket() with an mismatched protocol");
  106. errno = 0;
  107. sd = socket(info->domain, info->type, 4);
  108. if (!(sd == -1 && errno == EPROTONOSUPPORT)) {
  109. test_fail("socket() should fail with errno = EPROTONOSUPPORT");
  110. if (sd != -1) {
  111. CLOSE(sd);
  112. }
  113. }
  114. debug("Test socket() success");
  115. /*
  116. * open 2 sockets at once and *then* close them.
  117. * This will test that /dev/uds is cloning properly.
  118. */
  119. SOCKET(sd, info->domain, info->type, 0);
  120. SOCKET(sd2, info->domain, info->type, 0);
  121. rc = fstat(sd, &statbuf);
  122. if (rc == -1) {
  123. test_fail("fstat failed on sd");
  124. }
  125. rc = fstat(sd2, &statbuf2);
  126. if (rc == -1) {
  127. test_fail("fstat failed on sd2");
  128. }
  129. if (statbuf.st_dev == statbuf2.st_dev) {
  130. test_fail("/dev/uds isn't being cloned");
  131. }
  132. CLOSE(sd2);
  133. CLOSE(sd);
  134. debug("leaving test_socket()");
  135. }
  136. void test_getsockname(const struct socket_test_info *info)
  137. {
  138. int sd;
  139. int rc;
  140. struct sockaddr_storage sock_addr;
  141. socklen_t sock_addr_len;
  142. SOCKET(sd, info->domain, info->type, 0);
  143. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  144. if (rc == -1) {
  145. test_fail("bind() should have worked");
  146. }
  147. debug("Test getsockname() success");
  148. memset(&sock_addr, '\0', sizeof(sock_addr));
  149. sock_addr_len = sizeof(sock_addr);
  150. rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
  151. if (rc == -1) {
  152. test_fail("getsockname() should have worked");
  153. }
  154. info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
  155. sock_addr_len, "getsockname", 1);
  156. CLOSE(sd);
  157. }
  158. void test_bind(const struct socket_test_info *info)
  159. {
  160. struct sockaddr_storage sock_addr;
  161. socklen_t sock_addr_len;
  162. int sd;
  163. int sd2;
  164. int rc;
  165. debug("entering test_bind()");
  166. info->callback_cleanup();
  167. debug("Test bind() success");
  168. SOCKET(sd, info->domain, info->type, 0);
  169. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  170. if (rc == -1) {
  171. test_fail("bind() should have worked");
  172. }
  173. debug("Test getsockname() success");
  174. memset(&sock_addr, '\0', sizeof(sock_addr));
  175. sock_addr_len = sizeof(sock_addr);
  176. rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
  177. if (rc == -1) {
  178. test_fail("getsockname() should have worked");
  179. }
  180. info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
  181. sock_addr_len, "getsockname", 1);
  182. debug("Test bind() with a address that has already been bind()'d");
  183. SOCKET(sd2, info->domain, info->type, 0);
  184. errno = 0;
  185. rc = bind(sd2, info->serveraddr, info->serveraddrlen);
  186. if (!((rc == -1) && (errno == EADDRINUSE)) &&
  187. !info->bug_bind_in_use) {
  188. test_fail("bind() should have failed with EADDRINUSE");
  189. }
  190. CLOSE(sd2);
  191. CLOSE(sd);
  192. info->callback_cleanup();
  193. if (!info->bug_bind_null) {
  194. debug("Test bind() with a NULL address");
  195. SOCKET(sd, info->domain, info->type, 0);
  196. errno = 0;
  197. rc = bind(sd, (struct sockaddr *) NULL,
  198. sizeof(struct sockaddr_storage));
  199. if (!((rc == -1) && (errno == EFAULT))) {
  200. test_fail("bind() should have failed with EFAULT");
  201. }
  202. CLOSE(sd);
  203. }
  204. debug("leaving test_bind()");
  205. }
  206. void test_listen(const struct socket_test_info *info)
  207. {
  208. int rc;
  209. debug("entering test_listen()");
  210. debug("Test listen() with a bad file descriptor");
  211. errno = 0;
  212. rc = listen(-1, 0);
  213. if (!(rc == -1 && errno == EBADF)) {
  214. test_fail("listen(-1, 0) should have failed");
  215. }
  216. debug("Test listen() with a non-socket file descriptor");
  217. errno = 0;
  218. rc = listen(0, 0);
  219. /* Test on errno disabled here: there's currently no telling what this
  220. * will return. POSIX says it should be ENOTSOCK, MINIX3 libc returns
  221. * ENOSYS, and we used to test for ENOTTY here..
  222. */
  223. if (!(rc == -1)) {
  224. test_fail("listen(0, 0) should have failed");
  225. }
  226. debug("leaving test_listen()");
  227. }
  228. void test_shutdown(const struct socket_test_info *info)
  229. {
  230. int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
  231. int sd;
  232. int rc;
  233. int i;
  234. debug("entering test_shutdown()");
  235. /* test for each direction (read, write, read-write) */
  236. for (i = 0; i < 3; i++) {
  237. if (info->bug_shutdown_read && how[i] == SHUT_RD) continue;
  238. debug("test shutdown() with an invalid descriptor");
  239. errno = 0;
  240. rc = shutdown(-1, how[i]);
  241. if (!(rc == -1 && errno == EBADF) && !info->bug_shutdown) {
  242. test_fail("shutdown(-1, how[i]) should have failed");
  243. }
  244. debug("test shutdown() with a non-socket descriptor");
  245. errno = 0;
  246. rc = shutdown(0, how[i]);
  247. if (!(rc == -1 && errno == ENOTSOCK) && !info->bug_shutdown) {
  248. test_fail("shutdown() should have failed with "
  249. "ENOTSOCK");
  250. }
  251. debug("test shutdown() with a socket that is not connected");
  252. SOCKET(sd, info->domain, info->type, 0);
  253. errno = 0;
  254. rc = shutdown(sd, how[i]);
  255. if (!(rc == -1 && errno == ENOTCONN) &&
  256. !info->bug_shutdown_not_conn &&
  257. !info->bug_shutdown) {
  258. test_fail("shutdown() should have failed");
  259. }
  260. CLOSE(sd);
  261. }
  262. SOCKET(sd, info->domain, info->type, 0);
  263. errno = 0;
  264. rc = shutdown(sd, -1);
  265. if (!(rc == -1 && errno == ENOTCONN) &&
  266. !info->bug_shutdown_not_conn &&
  267. !info->bug_shutdown) {
  268. test_fail("shutdown(sd, -1) should have failed with ENOTCONN");
  269. }
  270. CLOSE(sd);
  271. debug("leaving test_shutdown()");
  272. }
  273. void test_close(const struct socket_test_info *info)
  274. {
  275. int sd, sd2;
  276. int rc, i;
  277. debug("entering test_close()");
  278. info->callback_cleanup();
  279. debug("Test close() success");
  280. SOCKET(sd, info->domain, info->type, 0);
  281. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  282. if (rc != 0) {
  283. test_fail("bind() should have worked");
  284. }
  285. CLOSE(sd);
  286. debug("Close an already closed file descriptor");
  287. errno = 0;
  288. rc = close(sd);
  289. if (!(rc == -1 && errno == EBADF)) {
  290. test_fail("close(sd) should have failed with EBADF");
  291. }
  292. info->callback_cleanup();
  293. debug("dup()'ing a file descriptor and closing both should work");
  294. SOCKET(sd, info->domain, info->type, 0);
  295. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  296. if (rc != 0) {
  297. test_fail("bind() should have worked");
  298. }
  299. errno = 0;
  300. sd2 = dup(sd);
  301. if (sd2 == -1) {
  302. test_fail("dup(sd) should have worked");
  303. } else {
  304. CLOSE(sd2);
  305. CLOSE(sd);
  306. }
  307. info->callback_cleanup();
  308. /* Create and close a socket a bunch of times.
  309. * If the implementation doesn't properly free the
  310. * socket during close(), eventually socket() will
  311. * fail when the internal descriptor table is full.
  312. */
  313. for (i = 0; i < 1024; i++) {
  314. SOCKET(sd, info->domain, info->type, 0);
  315. CLOSE(sd);
  316. }
  317. debug("leaving test_close()");
  318. }
  319. void test_sockopts(const struct socket_test_info *info)
  320. {
  321. int i;
  322. int rc;
  323. int sd;
  324. int option_value;
  325. socklen_t option_len;
  326. debug("entering test_sockopts()");
  327. for (i = 0; i < info->typecount; i++) {
  328. SOCKET(sd, info->domain, info->types[i], 0);
  329. debug("Test setsockopt() works");
  330. option_value = 0;
  331. option_len = sizeof(option_value);
  332. errno = 0;
  333. rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
  334. &option_len);
  335. if (rc != 0) {
  336. test_fail("setsockopt() should have worked");
  337. }
  338. if (option_value != info->types[i]) {
  339. test_fail("SO_TYPE didn't seem to work.");
  340. }
  341. CLOSE(sd);
  342. }
  343. SOCKET(sd, info->domain, info->type, 0);
  344. debug("Test setsockopt() works");
  345. option_value = 0;
  346. option_len = sizeof(option_value);
  347. errno = 0;
  348. rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
  349. if (rc != 0 && !info->bug_sockopt_sndbuf) {
  350. test_fail("getsockopt() should have worked");
  351. }
  352. if (info->expected_sndbuf >= 0 &&
  353. option_value != info->expected_sndbuf &&
  354. !info->bug_sockopt_sndbuf) {
  355. test_fail("SO_SNDBUF didn't seem to work.");
  356. }
  357. CLOSE(sd);
  358. SOCKET(sd, info->domain, info->type, 0);
  359. debug("Test setsockopt() works");
  360. option_value = 0;
  361. option_len = sizeof(option_value);
  362. errno = 0;
  363. rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
  364. if (rc != 0 && !info->bug_sockopt_rcvbuf) {
  365. test_fail("getsockopt() should have worked");
  366. }
  367. if (info->expected_rcvbuf >= 0 &&
  368. option_value != info->expected_rcvbuf &&
  369. !info->bug_sockopt_rcvbuf) {
  370. test_fail("SO_RCVBUF didn't seem to work.");
  371. }
  372. CLOSE(sd);
  373. debug("leaving test_sockopts()");
  374. }
  375. void test_read(const struct socket_test_info *info)
  376. {
  377. int rc;
  378. int fd;
  379. char buf[BUFSIZE];
  380. debug("entering test_read()");
  381. errno = 0;
  382. rc = read(-1, buf, sizeof(buf));
  383. if (!(rc == -1 && errno == EBADF)) {
  384. test_fail("read() should have failed with EBADF");
  385. }
  386. fd = open("/tmp", O_RDONLY);
  387. if (fd == -1) {
  388. test_fail("open(\"/tmp\", O_RDONLY) should have worked");
  389. }
  390. CLOSE(fd);
  391. debug("leaving test_read()");
  392. }
  393. void test_write(const struct socket_test_info *info)
  394. {
  395. int rc;
  396. char buf[BUFSIZE];
  397. debug("entering test_write()");
  398. errno = 0;
  399. rc = write(-1, buf, sizeof(buf));
  400. if (!(rc == -1 && errno == EBADF)) {
  401. test_fail("write() should have failed with EBADF");
  402. }
  403. debug("leaving test_write()");
  404. }
  405. void test_dup(const struct socket_test_info *info)
  406. {
  407. struct stat info1;
  408. struct stat info2;
  409. int sd, sd2;
  410. int rc;
  411. int i;
  412. debug("entering test_dup()");
  413. info->callback_cleanup();
  414. debug("Test dup()");
  415. SOCKET(sd, info->domain, info->type, 0);
  416. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  417. if (rc != 0) {
  418. test_fail("bind() should have worked");
  419. }
  420. errno = 0;
  421. sd2 = dup(sd);
  422. if (sd2 == -1) {
  423. test_fail("dup(sd) should have worked");
  424. }
  425. rc = fstat(sd, &info1);
  426. if (rc == -1) {
  427. test_fail("fstat(fd, &info1) failed");
  428. }
  429. rc = fstat(sd2, &info2);
  430. if (rc == -1) {
  431. test_fail("fstat(sd, &info2) failed");
  432. }
  433. if (info1.st_ino != info2.st_ino) {
  434. test_fail("dup() failed info1.st_ino != info2.st_ino");
  435. }
  436. CLOSE(sd);
  437. CLOSE(sd2);
  438. debug("Test dup() with a closed socket");
  439. errno = 0;
  440. rc = dup(sd);
  441. if (!(rc == -1 && errno == EBADF)) {
  442. test_fail("dup(sd) on a closed socket shouldn't have worked");
  443. }
  444. debug("Test dup() with socket descriptor of -1");
  445. errno = 0;
  446. rc = dup(-1);
  447. if (!(rc == -1 && errno == EBADF)) {
  448. test_fail("dup(-1) shouldn't have worked");
  449. }
  450. debug("Test dup() when all of the file descriptors are taken");
  451. SOCKET(sd, info->domain, info->type, 0);
  452. for (i = 4; i < getdtablesize(); i++) {
  453. rc = open("/dev/null", O_RDONLY);
  454. if (rc == -1) {
  455. test_fail("we couldn't open /dev/null for read");
  456. }
  457. }
  458. errno = 0;
  459. sd2 = dup(sd);
  460. if (!(sd2 == -1 && errno == EMFILE)) {
  461. test_fail("dup(sd) should have failed with errno = EMFILE");
  462. }
  463. for (i = 3; i < getdtablesize(); i++) {
  464. CLOSE(i);
  465. }
  466. info->callback_cleanup();
  467. debug("leaving test_dup()");
  468. }
  469. void test_dup2(const struct socket_test_info *info)
  470. {
  471. struct stat info1;
  472. struct stat info2;
  473. int sd;
  474. int fd;
  475. int rc;
  476. debug("entering test_dup2()");
  477. info->callback_cleanup();
  478. SOCKET(sd, info->domain, info->type, 0);
  479. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  480. if (rc != 0) {
  481. test_fail("bind() should have worked");
  482. }
  483. fd = open("/dev/null", O_RDONLY);
  484. if (fd == -1) {
  485. test_fail("open(\"/dev/null\", O_RDONLY) failed");
  486. }
  487. fd = dup2(sd, fd);
  488. if (fd == -1) {
  489. test_fail("dup2(sd, fd) failed.");
  490. }
  491. memset(&info1, '\0', sizeof(struct stat));
  492. memset(&info2, '\0', sizeof(struct stat));
  493. rc = fstat(fd, &info1);
  494. if (rc == -1) {
  495. test_fail("fstat(fd, &info1) failed");
  496. }
  497. rc = fstat(sd, &info2);
  498. if (rc == -1) {
  499. test_fail("fstat(sd, &info2) failed");
  500. }
  501. if (!(info1.st_ino == info2.st_ino &&
  502. major(info1.st_dev) == major(info2.st_dev) &&
  503. minor(info1.st_dev) == minor(info2.st_dev))) {
  504. test_fail("dup2() failed");
  505. }
  506. CLOSE(fd);
  507. CLOSE(sd);
  508. info->callback_cleanup();
  509. debug("leaving test_dup2()");
  510. }
  511. /*
  512. * A toupper() server. This toy server converts a string to upper case.
  513. */
  514. static void test_xfer_server(const struct socket_test_info *info, pid_t pid)
  515. {
  516. int i;
  517. struct timeval tv;
  518. fd_set readfds;
  519. int status;
  520. int rc;
  521. int sd;
  522. unsigned char buf[BUFSIZE];
  523. socklen_t client_addr_size;
  524. int client_sd;
  525. struct sockaddr_storage client_addr;
  526. status = 0;
  527. rc = 0;
  528. sd = 0;
  529. client_sd = 0;
  530. client_addr_size = sizeof(struct sockaddr_storage);
  531. memset(&buf, '\0', sizeof(buf));
  532. memset(&client_addr, '\0', sizeof(client_addr));
  533. SOCKET(sd, info->domain, info->type, 0);
  534. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  535. if (rc == -1) {
  536. test_fail("bind() should have worked");
  537. }
  538. rc = listen(sd, 8);
  539. if (rc == -1) {
  540. test_fail("listen(sd, 8) should have worked");
  541. }
  542. /* we're ready for connections, time to tell the client to start
  543. * the test
  544. */
  545. kill(pid, SIGUSR1);
  546. tv.tv_sec = 10;
  547. tv.tv_usec = 0;
  548. FD_ZERO(&readfds);
  549. FD_SET(sd, &readfds);
  550. /* use select() in case the client is really broken and never
  551. * attempts to connect (we don't want to block on accept()
  552. * forever).
  553. */
  554. rc = select(sd + 1, &readfds, NULL, NULL, &tv);
  555. if (rc == -1) {
  556. test_fail("[server] select() should not have failed");
  557. }
  558. if (rc != 1) {
  559. test_fail("[server] select() should have returned 1");
  560. printf("[server] select returned %d\n", rc);
  561. }
  562. if (!(FD_ISSET(sd, &readfds))) {
  563. test_fail("[server] client didn't connect within 10 seconds");
  564. kill(pid, SIGKILL);
  565. return;
  566. }
  567. client_sd = accept(sd, (struct sockaddr *) &client_addr,
  568. &client_addr_size);
  569. if (client_sd == -1) {
  570. test_fail("accept() should have worked");
  571. kill(pid, SIGKILL);
  572. return;
  573. } else {
  574. debug("[server] client accept()'d");
  575. }
  576. debug("[server] Reading message");
  577. rc = read(client_sd, buf, sizeof(buf));
  578. if (rc == -1) {
  579. test_fail("read() failed unexpectedly");
  580. kill(pid, SIGKILL);
  581. return;
  582. }
  583. debug("[server] we got the following message:");
  584. debug(buf);
  585. for (i = 0; i < rc && i < 127; i++) {
  586. buf[i] = toupper(buf[i]);
  587. }
  588. debug("[server] Writing message...");
  589. rc = write(client_sd, buf, sizeof(buf));
  590. if (rc == -1) {
  591. test_fail("write(client_sd, buf, sizeof(buf)) failed");
  592. kill(pid, SIGKILL);
  593. return;
  594. }
  595. if (rc < strlen((char *)buf)) {
  596. test_fail("[server] write didn't write all the bytes");
  597. }
  598. memset(&buf, '\0', sizeof(buf));
  599. debug("[server] Recv message");
  600. rc = recv(client_sd, buf, sizeof(buf), 0);
  601. if (rc == -1) {
  602. test_fail("recv() failed unexpectedly");
  603. kill(pid, SIGKILL);
  604. return;
  605. }
  606. debug("[server] we got the following message:");
  607. debug(buf);
  608. for (i = 0; i < rc && i < 127; i++) {
  609. buf[i] = toupper(buf[i]);
  610. }
  611. debug("[server] Sending message...");
  612. rc = send(client_sd, buf, sizeof(buf), 0);
  613. if (rc == -1) {
  614. test_fail("send(client_sd, buf, sizeof(buf), 0) failed");
  615. kill(pid, SIGKILL);
  616. return;
  617. }
  618. if (rc < strlen((char *)buf)) {
  619. test_fail("[server] write didn't write all the bytes");
  620. }
  621. memset(&buf, '\0', sizeof(buf));
  622. debug("[server] Recvfrom message");
  623. rc = recvfrom(client_sd, buf, sizeof(buf), 0, NULL, 0);
  624. if (rc == -1) {
  625. test_fail("recvfrom() failed unexpectedly");
  626. kill(pid, SIGKILL);
  627. return;
  628. }
  629. debug("[server] we got the following message:");
  630. debug(buf);
  631. for (i = 0; i < rc && i < 127; i++) {
  632. buf[i] = toupper(buf[i]);
  633. }
  634. debug("[server] Sendto message...");
  635. rc = sendto(client_sd, buf, sizeof(buf), 0, NULL, 0);
  636. if (rc == -1) {
  637. test_fail("sendto() failed");
  638. kill(pid, SIGKILL);
  639. return;
  640. }
  641. if (rc < strlen((char *)buf)) {
  642. test_fail("[server] write didn't write all the bytes");
  643. }
  644. shutdown(client_sd, SHUT_RDWR);
  645. CLOSE(client_sd);
  646. shutdown(sd, SHUT_RDWR);
  647. CLOSE(sd);
  648. /* wait for client to exit */
  649. do {
  650. errno = 0;
  651. rc = waitpid(pid, &status, 0);
  652. } while (rc == -1 && errno == EINTR);
  653. /* we use the exit status to get its error count */
  654. errct += WEXITSTATUS(status);
  655. }
  656. int server_ready = 0;
  657. /* signal handler for the client */
  658. void test_xfer_sighdlr(int sig)
  659. {
  660. debug("entering signal handler");
  661. switch (sig) {
  662. /* the server will send SIGUSR1 when it is time for us
  663. * to start the tests
  664. */
  665. case SIGUSR1:
  666. server_ready = 1;
  667. debug("got SIGUSR1, the server is ready for the client");
  668. break;
  669. default:
  670. debug("didn't get SIGUSR1");
  671. }
  672. debug("leaving signal handler");
  673. }
  674. /*
  675. * A toupper() client.
  676. */
  677. static void test_xfer_client(const struct socket_test_info *info)
  678. {
  679. struct timeval tv;
  680. fd_set readfds;
  681. struct sockaddr_storage peer_addr;
  682. socklen_t peer_addr_len;
  683. int sd;
  684. int rc;
  685. char buf[BUFSIZE];
  686. debug("[client] entering test_xfer_client()");
  687. errct = 0; /* reset error count */
  688. memset(&buf, '\0', sizeof(buf));
  689. while (server_ready == 0) {
  690. debug("[client] waiting for the server to signal");
  691. sleep(1);
  692. }
  693. peer_addr_len = sizeof(peer_addr);
  694. if (info->callback_xfer_prepclient) info->callback_xfer_prepclient();
  695. debug("[client] creating client socket");
  696. SOCKET(sd, info->domain, info->type, 0);
  697. debug("[client] connecting to server through the symlink");
  698. rc = connect(sd, info->clientaddrsym, info->clientaddrsymlen);
  699. if (rc == -1) {
  700. test_fail("[client] connect() should have worked");
  701. } else {
  702. debug("[client] connected");
  703. }
  704. debug("[client] testing getpeername()");
  705. memset(&peer_addr, '\0', sizeof(peer_addr));
  706. rc = getpeername(sd, (struct sockaddr *) &peer_addr, &peer_addr_len);
  707. if (rc == -1) {
  708. test_fail("[client] getpeername() should have worked");
  709. }
  710. /* we need to use the full path "/usr/src/test/DIR_56/test.sock"
  711. * because that is what is returned by getpeername().
  712. */
  713. info->callback_check_sockaddr((struct sockaddr *) &peer_addr,
  714. peer_addr_len, "getpeername", 1);
  715. strncpy(buf, "Hello, World!", sizeof(buf) - 1);
  716. debug("[client] send to server");
  717. rc = write(sd, buf, sizeof(buf));
  718. if (rc == -1) {
  719. test_fail("[client] write() failed unexpectedly");
  720. }
  721. memset(buf, '\0', sizeof(buf));
  722. debug("[client] read from server");
  723. rc = read(sd, buf, sizeof(buf));
  724. if (rc == -1) {
  725. test_fail("[client] read() failed unexpectedly");
  726. } else {
  727. debug("[client] we got the following message:");
  728. debug(buf);
  729. }
  730. if (strncmp(buf, "HELLO, WORLD!", sizeof(buf)) != 0) {
  731. test_fail("[client] We didn't get the correct response");
  732. }
  733. memset(&buf, '\0', sizeof(buf));
  734. strncpy(buf, "Bonjour!", sizeof(buf) - 1);
  735. debug("[client] send to server");
  736. rc = send(sd, buf, sizeof(buf), 0);
  737. if (rc == -1) {
  738. test_fail("[client] send() failed unexpectedly");
  739. }
  740. if (info->callback_xfer_peercred) info->callback_xfer_peercred(sd);
  741. debug("Testing select()");
  742. tv.tv_sec = 2;
  743. tv.tv_usec = 500000;
  744. FD_ZERO(&readfds);
  745. FD_SET(sd, &readfds);
  746. rc = select(sd + 1, &readfds, NULL, NULL, &tv);
  747. if (rc == -1) {
  748. test_fail("[client] select() should not have failed");
  749. }
  750. if (rc != 1) {
  751. test_fail("[client] select() should have returned 1");
  752. }
  753. if (!(FD_ISSET(sd, &readfds))) {
  754. test_fail("The server didn't respond within 2.5 seconds");
  755. }
  756. memset(buf, '\0', sizeof(buf));
  757. debug("[client] recv from server");
  758. rc = recv(sd, buf, sizeof(buf), 0);
  759. if (rc == -1) {
  760. test_fail("[client] recv() failed unexpectedly");
  761. } else {
  762. debug("[client] we got the following message:");
  763. debug(buf);
  764. }
  765. if (strncmp(buf, "BONJOUR!", sizeof(buf)) != 0) {
  766. test_fail("[client] We didn't get the right response.");
  767. }
  768. memset(&buf, '\0', sizeof(buf));
  769. strncpy(buf, "Hola!", sizeof(buf) - 1);
  770. debug("[client] sendto to server");
  771. rc = sendto(sd, buf, sizeof(buf), 0, NULL, 0);
  772. if (rc == -1) {
  773. test_fail("[client] sendto() failed");
  774. }
  775. debug("Testing select()");
  776. tv.tv_sec = 2;
  777. tv.tv_usec = 500000;
  778. FD_ZERO(&readfds);
  779. FD_SET(sd, &readfds);
  780. rc = select(sd + 1, &readfds, NULL, NULL, &tv);
  781. if (rc == -1) {
  782. test_fail("[client] select() should not have failed");
  783. }
  784. if (rc != 1) {
  785. test_fail("[client] select() should have returned 1");
  786. }
  787. if (!(FD_ISSET(sd, &readfds))) {
  788. test_fail("[client] The server didn't respond in 2.5 seconds");
  789. }
  790. memset(buf, '\0', sizeof(buf));
  791. debug("[client] recvfrom from server");
  792. rc = recvfrom(sd, buf, sizeof(buf), 0, NULL, 0);
  793. if (rc == -1) {
  794. test_fail("[cleint] recvfrom() failed unexpectedly");
  795. } else {
  796. debug("[client] we got the following message:");
  797. debug(buf);
  798. }
  799. if (strncmp(buf, "HOLA!", sizeof(buf)) != 0) {
  800. test_fail("[client] We didn't get the right response.");
  801. }
  802. debug("[client] closing socket");
  803. CLOSE(sd);
  804. debug("[client] leaving test_xfer_client()");
  805. exit(errct);
  806. }
  807. void test_xfer(const struct socket_test_info *info)
  808. {
  809. pid_t pid;
  810. debug("entering test_xfer()");
  811. info->callback_cleanup();
  812. /* the signal handler is only used by the client, but we have to
  813. * install it now. if we don't the server may signal the client
  814. * before the handler is installed.
  815. */
  816. debug("installing signal handler");
  817. if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
  818. test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
  819. }
  820. debug("signal handler installed");
  821. server_ready = 0;
  822. pid = fork();
  823. if (pid == -1) {
  824. test_fail("fork() failed");
  825. return;
  826. } else if (pid == 0) {
  827. debug("child");
  828. errct = 0;
  829. test_xfer_client(info);
  830. test_fail("we should never get here");
  831. exit(1);
  832. } else {
  833. debug("parent");
  834. test_xfer_server(info, pid);
  835. debug("parent done");
  836. }
  837. info->callback_cleanup();
  838. debug("leaving test_xfer()");
  839. }
  840. static void test_simple_client(const struct socket_test_info *info, int type)
  841. {
  842. char buf[BUFSIZE];
  843. int sd, rc;
  844. sd = socket(info->domain, type, 0);
  845. if (sd == -1) {
  846. test_fail("socket");
  847. exit(errct);
  848. }
  849. while (server_ready == 0) {
  850. debug("[client] waiting for the server");
  851. sleep(1);
  852. }
  853. bzero(buf, BUFSIZE);
  854. snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
  855. if (type == SOCK_DGRAM) {
  856. rc = sendto(sd, buf, strlen(buf) + 1, 0,
  857. info->clientaddr, info->clientaddrlen);
  858. if (rc == -1) {
  859. test_fail("sendto");
  860. exit(errct);
  861. }
  862. } else {
  863. rc = connect(sd, info->clientaddr, info->clientaddrlen);
  864. if (rc == -1) {
  865. test_fail("connect");
  866. exit(errct);
  867. }
  868. rc = write(sd, buf, strlen(buf) + 1);
  869. if (rc == -1) {
  870. test_fail("write");
  871. }
  872. memset(buf, '\0', BUFSIZE);
  873. rc = read(sd, buf, BUFSIZE);
  874. if (rc == -1) {
  875. test_fail("read");
  876. }
  877. if (strcmp("Hello, My Name is Server.", buf) != 0) {
  878. test_fail("didn't read the correct string");
  879. }
  880. }
  881. rc = close(sd);
  882. if (rc == -1) {
  883. test_fail("close");
  884. }
  885. exit(errct);
  886. }
  887. static void test_simple_server(const struct socket_test_info *info, int type,
  888. pid_t pid)
  889. {
  890. char buf[BUFSIZE];
  891. int sd, rc, client_sd, status;
  892. struct sockaddr_storage addr;
  893. socklen_t addr_len;
  894. addr_len = info->clientaddrlen;
  895. sd = socket(info->domain, type, 0);
  896. if (sd == -1) {
  897. test_fail("socket");
  898. }
  899. assert(info->clientaddrlen <= sizeof(addr));
  900. memcpy(&addr, info->clientaddr, info->clientaddrlen);
  901. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  902. if (rc == -1) {
  903. test_fail("bind");
  904. }
  905. if (type == SOCK_DGRAM) {
  906. /* ready for client */
  907. kill(pid, SIGUSR1);
  908. rc = recvfrom(sd, buf, BUFSIZE, 0,
  909. (struct sockaddr *) &addr, &addr_len);
  910. if (rc == -1) {
  911. test_fail("recvfrom");
  912. }
  913. } else {
  914. rc = listen(sd, 5);
  915. if (rc == -1) {
  916. test_fail("listen");
  917. }
  918. /* we're ready for connections, time to tell the client
  919. * to start the test
  920. */
  921. kill(pid, SIGUSR1);
  922. client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
  923. if (client_sd == -1) {
  924. test_fail("accept");
  925. }
  926. memset(buf, '\0', BUFSIZE);
  927. rc = read(client_sd, buf, BUFSIZE);
  928. if (rc == -1) {
  929. test_fail("read");
  930. }
  931. if (strcmp("Hello, My Name is Client.", buf) != 0) {
  932. test_fail("didn't read the correct string");
  933. }
  934. /* added for extra fun to make the client block on read() */
  935. sleep(1);
  936. bzero(buf, BUFSIZE);
  937. snprintf(buf, BUFSIZE-1, "Hello, My Name is Server.");
  938. rc = write(client_sd, buf, strlen(buf) + 1);
  939. if (rc == -1) {
  940. test_fail("write");
  941. }
  942. rc = close(client_sd);
  943. if (rc == -1) {
  944. test_fail("close");
  945. }
  946. }
  947. rc = close(sd);
  948. if (rc == -1) {
  949. test_fail("close");
  950. }
  951. /* wait for client to exit */
  952. do {
  953. errno = 0;
  954. rc = waitpid(pid, &status, 0);
  955. } while (rc == -1 && errno == EINTR);
  956. /* we use the exit status to get its error count */
  957. errct += WEXITSTATUS(status);
  958. }
  959. static void test_abort_client(const struct socket_test_info *info,
  960. int abort_type);
  961. static void test_abort_server(const struct socket_test_info *info,
  962. pid_t pid, int abort_type);
  963. void test_abort_client_server(const struct socket_test_info *info,
  964. int abort_type)
  965. {
  966. pid_t pid;
  967. debug("test_simple_client_server()");
  968. info->callback_cleanup();
  969. /* the signal handler is only used by the client, but we have to
  970. * install it now. if we don't the server may signal the client
  971. * before the handler is installed.
  972. */
  973. debug("installing signal handler");
  974. if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
  975. test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
  976. }
  977. debug("signal handler installed");
  978. server_ready = 0;
  979. pid = fork();
  980. if (pid == -1) {
  981. test_fail("fork() failed");
  982. return;
  983. } else if (pid == 0) {
  984. debug("child");
  985. errct = 0;
  986. test_abort_client(info, abort_type);
  987. test_fail("we should never get here");
  988. exit(1);
  989. } else {
  990. debug("parent");
  991. test_abort_server(info, pid, abort_type);
  992. debug("parent done");
  993. }
  994. info->callback_cleanup();
  995. }
  996. static void test_abort_client(const struct socket_test_info *info,
  997. int abort_type)
  998. {
  999. char buf[BUFSIZE];
  1000. int sd, rc;
  1001. sd = socket(info->domain, info->type, 0);
  1002. if (sd == -1) {
  1003. test_fail("socket");
  1004. exit(errct);
  1005. }
  1006. while (server_ready == 0) {
  1007. debug("[client] waiting for the server");
  1008. sleep(1);
  1009. }
  1010. bzero(buf, BUFSIZE);
  1011. snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
  1012. rc = connect(sd, info->clientaddr, info->clientaddrlen);
  1013. if (rc == -1) {
  1014. test_fail("connect");
  1015. exit(errct);
  1016. }
  1017. if (abort_type == 2) {
  1018. /* Give server a chance to close connection */
  1019. sleep(2);
  1020. rc = write(sd, buf, strlen(buf) + 1);
  1021. if (rc != -1) {
  1022. if (!info->ignore_write_conn_reset) {
  1023. test_fail("write should have failed\n");
  1024. }
  1025. } else if (errno != ECONNRESET) {
  1026. test_fail("errno should've been ECONNRESET\n");
  1027. }
  1028. }
  1029. rc = close(sd);
  1030. if (rc == -1) {
  1031. test_fail("close");
  1032. }
  1033. exit(errct);
  1034. }
  1035. static void test_abort_server(const struct socket_test_info *info,
  1036. pid_t pid, int abort_type)
  1037. {
  1038. char buf[BUFSIZE];
  1039. int sd, rc, client_sd, status;
  1040. struct sockaddr_storage addr;
  1041. socklen_t addr_len;
  1042. addr_len = info->clientaddrlen;
  1043. sd = socket(info->domain, info->type, 0);
  1044. if (sd == -1) {
  1045. test_fail("socket");
  1046. }
  1047. assert(sizeof(addr) >= info->clientaddrlen);
  1048. memcpy(&addr, info->clientaddr, info->clientaddrlen);
  1049. rc = bind(sd, info->serveraddr, info->serveraddrlen);
  1050. if (rc == -1) {
  1051. test_fail("bind");
  1052. }
  1053. rc = listen(sd, 5);
  1054. if (rc == -1) {
  1055. test_fail("listen");
  1056. }
  1057. /* we're ready for connections, time to tell the client
  1058. * to start the test
  1059. */
  1060. kill(pid, SIGUSR1);
  1061. client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
  1062. if (client_sd == -1) {
  1063. test_fail("accept");
  1064. }
  1065. if (abort_type == 1) {
  1066. memset(buf, '\0', BUFSIZE);
  1067. rc = read(client_sd, buf, BUFSIZE);
  1068. if (rc != -1 && (rc != 0 || !info->ignore_read_conn_reset)) {
  1069. test_fail("read should've failed or returned zero\n");
  1070. }
  1071. if (rc != 0 && errno != ECONNRESET) {
  1072. test_fail("errno should've been ECONNRESET\n");
  1073. }
  1074. } /* else if (abort_type == 2) { */
  1075. rc = close(client_sd);
  1076. if (rc == -1) {
  1077. test_fail("close");
  1078. }
  1079. /* } */
  1080. rc = close(sd);
  1081. if (rc == -1) {
  1082. test_fail("close");
  1083. }
  1084. /* wait for client to exit */
  1085. do {
  1086. errno = 0;
  1087. rc = waitpid(pid, &status, 0);
  1088. } while (rc == -1 && errno == EINTR);
  1089. /* we use the exit status to get its error count */
  1090. errct += WEXITSTATUS(status);
  1091. }
  1092. void test_simple_client_server(const struct socket_test_info *info, int type)
  1093. {
  1094. pid_t pid;
  1095. debug("entering test_simple_client_server()");
  1096. info->callback_cleanup();
  1097. /* the signal handler is only used by the client, but we have to
  1098. * install it now. if we don't the server may signal the client
  1099. * before the handler is installed.
  1100. */
  1101. debug("installing signal handler");
  1102. if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
  1103. test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
  1104. }
  1105. debug("signal handler installed");
  1106. server_ready = 0;
  1107. pid = fork();
  1108. if (pid == -1) {
  1109. test_fail("fork() failed");
  1110. return;
  1111. } else if (pid == 0) {
  1112. debug("child");
  1113. errct = 0;
  1114. test_simple_client(info, type);
  1115. test_fail("we should never get here");
  1116. exit(1);
  1117. } else {
  1118. debug("parent");
  1119. test_simple_server(info, type, pid);
  1120. debug("parent done");
  1121. }
  1122. info->callback_cleanup();
  1123. debug("leaving test_simple_client_server()");
  1124. }
  1125. void test_msg_dgram(const struct socket_test_info *info)
  1126. {
  1127. int rc;
  1128. int src;
  1129. int dst;
  1130. struct sockaddr_storage addr;
  1131. struct iovec iov[3];
  1132. struct msghdr msg1;
  1133. struct msghdr msg2;
  1134. char buf1[BUFSIZE];
  1135. char buf2[BUFSIZE];
  1136. char buf3[BUFSIZE];
  1137. debug("entering test_msg_dgram");
  1138. info->callback_cleanup();
  1139. src = socket(info->domain, SOCK_DGRAM, 0);
  1140. if (src == -1) {
  1141. test_fail("socket");
  1142. }
  1143. dst = socket(info->domain, SOCK_DGRAM, 0);
  1144. if (dst == -1) {
  1145. test_fail("socket");
  1146. }
  1147. rc = bind(src, info->serveraddr2, info->serveraddr2len);
  1148. if (rc == -1) {
  1149. test_fail("bind");
  1150. }
  1151. assert(info->clientaddrlen <= sizeof(addr));
  1152. memcpy(&addr, info->clientaddr, info->clientaddrlen);
  1153. rc = bind(dst, info->serveraddr, info->serveraddrlen);
  1154. if (rc == -1) {
  1155. test_fail("bind");
  1156. }
  1157. memset(&buf1, '\0', BUFSIZE);
  1158. memset(&buf2, '\0', BUFSIZE);
  1159. memset(&buf3, '\0', BUFSIZE);
  1160. strncpy(buf1, "Minix ", BUFSIZE-1);
  1161. strncpy(buf2, "is ", BUFSIZE-1);
  1162. strncpy(buf3, "great!", BUFSIZE-1);
  1163. iov[0].iov_base = buf1;
  1164. iov[0].iov_len = 6;
  1165. iov[1].iov_base = buf2;
  1166. iov[1].iov_len = 3;
  1167. iov[2].iov_base = buf3;
  1168. iov[2].iov_len = 32;
  1169. memset(&msg1, '\0', sizeof(struct msghdr));
  1170. msg1.msg_name = &addr;
  1171. msg1.msg_namelen = info->clientaddrlen;
  1172. msg1.msg_iov = iov;
  1173. msg1.msg_iovlen = 3;
  1174. msg1.msg_control = NULL;
  1175. msg1.msg_controllen = 0;
  1176. msg1.msg_flags = 0;
  1177. rc = sendmsg(src, &msg1, 0);
  1178. if (rc == -1) {
  1179. test_fail("sendmsg");
  1180. }
  1181. memset(&buf1, '\0', BUFSIZE);
  1182. memset(&buf2, '\0', BUFSIZE);
  1183. iov[0].iov_base = buf1;
  1184. iov[0].iov_len = 9;
  1185. iov[1].iov_base = buf2;
  1186. iov[1].iov_len = 32;
  1187. memset(&addr, '\0', sizeof(addr));
  1188. memset(&msg2, '\0', sizeof(struct msghdr));
  1189. msg2.msg_name = &addr;
  1190. msg2.msg_namelen = sizeof(addr);
  1191. msg2.msg_iov = iov;
  1192. msg2.msg_iovlen = 2;
  1193. msg2.msg_control = NULL;
  1194. msg2.msg_controllen = 0;
  1195. msg2.msg_flags = 0;
  1196. rc = recvmsg(dst, &msg2, 0);
  1197. if (rc == -1) {
  1198. test_fail("recvmsg");
  1199. }
  1200. if (strncmp(buf1, "Minix is ", 9) || strncmp(buf2, "great!", 6)) {
  1201. test_fail("recvmsg");
  1202. }
  1203. /* we need to use the full path "/usr/src/test/DIR_56/testb.sock"
  1204. * because that is what is returned by recvmsg().
  1205. */
  1206. info->callback_check_sockaddr((struct sockaddr *) &addr,
  1207. msg2.msg_namelen, "recvmsg", 2);
  1208. rc = close(dst);
  1209. if (rc == -1) {
  1210. test_fail("close");
  1211. }
  1212. rc = close(src);
  1213. if (rc == -1) {
  1214. test_fail("close");
  1215. }
  1216. info->callback_cleanup();
  1217. debug("leaving test_msg_dgram");
  1218. }
  1219. #define check_select(sd, rd, wr, block) \
  1220. check_select_internal(sd, rd, wr, block, 1, __LINE__)
  1221. #define check_select_cond(sd, rd, wr, block, allchecks) \
  1222. check_select_internal(sd, rd, wr, block, allchecks, __LINE__)
  1223. static void
  1224. check_select_internal(int sd, int rd, int wr, int block, int allchecks, int line)
  1225. {
  1226. fd_set read_set, write_set;
  1227. struct timeval tv;
  1228. FD_ZERO(&read_set);
  1229. if (rd != -1)
  1230. FD_SET(sd, &read_set);
  1231. FD_ZERO(&write_set);
  1232. if (wr != -1)
  1233. FD_SET(sd, &write_set);
  1234. tv.tv_sec = block ? 2 : 0;
  1235. tv.tv_usec = 0;
  1236. errno = 0;
  1237. if (select(sd + 1, &read_set, &write_set, NULL, &tv) < 0)
  1238. test_fail_fl("select() failed unexpectedly", __FILE__, line);
  1239. if (rd != -1 && !!FD_ISSET(sd, &read_set) != rd && allchecks)
  1240. test_fail_fl("select() mismatch on read operation",
  1241. __FILE__, line);
  1242. if (wr != -1 && !!FD_ISSET(sd, &write_set) != wr && allchecks)
  1243. test_fail_fl("select() mismatch on write operation",
  1244. __FILE__, line);
  1245. }
  1246. /*
  1247. * Verify that:
  1248. * - a nonblocking connecting socket for which there is no accepter, will
  1249. * return EINPROGRESS and complete in the background later;
  1250. * - a nonblocking listening socket will return EAGAIN on accept;
  1251. * - connecting a connecting socket yields EALREADY;
  1252. * - connecting a connected socket yields EISCONN;
  1253. * - selecting for read and write on a connecting socket will only satisfy the
  1254. * write only once it is connected;
  1255. * - doing a nonblocking write on a connecting socket yields EAGAIN;
  1256. * - doing a nonblocking read on a connected socket with no pending data yields
  1257. * EAGAIN.
  1258. */
  1259. void
  1260. test_nonblock(const struct socket_test_info *info)
  1261. {
  1262. char buf[BUFSIZE];
  1263. socklen_t len;
  1264. int server_sd, client_sd;
  1265. struct sockaddr_storage addr;
  1266. int status;
  1267. debug("entering test_nonblock()");
  1268. memset(buf, 0, sizeof(buf));
  1269. SOCKET(server_sd, info->domain, info->type, 0);
  1270. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1271. test_fail("bind() should have worked");
  1272. if (listen(server_sd, 8) == -1)
  1273. test_fail("listen() should have worked");
  1274. fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
  1275. check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1276. len = sizeof(addr);
  1277. if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1 ||
  1278. errno != EAGAIN)
  1279. test_fail("accept() should have yielded EAGAIN");
  1280. SOCKET(client_sd, info->domain, info->type, 0);
  1281. fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
  1282. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
  1283. test_fail("connect() should have failed");
  1284. } else if (errno != EINPROGRESS) {
  1285. test_fail("connect() should have yielded EINPROGRESS");
  1286. }
  1287. check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
  1288. !info->ignore_select_delay);
  1289. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
  1290. test_fail("connect() should have failed");
  1291. } else if (errno != EALREADY && errno != EISCONN) {
  1292. test_fail("connect() should have yielded EALREADY");
  1293. }
  1294. if (recv(client_sd, buf, sizeof(buf), 0) != -1 || errno != EAGAIN)
  1295. test_fail("recv() should have yielded EAGAIN");
  1296. /* This may be an implementation aspect, or even plain wrong (?). */
  1297. if (!info->ignore_send_waiting) {
  1298. if (send(client_sd, buf, sizeof(buf), 0) != -1) {
  1299. test_fail("send() should have failed");
  1300. } else if (errno != EAGAIN) {
  1301. test_fail("send() should have yielded EAGAIN");
  1302. }
  1303. }
  1304. switch (fork()) {
  1305. case 0:
  1306. errct = 0;
  1307. close(client_sd);
  1308. check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1309. len = sizeof(addr);
  1310. client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
  1311. if (client_sd == -1)
  1312. test_fail("accept() should have succeeded");
  1313. check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1314. close(server_sd);
  1315. /* Let the socket become writable in the parent process. */
  1316. sleep(1);
  1317. if (write(client_sd, buf, 1) != 1)
  1318. test_fail("write() should have succeeded");
  1319. /* Wait for the client side to close. */
  1320. check_select_cond(client_sd, 0 /*read*/, 1 /*write*/,
  1321. 0 /*block*/, !info->ignore_select_delay /*allchecks*/);
  1322. check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
  1323. check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1324. exit(errct);
  1325. case -1:
  1326. test_fail("can't fork");
  1327. default:
  1328. break;
  1329. }
  1330. close(server_sd);
  1331. check_select(client_sd, 0 /*read*/, 1 /*write*/, 1 /*block*/);
  1332. check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1333. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
  1334. errno != EISCONN)
  1335. test_fail("connect() should have yielded EISCONN");
  1336. check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
  1337. check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1338. if (read(client_sd, buf, 1) != 1)
  1339. test_fail("read() should have succeeded");
  1340. check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1341. if (read(client_sd, buf, 1) != -1 || errno != EAGAIN)
  1342. test_fail("read() should have yielded EAGAIN");
  1343. /* Let the child process block on the select waiting for the close. */
  1344. sleep(1);
  1345. close(client_sd);
  1346. errno = 0;
  1347. if (wait(&status) <= 0)
  1348. test_fail("wait() should have succeeded");
  1349. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  1350. test_fail("child process failed the test");
  1351. info->callback_cleanup();
  1352. debug("leaving test_nonblock()");
  1353. }
  1354. /*
  1355. * Verify that a nonblocking connect for which there is an accepter, succeeds
  1356. * immediately. A pretty lame test, only here for completeness.
  1357. */
  1358. void
  1359. test_connect_nb(const struct socket_test_info *info)
  1360. {
  1361. socklen_t len;
  1362. int server_sd, client_sd;
  1363. struct sockaddr_storage addr;
  1364. int status;
  1365. debug("entering test_connect_nb()");
  1366. SOCKET(server_sd, info->domain, info->type, 0);
  1367. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1368. test_fail("bind() should have worked");
  1369. if (listen(server_sd, 8) == -1)
  1370. test_fail("listen() should have worked");
  1371. switch (fork()) {
  1372. case 0:
  1373. errct = 0;
  1374. len = sizeof(addr);
  1375. if (accept(server_sd, (struct sockaddr *) &addr, &len) == -1)
  1376. test_fail("accept() should have succeeded");
  1377. exit(errct);
  1378. case -1:
  1379. test_fail("can't fork");
  1380. default:
  1381. break;
  1382. }
  1383. close(server_sd);
  1384. sleep(1);
  1385. SOCKET(client_sd, info->domain, info->type, 0);
  1386. fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
  1387. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != 0) {
  1388. if (!info->ignore_connect_delay) {
  1389. test_fail("connect() should have succeeded");
  1390. } else if (errno != EINPROGRESS) {
  1391. test_fail("connect() should have succeeded or "
  1392. "failed with EINPROGRESS");
  1393. }
  1394. }
  1395. close(client_sd);
  1396. if (wait(&status) <= 0)
  1397. test_fail("wait() should have succeeded");
  1398. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  1399. test_fail("child process failed the test");
  1400. info->callback_cleanup();
  1401. debug("leaving test_connect_nb()");
  1402. }
  1403. static void
  1404. dummy_handler(int sig)
  1405. {
  1406. /* Nothing. */
  1407. }
  1408. /*
  1409. * Verify that:
  1410. * - interrupting a blocking connect will return EINTR but complete in the
  1411. * background later;
  1412. * - doing a blocking write on an asynchronously connecting socket succeeds
  1413. * once the socket is connected.
  1414. * - doing a nonblocking write on a connected socket with lots of pending data
  1415. * yields EAGAIN.
  1416. */
  1417. void
  1418. test_intr(const struct socket_test_info *info)
  1419. {
  1420. struct sigaction act, oact;
  1421. char buf[BUFSIZE];
  1422. int isconn;
  1423. socklen_t len;
  1424. int server_sd, client_sd;
  1425. struct sockaddr_storage addr;
  1426. int r, status;
  1427. debug("entering test_intr()");
  1428. memset(buf, 0, sizeof(buf));
  1429. SOCKET(server_sd, info->domain, info->type, 0);
  1430. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1431. test_fail("bind() should have worked");
  1432. if (listen(server_sd, 8) == -1)
  1433. test_fail("listen() should have worked");
  1434. SOCKET(client_sd, info->domain, info->type, 0);
  1435. memset(&act, 0, sizeof(act));
  1436. act.sa_handler = dummy_handler;
  1437. if (sigaction(SIGALRM, &act, &oact) == -1)
  1438. test_fail("sigaction() should have succeeded");
  1439. if (info->domain != PF_INET) alarm(1);
  1440. isconn = 0;
  1441. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
  1442. if (!info->ignore_connect_unaccepted) {
  1443. test_fail("connect() should have failed");
  1444. }
  1445. isconn = 1;
  1446. } else if (errno != EINTR) {
  1447. test_fail("connect() should have yielded EINTR");
  1448. }
  1449. alarm(0);
  1450. check_select(client_sd, 0 /*read*/, isconn /*write*/, 0 /*block*/);
  1451. switch (fork()) {
  1452. case 0:
  1453. errct = 0;
  1454. close(client_sd);
  1455. check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1456. len = sizeof(addr);
  1457. client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
  1458. if (client_sd == -1)
  1459. test_fail("accept() should have succeeded");
  1460. check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1461. close(server_sd);
  1462. check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
  1463. check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1464. if (recv(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
  1465. test_fail("recv() should have yielded bytes");
  1466. /* No partial transfers should be happening. */
  1467. check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1468. sleep(1);
  1469. fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) |
  1470. O_NONBLOCK);
  1471. /* We can only test nonblocking writes by filling the pipe. */
  1472. while ((r = write(client_sd, buf, sizeof(buf))) > 0);
  1473. if (r != -1) {
  1474. test_fail("write() should have failed");
  1475. } else if (errno != EAGAIN) {
  1476. test_fail("write() should have yielded EAGAIN");
  1477. }
  1478. check_select(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/);
  1479. if (write(client_sd, buf, 1) != -1) {
  1480. test_fail("write() should have failed");
  1481. } else if (errno != EAGAIN) {
  1482. test_fail("write() should have yielded EAGAIN");
  1483. }
  1484. exit(errct);
  1485. case -1:
  1486. test_fail("can't fork");
  1487. default:
  1488. break;
  1489. }
  1490. close(server_sd);
  1491. if (send(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
  1492. test_fail("send() should have succeded");
  1493. check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1494. if (wait(&status) <= 0)
  1495. test_fail("wait() should have succeeded");
  1496. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  1497. test_fail("child process failed the test");
  1498. check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
  1499. close(client_sd);
  1500. sigaction(SIGALRM, &oact, NULL);
  1501. info->callback_cleanup();
  1502. debug("leaving test_intr()");
  1503. }
  1504. /*
  1505. * Verify that closing a connecting socket before it is accepted will result in
  1506. * no activity on the accepting side later.
  1507. */
  1508. void
  1509. test_connect_close(const struct socket_test_info *info)
  1510. {
  1511. int server_sd, client_sd;
  1512. struct sockaddr_storage addr;
  1513. socklen_t len;
  1514. debug("entering test_connect_close()");
  1515. SOCKET(server_sd, info->domain, info->type, 0);
  1516. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1517. test_fail("bind() should have worked");
  1518. if (listen(server_sd, 8) == -1)
  1519. test_fail("listen() should have worked");
  1520. fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
  1521. check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
  1522. SOCKET(client_sd, info->domain, info->type, 0);
  1523. fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
  1524. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
  1525. errno != EINPROGRESS)
  1526. test_fail("connect() should have yielded EINPROGRESS");
  1527. check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
  1528. !info->ignore_select_delay);
  1529. check_select_cond(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
  1530. !info->ignore_select_delay);
  1531. close(client_sd);
  1532. check_select_cond(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/,
  1533. !info->ignore_select_delay);
  1534. len = sizeof(addr);
  1535. errno = 0;
  1536. if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1) {
  1537. if (!info->ignore_accept_delay) {
  1538. test_fail("accept() should have failed");
  1539. }
  1540. } else if (errno != EAGAIN) {
  1541. test_fail("accept() should have yielded EAGAIN");
  1542. }
  1543. close(server_sd);
  1544. info->callback_cleanup();
  1545. debug("leaving test_connect_close()");
  1546. }
  1547. /*
  1548. * Verify that closing a listening socket will cause a blocking connect to fail
  1549. * with ECONNRESET, and that a subsequent write will yield EPIPE.
  1550. */
  1551. void
  1552. test_listen_close(const struct socket_test_info *info)
  1553. {
  1554. int server_sd, client_sd;
  1555. int status;
  1556. char byte;
  1557. debug("entering test_listen_close()");
  1558. SOCKET(server_sd, info->domain, info->type, 0);
  1559. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1560. test_fail("bind() should have worked");
  1561. if (listen(server_sd, 8) == -1)
  1562. test_fail("listen() should have worked");
  1563. switch (fork()) {
  1564. case 0:
  1565. sleep(1);
  1566. exit(0);
  1567. case -1:
  1568. test_fail("can't fork");
  1569. default:
  1570. break;
  1571. }
  1572. close(server_sd);
  1573. SOCKET(client_sd, info->domain, info->type, 0);
  1574. byte = 0;
  1575. if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
  1576. /* Yes, you fucked up the fix for the FIXME below. */
  1577. test_fail("write() should have yielded ENOTCONN");
  1578. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
  1579. if (!info->bug_connect_after_close) {
  1580. test_fail("connect() should have failed");
  1581. }
  1582. } else if (errno != ECONNRESET) {
  1583. test_fail("connect() should have yielded ECONNRESET");
  1584. }
  1585. /*
  1586. * FIXME: currently UDS cannot distinguish between sockets that have
  1587. * not yet been connected, and sockets that have been disconnected.
  1588. * Thus, we get the same error for both: ENOTCONN instead of EPIPE.
  1589. */
  1590. #if 0
  1591. if (write(client_sd, &byte, 1) != -1 || errno != EPIPE)
  1592. test_fail("write() should have yielded EPIPE");
  1593. #endif
  1594. close(client_sd);
  1595. if (wait(&status) <= 0)
  1596. test_fail("wait() should have succeeded");
  1597. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  1598. test_fail("child process failed the test");
  1599. info->callback_cleanup();
  1600. debug("leaving test_listen_close()");
  1601. }
  1602. /*
  1603. * Verify that closing a listening socket will cause a nonblocking connect to
  1604. * result in the socket becoming readable and writable, and yielding ECONNRESET
  1605. * and EPIPE on the next two writes, respectively.
  1606. */
  1607. void
  1608. test_listen_close_nb(const struct socket_test_info *info)
  1609. {
  1610. int server_sd, client_sd;
  1611. int status;
  1612. char byte;
  1613. debug("entering test_listen_close_nb()");
  1614. SOCKET(server_sd, info->domain, info->type, 0);
  1615. if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
  1616. test_fail("bind() should have worked");
  1617. if (listen(server_sd, 8) == -1)
  1618. test_fail("listen() should have worked");
  1619. switch (fork()) {
  1620. case 0:
  1621. sleep(1);
  1622. exit(0);
  1623. case -1:
  1624. test_fail("can't fork");
  1625. default:
  1626. break;
  1627. }
  1628. close(server_sd);
  1629. SOCKET(client_sd, info->domain, info->type, 0);
  1630. fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
  1631. if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
  1632. errno != EINPROGRESS)
  1633. test_fail("connect() should have yielded EINPROGRESS");
  1634. check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
  1635. !info->ignore_select_delay);
  1636. check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 1 /*block*/,
  1637. !info->ignore_select_delay);
  1638. byte = 0;
  1639. if (write(client_sd, &byte, 1) != -1) {
  1640. if (!info->ignore_write_conn_reset) {
  1641. test_fail("write() should have failed");
  1642. }
  1643. } else if (errno != ECONNRESET) {
  1644. test_fail("write() should have yielded ECONNRESET");
  1645. }
  1646. /*
  1647. * FIXME: currently UDS cannot distinguish between sockets that have
  1648. * not yet been connected, and sockets that have been disconnected.
  1649. * Thus, we get the same error for both: ENOTCONN instead of EPIPE.
  1650. */
  1651. #if 0
  1652. if (write(client_sd, &byte, 1) != -1 || errno != EPIPE)
  1653. test_fail("write() should have yielded EPIPE");
  1654. #endif
  1655. check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
  1656. !info->ignore_select_delay);
  1657. close(client_sd);
  1658. if (wait(&status) <= 0)
  1659. test_fail("wait() should have succeeded");
  1660. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  1661. test_fail("child process failed the test");
  1662. info->callback_cleanup();
  1663. debug("leaving test_listen_close_nb()");
  1664. }