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); |
07e98688 GE |
36 | |
37 | CPPUNIT_TEST_SUITE_END(); | |
38 | ||
39 | public: | |
40 | ||
41 | void setUp() | |
42 | { } | |
43 | ||
44 | void tearDown() | |
45 | { } | |
46 | ||
47 | void UnixCommToServer() | |
48 | { | |
49 | pid_t pid; | |
50 | string data; | |
51 | ||
52 | switch(pid=fork()) | |
53 | { | |
54 | case -1: | |
55 | { | |
56 | CPPUNIT_FAIL("fork error"); | |
57 | break; | |
58 | } | |
59 | case 0: | |
60 | // child | |
61 | { | |
62 | // wait till server is up | |
63 | sleep(1); | |
64 | socket_client_connection sc("./socket"); | |
65 | sc.write("hello"); | |
66 | // don't call atexit and stuff | |
67 | _exit(0); | |
68 | } | |
69 | ||
70 | default: | |
71 | // parent | |
72 | { | |
73 | socket_server ss("./socket"); | |
74 | ||
75 | // max 10 sec | |
76 | for (int i=0; i < 10; i++) | |
77 | { | |
78 | ss.fill_buffer(1000000); | |
79 | ||
80 | if(ss.get_packet(data)) | |
81 | break; | |
82 | } | |
83 | } | |
84 | } | |
85 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); | |
86 | } | |
87 | ||
88 | void UnixCommToServerAndBack() | |
89 | { | |
90 | pid_t pid; | |
91 | ||
92 | switch(pid=fork()) | |
93 | { | |
94 | case -1: | |
95 | { | |
96 | CPPUNIT_FAIL("fork error"); | |
97 | break; | |
98 | } | |
99 | case 0: | |
100 | // child | |
101 | { | |
102 | socket_server ss("./socket"); | |
103 | ss.set_logging(&cerr,debug); | |
104 | ||
105 | // max 10 sec | |
106 | for (int i=0; i < 10; i++) | |
107 | { | |
108 | ss.fill_buffer(1000000); | |
109 | ||
110 | string data; | |
111 | unsigned int cid; | |
112 | ||
113 | if(ss.get_packet(data,cid)) | |
114 | { | |
115 | server_connection* con=ss.get_connection(cid); | |
116 | ||
117 | if (data=="QUIT") | |
118 | break; | |
119 | ||
120 | if (data=="ABC") | |
121 | con->write("DEF"); | |
122 | else | |
123 | con->write("xyz"); | |
124 | } | |
125 | } | |
126 | // don't call atexit and stuff | |
127 | _exit(0); | |
128 | } | |
129 | ||
130 | default: | |
131 | // parent | |
132 | { | |
133 | string data; | |
134 | ||
135 | // wait till server is up | |
136 | sleep(1); | |
137 | socket_client_connection sc("./socket"); | |
138 | ||
139 | sc.write("ABC"); | |
140 | ||
141 | sc.fill_buffer(1000000); | |
142 | sc.get_packet(data); | |
143 | ||
144 | CPPUNIT_ASSERT_EQUAL(string("DEF"),data); | |
145 | ||
146 | sc.write("HAHA"); | |
147 | ||
148 | sc.fill_buffer(1000000); | |
149 | sc.get_packet(data); | |
150 | ||
151 | CPPUNIT_ASSERT_EQUAL(string("xyz"),data); | |
152 | ||
153 | sc.write("QUIT"); | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
cc68aabb GE |
158 | void IPCommToServer() |
159 | { | |
160 | pid_t pid; | |
161 | string data; | |
162 | ||
163 | switch(pid=fork()) | |
164 | { | |
165 | case -1: | |
166 | { | |
167 | CPPUNIT_FAIL("fork error"); | |
168 | break; | |
169 | } | |
170 | case 0: | |
171 | // child | |
172 | { | |
173 | // wait till server is up | |
174 | sleep(1); | |
175 | socket_client_connection sc("localhost",6666,socket_client_connection::max_retries_default); | |
176 | sc.write("hello"); | |
177 | // don't call atexit and stuff | |
178 | _exit(0); | |
179 | } | |
180 | ||
181 | default: | |
182 | // parent | |
183 | { | |
184 | socket_server ss(6666); | |
185 | ||
186 | // max 10 sec | |
187 | for (int i=0; i < 10; i++) | |
188 | { | |
189 | ss.fill_buffer(1000000); | |
190 | ||
191 | if(ss.get_packet(data)) | |
192 | break; | |
193 | } | |
194 | } | |
195 | } | |
196 | CPPUNIT_ASSERT_EQUAL(string("hello"),data); | |
197 | } | |
198 | ||
07e98688 GE |
199 | }; |
200 | ||
201 | CPPUNIT_TEST_SUITE_REGISTRATION(test_comm); |