]>
Commit | Line | Data |
---|---|---|
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 | */ | |
19 | module thrift.internal.test.protocol; | |
20 | ||
21 | import std.exception; | |
22 | import thrift.transport.memory; | |
23 | import thrift.protocol.base; | |
24 | ||
25 | version (unittest): | |
26 | ||
27 | void testContainerSizeLimit(Protocol)() if (isTProtocol!Protocol) { | |
28 | auto buffer = new TMemoryBuffer; | |
29 | auto prot = new Protocol(buffer); | |
30 | ||
31 | // Make sure reading fails if a container larger than the size limit is read. | |
32 | prot.containerSizeLimit = 3; | |
33 | ||
34 | { | |
35 | prot.writeListBegin(TList(TType.I32, 4)); | |
36 | prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol. | |
37 | prot.reset(); | |
38 | ||
39 | auto e = cast(TProtocolException)collectException(prot.readListBegin()); | |
40 | enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT); | |
41 | prot.reset(); | |
42 | buffer.reset(); | |
43 | } | |
44 | ||
45 | { | |
46 | prot.writeMapBegin(TMap(TType.I32, TType.I32, 4)); | |
47 | prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol. | |
48 | prot.reset(); | |
49 | ||
50 | auto e = cast(TProtocolException)collectException(prot.readMapBegin()); | |
51 | enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT); | |
52 | prot.reset(); | |
53 | buffer.reset(); | |
54 | } | |
55 | ||
56 | { | |
57 | prot.writeSetBegin(TSet(TType.I32, 4)); | |
58 | prot.writeI32(0); // Make sure size can be read e.g. for JSON protocol. | |
59 | prot.reset(); | |
60 | ||
61 | auto e = cast(TProtocolException)collectException(prot.readSetBegin()); | |
62 | enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT); | |
63 | prot.reset(); | |
64 | buffer.reset(); | |
65 | } | |
66 | ||
67 | // Make sure reading works if the containers are smaller than the limit or | |
68 | // no limit is set. | |
69 | foreach (limit; [3, 0, -1]) { | |
70 | prot.containerSizeLimit = limit; | |
71 | ||
72 | { | |
73 | prot.writeListBegin(TList(TType.I32, 2)); | |
74 | prot.writeI32(0); | |
75 | prot.writeI32(1); | |
76 | prot.writeListEnd(); | |
77 | prot.reset(); | |
78 | ||
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); | |
84 | prot.readListEnd(); | |
85 | ||
86 | prot.reset(); | |
87 | buffer.reset(); | |
88 | } | |
89 | ||
90 | { | |
91 | prot.writeMapBegin(TMap(TType.I32, TType.I32, 2)); | |
92 | prot.writeI32(0); | |
93 | prot.writeI32(1); | |
94 | prot.writeI32(2); | |
95 | prot.writeI32(3); | |
96 | prot.writeMapEnd(); | |
97 | prot.reset(); | |
98 | ||
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); | |
107 | prot.readMapEnd(); | |
108 | ||
109 | prot.reset(); | |
110 | buffer.reset(); | |
111 | } | |
112 | ||
113 | { | |
114 | prot.writeSetBegin(TSet(TType.I32, 2)); | |
115 | prot.writeI32(0); | |
116 | prot.writeI32(1); | |
117 | prot.writeSetEnd(); | |
118 | prot.reset(); | |
119 | ||
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); | |
125 | prot.readSetEnd(); | |
126 | ||
127 | prot.reset(); | |
128 | buffer.reset(); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | void testStringSizeLimit(Protocol)() if (isTProtocol!Protocol) { | |
134 | auto buffer = new TMemoryBuffer; | |
135 | auto prot = new Protocol(buffer); | |
136 | ||
137 | // Make sure reading fails if a string larger than the size limit is read. | |
138 | prot.stringSizeLimit = 3; | |
139 | ||
140 | { | |
141 | prot.writeString("asdf"); | |
142 | prot.reset(); | |
143 | ||
144 | auto e = cast(TProtocolException)collectException(prot.readString()); | |
145 | enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT); | |
146 | prot.reset(); | |
147 | buffer.reset(); | |
148 | } | |
149 | ||
150 | { | |
151 | prot.writeBinary([1, 2, 3, 4]); | |
152 | prot.reset(); | |
153 | ||
154 | auto e = cast(TProtocolException)collectException(prot.readBinary()); | |
155 | enforce(e && e.type == TProtocolException.Type.SIZE_LIMIT); | |
156 | prot.reset(); | |
157 | buffer.reset(); | |
158 | } | |
159 | ||
160 | // Make sure reading works if the containers are smaller than the limit or | |
161 | // no limit is set. | |
162 | foreach (limit; [3, 0, -1]) { | |
163 | prot.containerSizeLimit = limit; | |
164 | ||
165 | { | |
166 | prot.writeString("as"); | |
167 | prot.reset(); | |
168 | ||
169 | enforce(prot.readString() == "as"); | |
170 | prot.reset(); | |
171 | buffer.reset(); | |
172 | } | |
173 | ||
174 | { | |
175 | prot.writeBinary([1, 2]); | |
176 | prot.reset(); | |
177 | ||
178 | enforce(prot.readBinary() == [1, 2]); | |
179 | prot.reset(); | |
180 | buffer.reset(); | |
181 | } | |
182 | } | |
183 | } |