1 # Copyright 2016 gRPC authors.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 import http2_base_server
19 # Set the number of padding bytes per data frame to be very large
20 # relative to the number of data bytes for each data frame sent.
21 _LARGE_PADDING_LENGTH = 255
22 _SMALL_READ_CHUNK_SIZE = 5
25 class TestDataFramePadding(object):
27 In response to an incoming request, this test sends headers, followed by
28 data, followed by a reset stream frame. Client asserts that the RPC failed.
29 Client needs to deliver the complete message to the application layer.
32 def __init__(self, use_padding=True):
33 self._base_server = http2_base_server.H2ProtocolBaseServer()
34 self._base_server._handlers['DataReceived'] = self.on_data_received
35 self._base_server._handlers['WindowUpdated'] = self.on_window_update
36 self._base_server._handlers[
37 'RequestReceived'] = self.on_request_received
39 # _total_updates maps stream ids to total flow control updates received
40 self._total_updates = {}
41 # zero window updates so far for connection window (stream id '0')
42 self._total_updates[0] = 0
43 self._read_chunk_size = _SMALL_READ_CHUNK_SIZE
46 self._pad_length = _LARGE_PADDING_LENGTH
48 self._pad_length = None
50 def get_base_server(self):
51 return self._base_server
53 def on_data_received(self, event):
54 logging.info('on data received. Stream id: %d. Data length: %d' %
55 (event.stream_id, len(event.data)))
56 self._base_server.on_data_received_default(event)
57 if len(event.data) == 0:
59 sr = self._base_server.parse_received_data(event.stream_id)
61 # Check if full grpc msg has been read into the recv buffer yet
63 response_data = self._base_server.default_response_data(
65 logging.info('Stream id: %d. total resp size: %d' %
66 (event.stream_id, len(response_data)))
67 # Begin sending the response. Add ``self._pad_length`` padding to each
68 # data frame and split the whole message into data frames each carrying
69 # only self._read_chunk_size of data.
70 # The purpose is to have the majority of the data frame response bytes
71 # be padding bytes, since ``self._pad_length`` >> ``self._read_chunk_size``.
72 self._base_server.setup_send(response_data,
74 pad_length=self._pad_length,
75 read_chunk_size=self._read_chunk_size)
77 def on_request_received(self, event):
78 self._base_server.on_request_received_default(event)
79 logging.info('on request received. Stream id: %s.' % event.stream_id)
80 self._total_updates[event.stream_id] = 0
82 # Log debug info and try to resume sending on all currently active streams.
83 def on_window_update(self, event):
84 logging.info('on window update. Stream id: %s. Delta: %s' %
85 (event.stream_id, event.delta))
86 self._total_updates[event.stream_id] += event.delta
87 total = self._total_updates[event.stream_id]
88 logging.info('... - total updates for stream %d : %d' %
89 (event.stream_id, total))
90 self._base_server.on_window_update_default(
92 pad_length=self._pad_length,
93 read_chunk_size=self._read_chunk_size)