Commit | Line | Data |
---|---|---|
07e98688 GE |
1 | /*************************************************************************** |
2 | * Copyright (C) 2004 by Intra2net AG * | |
3 | * info@intra2net.com * | |
4 | * * | |
5 | ***************************************************************************/ | |
6 | ||
7 | #include <sys/types.h> | |
8 | #include <unistd.h> | |
9 | #include <errno.h> | |
10 | #include <signal.h> | |
11 | #include <stdio.h> | |
12 | ||
13 | #include <iostream> | |
14 | #include <string> | |
15 | #include <sstream> | |
16 | #include <stdexcept> | |
17 | ||
18 | #include <cppunit/extensions/TestFactoryRegistry.h> | |
19 | #include <cppunit/ui/text/TestRunner.h> | |
20 | #include <cppunit/extensions/HelperMacros.h> | |
21 | ||
22 | #include <socket_client.hxx> | |
23 | #include <socket_server.hxx> | |
24 | ||
25 | using namespace std; | |
26 | using namespace libt2n; | |
27 | using namespace CppUnit; | |
28 | ||
29 | class test_comm : public TestFixture | |
30 | { | |
31 | CPPUNIT_TEST_SUITE(test_comm); | |
32 | ||
33 | CPPUNIT_TEST(UnixCommToServer); | |
34 | CPPUNIT_TEST(UnixCommToServerAndBack); | |
cc68aabb | 35 | CPPUNIT_TEST(IPCommToServer); |
7087e187 | 36 | CPPUNIT_TEST(IPCommToServerAndBack); |
07e98688 GE |
37 | |
38 | CPPUNIT_TEST_SUITE_END(); | |
39 | ||
40 | public: | |
41 | ||
42 | void setUp() | |
43 | { } | |
44 | ||
45 | void tearDown() | |
46 | { } | |
47 | ||
48 | void UnixCommToServer() | |
49 | { | |
50 | pid_t pid; | |
51 | string data; | |
52 | ||
53 | switch(pid=fork()) | |
54 | { | |
55 | case -1: | |
56 | { | |
57 | CPPUNIT_FAIL("fork error"); | |
58 | break; | |
59 | } | |
60 | case 0: | |
61 | // child | |
62 | { | |
63 | // wait till server is up | |
64 | sleep(1); | |
65 | socket_client_connection sc("./socket"); | |
66 | sc.write("hello"); | |
67 | // don't call atexit and stuff | |
68 | _exit(0); | |
69 | } | |
70 | ||
71 | default: | |
72 | // parent | |
73 | { | |
74 | socket_server ss("./socket"); | |
75 | ||
76 | // max 10 sec | |
77 | for (int i=0; i < 10; i++) | |
78 | { | |
79 | ss.fill_buffer(1000000); | |
80 | ||
81 | if(ss.get_packet(data)) | |
82 | break; | |
83 | } | |
84 | } | |
85 | } | |
86 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); | |
87 | } | |
88 | ||
89 | void UnixCommToServerAndBack() | |
90 | { | |
91 | pid_t pid; | |
92 | ||
93 | switch(pid=fork()) | |
94 | { | |
95 | case -1: | |
96 | { | |
97 | CPPUNIT_FAIL("fork error"); | |
98 | break; | |
99 | } | |
100 | case 0: | |
101 | // child | |
102 | { | |
103 | socket_server ss("./socket"); | |
104 | ss.set_logging(&cerr,debug); | |
105 | ||
106 | // max 10 sec | |
107 | for (int i=0; i < 10; i++) | |
108 | { | |
109 | ss.fill_buffer(1000000); | |
110 | ||
111 | string data; | |
112 | unsigned int cid; | |
113 | ||
114 | if(ss.get_packet(data,cid)) | |
115 | { | |
116 | server_connection* con=ss.get_connection(cid); | |
117 | ||
118 | if (data=="QUIT") | |
119 | break; | |
120 | ||
121 | if (data=="ABC") | |
122 | con->write("DEF"); | |
123 | else | |
124 | con->write("xyz"); | |
125 | } | |
126 | } | |
127 | // don't call atexit and stuff | |
128 | _exit(0); | |
129 | } | |
130 | ||
131 | default: | |
132 | // parent | |
133 | { | |
134 | string data; | |
135 | ||
136 | // wait till server is up | |
137 | sleep(1); | |
138 | socket_client_connection sc("./socket"); | |
139 | ||
140 | sc.write("ABC"); | |
141 | ||
142 | sc.fill_buffer(1000000); | |
143 | sc.get_packet(data); | |
144 | ||
145 | CPPUNIT_ASSERT_EQUAL(string("DEF"),data); | |
146 | ||
147 | sc.write("HAHA"); | |
148 | ||
149 | sc.fill_buffer(1000000); | |
150 | sc.get_packet(data); | |
151 | ||
152 | CPPUNIT_ASSERT_EQUAL(string("xyz"),data); | |
153 | ||
154 | sc.write("QUIT"); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
cc68aabb GE |
159 | void IPCommToServer() |
160 | { | |
161 | pid_t pid; | |
162 | string data; | |
163 | ||
164 | switch(pid=fork()) | |
165 | { | |
166 | case -1: | |
167 | { | |
168 | CPPUNIT_FAIL("fork error"); | |
169 | break; | |
170 | } | |
171 | case 0: | |
172 | // child | |
173 | { | |
174 | // wait till server is up | |
175 | sleep(1); | |
b604df5f | 176 | socket_client_connection sc(6666); |
cc68aabb GE |
177 | sc.write("hello"); |
178 | // don't call atexit and stuff | |
179 | _exit(0); | |
180 | } | |
181 | ||
182 | default: | |
183 | // parent | |
184 | { | |
185 | socket_server ss(6666); | |
186 | ||
187 | // max 10 sec | |
188 | for (int i=0; i < 10; i++) | |
189 | { | |
190 | ss.fill_buffer(1000000); | |
191 | ||
192 | if(ss.get_packet(data)) | |
193 | break; | |
194 | } | |
195 | } | |
196 | } | |
197 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); | |
198 | } | |
199 | ||
7087e187 GE |
200 | void IPCommToServerAndBack() |
201 | { | |
202 | pid_t pid; | |
203 | ||
204 | switch(pid=fork()) | |
205 | { | |
206 | case -1: | |
207 | { | |
208 | CPPUNIT_FAIL("fork error"); | |
209 | break; | |
210 | } | |
211 | case 0: | |
212 | // child | |
213 | { | |
214 | socket_server ss(6666); | |
215 | ss.set_logging(&cerr,debug); | |
216 | ||
217 | // max 10 sec | |
218 | for (int i=0; i < 10; i++) | |
219 | { | |
220 | ss.fill_buffer(1000000); | |
221 | ||
222 | string data; | |
223 | unsigned int cid; | |
224 | ||
225 | if(ss.get_packet(data,cid)) | |
226 | { | |
227 | server_connection* con=ss.get_connection(cid); | |
228 | ||
229 | if (data=="QUIT") | |
230 | break; | |
231 | ||
232 | if (data=="ABC") | |
233 | con->write("DEF"); | |
234 | else | |
235 | con->write("xyz"); | |
236 | } | |
237 | } | |
238 | // don't call atexit and stuff | |
239 | _exit(0); | |
240 | } | |
241 | ||
242 | default: | |
243 | // parent | |
244 | { | |
245 | string data; | |
246 | ||
247 | // wait till server is up | |
248 | sleep(1); | |
b604df5f | 249 | socket_client_connection sc(6666); |
7087e187 GE |
250 | sc.write("ABC"); |
251 | ||
252 | sc.fill_buffer(1000000); | |
253 | sc.get_packet(data); | |
254 | ||
255 | CPPUNIT_ASSERT_EQUAL(string("DEF"),data); | |
256 | ||
257 | sc.write("HAHA"); | |
258 | ||
259 | sc.fill_buffer(1000000); | |
260 | sc.get_packet(data); | |
261 | ||
262 | CPPUNIT_ASSERT_EQUAL(string("xyz"),data); | |
263 | ||
264 | sc.write("QUIT"); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
07e98688 GE |
269 | }; |
270 | ||
271 | CPPUNIT_TEST_SUITE_REGISTRATION(test_comm); |