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%%
22-module(ssl_packet_SUITE).
23
24-behaviour(ct_suite).
25
26-include_lib("common_test/include/ct.hrl").
27
28%% Callback functions
29-export([all/0,
30         groups/0,
31         init_per_suite/1,
32         end_per_suite/1,
33         init_per_group/2,
34         end_per_group/2,
35         init_per_testcase/2,
36         end_per_testcase/2]).
37
38%% Testcases
39-export([packet_raw_passive_many_small/0,
40         packet_raw_passive_many_small/1,
41         packet_0_passive_many_small/0,
42         packet_0_passive_many_small/1,
43         packet_1_passive_many_small/0,
44         packet_1_passive_many_small/1,
45         packet_2_passive_many_small/0,
46         packet_2_passive_many_small/1,
47         packet_4_passive_many_small/0,
48         packet_4_passive_many_small/1,
49         packet_raw_passive_some_big/0,
50         packet_raw_passive_some_big/1,
51         packet_0_passive_some_big/0,
52         packet_0_passive_some_big/1,
53         packet_1_passive_some_big/0,
54         packet_1_passive_some_big/1,
55         packet_2_passive_some_big/0,
56         packet_2_passive_some_big/1,
57         packet_4_passive_some_big/0,
58         packet_4_passive_some_big/1,
59         packet_wait_passive/0,
60         packet_wait_passive/1,
61         packet_size_passive/0,
62         packet_size_passive/1,
63         header_decode_one_byte_passive/0,
64         header_decode_one_byte_passive/1,
65         header_decode_two_bytes_passive/0,
66         header_decode_two_bytes_passive/1,
67         header_decode_two_bytes_two_sent_passive/0,
68         header_decode_two_bytes_two_sent_passive/1,
69         header_decode_two_bytes_one_sent_passive/0,
70         header_decode_two_bytes_one_sent_passive/1,
71         packet_httph_passive/0,
72         packet_httph_passive/1,
73         packet_httph_bin_passive/0,
74         packet_httph_bin_passive/1,
75         packet_http_error_passive/0,
76         packet_http_error_passive/1,
77         packet_baddata_passive/0,
78         packet_baddata_passive/1,
79         packet_raw_active_once_many_small/0,
80         packet_raw_active_once_many_small/1,
81         packet_0_active_once_many_small/0,
82         packet_0_active_once_many_small/1,
83         packet_1_active_once_many_small/0,
84         packet_1_active_once_many_small/1,
85         packet_2_active_once_many_small/0,
86         packet_2_active_once_many_small/1,
87         packet_4_active_once_many_small/0,
88         packet_4_active_once_many_small/1,
89         packet_raw_active_once_some_big/0,
90         packet_raw_active_once_some_big/1,
91         packet_0_active_once_some_big/0,
92         packet_0_active_once_some_big/1,
93         packet_1_active_once_some_big/0,
94         packet_1_active_once_some_big/1,
95         packet_2_active_once_some_big/0,
96         packet_2_active_once_some_big/1,
97         packet_4_active_once_some_big/0,
98         packet_4_active_once_some_big/1,
99         packet_httph_active_once/0,
100         packet_httph_active_once/1,
101         packet_httph_bin_active_once/0,
102         packet_httph_bin_active_once/1,
103         packet_raw_active_many_small/0,
104         packet_raw_active_many_small/1,
105         packet_0_active_many_small/0,
106         packet_0_active_many_small/1,
107         packet_1_active_many_small/0,
108         packet_1_active_many_small/1,
109         packet_2_active_many_small/0,
110         packet_2_active_many_small/1,
111         packet_4_active_many_small/0,
112         packet_4_active_many_small/1,
113         packet_raw_active_some_big/0,
114         packet_raw_active_some_big/1,
115         packet_0_active_some_big/0,
116         packet_0_active_some_big/1,
117         packet_1_active_some_big/0,
118         packet_1_active_some_big/1,
119         packet_2_active_some_big/0,
120         packet_2_active_some_big/1,
121         packet_4_active_some_big/0,
122         packet_4_active_some_big/1,
123         packet_wait_active/0,
124         packet_wait_active/1,
125         packet_size_active/0,
126         packet_size_active/1,
127         packet_switch/0,
128         packet_switch/1,
129         header_decode_one_byte_active/0,
130         header_decode_one_byte_active/1,
131         header_decode_two_bytes_active/0,
132         header_decode_two_bytes_active/1,
133         header_decode_two_bytes_two_sent_active/0,
134         header_decode_two_bytes_two_sent_active/1,
135         header_decode_two_bytes_one_sent_active/0,
136         header_decode_two_bytes_one_sent_active/1,
137         packet_httph_active/0,
138         packet_httph_active/1,
139         packet_httph_bin_active/0,
140         packet_httph_bin_active/1,
141         packet_baddata_active/0,
142         packet_baddata_active/1,
143         packet_cdr_decode/0,
144         packet_cdr_decode/1,
145         packet_cdr_decode_list/0,
146	 packet_cdr_decode_list/1,
147         packet_http_decode/0,
148         packet_http_decode/1,
149         packet_http_decode_list/0,
150         packet_http_decode_list/1,
151         packet_http_bin_decode_multi/0,
152         packet_http_bin_decode_multi/1,
153         packet_line_decode/0,
154         packet_line_decode/1,
155         packet_line_decode_list/0,
156         packet_line_decode_list/1,
157         packet_asn1_decode/0,
158         packet_asn1_decode/1,
159         packet_asn1_decode_list/0,
160	 packet_asn1_decode_list/1,
161         packet_sunrm_decode/0,
162         packet_sunrm_decode/1,
163         packet_sunrm_decode_list/0,
164         packet_sunrm_decode_list/1,
165         packet_send_to_large/0,
166         packet_send_to_large/1,
167         packet_tpkt_decode/0,
168         packet_tpkt_decode/1,
169         packet_tpkt_decode_list/0,
170         packet_tpkt_decode_list/1,
171         reject_packet_opt/0,
172         reject_packet_opt/1
173        ]).
174
175%% Apply export
176-export([send_raw/3,
177         passive_raw/3,
178         passive_recv_packet/3,
179         send/3,
180         send_incomplete/3,
181         active_once_raw/4,
182         active_once_packet/3,
183         active_raw/3,
184         active_once_raw/3,
185         active_packet/3,
186         assert_packet_opt/2,
187         server_packet_decode/2,
188         client_packet_decode/2,
189         server_header_decode_active/3,
190         client_header_decode_active/3,
191         server_header_decode_passive/3,
192         client_header_decode_passive/3,
193         server_line_packet_decode/2,
194         server_line_packet_decode/4,
195         client_line_packet_decode/2,
196         client_line_packet_decode/5,
197         server_http_decode/2,
198         client_http_decode/2,
199         server_http_bin_decode/3,
200         client_http_bin_decode/3,
201         client_http_decode_list/2,
202         server_http_decode_error/2,
203         server_send_trailer/2,
204         client_http_decode_trailer_active/1,
205         client_http_decode_trailer_bin_active/1,
206         client_http_decode_trailer_active_once/1,
207         client_http_decode_trailer_bin_active_once/1,
208         client_http_decode_trailer_passive/1,
209         client_http_decode_trailer_bin_passive/1,
210         add_tpkt_header/1,
211         client_reject_packet_opt/2,
212         send_switch_packet/3,
213         recv_switch_packet/3
214         ]).
215
216-define(BYTE(X),     X:8/unsigned-big-integer).
217-define(UINT16(X),   X:16/unsigned-big-integer).
218-define(UINT24(X),   X:24/unsigned-big-integer).
219-define(UINT32(X),   X:32/unsigned-big-integer).
220-define(UINT64(X),   X:64/unsigned-big-integer).
221-define(STRING(X),   ?UINT32((byte_size(X))), (X)/binary).
222
223-define(byte(X),   << ?BYTE(X) >> ).
224-define(uint16(X), << ?UINT16(X) >> ).
225-define(uint24(X), << ?UINT24(X) >> ).
226-define(uint32(X), << ?UINT32(X) >> ).
227-define(uint64(X), << ?UINT64(X) >> ).
228
229-define(MANY, 1000).
230-define(SOME, 50).
231-define(BASE_TIMEOUT_SECONDS, 20).
232-define(SOME_SCALE, 2).
233-define(MANY_SCALE, 3).
234
235%%--------------------------------------------------------------------
236%% Common Test interface functions -----------------------------------
237%%--------------------------------------------------------------------
238all() ->
239    [
240     {group, 'tlsv1.3'},
241     {group, 'tlsv1.2'},
242     {group, 'tlsv1.1'},
243     {group, 'tlsv1'},
244     {group, 'dtlsv1.2'},
245     {group, 'dtlsv1'}
246    ].
247
248groups() ->
249    [{'tlsv1.3', [], socket_packet_tests() ++ protocol_packet_tests()},
250     {'tlsv1.2', [], socket_packet_tests() ++ protocol_packet_tests()},
251     {'tlsv1.1', [], socket_packet_tests() ++ protocol_packet_tests()},
252     {'tlsv1', [], socket_packet_tests() ++ protocol_packet_tests()},
253     %% We will not support any packet types if the transport is
254     %% not reliable. We might support it for DTLS over SCTP in the future
255     {'dtlsv1.2', [], [reject_packet_opt]},
256     {'dtlsv1', [],  [reject_packet_opt]}
257    ].
258
259socket_packet_tests() ->
260    socket_active_packet_tests() ++ socket_active_once_packet_tests() ++
261        socket_passive_packet_tests() ++ [packet_send_to_large, packet_tpkt_decode, packet_tpkt_decode_list].
262
263protocol_packet_tests() ->
264    protocol_active_packet_tests() ++ protocol_active_once_packet_tests() ++ protocol_passive_packet_tests() ++
265	[packet_cdr_decode, packet_cdr_decode_list,
266	 packet_http_decode, packet_http_decode_list,
267	 packet_http_bin_decode_multi,
268	 packet_line_decode, packet_line_decode_list,
269	 packet_asn1_decode, packet_asn1_decode_list,
270	 packet_sunrm_decode, packet_sunrm_decode_list].
271
272socket_passive_packet_tests() ->
273    [packet_raw_passive_many_small,
274     packet_0_passive_many_small,
275     packet_1_passive_many_small,
276     packet_2_passive_many_small,
277     packet_4_passive_many_small,
278     packet_raw_passive_some_big,
279     packet_0_passive_some_big,
280     packet_1_passive_some_big,
281     packet_2_passive_some_big,
282     packet_4_passive_some_big,
283     packet_wait_passive,
284     packet_size_passive,
285     %% inet header option should be deprecated!
286     header_decode_one_byte_passive,
287     header_decode_two_bytes_passive,
288     header_decode_two_bytes_two_sent_passive,
289     header_decode_two_bytes_one_sent_passive
290    ].
291
292protocol_passive_packet_tests() ->
293    [packet_httph_passive,
294     packet_httph_bin_passive,
295     packet_http_error_passive,
296     packet_baddata_passive
297    ].
298
299socket_active_once_packet_tests() ->
300    [packet_raw_active_once_many_small,
301     packet_0_active_once_many_small,
302     packet_1_active_once_many_small,
303     packet_2_active_once_many_small,
304     packet_4_active_once_many_small,
305     packet_raw_active_once_some_big,
306     packet_0_active_once_some_big,
307     packet_1_active_once_some_big,
308     packet_2_active_once_some_big,
309     packet_4_active_once_some_big
310    ].
311
312protocol_active_once_packet_tests() ->
313    [
314     packet_httph_active_once,
315     packet_httph_bin_active_once
316    ].
317
318socket_active_packet_tests() ->
319    [packet_raw_active_many_small,
320     packet_0_active_many_small,
321     packet_1_active_many_small,
322     packet_2_active_many_small,
323     packet_4_active_many_small,
324     packet_raw_active_some_big,
325     packet_0_active_some_big,
326     packet_1_active_some_big,
327     packet_2_active_some_big,
328     packet_4_active_some_big,
329     packet_wait_active,
330     packet_size_active,
331     packet_switch,
332     %% inet header option should be deprecated!
333     header_decode_one_byte_active,
334     header_decode_two_bytes_active,
335     header_decode_two_bytes_two_sent_active,
336     header_decode_two_bytes_one_sent_active
337    ].
338
339
340protocol_active_packet_tests() ->
341    [packet_httph_active,
342     packet_httph_bin_active,
343     packet_baddata_active
344    ].
345
346init_per_suite(Config) ->
347    catch crypto:stop(),
348    try crypto:start() of
349	ok ->
350	    ssl_test_lib:clean_start(),
351	    {ok, _} = make_certs:all(proplists:get_value(data_dir, Config),
352				     proplists:get_value(priv_dir, Config)),
353	    ssl_test_lib:cert_options(Config)
354    catch _:_ ->
355	    {skip, "Crypto did not start"}
356    end.
357
358end_per_suite(_Config) ->
359    ssl:stop(),
360    application:stop(crypto).
361
362init_per_group(GroupName, Config) ->
363    ssl_test_lib:init_per_group(GroupName, Config).
364
365end_per_group(GroupName, Config) ->
366  ssl_test_lib:end_per_group(GroupName, Config).
367
368init_per_testcase(_TestCase, Config) ->
369    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS}),
370    Config.
371
372
373end_per_testcase(_TestCase, Config) ->
374    Config.
375
376%%--------------------------------------------------------------------
377%% Test Cases --------------------------------------------------------
378%%--------------------------------------------------------------------
379
380packet_raw_passive_many_small() ->
381    [{doc,"Test packet option {packet, raw} in passive mode."}].
382
383packet_raw_passive_many_small(Config) when is_list(Config) ->
384    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
385    Data = "Packet option is {packet, raw}",
386    packet(Config, Data, send, passive_raw, ?MANY, raw, false).
387
388%%--------------------------------------------------------------------
389
390packet_raw_passive_some_big() ->
391    [{doc,"Test packet option {packet, raw} in passive mode."}].
392
393packet_raw_passive_some_big(Config) when is_list(Config) ->
394    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
395    Data = lists:append(lists:duplicate(100, "1234567890")),
396    packet(Config, Data, send, passive_raw, ?SOME, raw, false).
397%%--------------------------------------------------------------------
398packet_0_passive_many_small() ->
399    [{doc,"Test packet option {packet, 0} in passive mode."}].
400
401packet_0_passive_many_small(Config) when is_list(Config) ->
402    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
403    Data = "Packet option is {packet, 0}, equivalent to packet raw.",
404    packet(Config, Data, send, passive_raw, ?MANY, 0, false).
405
406%%--------------------------------------------------------------------
407packet_0_passive_some_big() ->
408    [{doc,"Test packet option {packet, 0} in passive mode."}].
409
410packet_0_passive_some_big(Config) when is_list(Config) ->
411    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
412    Data = lists:append(lists:duplicate(100, "1234567890")),
413    packet(Config, Data, send, passive_raw, ?SOME, 0, false).
414
415%%--------------------------------------------------------------------
416packet_1_passive_many_small() ->
417    [{doc,"Test packet option {packet, 1} in passive mode."}].
418
419packet_1_passive_many_small(Config) when is_list(Config) ->
420    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
421    Data = "Packet option is {packet, 1}",
422    packet(Config, Data, send, passive_recv_packet, ?MANY, 1, false).
423
424%%--------------------------------------------------------------------
425packet_1_passive_some_big() ->
426    [{doc,"Test packet option {packet, 1} in passive mode."}].
427
428packet_1_passive_some_big(Config) when is_list(Config) ->
429    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
430    Data = lists:append(lists:duplicate(255, "1")),
431    packet(Config, Data, send, passive_recv_packet, ?SOME, 1, false).
432
433%%--------------------------------------------------------------------
434packet_2_passive_many_small() ->
435    [{doc,"Test packet option {packet, 2} in passive mode"}].
436
437packet_2_passive_many_small(Config) when is_list(Config) ->
438    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
439    Data = "Packet option is {packet, 2}",
440    packet(Config, Data, send, passive_recv_packet, ?MANY, 2, false).
441
442%%--------------------------------------------------------------------
443packet_2_passive_some_big() ->
444    [{doc,"Test packet option {packet, 2} in passive mode"}].
445
446packet_2_passive_some_big(Config) when is_list(Config) ->
447    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
448    Data = lists:append(lists:duplicate(100, "1234567890")),
449    packet(Config, Data, send, passive_recv_packet, ?SOME, 2, false).
450
451%%--------------------------------------------------------------------
452packet_4_passive_many_small() ->
453    [{doc,"Test packet option {packet, 4} in passive mode"}].
454
455packet_4_passive_many_small(Config) when is_list(Config) ->
456    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
457    Data = "Packet option is {packet, 4}",
458    packet(Config, Data, send, passive_recv_packet, ?MANY, 4, false).
459
460%%--------------------------------------------------------------------
461packet_4_passive_some_big() ->
462    [{doc,"Test packet option {packet, 4} in passive mode"}].
463
464packet_4_passive_some_big(Config) when is_list(Config) ->
465    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
466    Data = lists:append(lists:duplicate(100, "1234567890")),
467    packet(Config, Data, send, passive_recv_packet, ?SOME, 4, false).
468
469%%--------------------------------------------------------------------
470packet_raw_active_once_many_small() ->
471    [{doc,"Test packet option {packet, raw} in active once mode."}].
472
473packet_raw_active_once_many_small(Config) when is_list(Config) ->
474    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
475    Data = "Packet option is {packet, raw}",
476    packet(Config, Data, send_raw, active_once_raw, ?MANY, raw, once).
477
478%%--------------------------------------------------------------------
479packet_raw_active_once_some_big() ->
480    [{doc,"Test packet option {packet, raw} in active once mode."}].
481
482packet_raw_active_once_some_big(Config) when is_list(Config) ->
483    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
484    Data = lists:append(lists:duplicate(100, "1234567890")),
485    packet(Config, Data, send_raw, active_once_raw, ?SOME, raw, once).
486
487%%--------------------------------------------------------------------
488packet_0_active_once_many_small() ->
489    [{doc,"Test packet option {packet, 0} in active once mode."}].
490
491packet_0_active_once_many_small(Config) when is_list(Config) ->
492    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
493    Data = "Packet option is {packet, 0}",
494    packet(Config, Data, send_raw, active_once_raw, ?MANY, 0, once).
495
496%%--------------------------------------------------------------------
497packet_0_active_once_some_big() ->
498    [{doc,"Test packet option {packet, 0} in active once mode."}].
499
500packet_0_active_once_some_big(Config) when is_list(Config) ->
501    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
502    Data = lists:append(lists:duplicate(100, "1234567890")),
503    packet(Config, Data, send_raw, active_once_raw, ?SOME, 0, once).
504
505%%--------------------------------------------------------------------
506packet_1_active_once_many_small() ->
507    [{doc,"Test packet option {packet, 1} in active once mode."}].
508
509packet_1_active_once_many_small(Config) when is_list(Config) ->
510    Data = "Packet option is {packet, 1}",
511    packet(Config, Data, send, active_once_packet, ?MANY, 1, once).
512
513%%--------------------------------------------------------------------
514packet_1_active_once_some_big() ->
515    [{doc,"Test packet option {packet, 1} in active once mode."}].
516
517packet_1_active_once_some_big(Config) when is_list(Config) ->
518    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
519    Data = lists:append(lists:duplicate(255, "1")),
520    packet(Config, Data, send, active_once_packet, ?SOME, 1, once).
521
522
523%%--------------------------------------------------------------------
524packet_2_active_once_many_small() ->
525    [{doc,"Test packet option {packet, 2} in active once mode"}].
526
527packet_2_active_once_many_small(Config) when is_list(Config) ->
528    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
529    Data = "Packet option is {packet, 2}",
530    packet(Config, Data, send, active_once_packet, ?MANY, 2, once).
531
532%%--------------------------------------------------------------------
533packet_2_active_once_some_big() ->
534    [{doc,"Test packet option {packet, 2} in active once mode"}].
535
536packet_2_active_once_some_big(Config) when is_list(Config) ->
537    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
538    Data = lists:append(lists:duplicate(100, "1234567890")),
539    packet(Config, Data, send, active_once_raw, ?SOME, 2, once).
540
541%%--------------------------------------------------------------------
542packet_4_active_once_many_small() ->
543    [{doc,"Test packet option {packet, 4} in active once mode"}].
544
545packet_4_active_once_many_small(Config) when is_list(Config) ->
546    Data = "Packet option is {packet, 4}",
547    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
548    packet(Config, Data, send, active_once_packet, ?MANY, 4, once).
549
550%%--------------------------------------------------------------------
551packet_4_active_once_some_big() ->
552    [{doc,"Test packet option {packet, 4} in active once mode"}].
553
554packet_4_active_once_some_big(Config) when is_list(Config) ->
555    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
556    Data = lists:append(lists:duplicate(100, "1234567890")),
557    packet(Config, Data, send, active_once_packet, ?SOME, 4, once).
558
559%%--------------------------------------------------------------------
560packet_raw_active_many_small() ->
561    [{doc,"Test packet option {packet, raw} in active mode."}].
562
563packet_raw_active_many_small(Config) when is_list(Config) ->
564    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
565    Data = "Packet option is {packet, raw}",
566    packet(Config, Data, send_raw, active_raw, ?MANY, raw, true).
567
568%%--------------------------------------------------------------------
569packet_raw_active_some_big() ->
570    [{doc,"Test packet option {packet, raw} in active mode."}].
571
572packet_raw_active_some_big(Config) when is_list(Config) ->
573    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
574    Data = lists:append(lists:duplicate(100, "1234567890")),
575    packet(Config, Data, send_raw, active_raw, ?SOME, raw, true).
576
577%%--------------------------------------------------------------------
578packet_0_active_many_small() ->
579    [{doc,"Test packet option {packet, 0} in active mode."}].
580
581packet_0_active_many_small(Config) when is_list(Config) ->
582    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
583    Data = "Packet option is {packet, 0}",
584    packet(Config, Data, send_raw, active_raw, ?MANY, 0, true).
585
586%%--------------------------------------------------------------------
587packet_0_active_some_big() ->
588    [{doc,"Test packet option {packet, 0} in active mode."}].
589
590packet_0_active_some_big(Config) when is_list(Config) ->
591    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
592    Data = lists:append(lists:duplicate(100, "1234567890")),
593    packet(Config, Data, send, active_raw, ?SOME, 0, true).
594
595%%--------------------------------------------------------------------
596packet_1_active_many_small() ->
597    [{doc,"Test packet option {packet, 1} in active mode."}].
598
599packet_1_active_many_small(Config) when is_list(Config) ->
600    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
601    Data = "Packet option is {packet, 1}",
602    packet(Config, Data, send, active_packet, ?MANY, 1, true).
603
604%%--------------------------------------------------------------------
605packet_1_active_some_big() ->
606    [{doc,"Test packet option {packet, 1} in active mode."}].
607
608packet_1_active_some_big(Config) when is_list(Config) ->
609    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
610    Data = lists:append(lists:duplicate(255, "1")),
611    packet(Config, Data, send, active_packet, ?SOME, 1, true).
612
613%%--------------------------------------------------------------------
614packet_2_active_many_small() ->
615    [{doc,"Test packet option {packet, 2} in active mode"}].
616
617packet_2_active_many_small(Config) when is_list(Config) ->
618      ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
619    Data = "Packet option is {packet, 2}",
620    packet(Config, Data, send, active_packet, ?MANY, 2, true).
621
622%%--------------------------------------------------------------------
623packet_2_active_some_big() ->
624    [{doc,"Test packet option {packet, 2} in active mode"}].
625
626packet_2_active_some_big(Config) when is_list(Config) ->
627    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
628    Data = lists:append(lists:duplicate(100, "1234567890")),
629    packet(Config, Data, send, active_packet, ?SOME, 2, true).
630
631%%--------------------------------------------------------------------
632packet_4_active_many_small() ->
633    [{doc,"Test packet option {packet, 4} in active mode"}].
634
635packet_4_active_many_small(Config) when is_list(Config) ->
636    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?MANY_SCALE}),
637    Data = "Packet option is {packet, 4}",
638    packet(Config, Data, send, active_packet, ?MANY, 4, true).
639
640%%--------------------------------------------------------------------
641packet_4_active_some_big() ->
642    [{doc,"Test packet option {packet, 4} in active mode"}].
643
644packet_4_active_some_big(Config) when is_list(Config) ->
645    ct:timetrap({seconds, ?BASE_TIMEOUT_SECONDS * ?SOME_SCALE}),
646    Data = lists:append(lists:duplicate(100, "1234567890")),
647    packet(Config, Data, send, active_packet, ?SOME, 4, true).
648
649%%--------------------------------------------------------------------
650packet_send_to_large() ->
651    [{doc,"Test setting the packet option {packet, 2} on the send side"}].
652
653packet_send_to_large(Config) when is_list(Config) ->
654    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
655    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
656    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
657
658    Data = lists:append(lists:duplicate(30, "1234567890")),
659
660    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
661					{from, self()},
662					{mfa, {?MODULE, send, [Data, 1]}},
663					{options, [{packet, 1}| ServerOpts]}]),
664    Port = ssl_test_lib:inet_port(Server),
665    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
666					{host, Hostname},
667					{from, self()},
668					{mfa, {?MODULE, active_packet, [Data, 1]}},
669					{options, [{active, true} | ClientOpts]}]),
670
671    ssl_test_lib:check_result(Server, {error, {badarg,
672					       {packet_to_large, 300, 255}}}),
673
674    ssl_test_lib:close(Server),
675    ssl_test_lib:close(Client).
676
677%%--------------------------------------------------------------------
678packet_wait_active() ->
679    [{doc,"Test waiting when complete packages have not arrived"}].
680
681packet_wait_active(Config) when is_list(Config) ->
682    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
683    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
684    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
685
686    Data = list_to_binary(lists:duplicate(100, "1234567890")),
687
688    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
689					{from, self()},
690					{mfa, {?MODULE, send_incomplete,
691					       [Data, ?SOME]}},
692					{options, ServerOpts}]),
693    Port = ssl_test_lib:inet_port(Server),
694    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
695					{host, Hostname},
696					{from, self()},
697					{mfa,
698					 {?MODULE, active_packet,
699					  [binary_to_list(Data), ?SOME]}},
700					{options, [{active, true},
701						   {packet, 4} |
702						   ClientOpts]}]),
703
704    ssl_test_lib:check_result(Client, ok),
705
706    ssl_test_lib:close(Server),
707    ssl_test_lib:close(Client).
708
709
710%%--------------------------------------------------------------------
711packet_wait_passive() ->
712    [{doc,"Test waiting when complete packages have not arrived"}].
713
714packet_wait_passive(Config) when is_list(Config) ->
715    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
716    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
717    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
718
719    Data = list_to_binary(lists:duplicate(100, "1234567890")),
720
721    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
722					{from, self()},
723					{mfa, {?MODULE, send_incomplete,
724					       [Data, ?SOME]}},
725					{options, ServerOpts}]),
726    Port = ssl_test_lib:inet_port(Server),
727    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
728					{host, Hostname},
729					{from, self()},
730					{mfa, {?MODULE, passive_recv_packet,
731					       [binary_to_list(Data), ?SOME]}},
732					{options, [{active, false},
733						   {packet, 4} |
734						   ClientOpts]}]),
735
736    ssl_test_lib:check_result(Client, ok),
737
738    ssl_test_lib:close(Server),
739    ssl_test_lib:close(Client).
740%%--------------------------------------------------------------------
741packet_baddata_active() ->
742    [{doc,"Test that if a bad packet arrives error msg is sent and socket is closed"}].
743
744packet_baddata_active(Config) when is_list(Config) ->
745    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
746    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
747    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
748
749    Data = list_to_binary(lists:duplicate(100, "1234567890")),
750
751    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
752					{from, self()},
753					{mfa, {?MODULE, send_incomplete ,[Data, 1]}},
754					{options, ServerOpts}]),
755    Port = ssl_test_lib:inet_port(Server),
756    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
757					{host, Hostname},
758					{from, self()},
759					{mfa, {?MODULE, active_packet, [Data, 1]}},
760					{options, [{active, true},
761						   {packet, cdr} |
762						   ClientOpts]}]),
763    receive
764	{Client, {ssl_error, _, {invalid_packet, _}}} ->
765            ok;
766	Unexpected ->
767	    ct:fail({unexpected, Unexpected})
768    end,
769
770
771    ssl_test_lib:close(Server),
772    ssl_test_lib:close(Client).
773%%--------------------------------------------------------------------
774packet_baddata_passive() ->
775    [{doc,"Test that if a bad packet arrives error msg is sent and socket is closed"}].
776
777packet_baddata_passive(Config) when is_list(Config) ->
778    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
779    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
780    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
781
782    Data = list_to_binary(lists:duplicate(100, "1234567890")),
783
784    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
785					{from, self()},
786					{mfa, {?MODULE, send_incomplete ,[Data, 1]}},
787					{options, ServerOpts}]),
788    Port = ssl_test_lib:inet_port(Server),
789    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
790					{host, Hostname},
791					{from, self()},
792					{mfa, {?MODULE, passive_recv_packet,
793					       [Data, 1]}},
794					{options, [{active, false},
795						   {packet, cdr} |
796						   ClientOpts]}]),
797
798    receive
799	{Client, {error, {invalid_packet, _}}} ->
800            ok;
801	Unexpected ->
802	    ct:fail({unexpected, Unexpected})
803    end,
804
805    ssl_test_lib:close(Server),
806    ssl_test_lib:close(Client).
807%%--------------------------------------------------------------------
808
809packet_size_active() ->
810    [{doc,"Test that if a packet of size larger than
811    packet_size arrives error msg is sent and socket is closed"}].
812
813packet_size_active(Config) when is_list(Config) ->
814    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
815    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
816    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
817
818    Data = list_to_binary(lists:duplicate(100, "1234567890")),
819
820    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
821					{from, self()},
822					{mfa, {?MODULE, send_incomplete ,[Data, 1]}},
823					{options, ServerOpts}]),
824    Port = ssl_test_lib:inet_port(Server),
825    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
826					{host, Hostname},
827					{from, self()},
828					{mfa, {?MODULE, active_packet, [Data, 1]}},
829					{options, [{active, true},
830						   {packet, 4}, {packet_size, 10} |
831						   ClientOpts]}]),
832    receive
833	{Client, {ssl_error, _, {invalid_packet, _}}}->
834            ok;
835	Unexpected ->
836	    ct:fail({unexpected, Unexpected})
837    end,
838
839    ssl_test_lib:close(Server),
840    ssl_test_lib:close(Client).
841%%--------------------------------------------------------------------
842
843packet_size_passive() ->
844    [{doc, "Test that if a packet of size larger
845    than packet_size arrives error msg is sent and socket is closed"}].
846
847packet_size_passive(Config) when is_list(Config) ->
848    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
849    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
850    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
851
852    Data = list_to_binary(lists:duplicate(100, "1234567890")),
853
854    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
855					{from, self()},
856					{mfa, {?MODULE, send_incomplete ,[Data, 1]}},
857					{options, ServerOpts}]),
858    Port = ssl_test_lib:inet_port(Server),
859    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
860					{host, Hostname},
861					{from, self()},
862					{mfa, {?MODULE, passive_recv_packet,
863					       [Data, 1]}},
864					{options, [{active, false},
865						   {packet, 4}, {packet_size, 30} |
866						   ClientOpts]}]),
867    receive
868	{Client, {error, {invalid_packet, _}}} ->
869            ok;
870	Unexpected ->
871	    ct:fail({unexpected, Unexpected})
872    end,
873
874    ssl_test_lib:close(Server),
875    ssl_test_lib:close(Client).
876
877
878%%--------------------------------------------------------------------
879packet_switch() ->
880    [{doc,"Test packet option {packet, 2} followd by {packet, 4}"}].
881
882packet_switch(Config) when is_list(Config) ->
883    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
884    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
885    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
886
887    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
888					{from, self()},
889					{mfa, {?MODULE, send_switch_packet ,["Hello World", 4]}},
890					{options, [{nodelay, true},{packet, 2} | ServerOpts]}]),
891    Port = ssl_test_lib:inet_port(Server),
892    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
893					{host, Hostname},
894					{from, self()},
895					{mfa, {?MODULE, recv_switch_packet, ["Hello World", 4]}},
896					{options, [{nodelay, true}, {packet, 2} |
897						   ClientOpts]}]),
898
899    ssl_test_lib:check_result(Client, ok, Server, ok),
900
901    ssl_test_lib:close(Server),
902    ssl_test_lib:close(Client).
903
904
905%%--------------------------------------------------------------------
906packet_cdr_decode() ->
907    [{doc,"Test setting the packet option {packet, cdr}, {mode, binary}"}].
908packet_cdr_decode(Config) when is_list(Config) ->
909    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
910    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
911    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
912
913    %% A valid cdr packet
914    Data = <<71,73,79,80,1,2,2,1,0,0,0,41,0,0,0,0,0,0,0,0,0,0,0,1,78,
915	     69,79,0,0,0,0,2,0,10,0,0,0,0,0,0,0,0,0,18,0,0,0,0,0,0,0,4,49>>,
916
917    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
918					{from, self()},
919					{mfa, {?MODULE, server_packet_decode,
920					       [Data]}},
921					{options, [{active, true}, binary,
922						   {packet, cdr}|ServerOpts]}]),
923
924    Port = ssl_test_lib:inet_port(Server),
925    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
926					{host, Hostname},
927					{from, self()},
928					{mfa, {?MODULE, client_packet_decode,
929					       [Data]}},
930					{options, [{active, true}, {packet, cdr},
931						   binary | ClientOpts]}]),
932
933    ssl_test_lib:check_result(Server, ok, Client, ok),
934
935    ssl_test_lib:close(Server),
936    ssl_test_lib:close(Client).
937
938%%--------------------------------------------------------------------
939packet_cdr_decode_list() ->
940    [{doc,"Test setting the packet option {packet, cdr} {mode, list}"}].
941packet_cdr_decode_list(Config) when is_list(Config) ->
942    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
943    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
944    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
945
946    %% A valid cdr packet
947    Data = [71,73,79,80,1,2,2,1,0,0,0,41,0,0,0,0,0,0,0,0,0,0,0,1,78,
948	     69,79,0,0,0,0,2,0,10,0,0,0,0,0,0,0,0,0,18,0,0,0,0,0,0,0,4,49],
949
950    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
951					{from, self()},
952					{mfa, {?MODULE, server_packet_decode,
953					       [Data]}},
954					{options, [{active, true}, list,
955						   {packet, cdr}|ServerOpts]}]),
956
957    Port = ssl_test_lib:inet_port(Server),
958    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
959					{host, Hostname},
960					{from, self()},
961					{mfa, {?MODULE, client_packet_decode,
962					       [Data]}},
963					{options, [{active, true}, {packet, cdr},
964						   list | ClientOpts]}]),
965
966    ssl_test_lib:check_result(Server, ok, Client, ok),
967
968    ssl_test_lib:close(Server),
969    ssl_test_lib:close(Client).
970
971%%--------------------------------------------------------------------
972packet_http_decode() ->
973    [{doc, "Test setting the packet option {packet, http} {mode, binary} "
974     "(Body will be binary http strings are lists)"}].
975
976packet_http_decode(Config) when is_list(Config) ->
977    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
978    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
979    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
980
981    Request = "GET / HTTP/1.1\r\n"
982              "host: www.example.com\r\n"
983	      "user-agent: HttpTester\r\n"
984	      "\r\n",
985    Response = "HTTP/1.1 200 OK\r\n"
986	       "\r\n"
987	       "Hello!",
988
989    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
990					{from, self()},
991					{mfa, {?MODULE, server_http_decode,
992					       [Response]}},
993					{options, [{active, true},binary,
994						   {packet, http} | ServerOpts]}]),
995
996    Port = ssl_test_lib:inet_port(Server),
997    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
998					{host, Hostname},
999					{from, self()},
1000					{mfa, {?MODULE, client_http_decode,
1001					       [Request]}},
1002					{options, [{active, true}, binary,
1003						   {packet, http} |
1004						   ClientOpts]}]),
1005
1006    ssl_test_lib:check_result(Server, ok, Client, ok),
1007
1008    ssl_test_lib:close(Server),
1009    ssl_test_lib:close(Client).
1010
1011
1012server_http_decode(Socket, HttpResponse) ->
1013    assert_packet_opt(Socket, http),
1014    receive
1015	{ssl, Socket, {http_request, 'GET', _, {1,1}}}  -> ok;
1016	Other1 -> exit({?LINE, Other1})
1017    end,
1018    assert_packet_opt(Socket, http),
1019    receive
1020	{ssl, Socket, {http_header, _, 'Host', _, "www.example.com"}}  -> ok;
1021	Other2 -> exit({?LINE, Other2})
1022    end,
1023    assert_packet_opt(Socket, http),
1024    receive
1025	{ssl, Socket, {http_header, _, 'User-Agent', _, "HttpTester"}}  -> ok;
1026	Other3 -> exit({?LINE, Other3})
1027    end,
1028    assert_packet_opt(Socket, http),
1029    receive
1030	{ssl, Socket, http_eoh}  -> ok;
1031	Other4 -> exit({?LINE, Other4})
1032    end,
1033    assert_packet_opt(Socket, http),
1034    spawn(fun() -> ssl:send(Socket, HttpResponse) end),
1035    ok.
1036
1037client_http_decode(Socket, HttpRequest) ->
1038    spawn(fun() -> ssl:send(Socket, HttpRequest) end),
1039    receive
1040	{ssl, Socket, {http_response, {1,1}, 200, "OK"}}  -> ok;
1041	Other1 -> exit({?LINE, Other1})
1042    end,
1043    receive
1044	{ssl, Socket, http_eoh}  -> ok;
1045	Other2 -> exit({?LINE, Other2})
1046    end,
1047    ok = ssl:setopts(Socket, [{packet, 0}]),
1048    receive
1049	{ssl, Socket, <<"Hello!">>}  -> ok;
1050	Other3 -> exit({?LINE, Other3})
1051    end,
1052    ok.
1053
1054%%--------------------------------------------------------------------
1055packet_http_decode_list() ->
1056    [{doc, "Test setting the packet option {packet, http}, {mode, list}"
1057      "(Body will be list too)"}].
1058packet_http_decode_list(Config) when is_list(Config) ->
1059    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1060    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1061    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1062
1063    Request = "GET / HTTP/1.1\r\n"
1064              "host: www.example.com\r\n"
1065	      "user-agent: HttpTester\r\n"
1066	      "\r\n",
1067    Response = "HTTP/1.1 200 OK\r\n"
1068	       "\r\n"
1069	       "Hello!",
1070
1071    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1072					{from, self()},
1073					{mfa, {?MODULE, server_http_decode,
1074					       [Response]}},
1075					{options, [{active, true}, binary,
1076						   {packet, http} |
1077						   ServerOpts]}]),
1078
1079    Port = ssl_test_lib:inet_port(Server),
1080    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1081					{host, Hostname},
1082					{from, self()},
1083					{mfa, {?MODULE, client_http_decode_list,
1084					       [Request]}},
1085					{options, [{active, true}, list,
1086						   {packet, http} |
1087						   ClientOpts]}]),
1088
1089    ssl_test_lib:check_result(Server, ok, Client, ok),
1090
1091    ssl_test_lib:close(Server),
1092    ssl_test_lib:close(Client).
1093
1094
1095client_http_decode_list(Socket, HttpRequest) ->
1096    spawn(fun() -> ssl:send(Socket, HttpRequest) end),
1097    receive
1098	{ssl, Socket, {http_response, {1,1}, 200, "OK"}}  -> ok;
1099	Other1 -> exit({?LINE, Other1})
1100    end,
1101    receive
1102	{ssl, Socket, http_eoh}  -> ok;
1103	Other2 -> exit({?LINE, Other2})
1104    end,
1105    ok = ssl:setopts(Socket, [{packet, 0}]),
1106    receive
1107	{ssl, Socket, "Hello!"}  -> ok;
1108	Other3 -> exit({?LINE, Other3})
1109    end,
1110    ok.
1111
1112%%--------------------------------------------------------------------
1113packet_http_bin_decode_multi() ->
1114    [{doc,"Test setting the packet option {packet, http_bin} with multiple requests"}].
1115packet_http_bin_decode_multi(Config) when is_list(Config) ->
1116    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1117    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1118    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1119
1120    Request = <<"GET / HTTP/1.1\r\n"
1121                "host: www.example.com\r\n"
1122	        "user-agent: HttpTester\r\n"
1123	        "\r\n">>,
1124    Response = <<"HTTP/1.1 200 OK\r\n"
1125	         "\r\n"
1126	         "Hello!">>,
1127    NumMsgs = 3,
1128
1129    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1130					{from, self()},
1131					{mfa, {?MODULE, server_http_bin_decode,
1132					       [Response, NumMsgs]}},
1133					{options, [{active, true}, binary,
1134						   {packet, http_bin} |
1135						   ServerOpts]}]),
1136
1137    Port = ssl_test_lib:inet_port(Server),
1138    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1139					{host, Hostname},
1140					{from, self()},
1141					{mfa, {?MODULE, client_http_bin_decode,
1142					       [Request, NumMsgs]}},
1143					{options, [{active, true}, binary,
1144						   {packet, http_bin} |
1145						   ClientOpts]}]),
1146
1147    ssl_test_lib:check_result(Server, ok, Client, ok),
1148
1149    ssl_test_lib:close(Server),
1150    ssl_test_lib:close(Client).
1151
1152
1153server_http_bin_decode(Socket, HttpResponse, Count) when Count > 0 ->
1154    assert_packet_opt(Socket, http_bin),
1155    receive
1156	{ssl, Socket, {http_request, 'GET', _, {1,1}}}  -> ok;
1157	Other1 -> exit({?LINE, Other1})
1158    end,
1159    assert_packet_opt(Socket, http_bin),
1160    receive
1161	{ssl, Socket, {http_header, _, 'Host', _, <<"www.example.com">>}}  -> ok;
1162	Other2 -> exit({?LINE, Other2})
1163    end,
1164    assert_packet_opt(Socket, http_bin),
1165    receive
1166	{ssl, Socket, {http_header, _, 'User-Agent', _, <<"HttpTester">>}}  -> ok;
1167	Other3 -> exit({?LINE, Other3})
1168    end,
1169    assert_packet_opt(Socket, http_bin),
1170    receive
1171	{ssl, Socket, http_eoh}  -> ok;
1172	Other4 -> exit({?LINE, Other4})
1173    end,
1174    assert_packet_opt(Socket, http_bin),
1175    spawn(fun() -> ssl:send(Socket, HttpResponse) end),
1176    server_http_bin_decode(Socket, HttpResponse, Count - 1);
1177server_http_bin_decode(_, _, _) ->
1178    ok.
1179
1180client_http_bin_decode(Socket, HttpRequest, Count) when Count > 0 ->
1181    spawn(fun() -> ssl:send(Socket, HttpRequest) end),
1182    receive
1183	{ssl, Socket, {http_response, {1,1}, 200, <<"OK">>}}  -> ok;
1184	Other1 -> exit({?LINE, Other1})
1185    end,
1186    receive
1187	{ssl, Socket, http_eoh}  -> ok;
1188	Other2 -> exit({?LINE, Other2})
1189    end,
1190    ok = ssl:setopts(Socket, [{packet, 0}]),
1191    receive
1192	{ssl, Socket, <<"Hello!">>}  -> ok;
1193	Other3 -> exit({?LINE, Other3})
1194    end,
1195    ok = ssl:setopts(Socket, [{packet, http_bin}]),
1196    client_http_bin_decode(Socket, HttpRequest, Count - 1);
1197client_http_bin_decode(_, _, _) ->
1198    ok.
1199
1200%%--------------------------------------------------------------------
1201packet_http_error_passive() ->
1202    [{doc,"Test setting the packet option {packet, http}, {active, false}"
1203      " with a incorrect http header."}].
1204
1205packet_http_error_passive(Config) when is_list(Config) ->
1206    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1207    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1208    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1209
1210    Request = "GET / HTTP/1.1\r\n"
1211              "host: www.example.com\r\n"
1212	      "user-agent HttpTester\r\n"
1213	      "\r\n",
1214    Response = "HTTP/1.1 200 OK\r\n"
1215	       "\r\n"
1216	       "Hello!",
1217
1218    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1219					{from, self()},
1220					{mfa, {?MODULE, server_http_decode_error,
1221					       [Response]}},
1222					{options, [{active, false}, binary,
1223						   {packet, http} |
1224						   ServerOpts]}]),
1225
1226    Port = ssl_test_lib:inet_port(Server),
1227    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1228					{host, Hostname},
1229					{from, self()},
1230					{mfa, {?MODULE, client_http_decode_list,
1231					       [Request]}},
1232					{options, [{active, true}, list,
1233						   {packet, http} |
1234						   ClientOpts]}]),
1235
1236    ssl_test_lib:check_result(Server, ok, Client, ok),
1237
1238    ssl_test_lib:close(Server),
1239    ssl_test_lib:close(Client).
1240
1241
1242server_http_decode_error(Socket, HttpResponse) ->
1243    assert_packet_opt(Socket, http),
1244
1245    {ok, {http_request, 'GET', _, {1,1}}} = ssl:recv(Socket, 0),
1246
1247    assert_packet_opt(Socket, http),
1248
1249    {ok, {http_header, _, 'Host', _, "www.example.com"}} = ssl:recv(Socket, 0),
1250    assert_packet_opt(Socket, http),
1251
1252    {ok, {http_error, _}} = ssl:recv(Socket, 0),
1253
1254    assert_packet_opt(Socket, http),
1255
1256    {ok, http_eoh} = ssl:recv(Socket, 0),
1257
1258    assert_packet_opt(Socket, http),
1259    spawn(fun() -> ssl:send(Socket, HttpResponse) end),
1260    ok.
1261%%--------------------------------------------------------------------
1262packet_httph_active() ->
1263    [{doc,"Test setting the packet option {packet, httph}"}].
1264
1265packet_httph_active(Config) when is_list(Config) ->
1266    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1267    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1268    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1269
1270    Trailer = "Content-Encoding: gzip\r\n"
1271	"\r\n",
1272
1273    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1274					{from, self()},
1275					{mfa, {?MODULE, server_send_trailer,
1276					       [Trailer]}},
1277					{options, [{active, true}, binary |
1278						   ServerOpts]}]),
1279
1280    Port = ssl_test_lib:inet_port(Server),
1281    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1282					{host, Hostname},
1283					{from, self()},
1284					{mfa, {?MODULE, client_http_decode_trailer_active,
1285					       []}},
1286					{options, [{active, true},
1287						   {packet, httph},
1288						   list |
1289						   ClientOpts]}]),
1290
1291    ssl_test_lib:check_result(Server, ok, Client, ok),
1292
1293    ssl_test_lib:close(Server),
1294    ssl_test_lib:close(Client).
1295
1296
1297server_send_trailer(Socket, Trailer)->
1298    ssl:send(Socket, Trailer),
1299    ok.
1300
1301client_http_decode_trailer_active(Socket) ->
1302    receive
1303	{ssl, Socket,
1304	 {http_header,36,'Content-Encoding',"Content-Encoding","gzip"}} ->
1305	    ok;
1306	Other1 ->
1307	    exit({?LINE, Other1})
1308    end,
1309    receive
1310	{ssl, Socket, http_eoh}  ->
1311	    ok;
1312	Other2 ->
1313	    exit({?LINE, Other2})
1314    end,
1315    ok.
1316
1317%%--------------------------------------------------------------------
1318packet_httph_bin_active() ->
1319    [{doc,"Test setting the packet option {packet, httph_bin}"}].
1320packet_httph_bin_active(Config) when is_list(Config) ->
1321    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1322    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1323    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1324
1325    Trailer = "Content-Encoding: gzip\r\n"
1326	"\r\n",
1327
1328    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1329					{from, self()},
1330					{mfa, {?MODULE, server_send_trailer,
1331					       [Trailer]}},
1332					{options, [{active, true}, binary |
1333						   ServerOpts]}]),
1334
1335    Port = ssl_test_lib:inet_port(Server),
1336    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1337					{host, Hostname},
1338					{from, self()},
1339					{mfa, {?MODULE, client_http_decode_trailer_bin_active,
1340					       []}},
1341					{options, [{active, true},
1342						   {packet, httph_bin},
1343						   list |
1344						   ClientOpts]}]),
1345
1346    ssl_test_lib:check_result(Server, ok, Client, ok),
1347
1348    ssl_test_lib:close(Server),
1349    ssl_test_lib:close(Client).
1350
1351client_http_decode_trailer_bin_active(Socket) ->
1352    receive
1353	{ssl, Socket,
1354	 {http_header,36,'Content-Encoding',<<"Content-Encoding">>, <<"gzip">>}} ->
1355	    ok;
1356	Other1 ->
1357	    exit({?LINE, Other1})
1358    end,
1359    receive
1360	{ssl, Socket, http_eoh}  ->
1361	    ok;
1362	Other2 ->
1363	    exit({?LINE, Other2})
1364    end,
1365    ok.
1366%%--------------------------------------------------------------------
1367packet_httph_active_once() ->
1368    [{doc,"Test setting the packet option {packet, httph}"}].
1369
1370packet_httph_active_once(Config) when is_list(Config) ->
1371    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1372    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1373    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1374
1375    Trailer = "Content-Encoding: gzip\r\n"
1376	"\r\n",
1377
1378    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1379					{from, self()},
1380					{mfa, {?MODULE, server_send_trailer,
1381					       [Trailer]}},
1382					{options, [{active, true}, binary |
1383						   ServerOpts]}]),
1384
1385    Port = ssl_test_lib:inet_port(Server),
1386    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1387					{host, Hostname},
1388					{from, self()},
1389					{mfa, {?MODULE, client_http_decode_trailer_active_once,
1390					       []}},
1391					{options, [{active, false},
1392						   {packet, httph},
1393						   list |
1394						   ClientOpts]}]),
1395
1396    ssl_test_lib:check_result(Server, ok, Client, ok),
1397
1398    ssl_test_lib:close(Server),
1399    ssl_test_lib:close(Client).
1400
1401
1402client_http_decode_trailer_active_once(Socket) ->
1403    ssl:setopts(Socket, [{active, once}]),
1404    receive
1405	{ssl, Socket,
1406	 {http_header,36,'Content-Encoding',"Content-Encoding","gzip"}} ->
1407	    ok;
1408	Other1 ->
1409	    exit({?LINE, Other1})
1410    end,
1411    ssl:setopts(Socket, [{active, once}]),
1412    receive
1413	{ssl, Socket, http_eoh}  ->
1414	    ok;
1415	Other2 ->
1416	    exit({?LINE, Other2})
1417    end,
1418    ok.
1419%%--------------------------------------------------------------------
1420packet_httph_bin_active_once() ->
1421    [{doc,"Test setting the packet option {packet, httph_bin}"}].
1422
1423packet_httph_bin_active_once(Config) when is_list(Config) ->
1424    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1425    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1426    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1427
1428    Trailer = "Content-Encoding: gzip\r\n"
1429	"\r\n",
1430
1431    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1432					{from, self()},
1433					{mfa, {?MODULE, server_send_trailer,
1434					       [Trailer]}},
1435					{options, [{active, true}, binary |
1436						   ServerOpts]}]),
1437
1438    Port = ssl_test_lib:inet_port(Server),
1439    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1440					{host, Hostname},
1441					{from, self()},
1442					{mfa, {?MODULE, client_http_decode_trailer_bin_active_once,
1443					       []}},
1444					{options, [{active, false},
1445						   {packet, httph_bin},
1446						   list |
1447						   ClientOpts]}]),
1448
1449    ssl_test_lib:check_result(Server, ok, Client, ok),
1450
1451    ssl_test_lib:close(Server),
1452    ssl_test_lib:close(Client).
1453
1454client_http_decode_trailer_bin_active_once(Socket) ->
1455    ssl:setopts(Socket, [{active, once}]),
1456    receive
1457	{ssl, Socket,
1458	 {http_header,36,'Content-Encoding',<<"Content-Encoding">>, <<"gzip">>}} ->
1459	    ok;
1460	Other1 ->
1461	    exit({?LINE, Other1})
1462    end,
1463    ssl:setopts(Socket, [{active, once}]),
1464    receive
1465	{ssl, Socket, http_eoh}  ->
1466	    ok;
1467	Other2 ->
1468	    exit({?LINE, Other2})
1469    end,
1470    ok.
1471
1472%%--------------------------------------------------------------------
1473
1474packet_httph_passive() ->
1475    [{doc,"Test setting the packet option {packet, httph}"}].
1476
1477packet_httph_passive(Config) when is_list(Config) ->
1478    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1479    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1480    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1481
1482    Trailer = "Content-Encoding: gzip\r\n"
1483	"\r\n",
1484
1485    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1486					{from, self()},
1487					{mfa, {?MODULE, server_send_trailer,
1488					       [Trailer]}},
1489					{options, [{active, true}, binary |
1490						   ServerOpts]}]),
1491
1492    Port = ssl_test_lib:inet_port(Server),
1493    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1494					{host, Hostname},
1495					{from, self()},
1496					{mfa, {?MODULE, client_http_decode_trailer_passive,
1497					       []}},
1498					{options, [{active, false},
1499						   {packet, httph},
1500						   list |
1501						   ClientOpts]}]),
1502
1503    ssl_test_lib:check_result(Server, ok, Client, ok),
1504
1505    ssl_test_lib:close(Server),
1506    ssl_test_lib:close(Client).
1507
1508client_http_decode_trailer_passive(Socket) ->
1509    {ok,{http_header,36,'Content-Encoding',"Content-Encoding","gzip"}} = ssl:recv(Socket, 0),
1510    {ok, http_eoh} = ssl:recv(Socket, 0),
1511    ok.
1512
1513%%--------------------------------------------------------------------
1514packet_httph_bin_passive() ->
1515    [{doc,"Test setting the packet option {packet, httph_bin}"}].
1516
1517packet_httph_bin_passive(Config) when is_list(Config) ->
1518    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1519    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1520    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1521
1522    Trailer = "Content-Encoding: gzip\r\n"
1523	"\r\n",
1524
1525    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1526					{from, self()},
1527					{mfa, {?MODULE, server_send_trailer,
1528					       [Trailer]}},
1529					{options, [{active, true}, binary |
1530						   ServerOpts]}]),
1531
1532    Port = ssl_test_lib:inet_port(Server),
1533    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1534					{host, Hostname},
1535					{from, self()},
1536					{mfa, {?MODULE, client_http_decode_trailer_bin_passive,
1537					       []}},
1538					{options, [{active, false},
1539						   {packet, httph_bin},
1540						   list |
1541						   ClientOpts]}]),
1542
1543    ssl_test_lib:check_result(Server, ok, Client, ok),
1544
1545    ssl_test_lib:close(Server),
1546    ssl_test_lib:close(Client).
1547
1548client_http_decode_trailer_bin_passive(Socket) ->
1549    {ok,{http_header,36,'Content-Encoding',<<"Content-Encoding">>,<<"gzip">>}} = ssl:recv(Socket, 0),
1550    {ok, http_eoh} = ssl:recv(Socket, 0),
1551    ok.
1552
1553%%--------------------------------------------------------------------
1554packet_line_decode() ->
1555    [{doc,"Test setting the packet option {packet, line}, {mode, binary}"}].
1556
1557packet_line_decode(Config) when is_list(Config) ->
1558    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1559    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1560    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1561
1562    Data = list_to_binary(lists:flatten(io_lib:format("Line ends here.~n"
1563						      "Now it is a new line.~n",
1564						      []))),
1565
1566    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1567					{from, self()},
1568					{mfa, {?MODULE, server_line_packet_decode,
1569					       [Data]}},
1570					{options, [{active, true}, binary,
1571						   {packet, line}|ServerOpts]}]),
1572
1573    Port = ssl_test_lib:inet_port(Server),
1574    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1575					{host, Hostname},
1576					{from, self()},
1577					{mfa, {?MODULE, client_line_packet_decode,
1578					       [Data]}},
1579					{options, [{active, true},
1580						   {packet, line},
1581						   binary | ClientOpts]}]),
1582
1583    ssl_test_lib:check_result(Server, ok, Client, ok),
1584
1585    ssl_test_lib:close(Server),
1586    ssl_test_lib:close(Client).
1587
1588%%--------------------------------------------------------------------
1589
1590packet_line_decode_list() ->
1591    [{doc,"Test setting the packet option {packet, line}, {mode, list}"}].
1592
1593packet_line_decode_list(Config) when is_list(Config) ->
1594    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1595    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1596    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1597
1598    Data = lists:flatten(io_lib:format("Line ends here.~n"
1599				       "Now it is a new line.~n", [])),
1600
1601    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1602					{from, self()},
1603					{mfa, {?MODULE,
1604					       server_line_packet_decode,
1605					       [Data]}},
1606					{options, [{active, true}, list,
1607						   {packet, line}|ServerOpts]}]),
1608
1609    Port = ssl_test_lib:inet_port(Server),
1610    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1611					{host, Hostname},
1612					{from, self()},
1613					{mfa, {?MODULE,
1614					       client_line_packet_decode,
1615					       [Data]}},
1616					{options, [{active, true},
1617						   {packet, line},
1618						   list | ClientOpts]}]),
1619
1620    ssl_test_lib:check_result(Server, ok, Client, ok),
1621
1622    ssl_test_lib:close(Server),
1623    ssl_test_lib:close(Client).
1624
1625
1626%%--------------------------------------------------------------------
1627
1628packet_asn1_decode() ->
1629    [{doc,"Test setting the packet option {packet, asn1}"}].
1630
1631packet_asn1_decode(Config) when is_list(Config) ->
1632    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1633    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1634    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1635
1636    File = proplists:get_value(certfile, ServerOpts),
1637
1638    %% A valid asn1 BER packet (DER is stricter BER)
1639    [{'Certificate', Data, _}] = ssl_test_lib:pem_to_der(File),
1640
1641    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1642					{from, self()},
1643					{mfa, {?MODULE, server_packet_decode,
1644					       [Data]}},
1645					{options, [{active, true}, binary,
1646						   {packet, asn1}|ServerOpts]}]),
1647
1648    Port = ssl_test_lib:inet_port(Server),
1649    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1650					{host, Hostname},
1651					{from, self()},
1652					{mfa, {?MODULE, client_packet_decode,
1653					       [Data]}},
1654					{options, [{active, true}, {packet, asn1},
1655						   binary | ClientOpts]}]),
1656
1657    ssl_test_lib:check_result(Server, ok, Client, ok),
1658
1659    ssl_test_lib:close(Server),
1660    ssl_test_lib:close(Client).
1661
1662%%--------------------------------------------------------------------
1663packet_asn1_decode_list() ->
1664    [{doc,"Test setting the packet option {packet, asn1}"}].
1665
1666packet_asn1_decode_list(Config) when is_list(Config) ->
1667    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1668    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1669    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1670
1671    File = proplists:get_value(certfile, ServerOpts),
1672
1673    %% A valid asn1 BER packet (DER is stricter BER)
1674    [{'Certificate', BinData, _}] = ssl_test_lib:pem_to_der(File),
1675
1676    Data = binary_to_list(BinData),
1677
1678    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1679					{from, self()},
1680					{mfa, {?MODULE, server_packet_decode,
1681					       [Data]}},
1682					{options, [{active, true}, list,
1683						   {packet, asn1}|ServerOpts]}]),
1684
1685    Port = ssl_test_lib:inet_port(Server),
1686    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1687					{host, Hostname},
1688					{from, self()},
1689					{mfa, {?MODULE, client_packet_decode,
1690					       [Data]}},
1691					{options, [{active, true}, {packet, asn1},
1692						   list | ClientOpts]}]),
1693
1694    ssl_test_lib:check_result(Server, ok, Client, ok),
1695
1696    ssl_test_lib:close(Server),
1697    ssl_test_lib:close(Client).
1698
1699%%--------------------------------------------------------------------
1700packet_tpkt_decode() ->
1701    [{doc,"Test setting the packet option {packet, tpkt}"}].
1702
1703packet_tpkt_decode(Config) when is_list(Config) ->
1704    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1705    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1706    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1707
1708    Data = list_to_binary(add_tpkt_header("TPKT data")),
1709
1710
1711    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1712					{from, self()},
1713					{mfa, {?MODULE, server_packet_decode,
1714					       [Data]}},
1715					{options, [{active, true}, binary,
1716						   {packet, tpkt}|ServerOpts]}]),
1717
1718    Port = ssl_test_lib:inet_port(Server),
1719    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1720					{host, Hostname},
1721					{from, self()},
1722					{mfa, {?MODULE, client_packet_decode,
1723					       [Data]}},
1724					{options, [{active, true}, {packet, tpkt},
1725						   binary | ClientOpts]}]),
1726
1727    ssl_test_lib:check_result(Server, ok, Client, ok),
1728
1729    ssl_test_lib:close(Server),
1730    ssl_test_lib:close(Client).
1731%%--------------------------------------------------------------------
1732packet_tpkt_decode_list() ->
1733    [{doc,"Test setting the packet option {packet, tpkt}"}].
1734
1735packet_tpkt_decode_list(Config) when is_list(Config) ->
1736    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1737    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1738    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1739
1740    Data = binary_to_list(list_to_binary(add_tpkt_header("TPKT data"))),
1741
1742    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1743					{from, self()},
1744					{mfa, {?MODULE, server_packet_decode,
1745					       [Data]}},
1746					{options, [{active, true}, list,
1747						   {packet, tpkt}|ServerOpts]}]),
1748
1749    Port = ssl_test_lib:inet_port(Server),
1750    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1751					{host, Hostname},
1752					{from, self()},
1753					{mfa, {?MODULE, client_packet_decode,
1754					       [Data]}},
1755					{options, [{active, true}, {packet, tpkt},
1756						   list | ClientOpts]}]),
1757
1758    ssl_test_lib:check_result(Server, ok, Client, ok),
1759
1760    ssl_test_lib:close(Server),
1761    ssl_test_lib:close(Client).
1762
1763%%--------------------------------------------------------------------
1764
1765%% packet_fcgi_decode() ->
1766%%     [{doc,"Test setting the packet option {packet, fcgi}"}].
1767
1768%% packet_fcgi_decode(Config) when is_list(Config) ->
1769%%     ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1770%%     ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1771%%     {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1772
1773%%     Data = ...
1774
1775%%     Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1776%% 					{from, self()},
1777%% 					{mfa, {?MODULE, server_packet_decode,
1778%% 					       [Data0, Data1]}},
1779%% 					{options, [{active, true}, binary,
1780%% 						   {packet, fcgi}|ServerOpts]}]),
1781
1782%%     Port = ssl_test_lib:inet_port(Server),
1783%%     Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1784%% 					{host, Hostname},
1785%% 					{from, self()},
1786%% 					{mfa, {?MODULE, client_packet_decode,
1787%% 					       [Data0, Data1]}},
1788%% 					{options, [{active, true}, {packet, fcgi},
1789%% 						   binary | ClientOpts]}]),
1790
1791%%     ssl_test_lib:check_result(Server, ok, Client, ok),
1792
1793%%     ssl_test_lib:close(Server),
1794%%     ssl_test_lib:close(Client).
1795
1796
1797%%--------------------------------------------------------------------
1798
1799packet_sunrm_decode() ->
1800    [{doc,"Test setting the packet option {packet, sunrm}"}].
1801packet_sunrm_decode(Config) when is_list(Config) ->
1802    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1803    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1804    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1805
1806    Data = <<11:32, "Hello world">>,
1807
1808    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1809					{from, self()},
1810					{mfa, {?MODULE, server_packet_decode,
1811					       [Data]}},
1812					{options, [{active, true}, binary,
1813						   {packet, sunrm}|ServerOpts]}]),
1814
1815    Port = ssl_test_lib:inet_port(Server),
1816    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1817					{host, Hostname},
1818					{from, self()},
1819					{mfa, {?MODULE, client_packet_decode,
1820					       [Data]}},
1821					{options, [{active, true}, {packet, sunrm},
1822						   binary | ClientOpts]}]),
1823
1824    ssl_test_lib:check_result(Server, ok, Client, ok),
1825
1826    ssl_test_lib:close(Server),
1827    ssl_test_lib:close(Client).
1828
1829%%--------------------------------------------------------------------
1830packet_sunrm_decode_list() ->
1831    [{doc,"Test setting the packet option {packet, sunrm}"}].
1832
1833packet_sunrm_decode_list(Config) when is_list(Config) ->
1834    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1835    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1836    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1837
1838    Data = binary_to_list(list_to_binary([<<11:32>>, "Hello world"])),
1839
1840    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1841					{from, self()},
1842					{mfa, {?MODULE, server_packet_decode,
1843					       [Data]}},
1844					{options, [{active, true}, list,
1845						   {packet, sunrm}|ServerOpts]}]),
1846
1847    Port = ssl_test_lib:inet_port(Server),
1848    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1849					{host, Hostname},
1850					{from, self()},
1851					{mfa, {?MODULE, client_packet_decode,
1852					       [Data]}},
1853					{options, [{active, true}, {packet, sunrm},
1854						   list | ClientOpts]}]),
1855
1856    ssl_test_lib:check_result(Server, ok, Client, ok),
1857
1858    ssl_test_lib:close(Server),
1859    ssl_test_lib:close(Client).
1860%%--------------------------------------------------------------------
1861
1862header_decode_one_byte_active() ->
1863    [{doc,"Test setting the packet option {header, 1}"}].
1864
1865header_decode_one_byte_active(Config) when is_list(Config) ->
1866    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1867    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1868    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1869
1870    Data = <<11:8, "Hello world">>,
1871
1872    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1873					{from, self()},
1874					{mfa, {?MODULE, server_header_decode_active,
1875					       [Data, [11 | <<"Hello world">>]]}},
1876					{options, [{active, true}, binary,
1877						   {header,1}|ServerOpts]}]),
1878
1879    Port = ssl_test_lib:inet_port(Server),
1880    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1881					{host, Hostname},
1882					{from, self()},
1883					{mfa, {?MODULE, client_header_decode_active,
1884					       [Data, [11 | <<"Hello world">> ]]}},
1885					{options, [{active, true}, binary, {header, 1}
1886						    | ClientOpts]}]),
1887
1888    ssl_test_lib:check_result(Server, ok, Client, ok),
1889
1890    ssl_test_lib:close(Server),
1891    ssl_test_lib:close(Client).
1892
1893%%--------------------------------------------------------------------
1894
1895header_decode_two_bytes_active() ->
1896    [{doc,"Test setting the packet option {header, 2}"}].
1897
1898header_decode_two_bytes_active(Config) when is_list(Config) ->
1899    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1900    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1901    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1902
1903    Data = <<11:8, "Hello world">>,
1904
1905    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1906					{from, self()},
1907					{mfa, {?MODULE, server_header_decode_active,
1908					       [Data, [11, $H | <<"ello world">> ]]}},
1909					{options, [{active, true}, binary,
1910						   {header,2}|ServerOpts]}]),
1911
1912    Port = ssl_test_lib:inet_port(Server),
1913    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1914					{host, Hostname},
1915					{from, self()},
1916					{mfa, {?MODULE, client_header_decode_active,
1917					       [Data, [11, $H | <<"ello world">> ]]}},
1918					{options, [{active, true}, {header, 2},
1919						   binary | ClientOpts]}]),
1920
1921    ssl_test_lib:check_result(Server, ok, Client, ok),
1922
1923    ssl_test_lib:close(Server),
1924    ssl_test_lib:close(Client).
1925
1926
1927%%--------------------------------------------------------------------
1928
1929header_decode_two_bytes_two_sent_active() ->
1930    [{doc,"Test setting the packet option {header, 2} and sending two byte"}].
1931
1932header_decode_two_bytes_two_sent_active(Config) when is_list(Config) ->
1933    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1934    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1935    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1936
1937    Data = <<"He">>,
1938
1939    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1940					{from, self()},
1941					{mfa, {?MODULE, server_header_decode_active,
1942					       [Data, [$H, $e | <<>>]]}},
1943					{options, [{active, true}, binary,
1944						   {header,2}|ServerOpts]}]),
1945
1946    Port = ssl_test_lib:inet_port(Server),
1947    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1948					{host, Hostname},
1949					{from, self()},
1950					{mfa, {?MODULE, client_header_decode_active,
1951					       [Data, [$H, $e | <<>>]]}},
1952					{options, [{active, true}, {header, 2},
1953						   binary | ClientOpts]}]),
1954
1955    ssl_test_lib:check_result(Server, ok, Client, ok),
1956
1957    ssl_test_lib:close(Server),
1958    ssl_test_lib:close(Client).
1959
1960
1961%%--------------------------------------------------------------------
1962
1963header_decode_two_bytes_one_sent_active() ->
1964    [{doc,"Test setting the packet option {header, 2} and sending one byte"}].
1965
1966header_decode_two_bytes_one_sent_active(Config) when is_list(Config) ->
1967    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
1968    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
1969    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
1970
1971    Data = <<"H">>,
1972
1973    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
1974					{from, self()},
1975					{mfa, {?MODULE, server_header_decode_active,
1976					       [Data, "H"]}},
1977					{options, [{active, true}, binary,
1978						   {header,2}|ServerOpts]}]),
1979
1980    Port = ssl_test_lib:inet_port(Server),
1981    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
1982					{host, Hostname},
1983					{from, self()},
1984					{mfa, {?MODULE, client_header_decode_active,
1985					       [Data, "H"]}},
1986					{options, [{active, true}, {header, 2},
1987						   binary | ClientOpts]}]),
1988
1989    ssl_test_lib:check_result(Server, ok, Client, ok),
1990
1991    ssl_test_lib:close(Server),
1992    ssl_test_lib:close(Client).
1993
1994%%--------------------------------------------------------------------
1995
1996header_decode_one_byte_passive() ->
1997    [{doc,"Test setting the packet option {header, 1}"}].
1998
1999header_decode_one_byte_passive(Config) when is_list(Config) ->
2000    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2001    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2002    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2003
2004    Data = <<11:8, "Hello world">>,
2005
2006    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2007					{from, self()},
2008					{mfa, {?MODULE, server_header_decode_passive,
2009					       [Data, [11 | <<"Hello world">>]]}},
2010					{options, [{active, false}, binary,
2011						   {header,1}|ServerOpts]}]),
2012
2013    Port = ssl_test_lib:inet_port(Server),
2014    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2015					{host, Hostname},
2016					{from, self()},
2017					{mfa, {?MODULE, client_header_decode_passive,
2018					       [Data, [11 | <<"Hello world">> ]]}},
2019					{options, [{active, false}, binary, {header, 1}
2020						   | ClientOpts]}]),
2021
2022    ssl_test_lib:check_result(Server, ok, Client, ok),
2023
2024    ssl_test_lib:close(Server),
2025    ssl_test_lib:close(Client).
2026
2027%%--------------------------------------------------------------------
2028
2029header_decode_two_bytes_passive() ->
2030    [{doc,"Test setting the packet option {header, 2}"}].
2031
2032header_decode_two_bytes_passive(Config) when is_list(Config) ->
2033    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2034    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2035    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2036
2037    Data = <<11:8, "Hello world">>,
2038
2039    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2040					{from, self()},
2041					{mfa, {?MODULE, server_header_decode_passive,
2042					       [Data, [11, $H | <<"ello world">> ]]}},
2043					{options, [{active, false}, binary,
2044						   {header,2}|ServerOpts]}]),
2045
2046    Port = ssl_test_lib:inet_port(Server),
2047    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2048					{host, Hostname},
2049					{from, self()},
2050					{mfa, {?MODULE, client_header_decode_passive,
2051					       [Data, [11, $H | <<"ello world">> ]]}},
2052					{options, [{active, false}, {header, 2},
2053						   binary | ClientOpts]}]),
2054
2055    ssl_test_lib:check_result(Server, ok, Client, ok),
2056
2057    ssl_test_lib:close(Server),
2058    ssl_test_lib:close(Client).
2059
2060
2061%%--------------------------------------------------------------------
2062
2063header_decode_two_bytes_two_sent_passive() ->
2064    [{doc,"Test setting the packet option {header, 2} and sending two byte"}].
2065
2066header_decode_two_bytes_two_sent_passive(Config) when is_list(Config) ->
2067    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2068    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2069    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2070
2071    Data = <<"He">>,
2072
2073    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2074					{from, self()},
2075					{mfa, {?MODULE, server_header_decode_passive,
2076					       [Data, [$H, $e | <<>>]]}},
2077					{options, [{active, false}, binary,
2078						   {header,2}|ServerOpts]}]),
2079
2080    Port = ssl_test_lib:inet_port(Server),
2081    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2082					{host, Hostname},
2083					{from, self()},
2084					{mfa, {?MODULE, client_header_decode_passive,
2085					       [Data, [$H, $e | <<>>]]}},
2086					{options, [{active, false}, {header, 2},
2087						   binary | ClientOpts]}]),
2088
2089    ssl_test_lib:check_result(Server, ok, Client, ok),
2090
2091    ssl_test_lib:close(Server),
2092    ssl_test_lib:close(Client).
2093
2094
2095%%--------------------------------------------------------------------
2096
2097header_decode_two_bytes_one_sent_passive() ->
2098    [{doc,"Test setting the packet option {header, 2} and sending one byte"}].
2099
2100header_decode_two_bytes_one_sent_passive(Config) when is_list(Config) ->
2101    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2102    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2103    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2104
2105    Data = <<"H">>,
2106
2107    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2108					{from, self()},
2109					{mfa, {?MODULE, server_header_decode_passive,
2110					       [Data, "H"]}},
2111					{options, [{active, false}, binary,
2112						   {header,2}|ServerOpts]}]),
2113
2114    Port = ssl_test_lib:inet_port(Server),
2115    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2116					{host, Hostname},
2117					{from, self()},
2118					{mfa, {?MODULE, client_header_decode_passive,
2119					       [Data, "H"]}},
2120					{options, [{active, false}, {header, 2},
2121						   binary | ClientOpts]}]),
2122
2123    ssl_test_lib:check_result(Server, ok, Client, ok),
2124
2125    ssl_test_lib:close(Server),
2126    ssl_test_lib:close(Client).
2127
2128%%--------------------------------------------------------------------
2129reject_packet_opt() ->
2130    [{doc,"Test packet option is rejected for DTLS over udp"}].
2131
2132reject_packet_opt(Config) when is_list(Config) ->
2133
2134    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2135
2136    {error,{options,{not_supported,{packet,4}}}} =
2137        ssl:listen(9999, [{packet, 4} | ServerOpts]),
2138    {error,{options,{not_supported,{packet_size,1}}}} =
2139        ssl:listen(9999, [{packet_size, 1} | ServerOpts]),
2140    {error,{options,{not_supported,{header,1}}}} =
2141        ssl:listen(9999, [{header, 1} | ServerOpts]),
2142
2143    client_reject_packet_opt(Config, {packet,4}),
2144    client_reject_packet_opt(Config, {packet_size, 1}),
2145    client_reject_packet_opt(Config, {header, 1}).
2146
2147%%--------------------------------------------------------------------
2148%% Internal functions ------------------------------------------------
2149%%--------------------------------------------------------------------
2150
2151packet(Config, Data, Send, Recv, Quantity, Packet, Active) when Packet == 0;
2152								Packet == raw ->
2153    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2154    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2155    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2156
2157    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2158					{from, self()},
2159					{mfa, {?MODULE, Send ,[Data, Quantity]}},
2160					{options, [{nodelay, true},{packet, Packet} | ServerOpts]}]),
2161    Port = ssl_test_lib:inet_port(Server),
2162    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2163					{host, Hostname},
2164					{from, self()},
2165					{mfa, {?MODULE, Recv, [Data, Quantity]}},
2166					{options, [{active, Active}, {nodelay, true},
2167						   {packet, Packet} |
2168						   ClientOpts]}]),
2169
2170    ssl_test_lib:check_result(Client, ok),
2171
2172    ssl_test_lib:close(Server),
2173    ssl_test_lib:close(Client);
2174
2175packet(Config, Data, Send, Recv, Quantity, Packet, Active) ->
2176    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2177    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2178    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2179
2180    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2181					{from, self()},
2182					{mfa, {?MODULE, Send ,[Data, Quantity]}},
2183					{options, [{packet, Packet}, {nodelay, true}| ServerOpts]
2184                                         ++ ssl_test_lib:bigger_buffers()}]),
2185    Port = ssl_test_lib:inet_port(Server),
2186    Client = ssl_test_lib:start_client([{node, ServerNode}, {port, Port},
2187					{host, Hostname},
2188					{from, self()},
2189					{mfa, {?MODULE, Recv, [Data, Quantity]}},
2190					{options, [{active, Active},
2191                                                   {nodelay, true},
2192						   {packet, Packet} |
2193						   ClientOpts] ++ ssl_test_lib:bigger_buffers()}]),
2194
2195    ssl_test_lib:check_result(Client, ok),
2196
2197    ssl_test_lib:close(Server),
2198    ssl_test_lib:close(Client).
2199
2200send_raw(Socket,_, 0) ->
2201    ssl:send(Socket, <<>>),
2202    no_result_msg;
2203send_raw(Socket, Data, N) ->
2204    ssl:send(Socket, Data),
2205    send_raw(Socket, Data, N-1).
2206
2207passive_raw(Socket, _, 0) ->
2208    {error, timeout} = ssl:recv(Socket, 0, 500),
2209    ok;
2210passive_raw(Socket, Data, N) ->
2211    Length = length(Data),
2212    {ok, Data} = ssl:recv(Socket, Length),
2213    passive_raw(Socket, Data, N-1).
2214
2215passive_recv_packet(Socket, _, 0) ->
2216    case ssl:recv(Socket, 0) of
2217	{ok, []} ->
2218	    {error, timeout} = ssl:recv(Socket, 0, 500),
2219	    ok;
2220	Other ->
2221	    {other, Other, ssl:connection_information(Socket, [session_id, cipher_suite]), 0}
2222    end;
2223passive_recv_packet(Socket, Data, N) ->
2224    case ssl:recv(Socket, 0, 10000) of
2225	{ok, Data} ->
2226	    passive_recv_packet(Socket, Data, N-1);
2227        {error, _} = Other ->
2228            Other
2229    end.
2230
2231send(Socket,_, 0) ->
2232    ssl:send(Socket, <<>>),
2233    no_result_msg;
2234send(Socket, Data, N) ->
2235    case ssl:send(Socket, [Data]) of
2236	ok ->
2237	    send(Socket, Data, N-1);
2238	Other ->
2239	    Other
2240    end.
2241
2242send_incomplete(Socket, Data, N) ->
2243    send_incomplete(Socket, Data, N, <<>>).
2244send_incomplete(Socket, _Data, 0, Prev) ->
2245    ssl:send(Socket, Prev),
2246    ssl:send(Socket, [?uint32(0)]),
2247    no_result_msg;
2248send_incomplete(Socket, Data, N, Prev) ->
2249    Length = byte_size(Data),
2250    <<Part1:42/binary, Rest/binary>> = Data,
2251    ssl:send(Socket, [Prev, ?uint32(Length), Part1]),
2252    send_incomplete(Socket, Data, N-1, Rest).
2253
2254active_once_raw(Socket, Data, N) ->
2255    active_once_raw(Socket, Data, N, []).
2256
2257active_once_raw(_, _, 0, _) ->
2258    ok;
2259active_once_raw(Socket, Data, N, Acc0) ->
2260    case lists:prefix(Data, Acc0) of
2261	true ->
2262	    DLen = length(Data),
2263	    Start = DLen + 1,
2264	    Len = length(Acc0) - DLen,
2265	    Acc = string:substr(Acc0, Start, Len),
2266	    active_once_raw(Socket, Data, N-1, Acc);
2267	false ->
2268	    receive
2269		{ssl, Socket, Info}  ->
2270		    ssl:setopts(Socket, [{active, once}]),
2271		    active_once_raw(Socket, Data, N, Acc0 ++ Info)
2272	    end
2273    end.
2274
2275active_once_packet(Socket,_, 0) ->
2276    receive
2277	{ssl, Socket, []} ->
2278	    ok;
2279	{ssl, Socket, Other} ->
2280	    {other, Other, ssl:connection_information(Socket,  [session_id, cipher_suite]), 0}
2281    end;
2282active_once_packet(Socket, Data, N) ->
2283    receive
2284	{ssl, Socket, Data} ->
2285            ssl:setopts(Socket, [{active, once}]),
2286            active_once_packet(Socket, Data, N-1)
2287    end.
2288
2289active_raw(Socket, Data, N) ->
2290    active_raw(Socket, (length(Data) * N)).
2291active_raw(_Socket, 0) ->
2292    ok;
2293active_raw(Socket, N) ->
2294    receive
2295	{ssl, Socket, Bytes} ->
2296            active_raw(Socket, N-length(Bytes))
2297    end.
2298
2299active_packet(Socket, _, 0) ->
2300    receive
2301	{ssl, Socket, []} ->
2302	    ok;
2303	Other ->
2304	    {other, Other, ssl:connection_information(Socket,  [session_id, cipher_suite]), 0}
2305    end;
2306active_packet(Socket, Data, N) ->
2307    receive
2308	{ssl, Socket, Data} ->
2309	    active_packet(Socket, Data, N-1);
2310	Other ->
2311	    Other
2312    end.
2313
2314assert_packet_opt(Socket, Type) ->
2315    {ok, [{packet, Type}]} = ssl:getopts(Socket, [packet]).
2316
2317server_packet_decode(Socket, Packet) ->
2318    receive
2319	{ssl, Socket, Packet}  -> ok;
2320	Other1 -> exit({?LINE, Other1})
2321    end,
2322    spawn(fun() -> ssl:send(Socket, Packet) end),
2323    receive
2324	{ssl, Socket, Packet}  -> ok;
2325	Other2 -> exit({?LINE, Other2})
2326    end,
2327    spawn(fun() -> ssl:send(Socket, Packet) end),
2328    ok.
2329
2330client_packet_decode(Socket, Packet) when is_binary(Packet)->
2331    <<P1:10/binary, P2/binary>> = Packet,
2332    client_packet_decode(Socket, P1, P2, Packet);
2333client_packet_decode(Socket, [Head | Tail] = Packet) ->
2334    client_packet_decode(Socket, [Head], Tail, Packet).
2335
2336client_packet_decode(Socket, P1, P2, Packet) ->
2337    spawn(fun() -> ssl:send(Socket, P1), ssl:send(Socket, P2)  end),
2338    receive
2339	{ssl, Socket, Packet}  -> ok;
2340	Other1 -> exit({?LINE, Other1})
2341    end,
2342    spawn(fun() -> ssl:send(Socket, Packet) end),
2343    receive
2344	{ssl, Socket, Packet}  -> ok;
2345	Other2 -> exit({?LINE, Other2})
2346    end.
2347
2348server_header_decode_active(Socket, Packet, Result) ->
2349    receive
2350	{ssl, Socket, Result}  ->
2351	    ok;
2352	{ssl, Socket, Other1} ->
2353	    check_header_result(Result, Other1)
2354    end,
2355    spawn(fun() -> ssl:send(Socket, Packet) end),
2356    ok.
2357
2358client_header_decode_active(Socket, Packet, Result) ->
2359    spawn(fun() -> ssl:send(Socket, Packet) end),
2360    receive
2361	{ssl, Socket, Result}  ->
2362	    ok;
2363	{ssl, Socket, Other1} ->
2364	    check_header_result(Result, Other1)
2365    end.
2366
2367server_header_decode_passive(Socket, Packet, Result) ->
2368    case ssl:recv(Socket, 0) of
2369	{ok, Result} ->
2370	    ok;
2371	{ok, Other} ->
2372	    check_header_result(Result, Other)
2373    end,
2374    spawn(fun() -> ssl:send(Socket, Packet) end),
2375    ok.
2376
2377client_header_decode_passive(Socket, Packet, Result) ->
2378    spawn(fun() -> ssl:send(Socket, Packet) end),
2379    case ssl:recv(Socket, 0) of
2380	{ok, Result} ->
2381	    ok;
2382	{ok, Other} ->
2383	    check_header_result(Result, Other)
2384    end.
2385
2386%% The inet header option is a broken option as it does not buffer until it gets enough data.
2387%% This check only checks that it has the same behavior as inet, but it is a quite useless
2388%% option and the bitsynax makes it obsolete!
2389check_header_result([Byte1 | _], [Byte1]) ->
2390    ok;
2391check_header_result([Byte1 | _], [Byte1| <<>>]) ->
2392    ok;
2393check_header_result([Byte1, Byte2 | _], [Byte1, Byte2]) ->
2394    ok;
2395check_header_result([Byte1, Byte2 | _], [Byte1, Byte2 | <<>>]) ->
2396    ok;
2397check_header_result(Expected,Got) ->
2398    exit({?LINE, {Expected, Got}}).
2399
2400server_line_packet_decode(Socket, Packet) when is_binary(Packet) ->
2401    [L1, L2] = string:tokens(binary_to_list(Packet), "\n"),
2402    server_line_packet_decode(Socket, list_to_binary(L1 ++ "\n"), list_to_binary(L2 ++ "\n"), Packet);
2403server_line_packet_decode(Socket, Packet) ->
2404    [L1, L2] = string:tokens(Packet, "\n"),
2405    server_line_packet_decode(Socket, L1 ++ "\n", L2 ++ "\n", Packet).
2406
2407server_line_packet_decode(Socket, L1, L2, Packet) ->
2408    receive
2409  	{ssl, Socket,  L1} -> ok;
2410  	Other1 -> exit({?LINE, Other1})
2411    end,
2412    receive
2413  	{ssl, Socket,  L2} -> ok;
2414  	Other2 -> exit({?LINE, Other2})
2415    end,
2416    spawn(fun() -> ssl:send(Socket, Packet) end),
2417    ok.
2418
2419client_line_packet_decode(Socket, Packet) when is_binary(Packet)->
2420    <<P1:10/binary, P2/binary>> = Packet,
2421    [L1, L2] = string:tokens(binary_to_list(Packet), "\n"),
2422    client_line_packet_decode(Socket, P1, P2, list_to_binary(L1 ++ "\n"), list_to_binary(L2 ++ "\n"));
2423client_line_packet_decode(Socket, [Head | Tail] = Packet) ->
2424    [L1, L2] = string:tokens(Packet, "\n"),
2425    client_line_packet_decode(Socket, [Head], Tail, L1 ++ "\n", L2 ++ "\n").
2426
2427client_line_packet_decode(Socket, P1, P2, L1, L2) ->
2428    spawn(fun() -> ssl:send(Socket, P1),  ssl:send(Socket, P2) end),
2429    receive
2430  	{ssl, Socket, L1} -> ok;
2431  	Other1 -> exit({?LINE, Other1})
2432    end,
2433    receive
2434  	{ssl, Socket,  L2} -> ok;
2435  	Other2 -> exit({?LINE, Other2})
2436    end.
2437
2438add_tpkt_header(Data) when is_binary(Data) ->
2439    L = byte_size(Data) + 4,
2440    [3, 0, ((L) bsr 8) band 16#ff, (L) band 16#ff ,Data];
2441add_tpkt_header(IOList) when is_list(IOList) ->
2442    Binary = list_to_binary(IOList),
2443    L = byte_size(Binary) + 4,
2444    [3, 0, ((L) bsr 8) band 16#ff, (L) band 16#ff , Binary].
2445
2446
2447client_reject_packet_opt(Config, PacketOpt) ->
2448    ServerOpts = ssl_test_lib:ssl_options(server_opts, Config),
2449    ClientOpts = ssl_test_lib:ssl_options(client_opts, Config),
2450    {ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),
2451
2452    Server = ssl_test_lib:start_server([{node, ClientNode}, {port, 0},
2453                                        {from, self()},
2454                                        {mfa, {ssl_test_lib, no_result_msg ,[]}},
2455                                        {options, ServerOpts}]),
2456    Port = ssl_test_lib:inet_port(Server),
2457    Client = ssl_test_lib:start_client_error([{node, ServerNode}, {port, Port},
2458                                              {host, Hostname},
2459                                              {from, self()},
2460                                              {mfa, {ssl_test_lib, no_result_msg, []}},
2461                                              {options, [PacketOpt |
2462                                                         ClientOpts]}]),
2463
2464    ssl_test_lib:check_result(Client, {error, {options, {not_supported, PacketOpt}}}).
2465
2466
2467send_switch_packet(SslSocket, Data, NextPacket) ->
2468    spawn(fun() -> ssl:send(SslSocket, Data) end),
2469    receive
2470        {ssl, SslSocket, "Hello World"} ->
2471            ssl:setopts(SslSocket, [{packet, NextPacket}]),
2472            spawn(fun() -> ssl:send(SslSocket, Data) end),
2473            receive
2474                {ssl, SslSocket, "Hello World"} ->
2475                    ok
2476            end
2477    end.
2478recv_switch_packet(SslSocket, Data, NextPacket) ->
2479    receive
2480        {ssl, SslSocket, "Hello World"} ->
2481            spawn(fun() -> ssl:send(SslSocket, Data) end),
2482            ssl:setopts(SslSocket, [{packet, NextPacket}]),
2483            receive
2484                {ssl, SslSocket, "Hello World"} ->
2485                    spawn(fun() -> ssl:send(SslSocket, Data) end),
2486                    ok
2487            end
2488    end.
2489