aboutsummaryrefslogblamecommitdiffstats
path: root/lib/kernel/test/inet_sockopt_SUITE.erl
blob: 1ef182ca1850849edc447b30cad8f7b2ffa582cf (plain) (tree)
1
2
3
4
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702


                   
                                                        















                                                                         
                                                    




























                                      


                                                                    






                                                                     
                                         
 
         



                                                          



            





                         
                                     
           

                                    
           
 










































































































































































































































































































































































































































































































































































































































                                                                               
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2007-2010. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
-module(inet_sockopt_SUITE).

-include_lib("test_server/include/test_server.hrl").


-define(C_GET_IPPROTO_TCP,1).
-define(C_GET_IPPROTO_IP,2).
-define(C_GET_SOL_SOCKET,3).
-define(C_GET_SOL_IP,4).

-define(C_GET_TCP_KEEPIDLE,11).
-define(C_GET_TCP_LINGER2,12).
-define(C_GET_TCP_INFO,13).
-define(C_GET_SO_REUSEADDR,14).
-define(C_GET_SO_KEEPALIVE,15).
-define(C_GET_SO_LINGER,16).

-define(C_GET_LINGER_SIZE,21).
-define(C_GET_TCP_INFO_SIZE,22).

-define(C_GET_OFF_LINGER_L_ONOFF,31).
-define(C_GET_OFF_LINGER_L_LINGER,32).
-define(C_GET_OFF_TCPI_SACKED,33).
-define(C_GET_OFF_TCPI_OPTIONS,34).

-define(C_GET_SIZ_LINGER_L_ONOFF,41).
-define(C_GET_SIZ_LINGER_L_LINGER,42).
-define(C_GET_SIZ_TCPI_SACKED,43).
-define(C_GET_SIZ_TCPI_OPTIONS,44).

-define(C_QUIT,99).

-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2, 
	 simple/1, loop_all/1, simple_raw/1, simple_raw_getbin/1, 
	 doc_examples_raw/1,doc_examples_raw_getbin/1,
	 large_raw/1,large_raw_getbin/1,combined/1,combined_getbin/1,
	 type_errors/1]).

-export([init_per_testcase/2, end_per_testcase/2]).


suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    [simple, loop_all, simple_raw, simple_raw_getbin,
     doc_examples_raw, doc_examples_raw_getbin, large_raw,
     large_raw_getbin, combined, combined_getbin,
     type_errors].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


init_per_testcase(_Func, Config) ->
    Dog = test_server:timetrap(test_server:seconds(60)),
    [{watchdog,Dog}|Config].

end_per_testcase(_Func, Config) ->
    Dog = ?config(watchdog, Config),
    test_server:timetrap_cancel(Dog).

simple(suite) -> [];
simple(doc) -> "Test inet:setopt/getopt simple functionality.";
simple(Config) when is_list(Config) ->
    ?line XOpt = case os:type() of
		     {unix,_} -> [{reuseaddr,true}];
		     _ -> []
		 end,
    ?line Opt = [{nodelay,true},
		 {keepalive,true},{packet,4},
		 {active,false}|XOpt],
    ?line OptTags = [X || {X,_} <- Opt],
    ?line {S1,S2} = create_socketpair(Opt, Opt),
    ?line {ok,Opt} = inet:getopts(S1,OptTags),
    ?line {ok,Opt} = inet:getopts(S2,OptTags),
    ?line COpt = [{X,case X of nodelay -> false;_ -> Y end} || {X,Y} <- Opt],
    ?line inet:setopts(S1,COpt),
    ?line {ok,COpt} = inet:getopts(S1,OptTags),
    ?line {ok,Opt} = inet:getopts(S2,OptTags),
    ?line gen_tcp:close(S1),
    ?line gen_tcp:close(S2),
    ok.

