-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathflow_control_SUITE.erl
184 lines (153 loc) · 5.88 KB
/
flow_control_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-module(flow_control_SUITE).
-include("http2.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
-compile([export_all]).
all() ->
[
exceed_server_connection_receive_window,
exceed_server_stream_receive_window,
server_buffer_response
].
init_per_suite(Config) ->
application:ensure_started(crypto),
Config.
end_per_suite(_Config) ->
ok.
init_per_testcase(
exceed_server_connection_receive_window,
Config) ->
PreChatterConfig =
[
{stream_callback_mod, server_connection_receive_window},
{initial_window_size, ?DEFAULT_INITIAL_WINDOW_SIZE * 2},
{flow_control, manual}
|Config],
chatterbox_test_buddy:start(PreChatterConfig);
init_per_testcase(
exceed_server_stream_receive_window,
Config) ->
PreChatterConfig =
[
{stream_callback_mod, server_stream_receive_window},
{initial_window_size, 64},
{flow_control, manual}
|Config],
chatterbox_test_buddy:start(PreChatterConfig);
init_per_testcase(server_buffer_response, Config) ->
PreChatterConfig =
[
{stream_callback_mod, flow_control_handler},
{initial_window_size, 64},
{flow_control, manual}
|Config],
chatterbox_test_buddy:start(PreChatterConfig);
init_per_testcase(_, Config) ->
Config.
end_per_testcase(_, Config) ->
chatterbox_test_buddy:stop(Config),
ok.
exceed_server_connection_receive_window(_Config) ->
Client = send_n_bytes(?DEFAULT_INITIAL_WINDOW_SIZE + 1),
%% Check for GO_AWAY
Resp = http2c:wait_for_n_frames(Client, 0, 1),
ct:pal("Resp: ~p", [Resp]),
?assertEqual(1, (length(Resp))),
[{GoAwayH, GoAway}] = Resp,
?assertEqual(?GOAWAY, (GoAwayH#frame_header.type)),
?assertEqual(?FLOW_CONTROL_ERROR, (h2_frame_goaway:error_code(GoAway))),
ok.
exceed_server_stream_receive_window(_Config) ->
Client = send_n_bytes(65),
%% First, pull off the window update frame we got on stream 0,
[WindowUpdate] = http2c:wait_for_n_frames(Client, 0, 1),
ct:pal("Expected window update, and got ~p", [WindowUpdate]),
%% now challenge that
{WUH, _} = WindowUpdate,
?assertEqual(?WINDOW_UPDATE, (WUH#frame_header.type)),
%% Check for RST_STREAM
Resp = http2c:wait_for_n_frames(Client, 3, 1),
ct:pal("Resp: ~p", [Resp]),
?assertEqual(1, (length(Resp))),
[{RstStreamH, RstStream}] = Resp,
?assertEqual(?RST_STREAM, (RstStreamH#frame_header.type)),
?assertEqual(?FLOW_CONTROL_ERROR, (h2_frame_rst_stream:error_code(RstStream))),
ok.
server_buffer_response(_Config) ->
WindowSize = 64,
application:load(chatterbox),
application:set_env(chatterbox, client_initial_window_size, WindowSize),
Headers = [{<<":path">>, <<"/">>},
{<<":method">>, <<"GET">>}],
{ok, Client} = http2c:start_link(),
{ok, {HeadersBin, _EC}} = hpack:encode(Headers, hpack:new_context()),
HF = {#frame_header{length=byte_size(HeadersBin),
type=?HEADERS,
flags=?FLAG_END_HEADERS bor ?FLAG_END_STREAM,
stream_id=3},
h2_frame_headers:new(HeadersBin)
},
http2c:send_unaltered_frames(Client, [HF]),
timer:sleep(300),
Resp1 = http2c:get_frames(Client, 3),
Size1 = data_frame_size(Resp1),
?assertEqual(WindowSize, Size1),
send_window_update(Client, 64),
timer:sleep(200),
Resp2 = http2c:get_frames(Client, 3),
Size2 = data_frame_size(Resp2),
?assertEqual(WindowSize, Size2),
send_window_update(Client, 64),
timer:sleep(200),
Resp3 = http2c:get_frames(Client, 3),
Size3 = data_frame_size(Resp3),
%% (68 * 2) - (64 * 2) = 8
?assertEqual(8, Size3).
data_frame_size(Frames) ->
DataFrames = lists:filter(fun({#frame_header{type=?DATA}, _}) -> true;
(_) -> false end, Frames),
lists:foldl(fun({_FH, DataP}, Acc) ->
Data = h2_frame_data:data(DataP),
Acc + byte_size(Data) end,
0, DataFrames).
send_window_update(Client, Size) ->
http2c:send_unaltered_frames(Client,
[{#frame_header{length=4,
type=?WINDOW_UPDATE,
stream_id=3},
h2_frame_window_update:new(Size)
}
]).
send_n_bytes(N) ->
%% We're up and running with a ridiculously small connection
%% window size of 64 bytes. The problem is that each stream will
%% have a recv window size of 64 bytes also, so both conditions
%% will be violated.
%% Also, a good client will not ever try and exceed the window
%% size, so we're going to use http2c to misbehave
{ok, Client} = http2c:start_link(),
%% Let's make a request:
Headers =
[
{<<":path">>, <<"/">>},
{<<":method">>, <<"POST">>}
],
{ok, {HeadersBin, _EncodeContext}} = hpack:encode(Headers, hpack:new_context()),
HeaderFrame = {#frame_header{
length=byte_size(HeadersBin),
type=?HEADERS,
flags=?FLAG_END_HEADERS,
stream_id=3
},
h2_frame_headers:new(HeadersBin)
},
http2c:send_unaltered_frames(Client, [HeaderFrame]),
%% If the server_connection_receive_window callback_mod worked,
%% this headers frame should have increased the stream's recv
%% window, but not the connections
%% So now, send N bytes and we should get some desired error.
Data = crypto:strong_rand_bytes(N),
Frames = h2_frame_data:to_frames(3, Data, #settings{}),
http2c:send_unaltered_frames(Client, Frames),
Client.
%% TODO: Tests for sending data when send_*_window is too small