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 #define _USE_MATH_DEFINES
21 #include <cmath>
22 #include "gen-cpp/DebugProtoTest_types.h"
23 #include <thrift/protocol/TDebugProtocol.h>
24 #include <thrift/stdcxx.h>
25 
26 #define BOOST_TEST_MODULE DebugProtoTest
27 #include <boost/test/unit_test.hpp>
28 
29 using namespace thrift::test::debug;
30 
31 static ::apache::thrift::stdcxx::shared_ptr<OneOfEach> ooe;
32 
testCaseSetup_1()33 void testCaseSetup_1() {
34   ooe.reset(new OneOfEach);
35   ooe->im_true = true;
36   ooe->im_false = false;
37   ooe->a_bite = 0x7f;
38   ooe->integer16 = 27000;
39   ooe->integer32 = 1 << 24;
40   ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
41   ooe->double_precision = M_PI;
42   ooe->some_characters = "Debug THIS!";
43   ooe->zomg_unicode = "\xd7\n\a\t";
44 }
45 
BOOST_AUTO_TEST_CASE(test_debug_proto_1)46 BOOST_AUTO_TEST_CASE(test_debug_proto_1) {
47   testCaseSetup_1();
48 
49   const std::string expected_result(
50     "OneOfEach {\n"
51     "  01: im_true (bool) = true,\n"
52     "  02: im_false (bool) = false,\n"
53     "  03: a_bite (byte) = 0x7f,\n"
54     "  04: integer16 (i16) = 27000,\n"
55     "  05: integer32 (i32) = 16777216,\n"
56     "  06: integer64 (i64) = 6000000000,\n"
57     "  07: double_precision (double) = 3.1415926535897931,\n"
58     "  08: some_characters (string) = \"Debug THIS!\",\n"
59     "  09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
60     "  10: what_who (bool) = false,\n"
61     "  11: base64 (string) = \"\",\n"
62     "  12: byte_list (list) = list<byte>[3] {\n"
63     "    [0] = 0x01,\n"
64     "    [1] = 0x02,\n"
65     "    [2] = 0x03,\n"
66     "  },\n"
67     "  13: i16_list (list) = list<i16>[3] {\n"
68     "    [0] = 1,\n"
69     "    [1] = 2,\n"
70     "    [2] = 3,\n"
71     "  },\n"
72     "  14: i64_list (list) = list<i64>[3] {\n"
73     "    [0] = 1,\n"
74     "    [1] = 2,\n"
75     "    [2] = 3,\n"
76     "  },\n"
77     "}");
78   const std::string result(apache::thrift::ThriftDebugString(*ooe));
79 
80   BOOST_CHECK_MESSAGE(!expected_result.compare(result),
81     "Expected:\n" << expected_result << "\nGotten:\n" << result);
82 }
83 
84 static ::apache::thrift::stdcxx::shared_ptr<Nesting> n;
85 
testCaseSetup_2()86 void testCaseSetup_2() {
87   testCaseSetup_1();
88 
89   n.reset(new Nesting);
90   n->my_ooe = *ooe;
91   n->my_ooe.integer16 = 16;
92   n->my_ooe.integer32 = 32;
93   n->my_ooe.integer64 = 64;
94   n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
95   n->my_ooe.some_characters = ":R (me going \"rrrr\")";
96   n->my_ooe.zomg_unicode     = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
97                                "\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
98                                "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
99                                "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
100                                "\xbc";
101   n->my_bonk.type = 31337;
102   n->my_bonk.message = "I am a bonk... xor!";
103 }
104 
BOOST_AUTO_TEST_CASE(test_debug_proto_2)105 BOOST_AUTO_TEST_CASE(test_debug_proto_2) {
106   testCaseSetup_2();
107 
108   const std::string expected_result(
109     "Nesting {\n"
110     "  01: my_bonk (struct) = Bonk {\n"
111     "    01: type (i32) = 31337,\n"
112     "    02: message (string) = \"I am a bonk... xor!\",\n"
113     "  },\n"
114     "  02: my_ooe (struct) = OneOfEach {\n"
115     "    01: im_true (bool) = true,\n"
116     "    02: im_false (bool) = false,\n"
117     "    03: a_bite (byte) = 0x7f,\n"
118     "    04: integer16 (i16) = 16,\n"
119     "    05: integer32 (i32) = 32,\n"
120     "    06: integer64 (i64) = 64,\n"
121     "    07: double_precision (double) = 1.6180339887498949,\n"
122     "    08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
123     "    09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\xd"
124       "0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb0"
125       "\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\xb"
126       "a\\xc7\\x83\\xe2\\x80\\xbc\",\n"
127     "    10: what_who (bool) = false,\n"
128     "    11: base64 (string) = \"\",\n"
129     "    12: byte_list (list) = list<byte>[3] {\n"
130     "      [0] = 0x01,\n"
131     "      [1] = 0x02,\n"
132     "      [2] = 0x03,\n"
133     "    },\n"
134     "    13: i16_list (list) = list<i16>[3] {\n"
135     "      [0] = 1,\n"
136     "      [1] = 2,\n"
137     "      [2] = 3,\n"
138     "    },\n"
139     "    14: i64_list (list) = list<i64>[3] {\n"
140     "      [0] = 1,\n"
141     "      [1] = 2,\n"
142     "      [2] = 3,\n"
143     "    },\n"
144     "  },\n"
145     "}");
146   const std::string result(apache::thrift::ThriftDebugString(*n));
147 
148   BOOST_CHECK_MESSAGE(!expected_result.compare(result),
149     "Expected:\n" << expected_result << "\nGotten:\n" << result);
150 }
151 
152 static ::apache::thrift::stdcxx::shared_ptr<HolyMoley> hm;
153 
testCaseSetup_3()154 void testCaseSetup_3() {
155   testCaseSetup_2();
156 
157   hm.reset(new HolyMoley);
158 
159   hm->big.push_back(*ooe);
160   hm->big.push_back(n->my_ooe);
161   hm->big[0].a_bite = 0x22;
162   hm->big[1].a_bite = 0x33;
163 
164   std::vector<std::string> stage1;
165   stage1.push_back("and a one");
166   stage1.push_back("and a two");
167   hm->contain.insert(stage1);
168   stage1.clear();
169   stage1.push_back("then a one, two");
170   stage1.push_back("three!");
171   stage1.push_back("FOUR!!");
172   hm->contain.insert(stage1);
173   stage1.clear();
174   hm->contain.insert(stage1);
175 
176   std::vector<Bonk> stage2;
177   hm->bonks["nothing"] = stage2;
178   stage2.resize(stage2.size() + 1);
179   stage2.back().type = 1;
180   stage2.back().message = "Wait.";
181   stage2.resize(stage2.size() + 1);
182   stage2.back().type = 2;
183   stage2.back().message = "What?";
184   hm->bonks["something"] = stage2;
185   stage2.clear();
186   stage2.resize(stage2.size() + 1);
187   stage2.back().type = 3;
188   stage2.back().message = "quoth";
189   stage2.resize(stage2.size() + 1);
190   stage2.back().type = 4;
191   stage2.back().message = "the raven";
192   stage2.resize(stage2.size() + 1);
193   stage2.back().type = 5;
194   stage2.back().message = "nevermore";
195   hm->bonks["poe"] = stage2;
196 }
197 
BOOST_AUTO_TEST_CASE(test_debug_proto_3)198 BOOST_AUTO_TEST_CASE(test_debug_proto_3) {
199   testCaseSetup_3();
200 
201   const std::string expected_result(
202     "HolyMoley {\n"
203     "  01: big (list) = list<struct>[2] {\n"
204     "    [0] = OneOfEach {\n"
205     "      01: im_true (bool) = true,\n"
206     "      02: im_false (bool) = false,\n"
207     "      03: a_bite (byte) = 0x22,\n"
208     "      04: integer16 (i16) = 27000,\n"
209     "      05: integer32 (i32) = 16777216,\n"
210     "      06: integer64 (i64) = 6000000000,\n"
211     "      07: double_precision (double) = 3.1415926535897931,\n"
212     "      08: some_characters (string) = \"Debug THIS!\",\n"
213     "      09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
214     "      10: what_who (bool) = false,\n"
215     "      11: base64 (string) = \"\",\n"
216     "      12: byte_list (list) = list<byte>[3] {\n"
217     "        [0] = 0x01,\n"
218     "        [1] = 0x02,\n"
219     "        [2] = 0x03,\n"
220     "      },\n"
221     "      13: i16_list (list) = list<i16>[3] {\n"
222     "        [0] = 1,\n"
223     "        [1] = 2,\n"
224     "        [2] = 3,\n"
225     "      },\n"
226     "      14: i64_list (list) = list<i64>[3] {\n"
227     "        [0] = 1,\n"
228     "        [1] = 2,\n"
229     "        [2] = 3,\n"
230     "      },\n"
231     "    },\n"
232     "    [1] = OneOfEach {\n"
233     "      01: im_true (bool) = true,\n"
234     "      02: im_false (bool) = false,\n"
235     "      03: a_bite (byte) = 0x33,\n"
236     "      04: integer16 (i16) = 16,\n"
237     "      05: integer32 (i32) = 32,\n"
238     "      06: integer64 (i64) = 64,\n"
239     "      07: double_precision (double) = 1.6180339887498949,\n"
240     "      08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
241     "      09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\"
242       "xd0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb"
243       "0\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\x"
244       "ba\\xc7\\x83\\xe2\\x80\\xbc\",\n"
245     "      10: what_who (bool) = false,\n"
246     "      11: base64 (string) = \"\",\n"
247     "      12: byte_list (list) = list<byte>[3] {\n"
248     "        [0] = 0x01,\n"
249     "        [1] = 0x02,\n"
250     "        [2] = 0x03,\n"
251     "      },\n"
252     "      13: i16_list (list) = list<i16>[3] {\n"
253     "        [0] = 1,\n"
254     "        [1] = 2,\n"
255     "        [2] = 3,\n"
256     "      },\n"
257     "      14: i64_list (list) = list<i64>[3] {\n"
258     "        [0] = 1,\n"
259     "        [1] = 2,\n"
260     "        [2] = 3,\n"
261     "      },\n"
262     "    },\n"
263     "  },\n"
264     "  02: contain (set) = set<list>[3] {\n"
265     "    list<string>[0] {\n"
266     "    },\n"
267     "    list<string>[2] {\n"
268     "      [0] = \"and a one\",\n"
269     "      [1] = \"and a two\",\n"
270     "    },\n"
271     "    list<string>[3] {\n"
272     "      [0] = \"then a one, two\",\n"
273     "      [1] = \"three!\",\n"
274     "      [2] = \"FOUR!!\",\n"
275     "    },\n"
276     "  },\n"
277     "  03: bonks (map) = map<string,list>[3] {\n"
278     "    \"nothing\" -> list<struct>[0] {\n"
279     "    },\n"
280     "    \"poe\" -> list<struct>[3] {\n"
281     "      [0] = Bonk {\n"
282     "        01: type (i32) = 3,\n"
283     "        02: message (string) = \"quoth\",\n"
284     "      },\n"
285     "      [1] = Bonk {\n"
286     "        01: type (i32) = 4,\n"
287     "        02: message (string) = \"the raven\",\n"
288     "      },\n"
289     "      [2] = Bonk {\n"
290     "        01: type (i32) = 5,\n"
291     "        02: message (string) = \"nevermore\",\n"
292     "      },\n"
293     "    },\n"
294     "    \"something\" -> list<struct>[2] {\n"
295     "      [0] = Bonk {\n"
296     "        01: type (i32) = 1,\n"
297     "        02: message (string) = \"Wait.\",\n"
298     "      },\n"
299     "      [1] = Bonk {\n"
300     "        01: type (i32) = 2,\n"
301     "        02: message (string) = \"What?\",\n"
302     "      },\n"
303     "    },\n"
304     "  },\n"
305     "}");
306   const std::string result(apache::thrift::ThriftDebugString(*hm));
307 
308   BOOST_CHECK_MESSAGE(!expected_result.compare(result),
309     "Expected:\n" << expected_result << "\nGotten:\n" << result);
310 }
311