loop_all(suite) -> [];
loop_all(doc) -> "Loop through all socket options and check that they work";
loop_all(Config) when is_list(Config) ->
    ?line ListenFailures = 
	lists:foldr(make_check_fun(listen,1),[],all_listen_options()),
    ?line ConnectFailures = 
	lists:foldr(make_check_fun(connect,2),[],all_connect_options()),
    ?line case ListenFailures++ConnectFailures of
	      [] ->
		  ?line ok;
	      Failed ->
		  ?line {comment,lists:flatten(
				   io_lib:format("Non mandatory failed:~w",
						 [Failed]))}
	  end.
			


simple_raw(suite) -> [];
simple_raw(doc) -> "Test simple setopt/getopt of raw options.";
simple_raw(Config) when is_list(Config) ->
    do_simple_raw(Config,false).
simple_raw_getbin(suite) -> [];
simple_raw_getbin(doc) -> "Test simple setopt/getopt of raw options, "
			      "with binaries in getopt.";
simple_raw_getbin(Config) when is_list(Config) ->
    do_simple_raw(Config,true).

do_simple_raw(Config,Binary) when is_list(Config) ->
    ?line Port = start_helper(Config),
    ?line SolSocket = ask_helper(Port,?C_GET_SOL_SOCKET),
    ?line SoKeepAlive = ask_helper(Port,?C_GET_SO_KEEPALIVE),
    ?line OptionTrue = {raw,SolSocket,SoKeepAlive,<<1:32/native>>},
    ?line OptionFalse = {raw,SolSocket,SoKeepAlive,<<0:32/native>>},
    ?line {S1,S2} = create_socketpair([OptionTrue],[{keepalive,true}]),
    ?line {ok,[{keepalive,true}]} = inet:getopts(S1,[keepalive]),
    ?line {ok,[{keepalive,true}]} = inet:getopts(S2,[keepalive]),
    ?line {ok,[{raw,SolSocket,SoKeepAlive,X1B}]} = 
	inet:getopts(S1,[{raw,SolSocket,SoKeepAlive,binarify(4,Binary)}]),
    ?line X1 = nintbin2int(X1B),
    ?line {ok,[{raw,SolSocket,SoKeepAlive,X2B}]} = 
	inet:getopts(S2,[{raw,SolSocket,SoKeepAlive,binarify(4,Binary)}]),
    ?line X2 = nintbin2int(X2B),
    ?line true = X1 > 0,
    ?line true = X2 > 0,
    ?line inet:setopts(S1,[{keepalive,false}]),
    ?line inet:setopts(S2,[OptionFalse]),
    ?line {ok,[{keepalive,false}]} = inet:getopts(S1,[keepalive]),
    ?line {ok,[{keepalive,false}]} = inet:getopts(S2,[keepalive]),
    ?line {ok,[{raw,SolSocket,SoKeepAlive,Y1B}]} = 
	inet:getopts(S1,[{raw,SolSocket,SoKeepAlive,binarify(4,Binary)}]),
    ?line Y1 = nintbin2int(Y1B),
    ?line {ok,[{raw,SolSocket,SoKeepAlive,Y2B}]} = 
	inet:getopts(S2,[{raw,SolSocket,SoKeepAlive,binarify(4,Binary)}]),
    ?line Y2 = nintbin2int(Y2B),
    ?line true = Y1 == 0,
    ?line true = Y2 == 0,
    ?line gen_tcp:close(S1),
    ?line gen_tcp:close(S2),
    ?line stop_helper(Port),
    ok.
    
nintbin2int(<<Int:32/native>>) -> Int;
nintbin2int(<<Int:24/native>>) -> Int;
nintbin2int(<<Int:16/native>>) -> Int;
nintbin2int(<<Int:8/native>>) -> Int;
nintbin2int(<<>>) -> 0.

doc_examples_raw(suite) -> [];
doc_examples_raw(doc) -> "Test that the example code from the documentation "
			     "works";
doc_examples_raw(Config) when is_list(Config) ->
    do_doc_examples_raw(Config,false).
