libt2n: (gerd) fixes, new logging concept (not working yet)
[libt2n] / test / simplecmd.cpp
CommitLineData
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
23#include <boost/archive/binary_oarchive.hpp>
24#include <boost/archive/binary_iarchive.hpp>
25#include <boost/serialization/serialization.hpp>
26
d184c648
GE
27#include <container.hxx>
28#include <socket_client.hxx>
29#include <socket_server.hxx>
30#include <command_client.hxx>
31#include <command_server.hxx>
32
d184c648
GE
33using namespace std;
34using namespace libt2n;
35using namespace CppUnit;
36
37string testfunc(const string& str)
38{
39 string ret(str);
40 ret+=", testfunc() was here";
41 return ret;
42}
43
44class testfunc_res : public result
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 {
53 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(result);
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
73class 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
95 result* operator()()
96 {
97 return new testfunc_res(testfunc(param));
98 }
99};
100
a7170401 101#include <boost/serialization/export.hpp>
d184c648
GE
102
103BOOST_CLASS_EXPORT(testfunc_cmd)
104BOOST_CLASS_EXPORT(testfunc_res)
105
106class test_simplecmd : public TestFixture
107{
108 CPPUNIT_TEST_SUITE(test_simplecmd);
109
110 CPPUNIT_TEST(SimpleCmd);
111
112 CPPUNIT_TEST_SUITE_END();
113
114 public:
115
116 void setUp()
117 {
118}
119
120 void tearDown()
121 { }
122
123 void SimpleCmd()
124 {
125 pid_t pid;
126
127 switch(pid=fork())
128 {
129 case -1:
130 {
131 CPPUNIT_FAIL("fork error");
132 break;
133 }
134 case 0:
135 // child
136 {
137 socket_server ss("./socket");
d184c648
GE
138 command_server cs(ss);
139
140 // max 10 sec
141 for (int i=0; i < 10; i++)
142 cs.handle(1000000);
143
144 // don't call atexit and stuff
145 _exit(0);
146 }
147
148 default:
149 // parent
150 {
151 // wait till server is up
152 sleep(1);
153 socket_client_connection sc("./socket");
a7170401 154 sc.set_logging(&cerr,debug);
d184c648
GE
155 command_client cc(sc);
156
157 result_container rc;
158 cc.send_command(new testfunc_cmd("hello"),rc);
159
160 string ret=dynamic_cast<testfunc_res*>(rc.get_result())->get_data();
161
162 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
163 }
164 }
165 }
166
167
168};
169
170CPPUNIT_TEST_SUITE_REGISTRATION(test_simplecmd);