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