doc_examples_raw_getbin(suite) -> [];
doc_examples_raw_getbin(doc) -> "Test that the example code from the "
				    "documentation works when getopt uses "
				    "binaries";
doc_examples_raw_getbin(Config) when is_list(Config) ->
    do_doc_examples_raw(Config,true).
do_doc_examples_raw(Config,Binary) when is_list(Config) ->
    ?line Port = start_helper(Config),
    ?line Proto = ask_helper(Port,?C_GET_IPPROTO_TCP),
    ?line TcpInfo = ask_helper(Port,?C_GET_TCP_INFO),
    ?line TcpInfoSize = ask_helper(Port,?C_GET_TCP_INFO_SIZE),
    ?line TcpiSackedOffset = ask_helper(Port,?C_GET_OFF_TCPI_SACKED),
    ?line TcpiOptionsOffset = ask_helper(Port,?C_GET_OFF_TCPI_OPTIONS),
    ?line TcpiSackedSize = ask_helper(Port,?C_GET_SIZ_TCPI_SACKED),
    ?line TcpiOptionsSize = ask_helper(Port,?C_GET_SIZ_TCPI_OPTIONS),
    ?line TcpLinger2 = ask_helper(Port,?C_GET_TCP_LINGER2),
    ?line stop_helper(Port),
    case all_ok([Proto,TcpInfo,TcpInfoSize,TcpiSackedOffset,
		 TcpiOptionsOffset,TcpiSackedSize,TcpiOptionsSize,
		 TcpLinger2]) of
	false ->
	    {skipped,"Does not run on this OS."};
	true ->
	    ?line {Sock,I} = create_socketpair([],[]),
	    ?line {ok,[{raw,Proto,TcpLinger2,<<OrigLinger:32/native>>}]} = 
		inet:getopts(Sock,[{raw,Proto,TcpLinger2,binarify(4,Binary)}]),
	    ?line NewLinger = OrigLinger div 2,
	    ?line ok = inet:setopts(Sock,[{raw,Proto,TcpLinger2,
					   <<NewLinger:32/native>>}]),
	    ?line {ok,[{raw,Proto,TcpLinger2,<<NewLinger:32/native>>}]} = 
		inet:getopts(Sock,[{raw,Proto,TcpLinger2,binarify(4,Binary)}]),
	    ?line ok = inet:setopts(Sock,[{raw,Proto,TcpLinger2,
					   <<OrigLinger:32/native>>}]),
	    ?line {ok,[{raw,Proto,TcpLinger2,<<OrigLinger:32/native>>}]} = 
		inet:getopts(Sock,[{raw,Proto,TcpLinger2,binarify(4,Binary)}]),
	    ?line {ok,[{raw,_,_,Info}]} = 
		inet:getopts(Sock,[{raw,Proto,TcpInfo,
				    binarify(TcpInfoSize,Binary)}]),
	    ?line Bit1 = TcpiSackedSize * 8,
            ?line <<_:TcpiSackedOffset/binary,
		   TcpiSacked:Bit1/native,_/binary>> = 
		Info,
	    ?line 0 = TcpiSacked,
	    ?line Bit2 = TcpiOptionsSize * 8,
            ?line <<_:TcpiOptionsOffset/binary,
		   TcpiOptions:Bit2/native,_/binary>> = 
		Info,
	    ?line true = TcpiOptions =/= 0,
	    ?line gen_tcp:close(Sock),
	    ?line gen_tcp:close(I),
	    ok
    end.
	    
large_raw(suite) -> [];
large_raw(doc) -> "Test structs and large/too large buffers when raw";
large_raw(Config) when is_list(Config) ->
    do_large_raw(Config,false).
large_raw_getbin(suite) -> [];
large_raw_getbin(doc) -> "Test structs and large/too large buffers when raw"
			 "using binaries to getopts";
