-
Notifications
You must be signed in to change notification settings - Fork 106
/
Socket.pm
4866 lines (3614 loc) · 144 KB
/
Socket.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Test::Nginx::Socket;
use lib 'lib';
use lib 'inc';
use v5.10.1;
use Test::Base -Base;
our $VERSION = '0.29';
use POSIX qw( SIGQUIT SIGKILL SIGTERM SIGHUP );
use Encode;
#use Data::Dumper;
use Time::HiRes qw(sleep time);
use Test::LongString;
use List::MoreUtils qw( any );
use List::Util qw( sum min );
use IO::Select ();
use File::Temp qw( tempfile );
use Digest::MD5 ();
use Digest::SHA ();
use POSIX ":sys_wait_h";
use Test::Nginx::Util;
#use Smart::Comments::JSON '###';
use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
use POSIX qw(EAGAIN);
use IO::Socket;
#our ($PrevRequest, $PrevConfig);
our @EXPORT = qw( env_to_nginx is_str plan run_tests run_test
repeat_each config_preamble worker_connections
master_process_enabled
no_long_string workers master_on master_off
log_level no_shuffle no_root_location use_hup
server_name
server_addr server_root html_dir server_port server_port_for_client
timeout no_nginx_manager check_accum_error_log
add_block_preprocessor bail_out add_cleanup_handler
add_response_body_check
);
our $CheckLeakCount = $ENV{TEST_NGINX_CHECK_LEAK_COUNT} // 100;
our $UseHttp2 = $Test::Nginx::Util::UseHttp2;
our $TotalConnectingTimeouts = 0;
our $PrevNginxPid;
sub send_request ($$$$@);
sub send_http_req_by_curl ($$$);
sub run_filter_helper($$$);
sub run_test_helper ($$);
sub test_stap ($$);
sub error_event_handler ($);
sub read_event_handler ($);
sub write_event_handler ($);
sub transform_response_body ($$$);
sub check_response_body ($$$$$$);
sub fmt_str ($);
sub gen_ab_cmd_from_req ($$@);
sub gen_curl_cmd_from_req ($$);
sub get_linear_regression_slope ($);
sub value_contains ($$);
$RunTestHelper = \&run_test_helper;
$CheckErrorLog = \&check_error_log;
$CheckShutdownErrorLog = \&check_shutdown_error_log;
sub set_http_config_filter ($) {
$FilterHttpConfig = shift;
}
our @ResponseBodyChecks;
sub add_response_body_check ($) {
push @ResponseBodyChecks, shift;
}
# This will parse a "request"" string. The expected format is:
# - One line for the HTTP verb (POST, GET, etc.) plus optional relative URL
# (default is /) plus optional HTTP version (default is HTTP/1.1).
# - More lines considered as the body of the request.
# Most people don't care about headers and this is enough.
#
# This function will return a reference to a hash with the parsed elements
# plus information on the parsing itself like "how many white spaces were
# skipped before the VERB" (skipped_before_method), "was the version provided"
# (http_ver_size = 0).
sub parse_request ($$) {
my ( $name, $rrequest ) = @_;
open my $in, '<', $rrequest;
my $first = <$in>;
if ( !$first ) {
bail_out("$name - Request line should be non-empty");
}
#$first =~ s/^\s+|\s+$//gs;
my ($before_meth, $meth, $after_meth);
my ($rel_url, $rel_url_size, $after_rel_url);
my ($http_ver, $http_ver_size, $after_http_ver);
my $end_line_size;
if ($first =~ /^(\s*)(\S+)( *)((\S+)( *))?((\S+)( *))?(\s*)$/) {
$before_meth = defined $1 ? length($1) : undef;
$meth = $2;
$after_meth = defined $3 ? length($3) : undef;
$rel_url = $5;
$rel_url_size = defined $5 ? length($5) : undef;
$after_rel_url = defined $6 ? length($6) : undef;
$http_ver = $8;
if (!defined $8) {
$http_ver_size = undef;
} else {
$http_ver_size = defined $8 ? length($8) : undef;
}
if (!defined $9) {
$after_http_ver = undef;
} else {
$after_http_ver = defined $9 ? length($9) : undef;
}
$end_line_size = defined $10 ? length($10) : undef;
} else {
bail_out("$name - Request line is not valid. Should be 'meth [url [version]]' but got \"$first\".");
}
if ( !defined $rel_url ) {
$rel_url = '/';
$rel_url_size = 0;
$after_rel_url = 0;
}
if ( !defined $http_ver ) {
$http_ver = 'HTTP/1.1';
$http_ver_size = 0;
$after_http_ver = 0;
}
#my $url = "http://localhost:$ServerPortForClient" . $rel_url;
my $content = do { local $/; <$in> };
my $content_size;
if ( !defined $content ) {
$content = "";
$content_size = 0;
} else {
$content_size = length($content);
}
#warn Dumper($content);
close $in;
return {
method => $meth,
url => $rel_url,
content => $content,
http_ver => $http_ver,
skipped_before_method => $before_meth,
method_size => length($meth),
skipped_after_method => $after_meth,
url_size => $rel_url_size,
skipped_after_url => $after_rel_url,
http_ver_size => $http_ver_size,
skipped_after_http_ver => $after_http_ver + $end_line_size,
content_size => $content_size,
};
}
# From a parsed request, builds the "moves" to apply to the original request
# to transform it (e.g. add missing version). Elements of the returned array
# are of 2 types:
# - d : number of characters to remove.
# - s_* : number of characters (s_s) to replace by value (s_v).
sub get_moves($) {
my ($parsed_req) = @_;
return ({d => $parsed_req->{skipped_before_method}},
{s_s => $parsed_req->{method_size},
s_v => $parsed_req->{method}},
{d => $parsed_req->{skipped_after_method}},
{s_s => $parsed_req->{url_size},
s_v => $parsed_req->{url}},
{d => $parsed_req->{skipped_after_url}},
{s_s => $parsed_req->{http_ver_size},
s_v => $parsed_req->{http_ver}},
{d => $parsed_req->{skipped_after_http_ver}},
{s_s => 0,
s_v => $parsed_req->{headers}},
{s_s => $parsed_req->{content_size},
s_v => $parsed_req->{content}}
);
}
# Apply moves (see above) to an array of packets that correspond to a request.
# The use of this function is explained in the build_request_from_packets
# function.
sub apply_moves($$) {
my ($r_packet, $r_move) = @_;
my $current_packet = shift @$r_packet;
my $current_move = shift @$r_move;
my $in_packet_cursor = 0;
my @result = ();
while (defined $current_packet) {
if (!defined $current_move) {
push @result, $current_packet;
$current_packet = shift @$r_packet;
$in_packet_cursor = 0;
} elsif (defined $current_move->{d}) {
# Remove stuff from packet
if ($current_move->{d} > length($current_packet) - $in_packet_cursor) {
# Eat up what is left of packet.
$current_move->{d} -= length($current_packet) - $in_packet_cursor;
if ($in_packet_cursor > 0) {
# Something in packet from previous iteration.
push @result, $current_packet;
}
$current_packet = shift @$r_packet;
$in_packet_cursor = 0;
} else {
# Remove from current point in current packet
substr($current_packet, $in_packet_cursor, $current_move->{d}) = '';
$current_move = shift @$r_move;
}
} else {
# Substitute stuff
if ($current_move->{s_s} > length($current_packet) - $in_packet_cursor) {
# {s_s=>3, s_v=>GET} on ['GE', 'T /foo']
$current_move->{s_s} -= length($current_packet) - $in_packet_cursor;
substr($current_packet, $in_packet_cursor) = substr($current_move->{s_v}, 0, length($current_packet) - $in_packet_cursor);
push @result, $current_packet;
$current_move->{s_v} = substr($current_move->{s_v}, length($current_packet) - $in_packet_cursor);
$current_packet = shift @$r_packet;
$in_packet_cursor = 0;
} else {
substr($current_packet, $in_packet_cursor, $current_move->{s_s}) = $current_move->{s_v};
$in_packet_cursor += length($current_move->{s_v});
$current_move = shift @$r_move;
}
}
}
return \@result;
}
# Given a request as an array of packets, will parse it, append the appropriate
# headers and return another array of packets.
# The function implemented here can be high-level summarized as:
# 1 - Concatenate all packets to obtain a string representation of request.
# 2 - Parse the string representation
# 3 - Get the "moves" from the parsing
# 4 - Apply the "moves" to the packets.
sub build_request_from_packets($$$$$) {
my ( $name, $more_headers, $is_chunked, $conn_header, $request_packets ) = @_;
# Concatenate packets as a string
my $parsable_request = '';
my @packet_length;
for my $one_packet (@$request_packets) {
$parsable_request .= $one_packet;
push @packet_length, length($one_packet);
}
# Parse the string representation.
my $parsed_req = parse_request( $name, \$parsable_request );
# Append headers
my $len_header = '';
if ( !$is_chunked
&& defined $parsed_req->{content}
&& $parsed_req->{content} ne ''
&& $more_headers !~ /(?:^|\n)Content-Length:/ )
{
$parsed_req->{content} =~ s/^\s+|\s+$//gs;
$len_header .=
"Content-Length: " . length( $parsed_req->{content} ) . "\r\n";
}
$more_headers =~ s/(?<!\r)\n/\r\n/gs;
my $headers = '';
if ($more_headers !~ /(?:^|\n)Host:/msi) {
$headers .= "Host: $ServerName\r\n";
}
if ($more_headers !~ /(?:^|\n)Connection/msi) {
$headers .= "Connection: $conn_header\r\n";
}
$headers .= "$more_headers$len_header\r\n";
$parsed_req->{method} .= ' ';
$parsed_req->{url} .= ' ';
$parsed_req->{http_ver} .= "\r\n";
$parsed_req->{headers} = $headers;
# Get the moves from parsing
my @elements_moves = get_moves($parsed_req);
# Apply them to the packets.
return apply_moves($request_packets, \@elements_moves);
}
sub parse_more_headers ($) {
my ($in) = @_;
my @headers = split /\n+/, $in;
my $is_chunked;
my $out = '';
for my $header (@headers) {
next if $header =~ /^\s*\#/;
#warn "HEADER: $header";
my ($key, $val) = split /:\s*/, $header, 2;
if (!defined $val) {
$val = '';
}
if (lc($key) eq 'transfer-encoding' and $val eq 'chunked') {
$is_chunked = 1;
}
#warn "[$key, $val]\n";
$out .= "$key: $val\r\n";
}
return $out, $is_chunked;
}
# Returns an array of array of hashes from the block. Each element of
# the first-level array is a request.
# Each request is an array of the "packets" to be sent. Each packet is a
# string to send, with an (optionnal) delay before sending it.
# This function parses (and therefore defines the syntax) of "request*"
# sections. See documentation for supported syntax.
sub get_req_from_block ($) {
my ($block) = @_;
my $name = $block->name;
my @req_list = ();
if (defined $block->raw_request) {
# Should be deprecated.
if (ref $block->raw_request && ref $block->raw_request eq 'ARRAY') {
# User already provided an array. So, he/she specified where the
# data should be split. This allows for backward compatibility but
# should use request with arrays as it provides the same functionnality.
my @rr_list = ();
for my $elt (@{ $block->raw_request }) {
push @rr_list, {value => $elt};
}
push @req_list, \@rr_list;
} else {
push @req_list, [{value => $block->raw_request}];
}
} else {
my $request;
if (defined $block->request_eval) {
diag "$name - request_eval DEPRECATED. Use request eval instead.";
$request = eval $block->request_eval;
if ($@) {
warn $@;
}
} else {
$request = $block->request;
if (defined $request) {
while ($request =~ s/^\s*\#[^\n]+\s+|^\s+//gs) {
# do nothing
}
}
#warn "my req: $request";
}
my $more_headers = $block->more_headers || '';
if ( $block->pipelined_requests ) {
my $reqs = $block->pipelined_requests;
if (!ref $reqs || ref $reqs ne 'ARRAY') {
bail_out(
"$name - invalid entries in --- pipelined_requests");
}
my $i = 0;
my $prq = "";
for my $request (@$reqs) {
my $conn_type;
if ($i == @$reqs - 1) {
$conn_type = 'close';
} else {
$conn_type = 'keep-alive';
}
my ($hdr, $is_chunked);
if (ref $more_headers eq 'ARRAY') {
#warn "Found ", scalar @$more_headers, " entries in --- more_headers.";
$hdr = $more_headers->[$i];
if (!defined $hdr) {
bail_out("--- more_headers lacks data for the $i pipelined request");
}
($hdr, $is_chunked) = parse_more_headers($hdr);
#warn "more headers: $hdr";
} else {
($hdr, $is_chunked) = parse_more_headers($more_headers);
}
my $r_br = build_request_from_packets($name, $hdr,
$is_chunked, $conn_type,
[$request] );
$prq .= $$r_br[0];
$i++;
}
push @req_list, [{value =>$prq}];
} else {
my ($is_chunked, $hdr);
# request section.
if (!ref $request) {
if (ref $more_headers eq 'ARRAY') {
#warn "Found ", scalar @$more_headers, " entries in --- more_headers.";
$hdr = $more_headers->[0];
if (!defined $hdr) {
bail_out("--- more_headers lacks data for the request");
}
($hdr, $is_chunked) = parse_more_headers($hdr);
#warn "more headers: $hdr";
} else {
($hdr, $is_chunked) = parse_more_headers($more_headers);
}
# One request and it is a good old string.
my $r_br = build_request_from_packets($name, $hdr,
$is_chunked, 'close',
[$request] );
push @req_list, [{value => $$r_br[0]}];
} elsif (ref $request eq 'ARRAY') {
# A bunch of requests...
my $i = 0;
for my $one_req (@$request) {
if (ref $more_headers eq 'ARRAY') {
#warn "Found ", scalar @$more_headers, " entries in --- more_headers.";
$hdr = $more_headers->[$i];
if (!defined $hdr) {
bail_out("--- more_headers lacks data for the "
. "${i}th request");
}
($hdr, $is_chunked) = parse_more_headers($hdr);
#warn "more headers: $hdr";
} else {
($hdr, $is_chunked) = parse_more_headers($more_headers);
}
if (!ref $one_req) {
# This request is a good old string.
my $r_br = build_request_from_packets($name, $hdr,
$is_chunked, 'close',
[$one_req] );
push @req_list, [{value => $$r_br[0]}];
} elsif (ref $one_req eq 'ARRAY') {
# Request expressed as a serie of packets
my @packet_array = ();
for my $one_packet (@$one_req) {
if (!ref $one_packet) {
# Packet is a string.
push @packet_array, $one_packet;
} elsif (ref $one_packet eq 'HASH'){
# Packet is a hash with a value...
push @packet_array, $one_packet->{value};
} else {
bail_out "$name - Invalid syntax. $one_packet should be a string or hash with value.";
}
}
my $transformed_packet_array = build_request_from_packets($name, $hdr,
$is_chunked, 'close',
\@packet_array);
my @transformed_req = ();
my $idx = 0;
for my $one_transformed_packet (@$transformed_packet_array) {
if (!ref $$one_req[$idx]) {
push @transformed_req, {value => $one_transformed_packet};
} else {
# Is a HASH (checked above as $one_packet)
$$one_req[$idx]->{value} = $one_transformed_packet;
push @transformed_req, $$one_req[$idx];
}
$idx++;
}
push @req_list, \@transformed_req;
} else {
bail_out "$name - Invalid syntax. $one_req should be a string or an array of packets.";
}
$i++;
}
} else {
bail_out(
"$name - invalid ---request : MUST be string or array of requests");
}
}
}
return \@req_list;
}
sub quote_sh_args ($) {
my ($args) = @_;
for my $arg (@$args) {
if ($arg =~ m{^[- "&%;,|?*.+=\w:/()]*$}) {
if ($arg =~ /[ "&%;,|?*()]/) {
$arg = "'$arg'";
}
next;
}
$arg =~ s/\\/\\\\/g;
$arg =~ s/'/\\'/g;
$arg =~ s/\n/\\n/g;
$arg =~ s/\r/\\r/g;
$arg =~ s/\t/\\t/g;
$arg = "\$'$arg'";
}
return "@$args";
}
sub run_filter_helper($$$) {
my ($block, $filter, $content) = @_;
my $name = $block->name;
if (ref $filter && ref $filter eq 'CODE') {
$content = $filter->($content);
} elsif (!ref $filter) {
for ($filter) {
if ($_ eq 'md5_hex') {
$content = Digest::MD5::md5_hex($content);
} elsif ($_ eq 'sha1_hex') {
$content = Digest::SHA::sha1_hex($content);
} elsif ($_ eq 'uc') {
$content = uc($content);
} elsif ($_ eq 'lc') {
$content = lc($content);
} elsif ($_ eq 'ucfirst') {
$content = ucfirst($content);
} elsif ($_ eq 'lcfirst') {
$content = lcfirst($content);
} elsif ($_ eq 'length') {
$content = length($content);
} else {
bail_out("$name - unknown filter, \"$filter\", "
. "specified in the --- response_body_filters section");
}
}
} else {
bail_out("$name - the --- response_body_filters section "
. "only supports subroutine reference values and string values");
}
return $content;
}
sub run_test_helper ($$) {
my ($block, $dry_run, $repeated_req_idx) = @_;
#warn "repeated req idx: $repeated_req_idx";
my $name = $block->name;
my $r_req_list = get_req_from_block($block);
if ( $#$r_req_list < 0 ) {
bail_out("$name - request empty");
}
if (defined $block->curl) {
my $req = $r_req_list->[0];
my $cmd = gen_curl_cmd_from_req($block, $req);
warn "# ", quote_sh_args($cmd), "\n";
}
if ($CheckLeak) {
$dry_run = "the \"check leak\" testing mode";
}
if ($Benchmark) {
$dry_run = "the \"benchmark\" testing mode";
}
if ($Benchmark && !defined $block->no_check_leak) {
warn "$name\n";
my $req = $r_req_list->[0];
my ($nreqs, $concur);
if ($Benchmark =~ /^\s*(\d+)(?:\s+(\d+))?\s*$/) {
($nreqs, $concur) = ($1, $2);
}
if ($BenchmarkWarmup) {
my $cmd = gen_ab_cmd_from_req($block, $req, $BenchmarkWarmup, $concur);
warn "Warming up with $BenchmarkWarmup requests...\n";
system @$cmd;
}
my $cmd = gen_ab_cmd_from_req($block, $req, $nreqs, $concur);
$cmd = quote_sh_args($cmd);
warn "$cmd\n";
system "unbuffer $cmd > /dev/stderr";
}
if ($CheckLeak && !defined $block->no_check_leak) {
warn "$name\n";
my $req = $r_req_list->[0];
my $cmd = gen_ab_cmd_from_req($block, $req);
# start a sub-process to run ab or weighttp
my $pid = fork();
if (!defined $pid) {
bail_out("$name - fork() failed: $!");
} elsif ($pid == 0) {
# child process
exec @$cmd;
} else {
# main process
$Test::Nginx::Util::ChildPid = $pid;
sleep(1);
my $ngx_pid = get_pid_from_pidfile($name);
if ($PrevNginxPid && $ngx_pid) {
my $i = 0;
while ($ngx_pid == $PrevNginxPid) {
sleep 0.01;
$ngx_pid = get_pid_from_pidfile($name);
if (++$i > 1000) {
bail_out("nginx cannot be started");
}
}
}
$PrevNginxPid = $ngx_pid;
my @rss_list;
for (my $i = 0; $i < $CheckLeakCount; $i++) {
sleep 0.02;
my $out = `ps -eo pid,rss|grep $ngx_pid`;
if ($? != 0 && !is_running($ngx_pid)) {
if (is_running($pid)) {
kill(SIGKILL, $pid);
waitpid($pid, 0);
}
my $tb = Test::More->builder;
$tb->no_ending(1);
Test::More::fail("$name - the nginx process $ngx_pid is gone");
last;
}
my @lines = grep { $_->[0] eq $ngx_pid }
map { s/^\s+|\s+$//g; [ split /\s+/, $_ ] }
split /\n/, $out;
if (@lines == 0) {
last;
}
if (@lines > 1) {
warn "Bad ps output: \"$out\"\n";
next;
}
my $ln = shift @lines;
push @rss_list, $ln->[1];
}
#if ($Test::Nginx::Util::Verbose) {
warn "LeakTest: [@rss_list]\n";
#}
if (@rss_list == 0) {
warn "LeakTest: k=N/A\n";
} else {
my $k = get_linear_regression_slope(\@rss_list);
warn "LeakTest: k=$k\n";
#$k = get_linear_regression_slope([1 .. 100]);
#warn "K = $k (1 expected)\n";
#$k = get_linear_regression_slope([map { $_ * 2 } 1 .. 100]);
#warn "K = $k (2 expected)\n";
}
if (is_running($pid)) {
kill(SIGKILL, $pid);
waitpid($pid, 0);
}
}
}
#warn "request: $req\n";
my $timeout = parse_time($block->timeout);
if ( !defined $timeout ) {
$timeout = timeout();
}
my $res;
my $req_idx = 0;
my ($n, $need_array);
for my $one_req (@$r_req_list) {
my ($raw_resp, $head_req);
if ($dry_run) {
$raw_resp = "200 OK HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
} else {
($raw_resp, $head_req) = send_request( $one_req, $block->raw_request_middle_delay,
$timeout, $block );
}
#warn "raw resonse: [$raw_resp]\n";
if ($block->pipelined_requests) {
$n = @{ $block->pipelined_requests };
$need_array = 1;
} else {
$need_array = $#$r_req_list > 0;
}
again:
if ($Test::Nginx::Util::Verbose) {
warn "!!! resp: [$raw_resp]";
}
if (!defined $raw_resp) {
$raw_resp = '';
}
my ( $raw_headers, $left );
if (!defined $block->ignore_response) {
if ($Test::Nginx::Util::Verbose) {
warn "parse response\n";
}
if (defined $block->http09) {
$res = HTTP::Response->new(200, "OK", [], $raw_resp);
$raw_headers = '';
} else {
( $res, $raw_headers, $left ) = parse_response( $name, $raw_resp, $head_req );
}
}
if (!$n) {
if ($left) {
my $name = $block->name;
$left =~ s/([\0-\037\200-\377])/sprintf('\x{%02x}',ord $1)/eg;
warn "WARNING: $name - unexpected extra bytes after last chunk in ",
"response: \"$left\"\n";
}
} else {
$raw_resp = $left;
$n--;
}
if (!defined $block->ignore_response) {
check_error_code($block, $res, $dry_run, $req_idx, $need_array);
check_raw_response_headers($block, $raw_headers, $dry_run, $req_idx, $need_array);
check_response_headers($block, $res, $raw_headers, $dry_run, $req_idx, $need_array);
transform_response_body($block, $res, $req_idx);
check_response_body($block, $res, $dry_run, $req_idx, $repeated_req_idx, $need_array);
}
if ($n || $req_idx < @$r_req_list - 1) {
if ($block->wait) {
sleep($block->wait);
}
check_error_log($block, $res, $dry_run, $repeated_req_idx, $need_array);
if (!defined $block->ignore_response) {
check_access_log($block, $dry_run, $repeated_req_idx);
}
}
$req_idx++;
if ($n) {
goto again;
}
}
if ($block->wait) {
sleep($block->wait);
}
if ($Test::Nginx::Util::Verbose) {
warn "Testing stap...\n";
}
test_stap($block, $dry_run);
check_error_log($block, $res, $dry_run, $repeated_req_idx, $need_array);
if (!defined $block->ignore_response) {
check_access_log($block, $dry_run, $repeated_req_idx);
}
}
sub test_stap ($$) {
my ($block, $dry_run) = @_;
return if !$block->{stap};
my $name = $block->name;
my $reason;
if ($dry_run) {
$reason = "the lack of directive $dry_run";
}
if (!$UseStap) {
$dry_run = 1;
$reason ||= "env TEST_NGINX_USE_STAP is not set";
}
my $fname = stap_out_fname();
if ($fname && ($fname eq '/dev/stdout' || $fname eq '/dev/stderr')) {
$dry_run = 1;
$reason ||= "TEST_NGINX_TAP_OUT is set to $fname";
}
my $stap_out = $block->stap_out;
my $stap_out_like = $block->stap_out_like;
my $stap_out_unlike = $block->stap_out_unlike;
SKIP: {
skip "$name - stap_out - tests skipped due to $reason", 1 if $dry_run;
my $fh = stap_out_fh();
if (!$fh) {
bail_out("no stap output file handle found");
}
my $out = '';
for (1..2) {
if (sleep_time() < 0.2) {
sleep 0.2;
} else {
sleep sleep_time();
}
while (<$fh>) {
$out .= $_;
}
if ($out) {
last;
}
}
if ($Test::Nginx::Util::Verbose) {
warn "stap out: $out\n";
}
if (defined $stap_out) {
if ($NoLongString) {
is($out, $block->stap_out, "$name - stap output expected");
} else {
is_string($out, $block->stap_out, "$name - stap output expected");
}
}
if (defined $stap_out_like) {
like($out || '', qr/$stap_out_like/sm,
"$name - stap output should match the pattern");
}
if (defined $stap_out_unlike) {
unlike($out || '', qr/$stap_out_unlike/sm,
"$name - stap output should not match the pattern");
}
}
}
# Helper function to retrieve a "check" (e.g. error_code) section. This also
# checks that tests with arrays of requests are arrays themselves.
sub get_indexed_value($$$$) {
my ($name, $value, $req_idx, $need_array) = @_;
if ($need_array) {
if (ref $value && ref $value eq 'ARRAY') {
return $$value[$req_idx];
}
bail_out("$name - You asked for many requests, the expected results should be arrays as well.");
} else {
# One element but still provided as an array.
if (ref $value && ref $value eq 'ARRAY') {
if ($req_idx != 0) {
bail_out("$name - SHOULD NOT HAPPEN: idx != 0 and don't need array.");
}
return $$value[0];
}
return $value;
}
}
sub check_error_code ($$$$$) {
my ($block, $res, $dry_run, $req_idx, $need_array) = @_;
my $name = $block->name;
SKIP: {
skip "$name - tests skipped due to $dry_run", 1 if $dry_run;
if ( defined $block->error_code_like ) {
my $val = get_indexed_value($name, $block->error_code_like, $req_idx, $need_array);
like( ($res && $res->code) || '',
qr/$val/sm,
"$name - status code ok" );
} elsif ( defined $block->error_code ) {
is( ($res && $res->code) || '',
get_indexed_value($name, $block->error_code, $req_idx, $need_array),
"$name - status code ok" );
} else {
is( ($res && $res->code) || '', 200, "$name - status code ok" );
}
}
}
sub check_raw_response_headers($$$$$) {
my ($block, $raw_headers, $dry_run, $req_idx, $need_array) = @_;
my $name = $block->name;
if (defined $block->raw_response_headers_like) {
SKIP: {
skip "$name - raw_response_headers_like - tests skipped due to $dry_run", 1 if $dry_run;
my $expected = get_indexed_value($name,
$block->raw_response_headers_like,
$req_idx,
$need_array);
like $raw_headers, qr/$expected/s, "$name - raw resp headers like";
}
}
if (defined $block->raw_response_headers_unlike) {
SKIP: {
skip "$name - raw_response_headers_unlike - tests skipped due to $dry_run", 1 if $dry_run;
my $expected = get_indexed_value($name,
$block->raw_response_headers_unlike,
$req_idx,
$need_array);
unlike $raw_headers, qr/$expected/s, "$name - raw resp headers unlike";
}
}
}
sub check_response_headers($$$$$) {
my ($block, $res, $raw_headers, $dry_run, $req_idx, $need_array) = @_;
my $name = $block->name;
if ( defined $block->response_headers ) {
my $headers = parse_headers( get_indexed_value($name,
$block->response_headers,
$req_idx,
$need_array));
while ( my ( $key, $val ) = each %$headers ) {
if ( !defined $val ) {
#warn "HIT";
SKIP: {
skip "$name - response_headers - tests skipped due to $dry_run", 1 if $dry_run;
unlike $raw_headers, qr/^\s*\Q$key\E\s*:/ms,
"$name - header $key not present in the raw headers";
}
next;
}
$val =~ s/\$ServerPort\b/$ServerPort/g;
$val =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;