]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/java/test/org/apache/thrift/transport/TestTFramedTransport.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / test / org / apache / thrift / transport / TestTFramedTransport.java
CommitLineData
f67539c2
TL
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 */
19package org.apache.thrift.transport;
20
21import java.io.BufferedOutputStream;
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.DataInputStream;
25import java.io.DataOutputStream;
26import java.io.IOException;
27import java.util.Arrays;
28
29import junit.framework.TestCase;
30
31public class TestTFramedTransport extends TestCase {
32
33 protected TTransport getTransport(TTransport underlying) {
34 return new TFramedTransport(underlying);
35 }
36
37 protected TTransport getTransport(TTransport underlying, int maxLength) {
38 return new TFramedTransport(underlying, maxLength);
39 }
40
41 public static byte[] byteSequence(int start, int end) {
42 byte[] result = new byte[end-start+1];
43 for (int i = 0; i <= (end-start); i++) {
44 result[i] = (byte)(start+i);
45 }
46 return result;
47 }
48
49 public void testRead() throws IOException, TTransportException {
50 ByteArrayOutputStream baos = new ByteArrayOutputStream();
51 DataOutputStream dos = new DataOutputStream(baos);
52 dos.writeInt(50);
53 dos.write(byteSequence(0, 49));
54
55 dos.writeInt(220);
56 dos.write(byteSequence(0, 219));
57
58 TMemoryBuffer membuf = new TMemoryBuffer(0);
59 membuf.write(baos.toByteArray());
60
61 ReadCountingTransport countTrans = new ReadCountingTransport(membuf);
62 TTransport trans = getTransport(countTrans);
63
64 byte[] readBuf = new byte[10];
65 trans.read(readBuf, 0, 10);
66 assertTrue(Arrays.equals(readBuf, byteSequence(0,9)));
67 assertEquals(2, countTrans.readCount);
68
69 trans.read(readBuf, 0, 10);
70 assertTrue(Arrays.equals(readBuf, byteSequence(10,19)));
71 assertEquals(2, countTrans.readCount);
72
73 assertEquals(30, trans.read(new byte[30], 0, 30));
74 assertEquals(2, countTrans.readCount);
75
76 readBuf = new byte[220];
77 assertEquals(220, trans.read(readBuf, 0, 220));
78 assertTrue(Arrays.equals(readBuf, byteSequence(0, 219)));
79 assertEquals(4, countTrans.readCount);
80 }
81
82 public void testInvalidFrameSize() throws IOException, TTransportException {
83 int maxLength = 128;
84
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 DataOutputStream dos = new DataOutputStream(baos);
87 dos.writeInt(130);
88 dos.write(byteSequence(0, 129));
89
90 TMemoryBuffer membuf = new TMemoryBuffer(0);
91 membuf.write(baos.toByteArray());
92
93 ReadCountingTransport countTrans = new ReadCountingTransport(membuf);
94 TTransport trans = getTransport(countTrans, maxLength);
95
96 byte[] readBuf = new byte[10];
97 try {
98 trans.read(readBuf, 0, 4);
99 fail("Expected a TTransportException");
100 } catch (TTransportException e) {
101 // We expect this exception because the frame we're trying to read is larger than our max frame length
102 assertEquals(TTransportException.CORRUPTED_DATA, e.getType());
103 }
104
105 assertFalse(trans.isOpen());
106
107 try {
108 trans.read(readBuf, 0, 4);
109 fail("Expected a TTransportException");
110 } catch (TTransportException e) {
111 // This time we get an exception indicating the connection was closed
112 assertEquals(TTransportException.NOT_OPEN, e.getType());
113 }
114 }
115
116 public void testWrite() throws TTransportException, IOException {
117 ByteArrayOutputStream baos = new ByteArrayOutputStream();
118 WriteCountingTransport countingTrans = new WriteCountingTransport(new TIOStreamTransport(new BufferedOutputStream(baos)));
119 TTransport trans = getTransport(countingTrans);
120
121 trans.write(byteSequence(0,100));
122 assertEquals(0, countingTrans.writeCount);
123 trans.write(byteSequence(101,200));
124 trans.write(byteSequence(201,255));
125 assertEquals(0, countingTrans.writeCount);
126
127 trans.flush();
128 assertEquals(1, countingTrans.writeCount);
129
130 trans.write(byteSequence(0, 245));
131 trans.flush();
132 assertEquals(2, countingTrans.writeCount);
133
134 DataInputStream din = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
135 assertEquals(256, din.readInt());
136
137 byte[] buf = new byte[256];
138 din.read(buf, 0, 256);
139 assertTrue(Arrays.equals(byteSequence(0,255), buf));
140
141 assertEquals(246, din.readInt());
142 buf = new byte[246];
143 din.read(buf, 0, 246);
144 assertTrue(Arrays.equals(byteSequence(0,245), buf));
145 }
146
147 public void testDirectRead() throws IOException, TTransportException {
148 ByteArrayOutputStream baos = new ByteArrayOutputStream();
149 DataOutputStream dos = new DataOutputStream(baos);
150 dos.writeInt(50);
151 dos.write(byteSequence(0, 49));
152 dos.writeInt(75);
153 dos.write(byteSequence(125, 200));
154
155 TMemoryBuffer membuf = new TMemoryBuffer(0);
156 membuf.write(baos.toByteArray());
157
158 ReadCountingTransport countTrans = new ReadCountingTransport(membuf);
159 TTransport trans = getTransport(countTrans);
160
161 assertEquals(0, trans.getBytesRemainingInBuffer());
162
163 byte[] readBuf = new byte[10];
164 trans.read(readBuf, 0, 10);
165 assertTrue(Arrays.equals(readBuf, byteSequence(0,9)));
166
167 assertEquals(40, trans.getBytesRemainingInBuffer());
168 assertEquals(10, trans.getBufferPosition());
169
170 trans.consumeBuffer(5);
171 assertEquals(35, trans.getBytesRemainingInBuffer());
172 assertEquals(15, trans.getBufferPosition());
173
174 assertEquals(2, countTrans.readCount);
175
176 assertEquals(35, trans.read(new byte[35], 0, 35));
177 assertEquals(0, trans.getBytesRemainingInBuffer());
178 assertEquals(50, trans.getBufferPosition());
179
180 trans.read(readBuf, 0, 10);
181 assertEquals(4, countTrans.readCount);
182 assertTrue(Arrays.equals(readBuf, byteSequence(125,134)));
183 assertEquals(65, trans.getBytesRemainingInBuffer());
184 assertEquals(10, trans.getBufferPosition());
185 }
186
187 public void testClear() throws IOException, TTransportException {
188 ByteArrayOutputStream baos = new ByteArrayOutputStream();
189 DataOutputStream dos = new DataOutputStream(baos);
190 dos.writeInt(220);
191 dos.write(byteSequence(0, 219));
192
193 TMemoryBuffer membuf = new TMemoryBuffer(0);
194 membuf.write(baos.toByteArray());
195
196 ReadCountingTransport countTrans = new ReadCountingTransport(membuf);
197 TTransport trans = getTransport(countTrans);
198
199 byte[] readBuf = new byte[220];
200 trans.read(readBuf, 0, 220);
201 assertTrue(Arrays.equals(readBuf, byteSequence(0,219)));
202
203 assertTrue(trans instanceof TFramedTransport || trans instanceof TFastFramedTransport);
204 if (trans instanceof TFramedTransport) {
205 assertTrue(trans.getBuffer() != null && trans.getBuffer().length > 0);
206 ((TFramedTransport) trans).clear();
207 assertTrue(trans.getBuffer() == null);
208 } else if (trans instanceof TFastFramedTransport) {
209 assertTrue(trans.getBuffer().length > TestTFastFramedTransport.INITIAL_CAPACITY);
210 ((TFastFramedTransport) trans).clear();
211 assertTrue(trans.getBuffer().length == TestTFastFramedTransport.INITIAL_CAPACITY);
212 }
213 }
214}