vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / net / select_test2.c
blob232f3f0bc9f73731ad84a5dee0cd136b95bcd4cd
1 #include <stdio.h>
2 #include <kernel/OS.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <errno.h>
7 #include "sys/socket.h"
8 #include "netinet/in.h"
9 #include "arpa/inet.h"
10 #include "sys/select.h"
12 #include "ufunc.h"
14 int main(int argc, char **argv)
16 int s, f;
17 int rv;
18 struct fd_set fdr, fdw, fde;
19 struct timeval tv;
20 int32 rtc;
21 char path[PATH_MAX];
23 test_banner("Select Test #2");
25 s = socket(AF_INET, SOCK_DGRAM, 0);
26 if (s < 0)
27 err(s, "Socket creation failed");
29 getcwd(path, PATH_MAX);
30 sprintf(path, "%s/select_test2.c", path);
31 f = open(path, O_RDWR);
33 if (f > 0 && s > 0) {
34 printf("\nsocket and fd created.\n");
35 } else {
36 err(-1, "Failed to create socket or fd\n");
39 FD_ZERO(&fdr);
40 FD_SET(s, &fdr);
41 FD_ZERO(&fdw);
42 FD_SET(s, &fdw);
43 FD_ZERO(&fde);
44 FD_SET(s, &fde);
46 tv.tv_sec = 5;
47 tv.tv_usec = 0;
48 printf("\nTest1\n=====\n\n");
49 printf("Trying with timeval (5 secs)...\n");
50 rtc = real_time_clock();
51 rv = select(s + 1, &fdr, NULL, &fde, &tv);
52 rtc = real_time_clock() - rtc;
53 printf("select gave %d (expecting 0) in %ld seconds\n", rv, rtc);
55 FD_ZERO(&fdr);
56 FD_SET(s, &fdr);
57 FD_SET(f, &fdr);
58 FD_ZERO(&fdw);
59 FD_SET(s, &fdw);
60 FD_ZERO(&fde);
61 FD_SET(s, &fde);
63 printf("\nTest2\n=====\n\n");
64 printf("Trying without timeval and both sockets and files...\n");
65 rv = select(f +1, &fdr, NULL, NULL, NULL);
66 printf("select gave %d (expecting 2)\n", rv);
68 if (rv > 0) {
69 if (FD_ISSET(s, &fdr))
70 printf("Data to read\n");
71 if (FD_ISSET(s, &fdw))
72 printf("OK to write\n");
73 if (FD_ISSET(s, &fde))
74 printf("Exception!\n");
75 if (FD_ISSET(f, &fdr))
76 printf("File is readable!\n");
77 } else if (rv == 0) {
78 printf("Timed out??? huh?!\n");
79 } else {
80 printf("errno = %d [%s]\n", errno, strerror(errno));
83 closesocket(s);
84 close(f);
86 printf("Test complete.\n");
88 return (0);