Commit | Line | Data |
---|---|---|
d184c648 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 | ||
a7170401 GE |
22 | #include <boost/archive/binary_oarchive.hpp> |
23 | #include <boost/archive/binary_iarchive.hpp> | |
d535333f GE |
24 | #include <boost/archive/xml_oarchive.hpp> |
25 | #include <boost/archive/xml_iarchive.hpp> | |
a7170401 GE |
26 | #include <boost/serialization/serialization.hpp> |
27 | ||
d184c648 GE |
28 | #include <container.hxx> |
29 | #include <socket_client.hxx> | |
30 | #include <socket_server.hxx> | |
31 | #include <command_client.hxx> | |
32 | #include <command_server.hxx> | |
33 | ||
d184c648 | 34 | using namespace std; |
d184c648 GE |
35 | using namespace CppUnit; |
36 | ||
37 | string testfunc(const string& str) | |
38 | { | |
39 | string ret(str); | |
40 | ret+=", testfunc() was here"; | |
41 | return ret; | |
42 | } | |
43 | ||
d535333f | 44 | class testfunc_res : public libt2n::result |
d184c648 GE |
45 | { |
46 | private: | |
47 | string res; | |
48 | ||
49 | friend class boost::serialization::access; | |
50 | template<class Archive> | |
51 | void serialize(Archive & ar, const unsigned int version) | |
52 | { | |
d535333f | 53 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result); |
d184c648 GE |
54 | ar & BOOST_SERIALIZATION_NVP(res); |
55 | } | |
56 | ||
57 | public: | |
58 | testfunc_res() | |
59 | { } | |
60 | ||
61 | testfunc_res(const string& str) | |
62 | { | |
63 | res=str; | |
64 | } | |
65 | ||
66 | string get_data() | |
67 | { | |
68 | return res; | |
69 | } | |
70 | }; | |
71 | ||
a7170401 | 72 | |
d184c648 GE |
73 | class testfunc_cmd : public libt2n::command |
74 | { | |
75 | private: | |
76 | string param; | |
77 | ||
78 | friend class boost::serialization::access; | |
79 | template<class Archive> | |
80 | void serialize(Archive & ar, const unsigned int version) | |
81 | { | |
d184c648 GE |
82 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command); |
83 | ar & BOOST_SERIALIZATION_NVP(param); | |
84 | } | |
85 | ||
86 | public: | |
87 | testfunc_cmd() | |
88 | { } | |
89 | ||
90 | testfunc_cmd(const string& str) | |
91 | { | |
92 | param=str; | |
93 | } | |
94 | ||
d535333f | 95 | libt2n::result* operator()() |
d184c648 GE |
96 | { |
97 | return new testfunc_res(testfunc(param)); | |
98 | } | |
99 | }; | |
100 | ||
a7170401 | 101 | #include <boost/serialization/export.hpp> |
d184c648 GE |
102 | |
103 | BOOST_CLASS_EXPORT(testfunc_cmd) | |
104 | BOOST_CLASS_EXPORT(testfunc_res) | |
105 | ||
d535333f GE |
106 | using namespace libt2n; |
107 | ||
d184c648 GE |
108 | class test_simplecmd : public TestFixture |
109 | { | |
110 | CPPUNIT_TEST_SUITE(test_simplecmd); | |
111 | ||
112 | CPPUNIT_TEST(SimpleCmd); | |
113 | ||
114 | CPPUNIT_TEST_SUITE_END(); | |
115 | ||
116 | public: | |
117 | ||
118 | void setUp() | |
119 | { | |
120 | } | |
121 | ||
122 | void tearDown() | |
123 | { } | |
124 | ||
125 | void SimpleCmd() | |
126 | { | |
127 | pid_t pid; | |
128 | ||
129 | switch(pid=fork()) | |
130 | { | |
131 | case -1: | |
132 | { | |
133 | CPPUNIT_FAIL("fork error"); | |
134 | break; | |
135 | } | |
136 | case 0: | |
137 | // child | |
138 | { | |
139 | socket_server ss("./socket"); | |
d184c648 GE |
140 | command_server cs(ss); |
141 | ||
142 | // max 10 sec | |
143 | for (int i=0; i < 10; i++) | |
144 | cs.handle(1000000); | |
145 | ||
146 | // don't call atexit and stuff | |
147 | _exit(0); | |
148 | } | |
149 | ||
150 | default: | |
151 | // parent | |
152 | { | |
153 | // wait till server is up | |
154 | sleep(1); | |
155 | socket_client_connection sc("./socket"); | |
a7170401 | 156 | sc.set_logging(&cerr,debug); |
d184c648 GE |
157 | command_client cc(sc); |
158 | ||
159 | result_container rc; | |
160 | cc.send_command(new testfunc_cmd("hello"),rc); | |
161 | ||
162 | string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data(); | |
163 | ||
164 | CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | ||
170 | }; | |
171 | ||
172 | CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd); |