large_raw_getbin(Config) when is_list(Config) ->
    do_large_raw(Config,true).
do_large_raw(Config,Binary) when is_list(Config) ->
    ?line Port = start_helper(Config),
    ?line Proto = ask_helper(Port,?C_GET_SOL_SOCKET),
    ?line Linger = ask_helper(Port,?C_GET_SO_LINGER),
    ?line LingerSize = ask_helper(Port,?C_GET_LINGER_SIZE),
    ?line LingerOnOffOffset = ask_helper(Port,?C_GET_OFF_LINGER_L_ONOFF),
    ?line LingerLingerOffset = ask_helper(Port,?C_GET_OFF_LINGER_L_LINGER),
    ?line LingerOnOffSize = ask_helper(Port,?C_GET_SIZ_LINGER_L_ONOFF),
    ?line LingerLingerSize = ask_helper(Port,?C_GET_SIZ_LINGER_L_LINGER),
    ?line stop_helper(Port),
    case all_ok([Proto,Linger,LingerSize,LingerOnOffOffset,
		 LingerLingerOffset,LingerOnOffSize,LingerLingerSize]) of
	false ->
	    {skipped,"Does not run on this OS."};
	true ->
	    ?line {Sock1,Sock2} = create_socketpair([{linger,{true,10}}],
					       [{linger,{false,0}}]),
	    ?line LargeSize = 1024, % Solaris can take up to 1024*9, 
				    % linux 1024*63... 
	    ?line TooLargeSize = 1024*64,
	    ?line {ok,[{raw,Proto,Linger,Linger1}]} = 
		inet:getopts(Sock1,[{raw,Proto,Linger,
				     binarify(LargeSize,Binary)}]),
	    ?line {ok,[{raw,Proto,Linger,Linger2}]} = 
		inet:getopts(Sock2,[{raw,Proto,Linger,
				     binarify(LingerSize,Binary)}]),
	    ?line true = byte_size(Linger1) =:= LingerSize,
	    ?line LingerLingerBits = LingerLingerSize * 8,
	    ?line LingerOnOffBits = LingerOnOffSize * 8,
	    ?line <<_:LingerLingerOffset/binary,
		   Ling1:LingerLingerBits/native,_/binary>> = Linger1,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off1:LingerOnOffBits/native,_/binary>> = Linger1,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off2:LingerOnOffBits/native,_/binary>> = Linger2,
	    ?line true = Off1 =/= 0,
	    ?line true = Off2 == 0,
	    ?line true = Ling1 == 10,
	    ?line {error,einval} = 
		inet:getopts(Sock1,[{raw,Proto,Linger,TooLargeSize}]),
	    ?line gen_tcp:close(Sock1),
	    ?line gen_tcp:close(Sock2),
	    ok
    end.

combined(suite) -> [];
combined(doc) -> "Test raw structs combined w/ other options ";
combined(Config) when is_list(Config) ->
    do_combined(Config,false).
combined_getbin(suite) -> [];
combined_getbin(doc) -> "Test raw structs combined w/ other options and "
			"binarise in getopts";
combined_getbin(Config) when is_list(Config) ->
    do_combined(Config,true).
