-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathctest.c
More file actions
136 lines (120 loc) · 3.09 KB
/
ctest.c
File metadata and controls
136 lines (120 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#define BUF_SIZE 10000
int waitForStart(int fd, unsigned char *buf, size_t buflen) {
ssize_t numRead;
for (;;) {
numRead = read(fd, buf, buflen);
if (numRead == -1) {
perror("read");
exit(EXIT_FAILURE);
}
write(STDOUT_FILENO, buf, numRead);
if (strstr(buf, "started") != NULL) {
break;
}
}
return 0;
}
int childCommnadTest(char *name, char *pathname, char **args) {
int pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
if (execvp(pathname, args) == -1) {
perror("execvp");
exit(EXIT_FAILURE);
}
}
// Wait for process exit.
if (-1 == wait(NULL)) {
perror("wait");
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
pid_t spid, cpid, w;
int fds[2];
struct sockaddr peaddr;
ssize_t numRead;
char buf[BUF_SIZE];
int ready = 0;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
perror("socketpair");
exit(EXIT_FAILURE);
}
// Start local.
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) {
if (dup2(fds[1], STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
if (dup2(fds[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
close(fds[1]);
execlp("./procurator-local", "procurator-local", "--remote-host",
"127.0.0.1", "--remote-port", "8838", "--remote-udp-port", "8839",
"--local-port", "8080", "--local-udp-port", "8081", "--password",
"foobar", (char *)NULL);
}
waitForStart(fds[0], buf, BUF_SIZE);
// Start server.
spid = fork();
if (spid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (spid == 0) {
if (dup2(fds[1], STDIN_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
if (dup2(fds[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
close(fds[1]);
execlp("./procurator-server", "procurator-server", "--remote-port", "8838",
"--remote-udp-port", "8839", "--password", "foobar", (char *)NULL);
}
waitForStart(fds[0], buf, BUF_SIZE);
// Run tests.
char *udpRelayArgs[] = {
"bash", "-c",
"echo "
"'00000001010000010035c2290100000100000000000003777777076578616d706c65036"
"36f6d0000010001' | xxd -r -p | nc -u -W 1 -w 2 127.0.0.1 8081",
(char *)NULL};
childCommnadTest("udp relay", "bash", udpRelayArgs);
char *curlArgs[] = {"curl",
"-v",
"http://www.example.com/",
"-L",
"--socks5-hostname",
"127.0.0.1:8080",
(char *)NULL};
childCommnadTest("curl proxy", "curl", curlArgs);
// Clean, kill local and server.
kill(spid, SIGHUP);
kill(cpid, SIGHUP);
printf("\nAll test finished!\n");
}