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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 module thrift.internal.test.protocol;
22 import thrift.transport.memory;
23 import thrift.protocol.base;
27 void testContainerSizeLimit(Protocol)() if (isTProtocol!Protocol) {
28 auto buffer = new TMemoryBuffer;
29 auto prot = new Protocol(buffer);
31 // Make sure reading fails if a container larger than the size limit is read.
32 prot.containerSizeLimit = 3;
35 prot.writeListBegin(TList(TType.I32, 4));
36 prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol.
39 auto e = cast(TProtocolException)collectException(prot.readListBegin());
40 enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT);
46 prot.writeMapBegin(TMap(TType.I32, TType.I32, 4));
47 prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol.
50 auto e = cast(TProtocolException)collectException(prot.readMapBegin());
51 enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT);
57 prot.writeSetBegin(TSet(TType.I32, 4));
58 prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol.
61 auto e = cast(TProtocolException)collectException(prot.readSetBegin());
62 enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT);
67 // Make sure reading works if the containers are smaller than the limit or
69 foreach (limit; [3, 0, -1]) {
70 prot.containerSizeLimit = limit;
73 prot.writeListBegin(TList(TType.I32, 2));
79 auto list = prot.readListBegin();
80 enforce(list.elemType == TType.I32);
81 enforce(list.size == 2);
82 enforce(prot.readI32() == 0);
83 enforce(prot.readI32() == 1);
91 prot.writeMapBegin(TMap(TType.I32, TType.I32, 2));
99 auto map = prot.readMapBegin();
100 enforce(map.keyType == TType.I32);
101 enforce(map.valueType == TType.I32);
102 enforce(map.size == 2);
103 enforce(prot.readI32() == 0);
104 enforce(prot.readI32() == 1);
105 enforce(prot.readI32() == 2);
106 enforce(prot.readI32() == 3);
114 prot.writeSetBegin(TSet(TType.I32, 2));
120 auto set = prot.readSetBegin();
121 enforce(set.elemType == TType.I32);
122 enforce(set.size == 2);
123 enforce(prot.readI32() == 0);
124 enforce(prot.readI32() == 1);
133 void testStringSizeLimit(Protocol)() if (isTProtocol!Protocol) {
134 auto buffer = new TMemoryBuffer;
135 auto prot = new Protocol(buffer);
137 // Make sure reading fails if a string larger than the size limit is read.
138 prot.stringSizeLimit = 3;
141 prot.writeString("asdf");
144 auto e = cast(TProtocolException)collectException(prot.readString());
145 enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT);
151 prot.writeBinary([1, 2, 3, 4]);
154 auto e = cast(TProtocolException)collectException(prot.readBinary());
155 enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT);
160 // Make sure reading works if the containers are smaller than the limit or
162 foreach (limit; [3, 0, -1]) {
163 prot.containerSizeLimit = limit;
166 prot.writeString("as");
169 enforce(prot.readString() == "as");
175 prot.writeBinary([1, 2]);
178 enforce(prot.readBinary() == [1, 2]);