do_combined(Config,Binary) when is_list(Config) ->
    ?line Port = start_helper(Config),
    ?line Proto = ask_helper(Port,?C_GET_SOL_SOCKET),
    ?line Linger = ask_helper(Port,?C_GET_SO_LINGER),
    ?line LingerSize = ask_helper(Port,?C_GET_LINGER_SIZE),
    ?line LingerOnOffOffset = ask_helper(Port,?C_GET_OFF_LINGER_L_ONOFF),
    ?line LingerLingerOffset = ask_helper(Port,?C_GET_OFF_LINGER_L_LINGER),
    ?line LingerOnOffSize = ask_helper(Port,?C_GET_SIZ_LINGER_L_ONOFF),
    ?line LingerLingerSize = ask_helper(Port,?C_GET_SIZ_LINGER_L_LINGER),
    ?line stop_helper(Port),
    case all_ok([Proto,Linger,LingerSize,LingerOnOffOffset,
		 LingerLingerOffset,LingerOnOffSize,LingerLingerSize]) of
	false ->
	    {skipped,"Does not run on this OS."};
	true ->
	    ?line LingerLingerBits = LingerLingerSize * 8,
	    ?line LingerOnOffBits = LingerOnOffSize * 8,
	    ?line {LingerOn,LingerOff} = 
		case LingerOnOffOffset <  LingerLingerOffset of
		    true ->
			Pad1 =
			    list_to_binary(
			      lists:duplicate(LingerOnOffOffset,
					      0)),
			Pad2Siz =
			    LingerLingerOffset - LingerOnOffSize -
			    LingerOnOffOffset,
			Pad2 =
			    list_to_binary(
			      lists:duplicate(Pad2Siz,
					      0)),
			Pad3Siz = LingerSize - LingerLingerSize -
			    LingerLingerOffset,
			Pad3 = list_to_binary(
				 lists:duplicate(Pad3Siz,
						 0)),
			{<<Pad1/binary,1:LingerOnOffBits/native,
			  Pad2/binary,10:LingerLingerBits/native,
			  Pad3/binary>>,
			 <<Pad1/binary,0:LingerOnOffBits/native,
			  Pad2/binary,0:LingerLingerBits/native,
			  Pad3/binary>>};
		    false ->
			Pad1 =
			    list_to_binary(
			      lists:duplicate(LingerLingerOffset,
					      0)),
			Pad2Siz =
			    LingerOnOffOffset - LingerLingerSize -
			    LingerLingerOffset,
			Pad2 =
			    list_to_binary(
			      lists:duplicate(Pad2Siz,
					      0)),
			Pad3Siz = LingerSize - LingerOnOffSize -
			    LingerOnOffOffset,
			Pad3 = list_to_binary(
				 lists:duplicate(Pad3Siz,
						 0)),
			{<<Pad1/binary,1:LingerLingerBits/native,
			  Pad2/binary,10:LingerOnOffBits/native,
			  Pad3/binary>>,
			 <<Pad1/binary,0:LingerLingerBits/native,
			  Pad2/binary,0:LingerOnOffBits/native,
			  Pad3/binary>>}
		end,
	    ?line RawLingerOn = {raw,Proto,Linger,LingerOn},
	    ?line RawLingerOff = {raw,Proto,Linger,LingerOff},
	    ?line {Sock1,Sock2} = 
		create_socketpair([{keepalive,true},
				   RawLingerOn],
				  [{keepalive,false},
				   RawLingerOff]),
	    ?line {ok,[{raw,Proto,Linger,Linger1},{keepalive,Keep1}]} = 
		inet:getopts(Sock1,[{raw,Proto,Linger, 
				     binarify(LingerSize,Binary)},keepalive]),
	    ?line {ok,[{raw,Proto,Linger,Linger2},{keepalive,Keep2}]} = 
		inet:getopts(Sock2,[{raw,Proto,Linger,
				     binarify(LingerSize,Binary)},keepalive]),
	    ?line true = byte_size(Linger1) =:= LingerSize,
	    ?line <<_:LingerLingerOffset/binary,
		   Ling1:LingerLingerBits/native,_/binary>> = Linger1,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off1:LingerOnOffBits/native,_/binary>> = Linger1,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off2:LingerOnOffBits/native,_/binary>> = Linger2,
	    ?line true = Off1 =/= 0,
	    ?line true = Off2 == 0,
	    ?line true = Ling1 == 10,
	    ?line true = Keep1 =:= true,
	    ?line true = Keep2 =:= false,
	    ?line {Sock3,Sock4} = 
		create_socketpair([RawLingerOn,{keepalive,true}],
				  [RawLingerOff,{keepalive,false}]),
	    ?line {ok,[{raw,Proto,Linger,Linger3},{keepalive,Keep3}]} = 
		inet:getopts(Sock3,[{raw,Proto,Linger,
				     binarify(LingerSize,Binary)},keepalive]),
	    ?line {ok,[{raw,Proto,Linger,Linger4},{keepalive,Keep4}]} = 
		inet:getopts(Sock4,[{raw,Proto,Linger,
				     binarify(LingerSize,Binary)},keepalive]),
	    ?line true = byte_size(Linger3) =:= LingerSize,
	    ?line <<_:LingerLingerOffset/binary,
		   Ling3:LingerLingerBits/native,_/binary>> = Linger3,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off3:LingerOnOffBits/native,_/binary>> = Linger3,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off4:LingerOnOffBits/native,_/binary>> = Linger4,
	    ?line true = Off3 =/= 0,
	    ?line true = Off4 == 0,
	    ?line true = Ling3 == 10,
	    ?line true = Keep3 =:= true,
	    ?line true = Keep4 =:= false,
	    ?line {Sock5,Sock6} = 
		create_socketpair([{packet,4},RawLingerOn,{keepalive,true}],
				  [{packet,2},RawLingerOff,{keepalive,false}]),
	    ?line {ok,[{packet,Pack5},{raw,Proto,Linger,Linger5},
		       {keepalive,Keep5}]} = 
		inet:getopts(Sock5,[packet,{raw,Proto,Linger,
					    binarify(LingerSize,Binary)},
				    keepalive]),
	    ?line {ok,[{packet,Pack6},{raw,Proto,Linger,Linger6},
		       {keepalive,Keep6}]} = 
		inet:getopts(Sock6,[packet,{raw,Proto,Linger,
					    binarify(LingerSize,Binary)},
				    keepalive]),
	    ?line true = byte_size(Linger5) =:= LingerSize,
	    ?line <<_:LingerLingerOffset/binary,
		   Ling5:LingerLingerBits/native,_/binary>> = Linger5,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off5:LingerOnOffBits/native,_/binary>> = Linger5,
	    ?line <<_:LingerOnOffOffset/binary,
		   Off6:LingerOnOffBits/native,_/binary>> = Linger6,
	    ?line true = Off5 =/= 0,
	    ?line true = Off6 == 0,
	    ?line true = Ling5 == 10,
	    ?line true = Keep5 =:= true,
	    ?line true = Keep6 =:= false,
	    ?line true = Pack5 =:= 4,
	    ?line true = Pack6 =:= 2,
	    ?line inet:setopts(Sock6,[{packet,4},RawLingerOn,
				      {keepalive,true}]),
	    ?line {ok,[{packet,Pack7},{raw,Proto,Linger,Linger7},
		       {keepalive,Keep7}]} = 
		inet:getopts(Sock6,[packet,{raw,Proto,Linger,
					    binarify(LingerSize,Binary)},
				    keepalive]),
	    ?line <<_:LingerOnOffOffset/binary,
		   Off7:LingerOnOffBits/native,_/binary>> = Linger7,
	    ?line true = Off7 =/= 0,
	    ?line true = Keep7 =:= true,
	    ?line true = Pack7 =:= 4,
	    ?line gen_tcp:close(Sock1),
	    ?line gen_tcp:close(Sock2),
	    ?line gen_tcp:close(Sock3),
	    ?line gen_tcp:close(Sock4),
	    ?line gen_tcp:close(Sock5),
	    ?line gen_tcp:close(Sock6),
	    ok
    end.

