mwrap.git  about / heads / tags
LD_PRELOAD malloc wrapper + line stats for Ruby
blob 76fe7d1fe7d5b25aceaf7820ea4407f7dde134bb 5619 bytes (raw)
$ git show HEAD:t/httpd.t	# shows this blob on the CLI

  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
 
#!perl -w
# Copyright (C) mwrap hackers <mwrap-perl@80x24.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
use v5.12;
use IO::Socket::UNIX;
use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
use POSIX qw(dup2 _exit mkfifo);
BEGIN { require './t/test_common.perl' };
my $env = { MWRAP => "socket_dir:$mwrap_tmp" };
my $f1 = "$mwrap_tmp/f1";
my $f2 = "$mwrap_tmp/f2";
mkfifo($f1, 0600) // plan(skip_all => "mkfifo: $!");
mkfifo($f2, 0600) // plan(skip_all => "mkfifo: $!");
my $src = $mwrap_src ? # $mwrap_src is Perl-only, Ruby otherwise
	"open my \$f1, '>', '$f1'; close \$f1; open my \$f2, '<', '$f2'" :
	"File.open('$f1', 'w').close; File.open('$f2', 'r').close";
my $pid = mwrap_run('httpd test', $env, '-e', $src);
my $spid;
my $mw_exit;
my $cleanup = sub {
	if (defined $spid) {
		if (kill('TERM', $spid)) {
			waitpid($spid, 0);
			$? == 0 or warn "rproxy died with \$?=$?";
		} else {
			warn "kill $spid: $!";
		}
		undef $spid;
	}
	use autodie;
	if (defined $pid) {
		my $exit = $?;
		open my $fh, '>', $f2;
		close $fh;
		waitpid($pid, 0);
		$mw_exit = $?;
		undef $pid;
		diag "err: ".slurp($mwrap_err);
		$? = $exit;
	}
};
END { $cleanup->() }

my $sock = "$mwrap_tmp/$pid.sock";
my %o = (Peer => $sock , Type => SOCK_STREAM);
local $SIG{PIPE} = 'IGNORE';

open my $fh, '<', $f1;
is(my $nil = <$fh>, undef, 'FIFO open');
close $fh;
ok(-S $sock, 'socket created');
my $c = IO::Socket::UNIX->new(%o);
ok($c, 'socket connected');
is(send($c, 'GET', MSG_NOSIGNAL), 3, 'trickled 3 bytes') or diag "send: $!";

my $cout = "$mwrap_tmp/cout";
my @curl = (qw(curl -sf --unix-socket), $sock, '-o', $cout);
push @curl, '-vS' if $ENV{V};
my $rc = system(@curl, "http://0/$pid/each/2000");
my $curl_unix;
SKIP: {
	skip 'curl lacks --unix-socket support', 1 if $rc == 512;
	is($rc, 0, 'curl /each');
	unlink($cout);
	$curl_unix = 1;

	$rc = system(@curl, "http://0/$pid/each/2000");
	is($rc, 0, 'curl /each');
	unlink($cout);

	$rc = system(@curl, "http://0/$pid/");
	is($rc, 0, 'curl / (PID root)');
	like(slurp($cout), qr/<html>/, 'root shown');

	$rc = system(@curl, '-XPOST', "http://0/$pid/trim");
	is($rc, 0, 'curl / (PID root)');
	like(slurp($cout), qr/trimming/, 'trim started');
	unlink($cout);
};

{
	my $req = " /$pid/each/20000 HTTP/1.0\r\n\r\n";
	is(send($c, $req, MSG_NOSIGNAL), length($req),
		'wrote rest of response') or diag "send: $!";
	my $x = do { local $/; <$c> } or diag "readline: $!";
	like($x, qr!</html>\n?\z!s, 'got complete HTML response');
}

