blob: cfc96a293f645bb2c59127244f4e827aea277cc9 [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 */
19
Roger Meier213a6642010-10-27 12:30:11 +000020#include <assert.h>
21#include <glib.h>
22
Roger Meiere3da7682013-01-11 11:41:53 +010023#include <thrift/c_glib/thrift_struct.h>
24#include <thrift/c_glib/protocol/thrift_protocol.h>
25#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
26#include <thrift/c_glib/transport/thrift_memory_buffer.h>
Roger Meier213a6642010-10-27 12:30:11 +000027#include "gen-c_glib/t_test_optional_required_test_types.h"
28
29#include "gen-c_glib/t_test_optional_required_test_types.c"
30
31static void
32write_to_read (ThriftStruct *w, ThriftStruct *r, GError **write_error,
33 GError **read_error)
34{
35 ThriftMemoryBuffer *tbuffer = NULL;
36 ThriftProtocol *protocol = NULL;
37
38 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
39 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
40 tbuffer, NULL);
41
42 thrift_struct_write (w, protocol, write_error);
43 thrift_struct_read (r, protocol, read_error);
44
45 g_object_unref (protocol);
46 g_object_unref (tbuffer);
47}
48
49static void
50test_old_school1 (void)
51{
52 TTestOldSchool *o = NULL;
53
54 o = g_object_new (T_TEST_TYPE_OLD_SCHOOL, NULL);
55 o->im_int = 10;
56 o->im_str = g_strdup ("test");
57 o->im_big = g_ptr_array_new ();
Roger Meierc75797d2012-04-28 11:33:58 +000058 g_ptr_array_free (o->im_big, TRUE);
59 o->im_big = NULL;
Roger Meier213a6642010-10-27 12:30:11 +000060 g_free (o->im_str);
Roger Meierc75797d2012-04-28 11:33:58 +000061 o->im_str = NULL;
Roger Meier213a6642010-10-27 12:30:11 +000062 g_object_unref (o);
63}
64
65/**
66 * Write to read with optional fields
67 */
68static void
69test_simple (void)
70{
71 TTestSimple *s1 = NULL, *s2 = NULL, *s3 = NULL;
72
73 s1 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
74 s2 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
75 s3 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
76
Simon South38e71552015-08-03 10:51:16 +000077 /* write-to-read with optional fields */
Roger Meier213a6642010-10-27 12:30:11 +000078 s1->im_optional = 10;
79 assert (s1->__isset_im_default == FALSE);
80 assert (s1->__isset_im_optional == FALSE);
81 write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL);
82 assert (s2->__isset_im_default = TRUE);
83 assert (s2->__isset_im_optional == FALSE);
84 assert (s2->im_optional == 0);
85
86 s1->__isset_im_optional = TRUE;
87 write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL);
88 assert (s3->__isset_im_default == TRUE);
89 assert (s3->__isset_im_optional == TRUE);
90 assert (s3->im_optional == 10);
91
92 g_object_unref (s1);
93 g_object_unref (s2);
94}
95
96/**
97 * Writing between optional and default
98 */
99static void
100test_tricky1 (void)
101{
102 TTestTricky1 *t1 = NULL;
103 TTestTricky2 *t2 = NULL;
104
105 t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
106 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
107
108 t2->im_optional = 10;
109 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL);
110 write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL);
111
112 assert (t1->__isset_im_default == FALSE);
113 assert (t2->__isset_im_optional == TRUE);
114 assert (t1->im_default == t2->im_optional);
115 assert (t1->im_default == 0);
116
117 g_object_unref (t1);
118 g_object_unref (t2);
119}
120
121/**
122 * Writing between default and required.
123 */
124static void
125test_tricky2 (void)
126{
127 TTestTricky1 *t1 = NULL;
128 TTestTricky3 *t3 = NULL;
129
130 t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
131 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
132
133 write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL);
134 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL);
135
136 assert (t1->__isset_im_default == TRUE);
137
138 g_object_unref (t1);
139 g_object_unref (t3);
140}
141
142/**
143 * Writing between optional and required.
144 */
145static void
146test_tricky3 (void)
147{
148 TTestTricky2 *t2 = NULL;
149 TTestTricky3 *t3 = NULL;
150
151 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
152 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
153
154 t2->__isset_im_optional = TRUE;
155
156 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, NULL);
157 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
158
159 g_object_unref (t2);
160 g_object_unref (t3);
161}
162
163/**
164 * Catch an optional not set exception. To quote the
165 * C++ test, "Mu-hu-ha-ha-ha!"
166 */
167static void
168test_tricky4 (void)
169{
170 TTestTricky2 *t2 = NULL;
171 TTestTricky3 *t3 = NULL;
172 GError *read_error = NULL;
173
174 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
175 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
176
Simon South38e71552015-08-03 10:51:16 +0000177 /* throws protocol exception */
Roger Meier213a6642010-10-27 12:30:11 +0000178 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error);
179 assert (read_error != NULL);
180 g_error_free (read_error);
181
182 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
183
184 assert (t2->__isset_im_optional);
185
186 g_object_unref (t2);
187 g_object_unref (t3);
188}
189
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +0900190static void
191test_non_set_binary (void)
192{
193 TTestBinaries *b1 = NULL;
194 TTestBinaries *b2 = NULL;
195 GError *error = NULL;
196
197 b1 = g_object_new (T_TEST_TYPE_BINARIES, NULL);
198 b2 = g_object_new (T_TEST_TYPE_BINARIES, NULL);
199
200 write_to_read (THRIFT_STRUCT (b1), THRIFT_STRUCT (b2), NULL, &error);
201 g_assert(!error);
202 write_to_read (THRIFT_STRUCT (b2), THRIFT_STRUCT (b1), NULL, &error);
203 g_assert(!error);
204 // OK. No segfault
205
206 g_object_unref (b1);
207 g_object_unref (b2);
208}
209
Roger Meier213a6642010-10-27 12:30:11 +0000210int
Roger Meierc1010922010-11-26 10:17:48 +0000211main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000212{
Jens Geyer1c190272015-07-28 23:15:18 +0200213#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meierc1010922010-11-26 10:17:48 +0000214 g_type_init();
Jens Geyer1c190272015-07-28 23:15:18 +0200215#endif
216
Roger Meierc1010922010-11-26 10:17:48 +0000217 g_test_init (&argc, &argv, NULL);
218
219 g_test_add_func ("/testoptionalrequired/OldSchool", test_old_school1);
220 g_test_add_func ("/testoptionalrequired/Simple", test_simple);
221 g_test_add_func ("/testoptionalrequired/Tricky1", test_tricky1);
222 g_test_add_func ("/testoptionalrequired/Tricky2", test_tricky2);
223 g_test_add_func ("/testoptionalrequired/Tricky3", test_tricky3);
224 g_test_add_func ("/testoptionalrequired/Tricky4", test_tricky4);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +0900225 g_test_add_func ("/testoptionalrequired/Binary", test_non_set_binary);
Roger Meierc1010922010-11-26 10:17:48 +0000226
227 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000228}