type_errors(suite) ->
    [];
type_errors(doc) ->
    "Test that raw data requests are not executed for bad types";
type_errors(Config) when is_list(Config) ->
    ?line BadSetOptions =
	[
	 {raw,x,3,<<1:32>>},
	 {raw,1,tre,<<1:32>>},
	 {raw,1,3,ko},
	 {raw,1,3,5},
	 {raw,1,3},
	 {raw,1},
	 {raw},
	 {raw,ett},
	 {raw,ett,tre},
	 {raw,{true,10}},
	 {raw,{ett,tre,<<1:32>>}},
	 {rav,1,3,<<1:32>>},
	 raw,
	 rav,
	 {linger,banan}
	],
    ?line BadGetOptions =
	[
	 {raw,x,3,<<1:32>>},
	 {raw,1,tre,<<1:32>>},
	 {raw,1,3,ko},
	 {raw,1,3,5.1},
	 {raw,1,3,-3},
	 {raw,1,3},
	 {raw,1},
	 {raw},
	 {raw,ett},
	 {raw,ett,tre},
	 {raw,{true,10}},
	 {raw,{ett,tre,<<1:32>>}},
	 {rav,1,3,<<1:32>>},
	 raw,
	 rav,
	 {linger,banan}
	],
    ?line lists:foreach(fun(Option) ->
				?line case 
				      catch create_socketpair([Option],[]) of
					  {'EXIT',badarg} ->
					      ?line ok;
					  Unexpected1 ->
					      ?line exit({unexpected,
							  Unexpected1})
				      end,
				?line case 
				      catch create_socketpair([],[Option]) of
					  {'EXIT',badarg} ->
					      ?line ok;
					  Unexpected2 ->
					      ?line exit({unexpected,
							  Unexpected2})
				      end,
				?line {Sock1,Sock2} = create_socketpair([],[]),
				?line case inet:setopts(Sock1, [Option]) of
					  {error,einval} ->
					      ?line ok;
					  Unexpected3 ->
					      ?line exit({unexpected,
							  Unexpected3})
				      end,
				?line gen_tcp:close(Sock1),
				?line gen_tcp:close(Sock2)
			end,BadSetOptions),
    ?line {Sock1,Sock2} = create_socketpair([],[]),
    ?line lists:foreach(fun(Option) ->
				?line case inet:getopts(Sock1, [Option]) of
					  {error,einval} ->
					      ?line ok;
					  Unexpected ->
					      ?line exit({unexpected,
							  Unexpected})
				      end
			end,BadGetOptions),
    ?line gen_tcp:close(Sock1),
    ?line gen_tcp:close(Sock2),
    ok.

