1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2020. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% 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, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21-module(ssl_payload_SUITE).
22
23%% Note: This directive should only be used in test suites.
24-compile(export_all).
25
26-include_lib("common_test/include/ct.hrl").
27
28-define(TIMEOUT, 600000).
29
30
31%%--------------------------------------------------------------------
32%% Common Test interface functions -----------------------------------
33%%--------------------------------------------------------------------
34all() ->
35    [
36     {group, 'tlsv1.3'},
37     {group, 'tlsv1.2'},
38     {group, 'tlsv1.1'},
39     {group, 'tlsv1'},
40     {group, 'sslv3'}
41    ].
42
43groups() ->
44    [
45     {'tlsv1.3', [], payload_tests()},
46     {'tlsv1.2', [], payload_tests()},
47     {'tlsv1.1', [], payload_tests()},
48     {'tlsv1', [], payload_tests()},
49     {'sslv3', [], payload_tests()}
50    ].
51
52payload_tests() ->
53    [server_echos_passive_small,
54     server_echos_passive_chunk_small,
55     server_echos_active_once_small,
56     server_echos_active_small,
57     client_echos_passive_small,
58     client_echos_passive_chunk_small,
59     client_echos_active_once_small,
60     client_echos_active_small,
61     server_echos_passive_big,
62     server_echos_passive_chunk_big,
63     server_echos_active_once_big,
64     server_echos_active_big,
65     client_echos_passive_big,
66     client_echos_passive_chunk_big,
67     client_echos_active_once_big,
68     client_echos_active_big,
69     server_echos_passive_huge,
70     server_echos_passive_chunk_huge,
71     server_echos_active_once_huge,
72     server_echos_active_huge,
73     client_echos_passive_huge,
74     client_echos_passive_chunk_huge,
75     client_echos_active_once_huge,
76     client_echos_active_huge,
77     client_active_once_server_close].
78
79init_per_suite(Config) ->
80    catch crypto:stop(),
81    try crypto:start() of
82	ok ->
83	    ssl_test_lib:clean_start(),
84	    {ok, _} =
85                make_certs:all(
86                  proplists:get_value(data_dir, Config),
87                  proplists:get_value(priv_dir, Config)),
88	    ssl_test_lib:cert_options(Config)
89    catch _:_  ->
90	    {skip, "Crypto did not start"}
91    end.
92
93end_per_suite(_Config) ->
94    ssl:stop(),
95    application:stop(crypto).
96
97init_per_group(GroupName, Config) ->
98     case ssl_test_lib:is_tls_version(GroupName) of
99	true ->
100	    case ssl_test_lib:sufficient_crypto_support(GroupName) of
101		true ->
102		    ssl_test_lib:init_tls_version(GroupName, Config);
103		false ->
104		    {skip, "Missing crypto support"}
105	    end;
106	_ ->
107	    ssl:start(),
108	    Config
109    end.
110
111end_per_group(GroupName, Config) ->
112    case ssl_test_lib:is_tls_version(GroupName) of
113        true ->
114            ssl_test_lib:clean_tls_version(Config);
115        false ->
116            Config
117    end.
118
119init_per_testcase(TestCase, Config)
120  when TestCase == server_echos_passive_huge;
121       TestCase == server_echos_passive_chunk_huge;
122       TestCase == server_echos_active_once_huge;
123       TestCase == server_echos_active_huge;
124       TestCase == client_echos_passive_huge;
125       TestCase == client_echos_passive_chunk_huge;
126       TestCase == client_echos_active_once_huge;
127       TestCase == client_echos_active_huge ->
128    case erlang:system_info(system_architecture) of
129	"sparc-sun-solaris2.10" ->
130	    {skip,"Will take to long time on an old Sparc"};
131	_ ->
132	    ct:timetrap({seconds, 90}),
133	    Config
134    end;
135
136init_per_testcase(TestCase, Config)
137  when TestCase == server_echos_passive_big;
138       TestCase == server_echos_passive_chunk_big;
139       TestCase == server_echos_active_once_big;
140       TestCase == server_echos_active_big;
141       TestCase == client_echos_passive_big;
142       TestCase == client_echos_passive_chunk_big;
143       TestCase == client_echos_active_once_big;
144       TestCase == client_echos_active_big ->
145    ct:timetrap({seconds, 60}),
146    Config;
147
148init_per_testcase(_TestCase, Config) ->
149    ct:timetrap({seconds, 15}),
150    Config.
151
152end_per_testcase(_TestCase, Config) ->
153    Config.
154%%--------------------------------------------------------------------
155%% Test Cases --------------------------------------------------------
156%%--------------------------------------------------------------------
157
158server_echos_passive_small() ->
159    [{doc, "Client sends 1000 bytes in passive mode to server, that receives them, "
160     "sends them back, and closes."}].
161
162server_echos_passive_small(Config) when is_list(Config) ->
163    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
164    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
165    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
166    %%
167    Data = binary:copy(<<"1234567890">>, 100),
168    server_echos_passive(
169      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
170
171%%--------------------------------------------------------------------
172
173server_echos_passive_chunk_small() ->
174    [{doc, "Client sends 1000 bytes in passive mode to server, that receives them in chunks, "
175     "sends them back, and closes."}].
176
177server_echos_passive_chunk_small(Config) when is_list(Config) ->
178    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
179    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
180    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
181    %%
182    Data = binary:copy(<<"1234567890">>, 100),
183    server_echos_passive_chunk(
184      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
185
186
187%%--------------------------------------------------------------------
188
189server_echos_active_once_small() ->
190    [{doc, "Client sends 1000 bytes in active once mode to server, that receives "
191     " them, sends them back, and closes."}].
192
193server_echos_active_once_small(Config) when is_list(Config) ->
194        ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
195    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
196    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
197    %%
198    Data = binary:copy(<<"1234567890">>, 100),
199    server_echos_active_once(
200      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
201
202%%--------------------------------------------------------------------
203
204server_echos_active_small() ->
205    [{doc, "Client sends 1000 bytes in active mode to server, that receives them, "
206     "sends them back, and closes."}].
207
208server_echos_active_small(Config) when is_list(Config) ->
209    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
210    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
211    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
212    %%
213    Data = binary:copy(<<"1234567890">>, 100),
214    server_echos_active(
215      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
216
217%%--------------------------------------------------------------------
218client_echos_passive_small() ->
219    [{doc, "Server sends 1000 bytes in passive mode to client, that receives them, "
220      "sends them back, and closes."}].
221
222client_echos_passive_small(Config) when is_list(Config) ->
223    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
224    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
225    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
226    %%
227    Data = binary:copy(<<"1234567890">>, 100),
228    client_echos_passive(
229      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
230
231%%--------------------------------------------------------------------
232client_echos_passive_chunk__small() ->
233    [{doc, "Server sends 1000 bytes in passive mode to client, that receives them in chunks, "
234      "sends them back, and closes."}].
235
236client_echos_passive_chunk_small(Config) when is_list(Config) ->
237    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
238    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
239    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
240    %%
241    Data = binary:copy(<<"1234567890">>, 100),
242    client_echos_passive_chunk(
243      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
244
245
246%%--------------------------------------------------------------------
247client_echos_active_once_small() ->
248    ["Server sends 1000 bytes in active once mode to client, that receives "
249     "them, sends them back, and closes."].
250
251client_echos_active_once_small(Config) when is_list(Config) ->
252    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
253    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
254    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
255    %%
256    Data = binary:copy(<<"1234567890">>, 100),
257    client_echos_active_once(
258      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
259
260%%--------------------------------------------------------------------
261client_echos_active_small() ->
262    [{doc, "Server sends 1000 bytes in active mode to client, that receives them, "
263      "sends them back, and closes."}].
264
265client_echos_active_small(Config) when is_list(Config) ->
266    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
267    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
268    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
269    %%
270    Data = binary:copy(<<"1234567890">>, 100),
271    client_echos_active(
272      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
273
274
275%%--------------------------------------------------------------------
276server_echos_passive_big() ->
277    [{doc, "Client sends 50000 bytes to server in passive mode, that receives them, "
278     "sends them back, and closes."}].
279
280server_echos_passive_big(Config) when is_list(Config) ->
281    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
282    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
283    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
284    %%
285    Data = binary:copy(<<"1234567890">>, 5000),
286    server_echos_passive(
287      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
288%%--------------------------------------------------------------------
289server_echos_passive_chunk_big() ->
290    [{doc, "Client sends 50000 bytes to server in passive mode, that receives them, "
291     "sends them back, and closes."}].
292
293server_echos_passive_chunk_big(Config) when is_list(Config) ->
294    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
295    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
296    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
297    %%
298    Data = binary:copy(<<"1234567890">>, 5000),
299    server_echos_passive_chunk(
300      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
301
302%%--------------------------------------------------------------------
303
304server_echos_active_once_big() ->
305    [{doc,"Client sends 50000 bytes to server in active once mode, that receives "
306      "them, sends them back, and closes."}].
307
308server_echos_active_once_big(Config) when is_list(Config) ->
309    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
310    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
311    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
312    %%
313    Data = binary:copy(<<"1234567890">>, 5000),
314    server_echos_active_once(
315      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
316
317%%--------------------------------------------------------------------
318
319server_echos_active_big() ->
320    [{doc, "Client sends 50000 bytes to server in active once mode, that receives "
321      " them, sends them back, and closes."}].
322
323server_echos_active_big(Config) when is_list(Config) ->
324    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
325    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
326    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
327    %%
328    Data = binary:copy(<<"1234567890">>, 5000),
329    server_echos_active(
330      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
331
332%%--------------------------------------------------------------------
333client_echos_passive_big() ->
334    [{doc, "Server sends 50000 bytes to client in passive mode, that receives them, "
335     "sends them back, and closes."}].
336
337client_echos_passive_big(Config) when is_list(Config) ->
338    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
339    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
340    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
341    %%
342    Data = binary:copy(<<"1234567890">>, 5000),
343    client_echos_passive(
344      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
345
346
347%%--------------------------------------------------------------------
348client_echos_passive_chunk_big() ->
349    [{doc, "Server sends 50000 bytes to client in passive mode, that receives them, "
350     "sends them back, and closes."}].
351
352client_echos_passive_chunk_big(Config) when is_list(Config) ->
353    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
354    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
355    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
356    %%
357    Data = binary:copy(<<"1234567890">>, 5000),
358    client_echos_passive_chunk(
359      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
360
361
362%%--------------------------------------------------------------------
363client_echos_active_once_big() ->
364    [{doc, "Server sends 50000 bytes to client in active once mode, that receives"
365      " them, sends them back, and closes."}].
366
367client_echos_active_once_big(Config) when is_list(Config) ->
368    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
369    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
370    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
371    %%
372    Data = binary:copy(<<"1234567890">>, 5000),
373    client_echos_active_once(
374      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
375
376%%--------------------------------------------------------------------
377client_echos_active_big() ->
378    [{doc, "Server sends 50000 bytes to client in active mode, that receives them, "
379      "sends them back, and closes."}].
380
381client_echos_active_big(Config) when is_list(Config) ->
382     ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
383    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
384    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
385    %%
386    Data = binary:copy(<<"1234567890">>, 5000),
387    client_echos_active(
388      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
389
390%%--------------------------------------------------------------------
391server_echos_passive_huge() ->
392    [{doc, "Client sends 500000 bytes to server in passive mode, that receives "
393      " them, sends them back, and closes."}].
394
395server_echos_passive_huge(Config) when is_list(Config) ->
396    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
397    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
398    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
399    %%
400    Data = binary:copy(<<"1234567890">>, 50000),
401    server_echos_passive(
402      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
403
404%%--------------------------------------------------------------------
405server_echos_passive_chunk_huge() ->
406    [{doc, "Client sends 500000 bytes to server in passive mode, that receives "
407      " them, sends them back, and closes."}].
408
409server_echos_passive_chunk_huge(Config) when is_list(Config) ->
410    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
411    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
412    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
413    %%
414    Data = binary:copy(<<"1234567890">>, 50000),
415    server_echos_passive_chunk(
416      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
417
418%%--------------------------------------------------------------------
419server_echos_active_once_huge() ->
420    [{doc, "Client sends 500000 bytes to server in active once mode, that receives "
421      "them, sends them back, and closes."}].
422
423server_echos_active_once_huge(Config) when is_list(Config) ->
424        ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
425    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
426    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
427    %%
428    Data = binary:copy(<<"1234567890">>, 50000),
429    server_echos_active_once(
430      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
431
432%%--------------------------------------------------------------------
433server_echos_active_huge() ->
434    [{doc, "Client sends 500000 bytes to server in active mode, that receives them, "
435     "sends them back, and closes."}].
436
437server_echos_active_huge(Config) when is_list(Config) ->
438    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
439    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
440    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
441    %%
442    Data = binary:copy(<<"1234567890">>, 50000),
443    server_echos_active(
444      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
445
446%%--------------------------------------------------------------------
447client_echos_passive_huge() ->
448    [{doc, "Server sends 500000 bytes to client in passive mode, that receives "
449     "them, sends them back, and closes."}].
450
451client_echos_passive_huge(Config) when is_list(Config) ->
452    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
453    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
454    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
455    %%
456    Data = binary:copy(<<"1234567890">>, 50000),
457    client_echos_passive(
458      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
459%%--------------------------------------------------------------------
460client_echos_passive_chunk_huge() ->
461    [{doc, "Server sends 500000 bytes to client in passive mode, that receives "
462     "them, sends them back, and closes."}].
463
464client_echos_passive_chunk_huge(Config) when is_list(Config) ->
465    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
466    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
467    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
468    %%
469    Data = binary:copy(<<"1234567890">>, 50000),
470    client_echos_passive_chunk(
471      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
472%%--------------------------------------------------------------------
473client_echos_active_once_huge() ->
474    [{doc, "Server sends 500000 bytes to client in active once mode, that receives "
475      "them, sends them back, and closes."}].
476
477client_echos_active_once_huge(Config) when is_list(Config) ->
478    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
479    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
480    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
481    %%
482    Data = binary:copy(<<"1234567890">>, 50000),
483    client_echos_active_once(
484      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
485
486%%--------------------------------------------------------------------
487client_echos_active_huge() ->
488    [{doc, "Server sends 500000 bytes to client in active mode, that receives them, "
489     "sends them back, and closes."}].
490
491client_echos_active_huge(Config) when is_list(Config) ->
492     ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
493    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
494    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
495    %%
496    Data = binary:copy(<<"1234567890">>, 50000),
497    client_echos_active(
498      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
499
500
501%%--------------------------------------------------------------------
502client_active_once_server_close() ->
503    [{doc, "Server sends 500000 bytes and immediately after closes the connection"
504     "Make sure client recives all data if possible"}].
505
506client_active_once_server_close(Config) when is_list(Config) ->
507    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
508    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
509    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
510    %%
511    Data = binary:copy(<<"1234567890">>, 50000),
512    client_active_once_server_close(
513      Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname).
514
515
516
517%%--------------------------------------------------------------------
518%% Internal functions ------------------------------------------------
519%%--------------------------------------------------------------------
520
521server_echos_passive(
522  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
523    Length = byte_size(Data),
524    Server =
525        ssl_test_lib:start_server(
526          [{node, ServerNode}, {port, 0},
527           {from, self()},
528           {mfa, {?MODULE, echoer, [Length]}},
529           {options, [{active, false}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
530    Port = ssl_test_lib:inet_port(Server),
531    Client =
532        ssl_test_lib:start_client(
533          [{node, ClientNode}, {port, Port},
534           {host, Hostname},
535           {from, self()},
536           {mfa, {?MODULE, sender, [Data]}},
537           {options, [{active, false}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers() }]),
538    %%
539    ssl_test_lib:check_result(Server, ok, Client, ok),
540    %%
541    ssl_test_lib:close(Server),
542    ssl_test_lib:close(Client).
543
544server_echos_passive_chunk(
545  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
546    Length = byte_size(Data),
547    Server =
548        ssl_test_lib:start_server(
549          [{node, ServerNode}, {port, 0},
550           {from, self()},
551           {mfa, {?MODULE, echoer_chunk, [Length]}},
552           {options, [{active, false}, {mode, binary} | ServerOpts]}]),
553    Port = ssl_test_lib:inet_port(Server),
554    Client =
555        ssl_test_lib:start_client(
556          [{node, ClientNode}, {port, Port},
557           {host, Hostname},
558           {from, self()},
559           {mfa, {?MODULE, sender, [Data]}},
560           {options, [{active, false}, {mode, binary} | ClientOpts]}]),
561    %%
562    ssl_test_lib:check_result(Server, ok, Client, ok),
563    %%
564    ssl_test_lib:close(Server),
565    ssl_test_lib:close(Client).
566
567server_echos_active_once(
568  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
569    Length = byte_size(Data),
570    Server =
571        ssl_test_lib:start_server(
572          [{node, ServerNode}, {port, 0},
573           {from, self()},
574           {mfa, {?MODULE, echoer_active_once, [Length]}},
575           {options, [{active, once}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
576    Port = ssl_test_lib:inet_port(Server),
577    Client =
578        ssl_test_lib:start_client(
579          [{node, ClientNode}, {port, Port},
580           {host, Hostname},
581           {from, self()},
582           {mfa, {?MODULE, sender_active_once, [Data]}},
583           {options, [{active, once}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers() }]),
584    %%
585    ssl_test_lib:check_result(Server, ok, Client, ok),
586    %%
587    ssl_test_lib:close(Server),
588    ssl_test_lib:close(Client).
589
590
591server_echos_active(
592  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
593    Length = byte_size(Data),
594    Server =
595        ssl_test_lib:start_server(
596          [{node, ServerNode}, {port, 0},
597           {from, self()},
598           {mfa, {?MODULE, echoer_active, [Length]}},
599           {options, [{active, true}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
600    Port = ssl_test_lib:inet_port(Server),
601    Client =
602        ssl_test_lib:start_client(
603          [{node, ClientNode}, {port, Port},
604           {host, Hostname},
605           {from, self()},
606           {mfa, {?MODULE, sender_active, [Data]}},
607           {options, [{active, true}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
608    %%
609    ssl_test_lib:check_result(Server, ok, Client, ok),
610    %%
611    ssl_test_lib:close(Server),
612    ssl_test_lib:close(Client).
613
614client_echos_passive(
615  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
616    Length = byte_size(Data),
617    Server =
618        ssl_test_lib:start_server(
619          [{node, ServerNode}, {port, 0},
620           {from, self()},
621           {mfa, {?MODULE, sender, [Data]}},
622           {options, [{active, false}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
623    Port = ssl_test_lib:inet_port(Server),
624    Client =
625        ssl_test_lib:start_client(
626          [{node, ClientNode}, {port, Port},
627           {host, Hostname},
628           {from, self()},
629           {mfa, {?MODULE, echoer, [Length]}},
630           {options, [{active, false}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
631    %%
632    ssl_test_lib:check_result(Server, ok, Client, ok),
633    %%
634    ssl_test_lib:close(Server),
635    ssl_test_lib:close(Client).
636
637
638client_echos_passive_chunk(
639  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
640    Length = byte_size(Data),
641    Server =
642        ssl_test_lib:start_server(
643          [{node, ServerNode}, {port, 0},
644           {from, self()},
645           {mfa, {?MODULE, sender, [Data]}},
646           {options, [{active, false}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
647    Port = ssl_test_lib:inet_port(Server),
648    Client =
649        ssl_test_lib:start_client(
650          [{node, ClientNode}, {port, Port},
651           {host, Hostname},
652           {from, self()},
653           {mfa, {?MODULE, echoer_chunk, [Length]}},
654           {options, [{active, false}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
655    %%
656    ssl_test_lib:check_result(Server, ok, Client, ok),
657    %%
658    ssl_test_lib:close(Server),
659    ssl_test_lib:close(Client).
660
661
662client_echos_active_once(
663  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
664    Length = byte_size(Data),
665    Server =
666        ssl_test_lib:start_server(
667          [{node, ServerNode}, {port, 0},
668           {from, self()},
669           {mfa, {?MODULE, sender_active_once, [Data]}},
670           {options, [{active, once}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
671    Port = ssl_test_lib:inet_port(Server),
672    Client =
673        ssl_test_lib:start_client(
674          [{node, ClientNode}, {port, Port},
675           {host, Hostname},
676           {from, self()},
677           {mfa, {?MODULE, echoer_active_once, [Length]}},
678           {options,[{active, once}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
679    %%
680    ssl_test_lib:check_result(Server, ok, Client, ok),
681    %%
682    ssl_test_lib:close(Server),
683    ssl_test_lib:close(Client).
684
685client_echos_active(
686  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
687    Length = byte_size(Data),
688    Server =
689        ssl_test_lib:start_server(
690          [{node, ServerNode}, {port, 0},
691           {from, self()},
692           {mfa, {?MODULE, sender_active, [Data]}},
693           {options, [{active, true}, {mode, binary} | ServerOpts] ++ ssl_test_lib:bigger_buffers()}]),
694    Port = ssl_test_lib:inet_port(Server),
695    Client =
696        ssl_test_lib:start_client(
697          [{node, ClientNode}, {port, Port},
698           {host, Hostname},
699           {from, self()},
700           {mfa, {?MODULE, echoer_active, [Length]}},
701           {options, [{active, true}, {mode, binary} | ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
702    %
703    ssl_test_lib:check_result(Server, ok, Client, ok),
704    %%
705    ssl_test_lib:close(Server),
706    ssl_test_lib:close(Client).
707
708client_active_once_server_close(
709  Data, ClientOpts, ServerOpts, ClientNode, ServerNode, Hostname) ->
710    Length = byte_size(Data),
711    Server =
712        ssl_test_lib:start_server(
713          [{node, ServerNode}, {port, 0},
714           {from, self()},
715           {mfa, {?MODULE, send_close, [Data]}},
716           {options, [{active, once}, {mode, binary} | ServerOpts]}]),
717    Port = ssl_test_lib:inet_port(Server),
718    Client =
719        ssl_test_lib:start_client(
720          [{node, ClientNode}, {port, Port},
721           {host, Hostname},
722           {from, self()},
723           {mfa, {ssl_test_lib, active_once_recv, [Length]}},
724           {options,[{active, once}, {mode, binary} | ClientOpts]}]),
725    %%
726    ssl_test_lib:check_result(Server, ok, Client, ok).
727
728send(_Socket, _Data, 0, _) ->
729    ok;
730send(Socket, Data, Count, RecvEcho) ->
731    ok = ssl:send(Socket, Data),
732    RecvEcho(),
733    send(Socket, Data, Count - 1, RecvEcho).
734
735send_close(Socket, Data) ->
736    ok = ssl:send(Socket, Data),
737    ssl:close(Socket).
738
739sender(Socket, Data) ->
740    ct:log("Sender recv: ~p~n", [ssl:getopts(Socket, [active])]),
741    send(Socket, Data, 100,
742              fun() ->
743                      ssl_test_lib:recv_disregard(Socket, byte_size(Data))
744              end).
745
746sender_active_once(Socket, Data) ->
747    ct:log("Sender active once: ~p~n", [ssl:getopts(Socket, [active])]),
748    send(Socket, Data, 100,
749         fun() ->
750                 ssl_test_lib:active_once_disregard(Socket, byte_size(Data))
751         end).
752
753sender_active(Socket, Data) ->
754    ct:log("Sender active: ~p~n", [ssl:getopts(Socket, [active])]),
755    send(Socket, Data, 100,
756         fun() ->
757                 ssl_test_lib:active_disregard(Socket, byte_size(Data))
758         end).
759
760echoer(Socket, Size) ->
761    ct:log("Echoer recv: ~p~n", [ssl:getopts(Socket, [active])]),
762    echo_recv(Socket, Size * 100).
763
764echoer_chunk(Socket, Size) ->
765    ct:log("Echoer recv: ~p~n", [ssl:getopts(Socket, [active])]),
766    echo_recv_chunk(Socket, Size, Size * 100).
767
768echoer_active_once(Socket, Size) ->
769    ct:log("Echoer active once: ~p~n", [ssl:getopts(Socket, [active])]),
770    echo_active_once(Socket, Size * 100).
771
772echoer_active(Socket, Size) ->
773    ct:log("Echoer active: ~p~n", [ssl:getopts(Socket, [active])]),
774    echo_active(Socket, Size * 100).
775
776
777%% Receive Size bytes
778echo_recv(_Socket, 0) ->
779    ok;
780echo_recv(Socket, Size) ->
781    {ok, Data} = ssl:recv(Socket, 0),
782    spawn(fun() -> ssl:send(Socket, Data) end),
783    echo_recv(Socket, Size - byte_size(Data)).
784
785
786%% Receive Size bytes
787echo_recv_chunk(_Socket, _, 0) ->
788    ok;
789echo_recv_chunk(Socket, ChunkSize, Size) ->
790    {ok, Data} = ssl:recv(Socket, ChunkSize),
791    spawn(fun() -> ssl:send(Socket, Data) end),
792    echo_recv_chunk(Socket, ChunkSize, Size - ChunkSize).
793
794
795%% Receive Size bytes
796echo_active_once(_Socket, 0) ->
797    ok;
798echo_active_once(Socket, Size) ->
799    receive
800        {ssl, Socket, Data} ->
801            spawn(fun() -> ssl:send(Socket, Data) end),
802            NewSize = Size - byte_size(Data),
803            ssl:setopts(Socket, [{active, once}]),
804            echo_active_once(Socket, NewSize)
805    end.
806
807%% Receive Size bytes
808echo_active(_Socket, 0) ->
809    ok;
810echo_active(Socket, Size) ->
811    receive
812        {ssl, Socket, Data} ->
813            spawn(fun() -> ssl:send(Socket, Data) end),
814            echo_active(Socket, Size - byte_size(Data))
815    end.
816
817
818