SKIP: {
	my (@rproxy, @missing);
	if (-e 'script/mwrap-rproxy') { # Perl version
		@rproxy = ($^X, '-w', './blib/script/mwrap-rproxy');
	} else {
		my $exe = `which mwrap-rproxy`;
		if ($? == 0 && defined($exe)) {
			chomp($rproxy[0] = $exe);
		} else {
			push @missing, 'mwrap-rproxy';
		}
	}
	for my $m (qw(Plack::Util HTTP::Tiny)) {
		eval "require $m" or push(@missing, $m);
	}
	skip join(', ', @missing).' missing', 1 if @missing;
	my $srv = IO::Socket::INET->new(LocalAddr => '127.0.0.1',
				ReuseAddr => 1, Proto => 'tcp',
				Type => SOCK_STREAM,
				Listen => 1024);
	$spid = fork;
	if ($spid == 0) {
		local $ENV{LISTEN_PID} = $$;
		local $ENV{LISTEN_FDS} = 1;
		my $fl = fcntl($srv, F_GETFD, 0);
		fcntl($srv, F_SETFD, $fl &= ~FD_CLOEXEC);
		if (fileno($srv) != 3) {
			dup2(fileno($srv), 3) or die "dup2: $!";
		}
		local $ENV{PLACK_ENV} = 'deployment' if !$ENV{V};
		no warnings 'exec';
		exec @rproxy, "--socket-dir=$mwrap_tmp";
		_exit(1);
	}
	my $http = HTTP::Tiny->new;
	my ($h, $p) = ($srv->sockhost, $srv->sockport);
	undef $srv;
	my $res = $http->get("http://$h:$p/");
	ok($res->{success}, 'listing success');
	like($res->{content}, qr!/$pid/each/\d+!, 'got listing for each');
	$res = $http->get("http://$h:$p/$pid/each/1");
	ok($res->{success}, 'each/1 success');
	my $t = '/at/$LOCATION link in /each/$NUM';
	if ($res->{content} =~ m!href="\.\./at/([^"]+)"!) {
		my $loc = $1;
		ok($t);
		$res = $http->get("http://$h:$p/$pid/at/$1");
		ok($res->{success}, '/at/$LOCATION endpoint');
		like($res->{content}, qr!\blive allocations at\b!,
			'live allocations shown');
	} else {
		fail($t);
	}
	if ($ENV{INTERACTIVE}) {
		diag "http://$h:$p/$pid/each/1 up for interactive testing";
		diag "- press Enter when done -";
		my $ok = <STDIN>;
	}
}

SKIP: {
	skip 'no reset w/o curl --unix-socket', 1 if !$curl_unix;
	my ($sqlite_v) = (`sqlite3 --version` =~ /([\d+\.]+)/);
	if ($?) {
		diag 'sqlite3 missing or broken';
		$sqlite_v = 0;
	} else {
		my @v = split(/\./, $sqlite_v);
		$sqlite_v = ($v[0] << 16) | ($v[1] << 8) | $v[2];
		diag 'sqlite_v='.sprintf('0x%x', $sqlite_v);
	}
	$rc = system(@curl, "http://0/$pid/each/100.csv");
	is($rc, 0, '.csv retrieved') or skip 'CSV failed', 1;
	my $db = "$mwrap_tmp/t.sqlite3";

	if ($sqlite_v >= 0x32000) {
		$rc = system(qw(sqlite3), $db,".import --csv $cout mwrap_each");
		is($rc, 0, 'sqlite3 import');
		my $n = `sqlite3 $db 'SELECT COUNT(*) FROM mwrap_each'`;
		is($?, 0, 'sqlite3 count');
		my $exp = split(/\n/, slurp($cout));
		is($n + 1, $exp, 'imported all rows into sqlite');
	} else {
		diag "sqlite 3.32.0+ needed for `.import --csv'";
	}

	$rc = system(@curl, qw(-d x=y), "http://0/$pid/reset");
	is($rc, 0, 'curl /reset');
	$rc = system(@curl, qw(-HX-Mwrap-BT:10 -XPOST),
			"http://0/$pid/ctl");
	is($rc, 0, 'curl /ctl (X-Mwrap-BT)');
	like(slurp($cout), qr/\bMWRAP=bt:10\b/, 'changed bt depth');

	$rc = system(@curl, qw(-HX-Mwrap-BT:10 -d blah http://0/ctl));
	is($rc >> 8, 22, '404 w/o PID prefix');
};


diag slurp($cout) if $ENV{V};
$cleanup->();
ok(!-e $sock, 'socket unlinked after cleanup');
is($mw_exit, 0, 'perl exited with $?==0');
done_testing;

git clone https://80x24.org/mwrap.git