blob: e1db28faedc8790bab27c260759ec2f036322bc8 [file] [log] [blame]
Alexander Polcyn50fdc8a2017-02-09 21:06:08 -08001# Copyright 2016, Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import http2_base_server
31import logging
32import messages_pb2
33
34# Set the number of padding bytes per data frame to be very large
35# relative to the number of data bytes for each data frame sent.
36_LARGE_PADDING_LENGTH = 255
37_SMALL_READ_CHUNK_SIZE = 5
38
39class TestDataFramePadding(object):
40 """
41 In response to an incoming request, this test sends headers, followed by
42 data, followed by a reset stream frame. Client asserts that the RPC failed.
43 Client needs to deliver the complete message to the application layer.
44 """
45 def __init__(self, use_padding=True):
46 self._base_server = http2_base_server.H2ProtocolBaseServer()
47 self._base_server._handlers['DataReceived'] = self.on_data_received
48 self._base_server._handlers['WindowUpdated'] = self.on_window_update
49 self._base_server._handlers['RequestReceived'] = self.on_request_received
50
51 # _total_updates maps stream ids to total flow control updates received
52 self._total_updates = {}
53 # zero window updates so far for connection window (stream id '0')
54 self._total_updates[0] = 0
55 self._read_chunk_size = _SMALL_READ_CHUNK_SIZE
56
57 if use_padding:
58 self._pad_length = _LARGE_PADDING_LENGTH
59 else:
60 self._pad_length = None
61
62 def get_base_server(self):
63 return self._base_server
64
65 def on_data_received(self, event):
66 logging.info('on data received. Stream id: %d. Data length: %d' % (event.stream_id, len(event.data)))
67 self._base_server.on_data_received_default(event)
68 if len(event.data) == 0:
69 return
70 sr = self._base_server.parse_received_data(event.stream_id)
71 stream_bytes = ''
72 # Check if full grpc msg has been read into the recv buffer yet
73 if sr:
74 response_data = self._base_server.default_response_data(sr.response_size)
75 logging.info('Stream id: %d. total resp size: %d' % (event.stream_id, len(response_data)))
76 # Begin sending the response. Add ``self._pad_length`` padding to each
77 # data frame and split the whole message into data frames each carrying
78 # only self._read_chunk_size of data.
79 # The purpose is to have the majority of the data frame response bytes
80 # be padding bytes, since ``self._pad_length`` >> ``self._read_chunk_size``.
81 self._base_server.setup_send(response_data , event.stream_id, pad_length=self._pad_length, read_chunk_size=self._read_chunk_size)
82
83 def on_request_received(self, event):
84 self._base_server.on_request_received_default(event)
85 logging.info('on request received. Stream id: %s.' % event.stream_id)
86 self._total_updates[event.stream_id] = 0
87
88 # Log debug info and try to resume sending on all currently active streams.
89 def on_window_update(self, event):
90 logging.info('on window update. Stream id: %s. Delta: %s' % (event.stream_id, event.delta))
91 self._total_updates[event.stream_id] += event.delta
92 total = self._total_updates[event.stream_id]
93 logging.info('... - total updates for stream %d : %d' % (event.stream_id, total))
94 self._base_server.on_window_update_default(event, pad_length=self._pad_length, read_chunk_size=self._read_chunk_size)