all_ok([]) ->
    true;
all_ok([H|T]) when H >= 0 ->
    all_ok(T);
all_ok(_) ->
    false.

			  
make_check_fun(Type,Element) ->
    fun({Name,V1,V2,Mand,Chang},Acc) ->
	    ?line {LO1,CO1} = setelement(Element,{[],[]}, [{Name,V1}]),
	    ?line {LO2,CO2} = setelement(Element,{[],[]}, [{Name,V2}]),
	    ?line {X1,Y1} = create_socketpair(LO1,CO1),
	    ?line {X2,Y2} = create_socketpair(LO2,CO2),
	    ?line S1 = element(Element,{X1,Y1}),
	    ?line S2 = element(Element,{X2,Y2}),
	    ?line {ok,[{Name,R1}]} = inet:getopts(S1,[Name]),
	    ?line {ok,[{Name,R2}]} = inet:getopts(S2,[Name]),
	    NewAcc =
		case R1 =/= R2 of
		    true ->
			case Chang of
			    true ->
				?line inet:setopts(S1,[{Name,V2}]),
				?line {ok,[{Name,R3}]} = 
				    inet:getopts(S1,[Name]),
				case {R3 =/= R1, R3 =:= R2} of
				    {true,true} ->
					?line Acc;
				    _ ->
					case Mand of
					    true ->
						?line exit
							({failed_sockopt,
							  {change,
							   Name}});
					    false ->
						?line [{change,Name}|Acc]
					end
				end;
			    false ->
				?line Acc
			end;
		    false ->
			case Mand of
			    true ->
				?line exit({failed_sockopt,
					    {Type,Name}});
			    false ->
				?line [{Type,Name}|Acc]
			end
		end,
	    ?line gen_tcp:close(X1),
	    ?line gen_tcp:close(Y1),
	    ?line gen_tcp:close(X2),
	    ?line gen_tcp:close(Y2),
	    NewAcc
     end.

