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  * Contains some contributions under the Thrift Software License.
20  * Please see doc/old-thrift-license.txt in the Thrift distribution for
21  * details.
22  */
23 
24 #include "gen-cpp/Recursive_types.h"
25 #include <thrift/protocol/TBinaryProtocol.h>
26 #include <memory>
27 #include <thrift/transport/TBufferTransports.h>
28 
29 #define BOOST_TEST_MODULE RecursiveTest
30 #include <boost/test/unit_test.hpp>
31 
32 using apache::thrift::transport::TMemoryBuffer;
33 using apache::thrift::protocol::TBinaryProtocol;
34 using std::shared_ptr;
35 
BOOST_AUTO_TEST_CASE(test_recursive_1)36 BOOST_AUTO_TEST_CASE(test_recursive_1) {
37   shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
38   shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
39 
40   RecTree tree;
41   RecTree child;
42   tree.children.push_back(child);
43 
44   tree.write(prot.get());
45 
46   RecTree result;
47   result.read(prot.get());
48   BOOST_CHECK(tree == result);
49 }
50 
BOOST_AUTO_TEST_CASE(test_recursive_2)51 BOOST_AUTO_TEST_CASE(test_recursive_2) {
52   shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
53   shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
54 
55   RecList l;
56   shared_ptr<RecList> l2(new RecList);
57   l.nextitem = l2;
58 
59   l.write(prot.get());
60 
61   RecList resultlist;
62   resultlist.read(prot.get());
63   BOOST_CHECK(resultlist.nextitem != nullptr);
64   BOOST_CHECK(resultlist.nextitem->nextitem == nullptr);
65 }
66 
BOOST_AUTO_TEST_CASE(test_recursive_3)67 BOOST_AUTO_TEST_CASE(test_recursive_3) {
68   shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
69   shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
70 
71   CoRec c;
72   shared_ptr<CoRec2> r(new CoRec2);
73   c.other = r;
74 
75   c.write(prot.get());
76 
77   c.read(prot.get());
78   BOOST_CHECK(c.other != nullptr);
79   BOOST_CHECK(c.other->other.other == nullptr);
80 }
81 
BOOST_AUTO_TEST_CASE(test_recursive_4)82 BOOST_AUTO_TEST_CASE(test_recursive_4) {
83   shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer());
84   shared_ptr<TBinaryProtocol> prot(new TBinaryProtocol(buf));
85 
86   shared_ptr<RecList> depthLimit(new RecList);
87   depthLimit->nextitem = depthLimit;
88   BOOST_CHECK_THROW(depthLimit->write(prot.get()),
89     apache::thrift::protocol::TProtocolException);
90 
91   depthLimit->nextitem.reset();
92 }
93