1 /*
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  */
19 
20 /* test a C client with a C++ server  (that makes sense...) */
21 
22 #include <thrift/config.h>
23 
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <thrift/protocol/TBinaryProtocol.h>
32 #include <thrift/protocol/TDebugProtocol.h>
33 #include <thrift/server/TSimpleServer.h>
34 #include <memory>
35 #include <thrift/transport/TServerSocket.h>
36 #include "ThriftTest.h"
37 #include "ThriftTest_types.h"
38 
39 #include <iostream>
40 #include <map>
41 #include <set>
42 #include <string>
43 #include <vector>
44 
45 using namespace apache::thrift;
46 using namespace apache::thrift::concurrency;
47 using namespace apache::thrift::protocol;
48 using namespace apache::thrift::server;
49 using namespace apache::thrift::transport;
50 
51 using namespace thrift::test;
52 
53 using std::cout;
54 using std::endl;
55 using std::fixed;
56 using std::make_pair;
57 using std::map;
58 using std::set;
59 using std::string;
60 using std::vector;
61 
62 #define TEST_PORT 9980
63 
64 // Extra functions required for ThriftTest_types to work
65 namespace thrift { namespace test {
66 
operator <(thrift::test::Insanity const & other) const67 bool Insanity::operator<(thrift::test::Insanity const& other) const {
68   using apache::thrift::ThriftDebugString;
69   return ThriftDebugString(*this) < ThriftDebugString(other);
70 }
71 
72 }}
73 
74 class TestHandler : public ThriftTestIf {
75   public:
76   TestHandler() = default;
77 
testVoid()78   void testVoid() override {
79     cout << "[C -> C++] testVoid()" << endl;
80   }
81 
testString(string & out,const string & thing)82   void testString(string& out, const string &thing) override {
83     cout << "[C -> C++] testString(\"" << thing << "\")" << endl;
84     out = thing;
85   }
86 
testBool(const bool thing)87   bool testBool(const bool thing) override {
88     cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << endl;
89     return thing;
90   }
testByte(const int8_t thing)91   int8_t testByte(const int8_t thing) override {
92     cout << "[C -> C++] testByte(" << (int)thing << ")" << endl;
93     return thing;
94   }
testI32(const int32_t thing)95   int32_t testI32(const int32_t thing) override {
96     cout << "[C -> C++] testI32(" << thing << ")" << endl;
97     return thing;
98   }
99 
testI64(const int64_t thing)100   int64_t testI64(const int64_t thing) override {
101     cout << "[C -> C++] testI64(" << thing << ")" << endl;
102     return thing;
103   }
104 
testDouble(const double thing)105   double testDouble(const double thing) override {
106     cout.precision(6);
107     cout << "[C -> C++] testDouble(" << fixed << thing << ")" << endl;
108     return thing;
109   }
110 
testBinary(string & out,const string & thing)111   void testBinary(string& out, const string &thing) override {
112     cout << "[C -> C++] testBinary(\"" << thing << "\")" << endl;
113     out = thing;
114   }
115 
testStruct(Xtruct & out,const Xtruct & thing)116   void testStruct(Xtruct& out, const Xtruct &thing) override {
117     cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << endl;
118     out = thing;
119   }
120 
testNest(Xtruct2 & out,const Xtruct2 & nest)121   void testNest(Xtruct2& out, const Xtruct2& nest) override {
122     const Xtruct &thing = nest.struct_thing;
123     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;
124     out = nest;
125   }
126 
testMap(map<int32_t,int32_t> & out,const map<int32_t,int32_t> & thing)127   void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override {
128     cout << "[C -> C++] testMap({";
129     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 {
135         cout << ", ";
136       }
137       cout << m_iter->first << " => " << m_iter->second;
138     }
139     cout << "})" << endl;
140     out = thing;
141   }
142 
testStringMap(map<std::string,std::string> & out,const map<std::string,std::string> & thing)143   void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override {
144     cout << "[C -> C++] testStringMap({";
145     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 {
151         cout << ", ";
152       }
153       cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\"";
154     }
155     cout << "})" << endl;
156     out = thing;
157   }
158 
159 
testSet(set<int32_t> & out,const set<int32_t> & thing)160   void testSet(set<int32_t> &out, const set<int32_t> &thing) override {
161     cout << "[C -> C++] testSet({";
162     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 {
168         cout << ", ";
169       }
170       cout << *s_iter;
171     }
172     cout << "})" << endl;
173     out = thing;
174   }
175 
testList(vector<int32_t> & out,const vector<int32_t> & thing)176   void testList(vector<int32_t> &out, const vector<int32_t> &thing) override {
177     cout << "[C -> C++] testList({";
178     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;
183       } else {
184         cout << ", ";
185       }
186       cout << *l_iter;
187     }
188     cout << "})" << endl;
189     out = thing;
190   }
191 
testEnum(const Numberz::type thing)192   Numberz::type testEnum(const Numberz::type thing) override {
193     cout << "[C -> C++] testEnum(" << thing << ")" << endl;
194     return thing;
195   }
196 
testTypedef(const UserId thing)197   UserId testTypedef(const UserId thing) override {
198     cout << "[C -> C++] testTypedef(" << thing << ")" << endl;
199     return thing;  }
200 
testMapMap(map<int32_t,map<int32_t,int32_t>> & mapmap,const int32_t hello)201   void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override {
202     cout << "[C -> C++] testMapMap(" << hello << ")" << endl;
203 
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 
testInsanity(map<UserId,map<Numberz::type,Insanity>> & insane,const Insanity & argument)216   void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override {
217     THRIFT_UNUSED_VARIABLE (argument);
218 
219     cout << "[C -> C++] testInsanity()" << endl;
220 
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 
252     cout << "return = {";
253     map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter;
254     for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) {
255       cout << i_iter->first << " => {";
256       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) {
260         cout << i2_iter->first << " => {";
261         map<Numberz::type, UserId> userMap = i2_iter->second.userMap;
262         map<Numberz::type, UserId>::const_iterator um;
263         cout << "{";
264         for (um = userMap.begin(); um != userMap.end(); ++um) {
265           cout << um->first << " => " << um->second << ", ";
266         }
267         cout << "}, ";
268 
269         vector<Xtruct> xtructs = i2_iter->second.xtructs;
270         vector<Xtruct>::const_iterator x;
271         cout << "{";
272         for (x = xtructs.begin(); x != xtructs.end(); ++x) {
273           cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, ";
274         }
275         cout << "}";
276 
277         cout << "}, ";
278       }
279       cout << "}, ";
280     }
281     cout << "}" << endl;
282 
283 
284   }
285 
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)286   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 {
287     THRIFT_UNUSED_VARIABLE (arg3);
288     THRIFT_UNUSED_VARIABLE (arg4);
289     THRIFT_UNUSED_VARIABLE (arg5);
290 
291     cout << "[C -> C++] testMulti()" << endl;
292 
293     hello.string_thing = "Hello2";
294     hello.byte_thing = arg0;
295     hello.i32_thing = arg1;
296     hello.i64_thing = (int64_t)arg2;
297   }
298 
testException(const std::string & arg)299   void testException(const std::string &arg)
300     noexcept(false)  override
301   {
302     cout << "[C -> C++] testException(" << arg << ")" << endl;
303     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 
testMultiException(Xtruct & result,const std::string & arg0,const std::string & arg1)318   void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) noexcept(false) override {
319 
320     cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << endl;
321 
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 
testOneway(int sleepFor)338   void testOneway(int sleepFor) override {
339     cout << "testOneway(" << sleepFor << "): Sleeping..." << endl;
340     sleep(sleepFor);
341     cout << "testOneway(" << sleepFor << "): done sleeping!" << endl;
342   }
343 };
344 
345 // C CLIENT
346 extern "C" {
347 
348 #undef THRIFT_SOCKET /* from lib/cpp */
349 
350 #include "t_test_thrift_test.h"
351 #include "t_test_thrift_test_types.h"
352 #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>
355 
356 static void
test_thrift_client(void)357 test_thrift_client (void)
358 {
359   ThriftSocket *tsocket = nullptr;
360   ThriftBinaryProtocol *protocol = nullptr;
361   TTestThriftTestClient *client = nullptr;
362   TTestThriftTestIf *iface = nullptr;
363   GError *error = nullptr;
364   gchar *string = nullptr;
365   gint8 byte = 0;
366   gint16 i16 = 0;
367   gint32 i32 = 0, another_i32 = 56789;
368   gint64 i64 = 0;
369   double dbl = 0.0;
370   TTestXtruct *xtruct_in, *xtruct_out;
371   TTestXtruct2 *xtruct2_in, *xtruct2_out;
372   GHashTable *map_in = nullptr, *map_out = nullptr;
373   GHashTable *set_in = nullptr, *set_out = nullptr;
374   GArray *list_in = nullptr, *list_out = nullptr;
375   TTestNumberz enum_in, enum_out;
376   TTestUserId user_id_in, user_id_out;
377   GHashTable *insanity_in = nullptr;
378   TTestXtruct *xtruct1, *xtruct2;
379   TTestInsanity *insanity_out = nullptr;
380   TTestXtruct *multi_in = nullptr;
381   GHashTable *multi_map_out = nullptr;
382   TTestXception *xception = nullptr;
383   TTestXception2 *xception2 = nullptr;
384 
385 #if (!GLIB_CHECK_VERSION (2, 36, 0))
386   // initialize gobject
387   g_type_init ();
388 #endif
389 
390   // create a C client
391   tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET,
392                           "hostname", "localhost",
393                           "port", TEST_PORT, nullptr);
394   protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
395                            "transport",
396                            tsocket, nullptr);
397   client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr);
398   iface = T_TEST_THRIFT_TEST_IF (client);
399 
400   // open and send
401   thrift_transport_open (THRIFT_TRANSPORT(tsocket), nullptr);
402 
403   assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE);
404   assert (error == nullptr);
405 
406   assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE);
407   assert (strcmp (string, "test123") == 0);
408   g_free (string);
409   assert (error == nullptr);
410 
411   assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE);
412   assert (byte == 5);
413   assert (error == nullptr);
414 
415   assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE);
416   assert (i32 == 123);
417   assert (error == nullptr);
418 
419   assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE);
420   assert (i64 == 12345);
421   assert (error == nullptr);
422 
423   assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE);
424   assert (dbl == 5.6);
425   assert (error == nullptr);
426 
427   xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
428   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;
436   xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr);
437   assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE);
438   assert (error == nullptr);
439 
440   xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
441   xtruct2_out->byte_thing = 1;
442   xtruct2_out->__isset_byte_thing = TRUE;
443   if (xtruct2_out->struct_thing != nullptr)
444     g_object_unref(xtruct2_out->struct_thing);
445   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;
449   xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr);
450   assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE);
451   assert (error == nullptr);
452 
453   g_object_unref (xtruct2_out);
454   g_object_unref (xtruct2_in);
455   g_object_unref (xtruct_in);
456 
457   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);
459   assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE);
460   assert (error == nullptr);
461   g_hash_table_destroy (map_out);
462   g_hash_table_destroy (map_in);
463 
464   map_out = g_hash_table_new (nullptr, nullptr);
465   map_in = g_hash_table_new (nullptr, nullptr);
466   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);
471   assert (error == nullptr);
472   g_hash_table_destroy (map_out);
473   g_hash_table_destroy (map_in);
474 
475   set_out = g_hash_table_new (nullptr, nullptr);
476   set_in = g_hash_table_new (nullptr, nullptr);
477   g_hash_table_insert (set_out, &i32, &i32);
478   assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE);
479   assert (error == nullptr);
480   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);
489   assert (error == nullptr);
490   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);
496   assert (error == nullptr);
497 
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);
501   assert (error == nullptr);
502 
503   map_in = g_hash_table_new (nullptr, nullptr);
504   assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE);
505   assert (error == nullptr);
506   g_hash_table_destroy (map_in);
507 
508   // insanity
509   insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr);
510   insanity_out->userMap = g_hash_table_new (nullptr, nullptr);
511   g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out);
512 
513   xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
514   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;
522   xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
523   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 
532   insanity_in = g_hash_table_new (nullptr, nullptr);
533   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);
539 
540   multi_map_out = g_hash_table_new (nullptr, nullptr);
541   string = g_strdup ("abc123");
542   g_hash_table_insert (multi_map_out, &i16, string);
543   multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr);
544   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);
549   g_free (string);
550 
551   assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE);
552   assert (xception->errorCode == 1001);
553   g_error_free (error);
554   error = nullptr;
555   g_object_unref (xception);
556   xception = nullptr;
557 
558   assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE);
559   g_error_free (error);
560   error = nullptr;
561   assert (xception == nullptr);
562 
563   assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE);
564   assert (error == nullptr);
565 
566   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);
568   assert (xception->errorCode == 1001);
569   assert (xception2 == nullptr);
570   g_error_free (error);
571   error = nullptr;
572   g_object_unref (xception);
573   g_object_unref (multi_in);
574   xception = nullptr;
575   multi_in = nullptr;
576 
577   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);
579   assert (xception2->errorCode == 2002);
580   assert (xception == nullptr);
581   g_error_free (error);
582   error = nullptr;
583   g_object_unref (xception2);
584   g_object_unref (multi_in);
585   xception2 = nullptr;
586   multi_in = nullptr;
587 
588   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);
591   g_object_unref(multi_in);
592   multi_in = nullptr;
593 
594   assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE);
595   assert (error == nullptr);
596 
597   /* sleep to let the oneway call go through */
598   sleep (5);
599 
600   thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr);
601   g_object_unref (client);
602   g_object_unref (protocol);
603   g_object_unref (tsocket);
604 }
605 
606 
607 } /* extern "C" */
608 
609 
610 static void
bailout(int signum)611 bailout (int signum)
612 {
613   THRIFT_UNUSED_VARIABLE (signum);
614 
615   exit (1);
616 }
617 
618 int
main(void)619 main (void)
620 {
621   int status;
622   int pid = fork ();
623   assert (pid >= 0);
624 
625   if (pid == 0) /* child */
626   {
627     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());
632     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);
640     assert (wait (&status) == pid);
641   }
642 
643   return 0;
644 }
645 
646