% {OptionName,Value1,Value2,Mandatory,Changeable}
all_listen_options() ->
    [{tos,0,1,false,true}, 
     {priority,0,1,false,true}, 
     {reuseaddr,false,true,false,true}, 
     {keepalive,false,true,true,true}, 
     {linger, {false,10}, {true,10},true,true},
     {sndbuf,2048,4096,false,true}, 
     {recbuf,2048,4096,false,true}, 
     {nodelay,false,true,true,true},
     {header,2,4,true,true}, 
     {active,false,true,true,false}, 
     {packet,2,4,true,true}, 
     {buffer,1000,2000,true,true}, 
     {mode,list,binary,true,true}, 
     {deliver,term,port,true,true}, 
     {exit_on_close, true, false, true, true},
     %{high_watermark,4096,8192,true,true}, 
     %{low_watermark,2048,4096,true,true}, 
     {bit8,on,off,true,true}, 
     {send_timeout,infinity,1000,true,true},
     {send_timeout_close,false,true,true,true},
     {delay_send,false,true,true,true}, 
     {packet_size,0,4,true,true}
    ].
all_connect_options() ->
    [{tos,0,1,false,true}, 
     {priority,0,1,false,true}, 
     {reuseaddr,false,true,false,true}, 
     {keepalive,false,true,true,true}, 
     {linger, {false,10}, {true,10},true,true},
     {sndbuf,2048,4096,false,true}, 
     {recbuf,2048,4096,false,true}, 
     {nodelay,false,true,true,true},
     {header,2,4,true,true}, 
     {active,false,true,true,false}, 
     {packet,2,4,true,true}, 
     {buffer,1000,2000,true,true}, 
     {mode,list,binary,true,true}, 
     {deliver,term,port,true,true}, 
     {exit_on_close, true, false, true, true},
     {high_watermark,4096,8192,false,true}, 
     {low_watermark,2048,4096,false,true}, 
     {bit8,on,off,true,true}, 
     {send_timeout,infinity,1000,true,true},
     {send_timeout_close,false,true,true,true},
     {delay_send,false,true,true,true}, 
     {packet_size,0,4,true,true}
    ].
    

create_socketpair(ListenOptions,ConnectOptions) ->
    ?line {ok,LS}=gen_tcp:listen(0,ListenOptions),
    ?line {ok,Port}=inet:port(LS),
    ?line {ok,CS}=gen_tcp:connect(localhost,Port,ConnectOptions),
    ?line {ok,AS}=gen_tcp:accept(LS),
    ?line gen_tcp:close(LS),
    {AS,CS}.


start_helper(Config) ->
    Progname = filename:join(?config(data_dir, Config), "sockopt_helper"),
    Port = open_port({spawn,Progname},[eof,line]),
    Port.

ask_helper(Port,Code) ->
    Com = integer_to_list(Code)++"\n",
    Port ! {self(),{command,Com}},
    receive
	{Port,{data,{eol,Text}}} ->
	    list_to_integer(Text);
	Other ->
	    exit({error,{unexpected_data_from_helper,Other}})
    after 3000 ->
	    exit({error,helper_timeout})
    end.

stop_helper(Port) ->
    catch ask_helper(Port,?C_QUIT),
    receive
	{Port,eof} ->
	    Port ! {self(), close},
	    receive
		{Port,closed} ->
		    ok
	    after 1000 ->
		    timeout
	    end
    after 1000 ->
	    timeout
    end.

binarify(Size,Binary) when Binary =:= true ->
    <<0:Size/unit:8>>;
binarify(Size,Binary) when Binary =:= false ->
    Size.