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