blob: 77cd24a8f22159e9928f77d9ada77e5506faaf5c [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;
James E. King, III82ae9572017-08-05 12:23:54 -040054using std::fixed;
55using std::make_pair;
56using std::map;
57using std::set;
58using std::string;
59using std::vector;
60
Roger Meier213a6642010-10-27 12:30:11 +000061#define TEST_PORT 9980
62
63// Extra functions required for ThriftTest_types to work
64namespace thrift { namespace test {
65
66bool Insanity::operator<(thrift::test::Insanity const& other) const {
67 using apache::thrift::ThriftDebugString;
68 return ThriftDebugString(*this) < ThriftDebugString(other);
69}
70
71}}
72
73class TestHandler : public ThriftTestIf {
74 public:
Sebastian Zenker042580f2019-01-29 15:48:12 +010075 TestHandler() = default;
Roger Meier213a6642010-10-27 12:30:11 +000076
Sebastian Zenker042580f2019-01-29 15:48:12 +010077 void testVoid() override {
CJCombrink4a280d52024-03-14 19:57:41 +010078 cout << "[C -> C++] testVoid()" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +000079 }
80
Sebastian Zenker042580f2019-01-29 15:48:12 +010081 void testString(string& out, const string &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010082 cout << "[C -> C++] testString(\"" << thing << "\")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +000083 out = thing;
84 }
85
Sebastian Zenker042580f2019-01-29 15:48:12 +010086 bool testBool(const bool thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010087 cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << '\n';
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090088 return thing;
89 }
Sebastian Zenker042580f2019-01-29 15:48:12 +010090 int8_t testByte(const int8_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010091 cout << "[C -> C++] testByte(" << (int)thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +000092 return thing;
93 }
Sebastian Zenker042580f2019-01-29 15:48:12 +010094 int32_t testI32(const int32_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +010095 cout << "[C -> C++] testI32(" << thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +000096 return thing;
97 }
98
Sebastian Zenker042580f2019-01-29 15:48:12 +010099 int64_t testI64(const int64_t thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100100 cout << "[C -> C++] testI64(" << thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000101 return thing;
102 }
103
Sebastian Zenker042580f2019-01-29 15:48:12 +0100104 double testDouble(const double thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500105 cout.precision(6);
CJCombrink4a280d52024-03-14 19:57:41 +0100106 cout << "[C -> C++] testDouble(" << fixed << thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000107 return thing;
108 }
109
Sebastian Zenker042580f2019-01-29 15:48:12 +0100110 void testBinary(string& out, const string &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100111 cout << "[C -> C++] testBinary(\"" << thing << "\")" << '\n';
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100112 out = thing;
113 }
114
CJCombrink4b909092024-04-27 19:51:39 +0200115 std::string testUuid(const std::string thing) override {
116 cout << "[C -> C++] testUuid(\"" << std::hex << thing << "\")" << '\n';
117 return thing;
118 }
119
Sebastian Zenker042580f2019-01-29 15:48:12 +0100120 void testStruct(Xtruct& out, const Xtruct &thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100121 cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000122 out = thing;
123 }
124
Sebastian Zenker042580f2019-01-29 15:48:12 +0100125 void testNest(Xtruct2& out, const Xtruct2& nest) override {
Roger Meier213a6642010-10-27 12:30:11 +0000126 const Xtruct &thing = nest.struct_thing;
CJCombrink4a280d52024-03-14 19:57:41 +0100127 cout << "[C -> C++] testNest({" << (int)nest.byte_thing << ", {\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "}, " << nest.i32_thing << "})" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000128 out = nest;
129 }
130
Sebastian Zenker042580f2019-01-29 15:48:12 +0100131 void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500132 cout << "[C -> C++] testMap({";
Roger Meier213a6642010-10-27 12:30:11 +0000133 map<int32_t, int32_t>::const_iterator m_iter;
134 bool first = true;
135 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
136 if (first) {
137 first = false;
138 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500139 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000140 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500141 cout << m_iter->first << " => " << m_iter->second;
Roger Meier213a6642010-10-27 12:30:11 +0000142 }
CJCombrink4a280d52024-03-14 19:57:41 +0100143 cout << "})" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000144 out = thing;
145 }
146
Sebastian Zenker042580f2019-01-29 15:48:12 +0100147 void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500148 cout << "[C -> C++] testStringMap({";
Roger Meiera1c416f2011-06-17 19:40:48 +0000149 map<std::string, std::string>::const_iterator m_iter;
150 bool first = true;
151 for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) {
152 if (first) {
153 first = false;
154 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500155 cout << ", ";
Roger Meiera1c416f2011-06-17 19:40:48 +0000156 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500157 cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\"";
Roger Meiera1c416f2011-06-17 19:40:48 +0000158 }
CJCombrink4a280d52024-03-14 19:57:41 +0100159 cout << "})" << '\n';
Roger Meiera1c416f2011-06-17 19:40:48 +0000160 out = thing;
161 }
162
163
Sebastian Zenker042580f2019-01-29 15:48:12 +0100164 void testSet(set<int32_t> &out, const set<int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500165 cout << "[C -> C++] testSet({";
Roger Meier213a6642010-10-27 12:30:11 +0000166 set<int32_t>::const_iterator s_iter;
167 bool first = true;
168 for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) {
169 if (first) {
170 first = false;
171 } else {
Simon Southbf8f7b42015-12-23 20:29:29 -0500172 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000173 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500174 cout << *s_iter;
Roger Meier213a6642010-10-27 12:30:11 +0000175 }
CJCombrink4a280d52024-03-14 19:57:41 +0100176 cout << "})" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000177 out = thing;
178 }
179
Sebastian Zenker042580f2019-01-29 15:48:12 +0100180 void testList(vector<int32_t> &out, const vector<int32_t> &thing) override {
Simon Southbf8f7b42015-12-23 20:29:29 -0500181 cout << "[C -> C++] testList({";
Roger Meier213a6642010-10-27 12:30:11 +0000182 vector<int32_t>::const_iterator l_iter;
183 bool first = true;
184 for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) {
185 if (first) {
186 first = false;
Simon Southbf8f7b42015-12-23 20:29:29 -0500187 } else {
188 cout << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000189 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500190 cout << *l_iter;
Roger Meier213a6642010-10-27 12:30:11 +0000191 }
CJCombrink4a280d52024-03-14 19:57:41 +0100192 cout << "})" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000193 out = thing;
194 }
195
Sebastian Zenker042580f2019-01-29 15:48:12 +0100196 Numberz::type testEnum(const Numberz::type thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100197 cout << "[C -> C++] testEnum(" << thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000198 return thing;
199 }
200
Sebastian Zenker042580f2019-01-29 15:48:12 +0100201 UserId testTypedef(const UserId thing) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100202 cout << "[C -> C++] testTypedef(" << thing << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000203 return thing; }
204
Sebastian Zenker042580f2019-01-29 15:48:12 +0100205 void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100206 cout << "[C -> C++] testMapMap(" << hello << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000207
208 map<int32_t,int32_t> pos;
209 map<int32_t,int32_t> neg;
210 for (int i = 1; i < 5; i++) {
211 pos.insert(make_pair(i,i));
212 neg.insert(make_pair(-i,-i));
213 }
214
215 mapmap.insert(make_pair(4, pos));
216 mapmap.insert(make_pair(-4, neg));
217
218 }
219
Sebastian Zenker042580f2019-01-29 15:48:12 +0100220 void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override {
Simon South38e71552015-08-03 10:51:16 +0000221 THRIFT_UNUSED_VARIABLE (argument);
222
CJCombrink4a280d52024-03-14 19:57:41 +0100223 cout << "[C -> C++] testInsanity()" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000224
225 Xtruct hello;
226 hello.string_thing = "Hello2";
227 hello.byte_thing = 2;
228 hello.i32_thing = 2;
229 hello.i64_thing = 2;
230
231 Xtruct goodbye;
232 goodbye.string_thing = "Goodbye4";
233 goodbye.byte_thing = 4;
234 goodbye.i32_thing = 4;
235 goodbye.i64_thing = 4;
236
237 Insanity crazy;
238 crazy.userMap.insert(make_pair(Numberz::EIGHT, 8));
239 crazy.xtructs.push_back(goodbye);
240
241 Insanity looney;
242 crazy.userMap.insert(make_pair(Numberz::FIVE, 5));
243 crazy.xtructs.push_back(hello);
244
245 map<Numberz::type, Insanity> first_map;
246 map<Numberz::type, Insanity> second_map;
247
248 first_map.insert(make_pair(Numberz::TWO, crazy));
249 first_map.insert(make_pair(Numberz::THREE, crazy));
250
251 second_map.insert(make_pair(Numberz::SIX, looney));
252
253 insane.insert(make_pair(1, first_map));
254 insane.insert(make_pair(2, second_map));
255
Simon Southbf8f7b42015-12-23 20:29:29 -0500256 cout << "return = {";
Roger Meier213a6642010-10-27 12:30:11 +0000257 map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
258 for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500259 cout << i_iter->first << " => {";
Roger Meier213a6642010-10-27 12:30:11 +0000260 map<Numberz::type,Insanity>::const_iterator i2_iter;
261 for (i2_iter = i_iter->second.begin();
262 i2_iter != i_iter->second.end();
263 ++i2_iter) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500264 cout << i2_iter->first << " => {";
Roger Meier213a6642010-10-27 12:30:11 +0000265 map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
266 map<Numberz::type, UserId>::const_iterator um;
Simon Southbf8f7b42015-12-23 20:29:29 -0500267 cout << "{";
Roger Meier213a6642010-10-27 12:30:11 +0000268 for (um = userMap.begin(); um != userMap.end(); ++um) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500269 cout << um->first << " => " << um->second << ", ";
Roger Meier213a6642010-10-27 12:30:11 +0000270 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500271 cout << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000272
273 vector<Xtruct> xtructs = i2_iter->second.xtructs;
274 vector<Xtruct>::const_iterator x;
Simon Southbf8f7b42015-12-23 20:29:29 -0500275 cout << "{";
Roger Meier213a6642010-10-27 12:30:11 +0000276 for (x = xtructs.begin(); x != xtructs.end(); ++x) {
Simon Southbf8f7b42015-12-23 20:29:29 -0500277 cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, ";
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 << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000282 }
Simon Southbf8f7b42015-12-23 20:29:29 -0500283 cout << "}, ";
Roger Meier213a6642010-10-27 12:30:11 +0000284 }
CJCombrink4a280d52024-03-14 19:57:41 +0100285 cout << "}" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000286
287
288 }
289
Sebastian Zenker042580f2019-01-29 15:48:12 +0100290 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 +0000291 THRIFT_UNUSED_VARIABLE (arg3);
292 THRIFT_UNUSED_VARIABLE (arg4);
293 THRIFT_UNUSED_VARIABLE (arg5);
294
CJCombrink4a280d52024-03-14 19:57:41 +0100295 cout << "[C -> C++] testMulti()" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000296
297 hello.string_thing = "Hello2";
298 hello.byte_thing = arg0;
299 hello.i32_thing = arg1;
300 hello.i64_thing = (int64_t)arg2;
301 }
302
303 void testException(const std::string &arg)
zeshuai007c821d252020-08-14 15:44:02 +0800304 noexcept(false) override
Roger Meier213a6642010-10-27 12:30:11 +0000305 {
CJCombrink4a280d52024-03-14 19:57:41 +0100306 cout << "[C -> C++] testException(" << arg << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000307 if (arg.compare("Xception") == 0) {
308 Xception e;
309 e.errorCode = 1001;
310 e.message = arg;
311 throw e;
312 } else if (arg.compare("ApplicationException") == 0) {
313 apache::thrift::TException e;
314 throw e;
315 } else {
316 Xtruct result;
317 result.string_thing = arg;
318 return;
319 }
320 }
321
zeshuai007c821d252020-08-14 15:44:02 +0800322 void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) noexcept(false) override {
Roger Meier213a6642010-10-27 12:30:11 +0000323
CJCombrink4a280d52024-03-14 19:57:41 +0100324 cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000325
326 if (arg0.compare("Xception") == 0) {
327 Xception e;
328 e.errorCode = 1001;
329 e.message = "This is an Xception";
330 throw e;
331 } else if (arg0.compare("Xception2") == 0) {
332 Xception2 e;
333 e.errorCode = 2002;
334 e.struct_thing.string_thing = "This is an Xception2";
335 throw e;
336 } else {
337 result.string_thing = arg1;
338 return;
339 }
340 }
341
Sebastian Zenker042580f2019-01-29 15:48:12 +0100342 void testOneway(int sleepFor) override {
CJCombrink4a280d52024-03-14 19:57:41 +0100343 cout << "testOneway(" << sleepFor << "): Sleeping..." << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000344 sleep(sleepFor);
CJCombrink4a280d52024-03-14 19:57:41 +0100345 cout << "testOneway(" << sleepFor << "): done sleeping!" << '\n';
Roger Meier213a6642010-10-27 12:30:11 +0000346 }
347};
348
349// C CLIENT
350extern "C" {
351
Jim King79c99112015-04-30 07:10:08 -0400352#undef THRIFT_SOCKET /* from lib/cpp */
353
Roger Meier213a6642010-10-27 12:30:11 +0000354#include "t_test_thrift_test.h"
355#include "t_test_thrift_test_types.h"
Roger Meiere3da7682013-01-11 11:41:53 +0100356#include <thrift/c_glib/transport/thrift_socket.h>
357#include <thrift/c_glib/protocol/thrift_protocol.h>
358#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
Roger Meier213a6642010-10-27 12:30:11 +0000359
360static void
361test_thrift_client (void)
362{
Sebastian Zenker042580f2019-01-29 15:48:12 +0100363 ThriftSocket *tsocket = nullptr;
364 ThriftBinaryProtocol *protocol = nullptr;
365 TTestThriftTestClient *client = nullptr;
366 TTestThriftTestIf *iface = nullptr;
367 GError *error = nullptr;
368 gchar *string = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000369 gint8 byte = 0;
370 gint16 i16 = 0;
James E. King, III82ae9572017-08-05 12:23:54 -0400371 gint32 i32 = 0, another_i32 = 56789;
Roger Meier213a6642010-10-27 12:30:11 +0000372 gint64 i64 = 0;
373 double dbl = 0.0;
374 TTestXtruct *xtruct_in, *xtruct_out;
375 TTestXtruct2 *xtruct2_in, *xtruct2_out;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100376 GHashTable *map_in = nullptr, *map_out = nullptr;
377 GHashTable *set_in = nullptr, *set_out = nullptr;
378 GArray *list_in = nullptr, *list_out = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000379 TTestNumberz enum_in, enum_out;
James E. King, III82ae9572017-08-05 12:23:54 -0400380 TTestUserId user_id_in, user_id_out;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100381 GHashTable *insanity_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000382 TTestXtruct *xtruct1, *xtruct2;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100383 TTestInsanity *insanity_out = nullptr;
384 TTestXtruct *multi_in = nullptr;
385 GHashTable *multi_map_out = nullptr;
386 TTestXception *xception = nullptr;
387 TTestXception2 *xception2 = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000388
Jens Geyer1c190272015-07-28 23:15:18 +0200389#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier213a6642010-10-27 12:30:11 +0000390 // initialize gobject
391 g_type_init ();
Jens Geyer1c190272015-07-28 23:15:18 +0200392#endif
Roger Meier213a6642010-10-27 12:30:11 +0000393
394 // create a C client
James E. King, III82ae9572017-08-05 12:23:54 -0400395 tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
Roger Meier213a6642010-10-27 12:30:11 +0000396 "hostname", "localhost",
zeshuai00726681fb2020-06-03 17:24:38 +0800397 "port", TEST_PORT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000398 protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
399 "transport",
zeshuai00726681fb2020-06-03 17:24:38 +0800400 tsocket, nullptr);
401 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 +0000402 iface = T_TEST_THRIFT_TEST_IF (client);
403
404 // open and send
Sebastian Zenker042580f2019-01-29 15:48:12 +0100405 thrift_transport_open (THRIFT_TRANSPORT(tsocket), nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000406
407 assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100408 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000409
410 assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
411 assert (strcmp (string, "test123") == 0);
412 g_free (string);
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_byte (iface, &byte, (gint8) 5, &error) == TRUE);
416 assert (byte == 5);
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_i32 (iface, &i32, 123, &error) == TRUE);
420 assert (i32 == 123);
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_i64 (iface, &i64, 12345, &error) == TRUE);
424 assert (i64 == 12345);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100425 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000426
427 assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
428 assert (dbl == 5.6);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100429 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000430
Sebastian Zenker042580f2019-01-29 15:48:12 +0100431 xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000432 xtruct_out->byte_thing = 1;
433 xtruct_out->__isset_byte_thing = TRUE;
434 xtruct_out->i32_thing = 15;
435 xtruct_out->__isset_i32_thing = TRUE;
436 xtruct_out->i64_thing = 151;
437 xtruct_out->__isset_i64_thing = TRUE;
438 xtruct_out->string_thing = g_strdup ("abc123");
439 xtruct_out->__isset_string_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100440 xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000441 assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100442 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000443
Sebastian Zenker042580f2019-01-29 15:48:12 +0100444 xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000445 xtruct2_out->byte_thing = 1;
446 xtruct2_out->__isset_byte_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100447 if (xtruct2_out->struct_thing != nullptr)
Roger Meierc75797d2012-04-28 11:33:58 +0000448 g_object_unref(xtruct2_out->struct_thing);
Roger Meier213a6642010-10-27 12:30:11 +0000449 xtruct2_out->struct_thing = xtruct_out;
450 xtruct2_out->__isset_struct_thing = TRUE;
451 xtruct2_out->i32_thing = 123;
452 xtruct2_out->__isset_i32_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100453 xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000454 assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100455 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000456
457 g_object_unref (xtruct2_out);
458 g_object_unref (xtruct2_in);
Roger Meier213a6642010-10-27 12:30:11 +0000459 g_object_unref (xtruct_in);
460
Sebastian Zenker042580f2019-01-29 15:48:12 +0100461 map_out = g_hash_table_new (nullptr, nullptr);
462 map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32);
Roger Meier213a6642010-10-27 12:30:11 +0000463 assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100464 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000465 g_hash_table_destroy (map_out);
466 g_hash_table_destroy (map_in);
467
Sebastian Zenker042580f2019-01-29 15:48:12 +0100468 map_out = g_hash_table_new (nullptr, nullptr);
469 map_in = g_hash_table_new (nullptr, nullptr);
Roger Meiera1c416f2011-06-17 19:40:48 +0000470 g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123"));
471 g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces "));
472 g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same"));
473 g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key"));
474 assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100475 assert (error == nullptr);
Roger Meiera1c416f2011-06-17 19:40:48 +0000476 g_hash_table_destroy (map_out);
477 g_hash_table_destroy (map_in);
478
Sebastian Zenker042580f2019-01-29 15:48:12 +0100479 set_out = g_hash_table_new (nullptr, nullptr);
480 set_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000481 g_hash_table_insert (set_out, &i32, &i32);
482 assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100483 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000484 g_hash_table_destroy (set_out);
485 g_hash_table_destroy (set_in);
486
487 list_out = g_array_new(TRUE, TRUE, sizeof(gint32));
488 list_in = g_array_new(TRUE, TRUE, sizeof(gint32));
489 another_i32 = 456;
490 g_array_append_val (list_out, i32);
491 g_array_append_val (list_out, another_i32);
492 assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100493 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000494 g_array_free (list_out, TRUE);
495 g_array_free (list_in, TRUE);
496
497 enum_out = T_TEST_NUMBERZ_ONE;
498 assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE);
499 assert (enum_in == enum_out);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100500 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000501
502 user_id_out = 12345;
503 assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE);
504 assert (user_id_in == user_id_out);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100505 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000506
Sebastian Zenker042580f2019-01-29 15:48:12 +0100507 map_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000508 assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100509 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000510 g_hash_table_destroy (map_in);
511
512 // insanity
Sebastian Zenker042580f2019-01-29 15:48:12 +0100513 insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr);
514 insanity_out->userMap = g_hash_table_new (nullptr, nullptr);
Roger Meier0cc6d3c2014-09-04 00:24:17 +0200515 g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
Roger Meier213a6642010-10-27 12:30:11 +0000516
Sebastian Zenker042580f2019-01-29 15:48:12 +0100517 xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000518 xtruct1->byte_thing = 1;
519 xtruct1->__isset_byte_thing = TRUE;
520 xtruct1->i32_thing = 15;
521 xtruct1->__isset_i32_thing = TRUE;
522 xtruct1->i64_thing = 151;
523 xtruct1->__isset_i64_thing = TRUE;
524 xtruct1->string_thing = g_strdup ("abc123");
525 xtruct1->__isset_string_thing = TRUE;
Sebastian Zenker042580f2019-01-29 15:48:12 +0100526 xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000527 xtruct2->byte_thing = 1;
528 xtruct2->__isset_byte_thing = TRUE;
529 xtruct2->i32_thing = 15;
530 xtruct2->__isset_i32_thing = TRUE;
531 xtruct2->i64_thing = 151;
532 xtruct2->__isset_i64_thing = TRUE;
533 xtruct2->string_thing = g_strdup ("abc123");
534 xtruct2->__isset_string_thing = TRUE;
535
Sebastian Zenker042580f2019-01-29 15:48:12 +0100536 insanity_in = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000537 g_ptr_array_add (insanity_out->xtructs, xtruct1);
538 g_ptr_array_add (insanity_out->xtructs, xtruct2);
539 assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE);
540
541 g_hash_table_unref (insanity_in);
542 g_ptr_array_free (insanity_out->xtructs, TRUE);
Roger Meier213a6642010-10-27 12:30:11 +0000543
Sebastian Zenker042580f2019-01-29 15:48:12 +0100544 multi_map_out = g_hash_table_new (nullptr, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000545 string = g_strdup ("abc123");
546 g_hash_table_insert (multi_map_out, &i16, string);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100547 multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000548 assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE);
549 assert (multi_in->i32_thing == i32);
550 assert (multi_in->i64_thing == i64);
551 g_object_unref (multi_in);
552 g_hash_table_unref (multi_map_out);
Roger Meierc75797d2012-04-28 11:33:58 +0000553 g_free (string);
Roger Meier213a6642010-10-27 12:30:11 +0000554
555 assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
556 assert (xception->errorCode == 1001);
557 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100558 error = nullptr;
Roger Meierc75797d2012-04-28 11:33:58 +0000559 g_object_unref (xception);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100560 xception = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000561
562 assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
563 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100564 error = nullptr;
565 assert (xception == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000566
567 assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100568 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000569
Sebastian Zenker042580f2019-01-29 15:48:12 +0100570 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
571 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 +0000572 assert (xception->errorCode == 1001);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100573 assert (xception2 == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000574 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100575 error = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000576 g_object_unref (xception);
Roger Meierc75797d2012-04-28 11:33:58 +0000577 g_object_unref (multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100578 xception = nullptr;
579 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000580
Sebastian Zenker042580f2019-01-29 15:48:12 +0100581 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
582 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 +0000583 assert (xception2->errorCode == 2002);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100584 assert (xception == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000585 g_error_free (error);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100586 error = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000587 g_object_unref (xception2);
Roger Meierc75797d2012-04-28 11:33:58 +0000588 g_object_unref (multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100589 xception2 = nullptr;
590 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000591
Sebastian Zenker042580f2019-01-29 15:48:12 +0100592 multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
593 assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE);
594 assert (error == nullptr);
Roger Meierc75797d2012-04-28 11:33:58 +0000595 g_object_unref(multi_in);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100596 multi_in = nullptr;
Roger Meier213a6642010-10-27 12:30:11 +0000597
598 assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100599 assert (error == nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000600
601 /* sleep to let the oneway call go through */
602 sleep (5);
603
Sebastian Zenker042580f2019-01-29 15:48:12 +0100604 thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr);
Roger Meier213a6642010-10-27 12:30:11 +0000605 g_object_unref (client);
606 g_object_unref (protocol);
607 g_object_unref (tsocket);
608}
609
610
611} /* extern "C" */
612
613
614static void
615bailout (int signum)
616{
Simon South38e71552015-08-03 10:51:16 +0000617 THRIFT_UNUSED_VARIABLE (signum);
618
Roger Meier213a6642010-10-27 12:30:11 +0000619 exit (1);
620}
621
622int
Simon South38e71552015-08-03 10:51:16 +0000623main (void)
Roger Meier213a6642010-10-27 12:30:11 +0000624{
625 int status;
626 int pid = fork ();
627 assert (pid >= 0);
628
629 if (pid == 0) /* child */
630 {
cyy316723a2019-01-05 16:35:14 +0800631 std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
632 std::shared_ptr<TestHandler> testHandler(new TestHandler());
633 std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler));
634 std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT));
635 std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
Roger Meier213a6642010-10-27 12:30:11 +0000636 TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory);
637 signal (SIGALRM, bailout);
638 alarm (60);
639 simpleServer.serve();
640 } else {
641 sleep (1);
642 test_thrift_client ();
643 kill (pid, SIGINT);
Simon South38e71552015-08-03 10:51:16 +0000644 assert (wait (&status) == pid);
Roger Meier213a6642010-10-27 12:30:11 +0000645 }
646
647 return 0;
648}
649