blob: 77daf3d37fae55ebd16e2927afb564b322eb07ef [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Roger Meier213a6642010-10-27 12:30:11 +000019
James E. King, III82ae9572017-08-05 12:23:54 -040020/* test a C client with a C++ server (that makes sense...) */
Roger Meier213a6642010-10-27 12:30:11 +000021
Christopher Friedt9c0de2d2022-11-24 20:13:21 -050022#include <thrift/config.h>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27
Roger Meier213a6642010-10-27 12:30:11 +000028#include <signal.h>
29#include <sys/types.h>
30#include <sys/wait.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000031#include <thrift/protocol/TBinaryProtocol.h>
32#include <thrift/protocol/TDebugProtocol.h>
33#include <thrift/server/TSimpleServer.h>
cyy316723a2019-01-05 16:35:14 +080034#include <memory>
Roger Meier49ff8b12012-04-13 09:12:31 +000035#include <thrift/transport/TServerSocket.h>
Roger Meier213a6642010-10-27 12:30:11 +000036#include "ThriftTest.h"
37#include "ThriftTest_types.h"
38
39#include <iostream>
James E. King, III82ae9572017-08-05 12:23:54 -040040#include <map>
41#include <set>
42#include <string>
43#include <vector>
Roger Meier213a6642010-10-27 12:30:11 +000044
45using namespace apache::thrift;
46using namespace apache::thrift::concurrency;
47using namespace apache::thrift::protocol;
Roger Meier213a6642010-10-27 12:30:11 +000048using namespace apache::thrift::server;
James E. King, III82ae9572017-08-05 12:23:54 -040049using namespace apache::thrift::transport;
Roger Meier213a6642010-10-27 12:30:11 +000050
51using namespace thrift::test;
52
James E. King, III82ae9572017-08-05 12:23:54 -040053using std::cout;
54using std::endl;
55using std::fixed;
56using std::make_pair;
57using std::map;
58using std::set;
59using std::string;
60using std::vector;
61
Roger Meier213a6642010-10-27 12:30:11 +000062#define TEST_PORT 9980
63
64// Extra functions required for ThriftTest_types to work
65namespace thrift { namespace test {
66
67bool Insanity::operator<(thrift::test::Insanity const& other) const {
68 using apache::thrift::ThriftDebugString;
69 return ThriftDebugString(*this) < ThriftDebugString(other);
70}
71
72}}
73
74class TestHandler : public ThriftTestIf {
75 public:
Sebastian Zenker042580f2019-01-29 15:48:12 +010076 TestHandler() = default;
Roger Meier213a6642010-10-27 12:30:11 +000077
Sebastian Zenker042580f2019-01-29 15:48:12 +010078 void testVoid() override {
Simon Southbf8f7b42015-12-23 20:29:29 -050079 cout << "[C -> C++] testVoid()" << endl;
Roger Meier213a6642010-10-27 12:30:11 +000080 }
81
Sebastian Zenker042580f2019-01-29 15:48:12 +010082 void testString(string& out, const string &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -050083 cout << "[C -> C++] testString(\"" << thing << "\")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +000084 out = thing;
85 }
86
Sebastian Zenker042580f2019-01-29 15:48:12 +010087 bool testBool(const bool thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -050088 cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << endl;
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090089 return thing;
90 }
Sebastian Zenker042580f2019-01-29 15:48:12 +010091 int8_t testByte(const int8_t thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -050092 cout << "[C -> C++] testByte(" << (int)thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +000093 return thing;
94 }
Sebastian Zenker042580f2019-01-29 15:48:12 +010095 int32_t testI32(const int32_t thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -050096 cout << "[C -> C++] testI32(" << thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +000097 return thing;
98 }
99
Sebastian Zenker042580f2019-01-29 15:48:12 +0100100 int64_t testI64(const int64_t thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500101 cout << "[C -> C++] testI64(" << thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000102 return thing;
103 }
104
Sebastian Zenker042580f2019-01-29 15:48:12 +0100105 double testDouble(const double thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500106 cout.precision(6);
107 cout << "[C -> C++] testDouble(" << fixed << thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000108 return thing;
109 }
110
Sebastian Zenker042580f2019-01-29 15:48:12 +0100111 void testBinary(string& out, const string &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500112 cout << "[C -> C++] testBinary(\"" << thing << "\")" << endl;
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100113 out = thing;
114 }
115
Sebastian Zenker042580f2019-01-29 15:48:12 +0100116 void testStruct(Xtruct& out, const Xtruct &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500117 cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000118 out = thing;
119 }
120
Sebastian Zenker042580f2019-01-29 15:48:12 +0100121 void testNest(Xtruct2& out, const Xtruct2& nest) override {
Roger Meier213a6642010-10-27 12:30:11 +0000122 const Xtruct &thing = nest.struct_thing;
Simon Southbf8f7b42015-12-23 20:29:29 -0500123 cout << "[C -> C++] testNest({" << (int)nest.byte_thing << ", {\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "}, " << nest.i32_thing << "})" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000124 out = nest;
125 }
126
Sebastian Zenker042580f2019-01-29 15:48:12 +0100127 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500128 cout << "[C -> C++] testMap({";
Roger Meier213a6642010-10-27 12:30:11 +0000129 map<int32_t, int32_t>::const_iterator m_iter;
130 bool first = true;
131 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
132 if (first) {
133 first = false;
134 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500135 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000136 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500137 cout << m_iter->first << " => " << m_iter->second;
Roger Meier213a6642010-10-27 12:30:11 +0000138 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500139 cout << "})" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000140 out = thing;
141 }
142
Sebastian Zenker042580f2019-01-29 15:48:12 +0100143 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500144 cout << "[C -> C++] testStringMap({";
Roger Meiera1c416f2011-06-17 19:40:48 +0000145 map<std::string, std::string>::const_iterator m_iter;
146 bool first = true;
147 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
148 if (first) {
149 first = false;
150 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500151 cout << ", ";
Roger Meiera1c416f2011-06-17 19:40:48 +0000152 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500153 cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\"";
Roger Meiera1c416f2011-06-17 19:40:48 +0000154 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500155 cout << "})" << endl;
Roger Meiera1c416f2011-06-17 19:40:48 +0000156 out = thing;
157 }
158
159
Sebastian Zenker042580f2019-01-29 15:48:12 +0100160 void testSet(set<int32_t> &out, const set<int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500161 cout << "[C -> C++] testSet({";
Roger Meier213a6642010-10-27 12:30:11 +0000162 set<int32_t>::const_iterator s_iter;
163 bool first = true;
164 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
165 if (first) {
166 first = false;
167 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500168 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000169 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500170 cout << *s_iter;
Roger Meier213a6642010-10-27 12:30:11 +0000171 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500172 cout << "})" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000173 out = thing;
174 }
175
Sebastian Zenker042580f2019-01-29 15:48:12 +0100176 void testList(vector<int32_t> &out, const vector<int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500177 cout << "[C -> C++] testList({";
Roger Meier213a6642010-10-27 12:30:11 +0000178 vector<int32_t>::const_iterator l_iter;
179 bool first = true;
180 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
181 if (first) {
182 first = false;
Simon Southbf8f7b42015-12-23 20:29:29 -0500183 } else {
184 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000185 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500186 cout << *l_iter;
Roger Meier213a6642010-10-27 12:30:11 +0000187 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500188 cout << "})" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000189 out = thing;
190 }
191
Sebastian Zenker042580f2019-01-29 15:48:12 +0100192 Numberz::type testEnum(const Numberz::type thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500193 cout << "[C -> C++] testEnum(" << thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000194 return thing;
195 }
196
Sebastian Zenker042580f2019-01-29 15:48:12 +0100197 UserId testTypedef(const UserId thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500198 cout << "[C -> C++] testTypedef(" << thing << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000199 return thing; }
200
Sebastian Zenker042580f2019-01-29 15:48:12 +0100201 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500202 cout << "[C -> C++] testMapMap(" << hello << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000203
204 map<int32_t,int32_t> pos;
205 map<int32_t,int32_t> neg;
206 for (int i = 1; i < 5; i++) {
207 pos.insert(make_pair(i,i));
208 neg.insert(make_pair(-i,-i));
209 }
210
211 mapmap.insert(make_pair(4, pos));
212 mapmap.insert(make_pair(-4, neg));
213
214 }
215
Sebastian Zenker042580f2019-01-29 15:48:12 +0100216 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override {
Simon South38e71552015-08-03 10:51:16 +0000217 THRIFT_UNUSED_VARIABLE (argument);
218
Simon Southbf8f7b42015-12-23 20:29:29 -0500219 cout << "[C -> C++] testInsanity()" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000220
221 Xtruct hello;
222 hello.string_thing = "Hello2";
223 hello.byte_thing = 2;
224 hello.i32_thing = 2;
225 hello.i64_thing = 2;
226
227 Xtruct goodbye;
228 goodbye.string_thing = "Goodbye4";
229 goodbye.byte_thing = 4;
230 goodbye.i32_thing = 4;
231 goodbye.i64_thing = 4;
232
233 Insanity crazy;
234 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
235 crazy.xtructs.push_back(goodbye);
236
237 Insanity looney;
238 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
239 crazy.xtructs.push_back(hello);
240
241 map<Numberz::type, Insanity> first_map;
242 map<Numberz::type, Insanity> second_map;
243
244 first_map.insert(make_pair(Numberz::TWO, crazy));
245 first_map.insert(make_pair(Numberz::THREE, crazy));
246
247 second_map.insert(make_pair(Numberz::SIX, looney));
248
249 insane.insert(make_pair(1, first_map));
250 insane.insert(make_pair(2, second_map));
251
Simon Southbf8f7b42015-12-23 20:29:29 -0500252 cout << "return = {";
Roger Meier213a6642010-10-27 12:30:11 +0000253 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
254 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500255 cout << i_iter->first << " => {";
Roger Meier213a6642010-10-27 12:30:11 +0000256 map<Numberz::type,Insanity>::const_iterator i2_iter;
257 for (i2_iter = i_iter->second.begin();
258 i2_iter != i_iter->second.end();
259 ++i2_iter) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500260 cout << i2_iter->first << " => {";
Roger Meier213a6642010-10-27 12:30:11 +0000261 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
262 map<Numberz::type, UserId>::const_iterator um;
Simon Southbf8f7b42015-12-23 20:29:29 -0500263 cout << "{";
Roger Meier213a6642010-10-27 12:30:11 +0000264 for (um = userMap.begin(); um != userMap.end(); ++um) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500265 cout << um->first << " => " << um->second << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000266 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500267 cout << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000268
269 vector<Xtruct> xtructs = i2_iter->second.xtructs;
270 vector<Xtruct>::const_iterator x;
Simon Southbf8f7b42015-12-23 20:29:29 -0500271 cout << "{";
Roger Meier213a6642010-10-27 12:30:11 +0000272 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500273 cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000274 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500275 cout << "}";
Roger Meier213a6642010-10-27 12:30:11 +0000276
Simon Southbf8f7b42015-12-23 20:29:29 -0500277 cout << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000278 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500279 cout << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000280 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500281 cout << "}" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000282
283
284 }
285
Sebastian Zenker042580f2019-01-29 15:48:12 +0100286 void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz::type arg4, const UserId arg5) override {
Simon South38e71552015-08-03 10:51:16 +0000287 THRIFT_UNUSED_VARIABLE (arg3);
288 THRIFT_UNUSED_VARIABLE (arg4);
289 THRIFT_UNUSED_VARIABLE (arg5);
290
Simon Southbf8f7b42015-12-23 20:29:29 -0500291 cout << "[C -> C++] testMulti()" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000292
293 hello.string_thing = "Hello2";
294 hello.byte_thing = arg0;
295 hello.i32_thing = arg1;
296 hello.i64_thing = (int64_t)arg2;
297 }
298
299 void testException(const std::string &arg)
zeshuai007c821d252020-08-14 15:44:02 +0800300 noexcept(false) override
Roger Meier213a6642010-10-27 12:30:11 +0000301 {
Simon Southbf8f7b42015-12-23 20:29:29 -0500302 cout << "[C -> C++] testException(" << arg << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000303 if (arg.compare("Xception") == 0) {
304 Xception e;
305 e.errorCode = 1001;
306 e.message = arg;
307 throw e;
308 } else if (arg.compare("ApplicationException") == 0) {
309 apache::thrift::TException e;
310 throw e;
311 } else {
312 Xtruct result;
313 result.string_thing = arg;
314 return;
315 }
316 }
317
zeshuai007c821d252020-08-14 15:44:02 +0800318 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) noexcept(false) override {
Roger Meier213a6642010-10-27 12:30:11 +0000319
Simon Southbf8f7b42015-12-23 20:29:29 -0500320 cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000321
322 if (arg0.compare("Xception") == 0) {
323 Xception e;
324 e.errorCode = 1001;
325 e.message = "This is an Xception";
326 throw e;
327 } else if (arg0.compare("Xception2") == 0) {
328 Xception2 e;
329 e.errorCode = 2002;
330 e.struct_thing.string_thing = "This is an Xception2";
331 throw e;
332 } else {
333 result.string_thing = arg1;
334 return;
335 }
336 }
337
Sebastian Zenker042580f2019-01-29 15:48:12 +0100338 void testOneway(int sleepFor) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500339 cout << "testOneway(" << sleepFor << "): Sleeping..." << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000340 sleep(sleepFor);
Simon Southbf8f7b42015-12-23 20:29:29 -0500341 cout << "testOneway(" << sleepFor << "): done sleeping!" << endl;
Roger Meier213a6642010-10-27 12:30:11 +0000342 }
343};
344
345// C CLIENT
346extern "C" {
347
Jim King79c99112015-04-30 07:10:08 -0400348#undef THRIFT_SOCKET /* from lib/cpp */
349
Roger Meier213a6642010-10-27 12:30:11 +0000350#include "t_test_thrift_test.h"
351#include "t_test_thrift_test_types.h"
Roger Meiere3da7682013-01-11 11:41:53 +0100352#include <thrift/c_glib/transport/thrift_socket.h>
353#include <thrift/c_glib/protocol/thrift_protocol.h>
354#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Roger Meier213a6642010-10-27 12:30:11 +0000355
356static void
357test_thrift_client (void)
358{
Sebastian Zenker042580f2019-01-29 15:48:12 +0100359 ThriftSocket *tsocket = nullptr;
360 ThriftBinaryProtocol *protocol = nullptr;
361 TTestThriftTestClient *client = nullptr;
362 TTestThriftTestIf *iface = nullptr;
363 GError *error = nullptr;
364 gchar *string = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000365 gint8 byte = 0;
366 gint16 i16 = 0;
James E. King, III82ae9572017-08-05 12:23:54 -0400367 gint32 i32 = 0, another_i32 = 56789;
Roger Meier213a6642010-10-27 12:30:11 +0000368 gint64 i64 = 0;
369 double dbl = 0.0;
370 TTestXtruct *xtruct_in, *xtruct_out;
371 TTestXtruct2 *xtruct2_in, *xtruct2_out;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100372 GHashTable *map_in = nullptr, *map_out = nullptr;
373 GHashTable *set_in = nullptr, *set_out = nullptr;
374 GArray *list_in = nullptr, *list_out = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000375 TTestNumberz enum_in, enum_out;
James E. King, III82ae9572017-08-05 12:23:54 -0400376 TTestUserId user_id_in, user_id_out;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100377 GHashTable *insanity_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000378 TTestXtruct *xtruct1, *xtruct2;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100379 TTestInsanity *insanity_out = nullptr;
380 TTestXtruct *multi_in = nullptr;
381 GHashTable *multi_map_out = nullptr;
382 TTestXception *xception = nullptr;
383 TTestXception2 *xception2 = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000384
Jens Geyer1c190272015-07-28 23:15:18 +0200385#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000386 // initialize gobject
387 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200388#endif
Roger Meier213a6642010-10-27 12:30:11 +0000389
390 // create a C client
James E. King, III82ae9572017-08-05 12:23:54 -0400391 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
Roger Meier213a6642010-10-27 12:30:11 +0000392 "hostname", "localhost",
zeshuai00726681fb2020-06-03 17:24:38 +0800393 "port", TEST_PORT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000394 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
395 "transport",
zeshuai00726681fb2020-06-03 17:24:38 +0800396 tsocket, nullptr);
397 client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000398 iface = T_TEST_THRIFT_TEST_IF (client);
399
400 // open and send
Sebastian Zenker042580f2019-01-29 15:48:12 +0100401 thrift_transport_open (THRIFT_TRANSPORT(tsocket), nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000402
403 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100404 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000405
406 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
407 assert (strcmp (string, "test123") == 0);
408 g_free (string);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100409 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000410
411 assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
412 assert (byte == 5);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100413 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000414
415 assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
416 assert (i32 == 123);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100417 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000418
419 assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
420 assert (i64 == 12345);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100421 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000422
423 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
424 assert (dbl == 5.6);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100425 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000426
Sebastian Zenker042580f2019-01-29 15:48:12 +0100427 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000428 xtruct_out->byte_thing = 1;
429 xtruct_out->__isset_byte_thing = TRUE;
430 xtruct_out->i32_thing = 15;
431 xtruct_out->__isset_i32_thing = TRUE;
432 xtruct_out->i64_thing = 151;
433 xtruct_out->__isset_i64_thing = TRUE;
434 xtruct_out->string_thing = g_strdup ("abc123");
435 xtruct_out->__isset_string_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100436 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000437 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100438 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000439
Sebastian Zenker042580f2019-01-29 15:48:12 +0100440 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000441 xtruct2_out->byte_thing = 1;
442 xtruct2_out->__isset_byte_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100443 if (xtruct2_out->struct_thing != nullptr)
Roger Meierc75797d2012-04-28 11:33:58 +0000444 g_object_unref(xtruct2_out->struct_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000445 xtruct2_out->struct_thing = xtruct_out;
446 xtruct2_out->__isset_struct_thing = TRUE;
447 xtruct2_out->i32_thing = 123;
448 xtruct2_out->__isset_i32_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100449 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000450 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100451 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000452
453 g_object_unref (xtruct2_out);
454 g_object_unref (xtruct2_in);
Roger Meier213a6642010-10-27 12:30:11 +0000455 g_object_unref (xtruct_in);
456
Sebastian Zenker042580f2019-01-29 15:48:12 +0100457 map_out = g_hash_table_new (nullptr, nullptr);
458 map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32);
Roger Meier213a6642010-10-27 12:30:11 +0000459 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100460 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000461 g_hash_table_destroy (map_out);
462 g_hash_table_destroy (map_in);
463
Sebastian Zenker042580f2019-01-29 15:48:12 +0100464 map_out = g_hash_table_new (nullptr, nullptr);
465 map_in = g_hash_table_new (nullptr, nullptr);
Roger Meiera1c416f2011-06-17 19:40:48 +0000466 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
467 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
468 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
469 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
470 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100471 assert (error == nullptr);
Roger Meiera1c416f2011-06-17 19:40:48 +0000472 g_hash_table_destroy (map_out);
473 g_hash_table_destroy (map_in);
474
Sebastian Zenker042580f2019-01-29 15:48:12 +0100475 set_out = g_hash_table_new (nullptr, nullptr);
476 set_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000477 g_hash_table_insert (set_out, &i32, &i32);
478 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100479 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000480 g_hash_table_destroy (set_out);
481 g_hash_table_destroy (set_in);
482
483 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
484 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
485 another_i32 = 456;
486 g_array_append_val (list_out, i32);
487 g_array_append_val (list_out, another_i32);
488 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100489 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000490 g_array_free (list_out, TRUE);
491 g_array_free (list_in, TRUE);
492
493 enum_out = T_TEST_NUMBERZ_ONE;
494 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
495 assert (enum_in == enum_out);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100496 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000497
498 user_id_out = 12345;
499 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
500 assert (user_id_in == user_id_out);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100501 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000502
Sebastian Zenker042580f2019-01-29 15:48:12 +0100503 map_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000504 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100505 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000506 g_hash_table_destroy (map_in);
507
508 // insanity
Sebastian Zenker042580f2019-01-29 15:48:12 +0100509 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr);
510 insanity_out->userMap = g_hash_table_new (nullptr, nullptr);
Roger Meier0cc6d3c2014-09-04 00:24:17 +0200511 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
Roger Meier213a6642010-10-27 12:30:11 +0000512
Sebastian Zenker042580f2019-01-29 15:48:12 +0100513 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000514 xtruct1->byte_thing = 1;
515 xtruct1->__isset_byte_thing = TRUE;
516 xtruct1->i32_thing = 15;
517 xtruct1->__isset_i32_thing = TRUE;
518 xtruct1->i64_thing = 151;
519 xtruct1->__isset_i64_thing = TRUE;
520 xtruct1->string_thing = g_strdup ("abc123");
521 xtruct1->__isset_string_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100522 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000523 xtruct2->byte_thing = 1;
524 xtruct2->__isset_byte_thing = TRUE;
525 xtruct2->i32_thing = 15;
526 xtruct2->__isset_i32_thing = TRUE;
527 xtruct2->i64_thing = 151;
528 xtruct2->__isset_i64_thing = TRUE;
529 xtruct2->string_thing = g_strdup ("abc123");
530 xtruct2->__isset_string_thing = TRUE;
531
Sebastian Zenker042580f2019-01-29 15:48:12 +0100532 insanity_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000533 g_ptr_array_add (insanity_out->xtructs, xtruct1);
534 g_ptr_array_add (insanity_out->xtructs, xtruct2);
535 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
536
537 g_hash_table_unref (insanity_in);
538 g_ptr_array_free (insanity_out->xtructs, TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000539
Sebastian Zenker042580f2019-01-29 15:48:12 +0100540 multi_map_out = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000541 string = g_strdup ("abc123");
542 g_hash_table_insert (multi_map_out, &i16, string);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100543 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000544 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
545 assert (multi_in->i32_thing == i32);
546 assert (multi_in->i64_thing == i64);
547 g_object_unref (multi_in);
548 g_hash_table_unref (multi_map_out);
Roger Meierc75797d2012-04-28 11:33:58 +0000549 g_free (string);
Roger Meier213a6642010-10-27 12:30:11 +0000550
551 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
552 assert (xception->errorCode == 1001);
553 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100554 error = nullptr;
Roger Meierc75797d2012-04-28 11:33:58 +0000555 g_object_unref (xception);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100556 xception = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000557
558 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
559 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100560 error = nullptr;
561 assert (xception == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000562
563 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100564 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000565
Sebastian Zenker042580f2019-01-29 15:48:12 +0100566 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
567 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", nullptr, &xception, &xception2, &error) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000568 assert (xception->errorCode == 1001);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100569 assert (xception2 == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000570 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100571 error = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000572 g_object_unref (xception);
Roger Meierc75797d2012-04-28 11:33:58 +0000573 g_object_unref (multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100574 xception = nullptr;
575 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000576
Sebastian Zenker042580f2019-01-29 15:48:12 +0100577 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
578 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", nullptr, &xception, &xception2, &error) == FALSE);
Roger Meier213a6642010-10-27 12:30:11 +0000579 assert (xception2->errorCode == 2002);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100580 assert (xception == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000581 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100582 error = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000583 g_object_unref (xception2);
Roger Meierc75797d2012-04-28 11:33:58 +0000584 g_object_unref (multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100585 xception2 = nullptr;
586 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000587
Sebastian Zenker042580f2019-01-29 15:48:12 +0100588 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
589 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE);
590 assert (error == nullptr);
Roger Meierc75797d2012-04-28 11:33:58 +0000591 g_object_unref(multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100592 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000593
594 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100595 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000596
597 /* sleep to let the oneway call go through */
598 sleep (5);
599
Sebastian Zenker042580f2019-01-29 15:48:12 +0100600 thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000601 g_object_unref (client);
602 g_object_unref (protocol);
603 g_object_unref (tsocket);
604}
605
606
607} /* extern "C" */
608
609
610static void
611bailout (int signum)
612{
Simon South38e71552015-08-03 10:51:16 +0000613 THRIFT_UNUSED_VARIABLE (signum);
614
Roger Meier213a6642010-10-27 12:30:11 +0000615 exit (1);
616}
617
618int
Simon South38e71552015-08-03 10:51:16 +0000619main (void)
Roger Meier213a6642010-10-27 12:30:11 +0000620{
621 int status;
622 int pid = fork ();
623 assert (pid >= 0);
624
625 if (pid == 0) /* child */
626 {
cyy316723a2019-01-05 16:35:14 +0800627 std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
628 std::shared_ptr<TestHandler> testHandler(new TestHandler());
629 std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
630 std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
631 std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Roger Meier213a6642010-10-27 12:30:11 +0000632 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
633 signal (SIGALRM, bailout);
634 alarm (60);
635 simpleServer.serve();
636 } else {
637 sleep (1);
638 test_thrift_client ();
639 kill (pid, SIGINT);
Simon South38e71552015-08-03 10:51:16 +0000640 assert (wait (&status) == pid);
Roger Meier213a6642010-10-27 12:30:11 +0000641 }
642
643 return 0;
644}
645