blob: f3091a48779aeb3459a11a7d769e6235e5987628 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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 Meier49ff8b12012-04-13 09:12:31 +000020#include <thrift/Thrift.h>
cyy316723a2019-01-05 16:35:14 +080021#include <memory>
Roger Meier49ff8b12012-04-13 09:12:31 +000022#include <thrift/transport/TTransportUtils.h>
23#include <thrift/transport/TBufferTransports.h>
Claudius Heine5ef662b2015-06-24 10:03:50 +020024
25#define BOOST_TEST_MODULE TPipedTransportTest
26#include <boost/test/unit_test.hpp>
27
T Jake Lucianib5e62212009-01-31 22:36:20 +000028using apache::thrift::transport::TTransportException;
29using apache::thrift::transport::TPipedTransport;
30using apache::thrift::transport::TMemoryBuffer;
James E. King, III82ae9572017-08-05 12:23:54 -040031using namespace apache::thrift;
David Reiss1ffb61b2008-04-08 05:07:26 +000032
jfarrelle2e5e222015-08-25 14:59:40 -040033BOOST_AUTO_TEST_CASE(test_read_write) {
cyy316723a2019-01-05 16:35:14 +080034 std::shared_ptr<TMemoryBuffer> underlying(new TMemoryBuffer);
35 std::shared_ptr<TMemoryBuffer> pipe(new TMemoryBuffer);
36 std::shared_ptr<TPipedTransport> trans(new TPipedTransport(underlying, pipe));
David Reiss1ffb61b2008-04-08 05:07:26 +000037
38 uint8_t buffer[4];
39
40 underlying->write((uint8_t*)"abcd", 4);
41 trans->readAll(buffer, 2);
Claudius Heine5ef662b2015-06-24 10:03:50 +020042 BOOST_CHECK(std::string((char*)buffer, 2) == "ab");
David Reiss1ffb61b2008-04-08 05:07:26 +000043 trans->readEnd();
Claudius Heine5ef662b2015-06-24 10:03:50 +020044 BOOST_CHECK(pipe->getBufferAsString() == "ab");
David Reiss1ffb61b2008-04-08 05:07:26 +000045 pipe->resetBuffer();
46 underlying->write((uint8_t*)"ef", 2);
47 trans->readAll(buffer, 2);
Claudius Heine5ef662b2015-06-24 10:03:50 +020048 BOOST_CHECK(std::string((char*)buffer, 2) == "cd");
David Reiss1ffb61b2008-04-08 05:07:26 +000049 trans->readAll(buffer, 2);
Claudius Heine5ef662b2015-06-24 10:03:50 +020050 BOOST_CHECK(std::string((char*)buffer, 2) == "ef");
David Reiss1ffb61b2008-04-08 05:07:26 +000051 trans->readEnd();
Claudius Heine5ef662b2015-06-24 10:03:50 +020052 BOOST_CHECK(pipe->getBufferAsString() == "cdef");
David Reiss1ffb61b2008-04-08 05:07:26 +000053}