view referer.t @ 1752:ba6e24e38f03

Tests: improved stop_daemons() to send signal again. As was observed, it's possible that a signal to complete a uwsgi daemon can be ignored while it is starting up, which results in tests hang due to eternal waiting on child processes termination. Notably, it is seen when running tests with a high number of prove jobs on a low-profile VM against nginx with broken modules and/or configuration. To reproduce: $ TEST_NGINX_GLOBALS=ERROR prove -j16 uwsgi*.t Inspecting uwsgi under ktrace on FreeBSD confirms that a SIGTERM signal is ignored at the very beginning of uwsgi startup. It is then replaced with a default action after listen(), thus waiting until uwsgi is ready to accept new TCP connections doesn't completely solve the hang window. The fix is to retry sending a signal some time after waitpid(WNOHANG) continuously demonstrated no progress with reaping a signaled process. It is modelled after f13ead27f89c that improved stop() for nginx.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 29 Dec 2021 22:29:23 +0300
parents 882267679006
children
line wrap: on
line source

#!/usr/bin/perl

# (C) Sergey Kandaurov

# Tests for referer module.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http referer rewrite/)->plan(54);

$t->write_file_expand('nginx.conf', <<'EOF');

%%TEST_GLOBALS%%

daemon off;

events {
}

http {
    %%TEST_GLOBALS_HTTP%%

    server {
        listen       127.0.0.1:8080;
        server_name  another;

        valid_referers server_names;
        return 200 "$host value $invalid_referer";
    }

    server {
        listen       127.0.0.1:8080;
        server_name  _;

        location / {
            valid_referers server_names;
            return 200 "$host value $invalid_referer";
        }
        server_name  below;
    }

    server {
        listen       127.0.0.1:8080;
        server_name  localhost ~bar ~^anchoredre$;

        location /blocked {
            valid_referers blocked www.example.org;
            return 200 "value $invalid_referer";
        }
        location /none {
            valid_referers none www.example.org;
            return 200 "value $invalid_referer";
        }
        location /simple {
            valid_referers www.example.org;
            return 200 "value $invalid_referer";
        }
        location /regex {
            valid_referers ~example.org ~(?-i)example.net;
            return 200 "value $invalid_referer";
        }
        location /regex2 {
            valid_referers ~example.org/uri;
            return 200 "value $invalid_referer";
        }
        location /regex3 {
            valid_referers ~example.org$;
            return 200 "value $invalid_referer";
        }
        location /uri {
            valid_referers www.example.org/uri;
            return 200 "value $invalid_referer";
        }
        location /sn {
            valid_referers server_names;
            return 200 "value $invalid_referer";
        }
        location /sn_blocked {
            valid_referers blocked server_names;
            return 200 "value $invalid_referer";
        }
        location /wc {
            valid_referers *.example.com *.example.org www.example.* example.*;
            return 200 "value $invalid_referer";
        }
        location /long {
            valid_referers ~.*;
            return 200 "value $invalid_referer";
        }
        location /wc2 {
            valid_referers www.example.*/uri;
            return 200 "value $invalid_referer";
        }
    }
}

EOF

$t->run();

###############################################################################

ok(valid('/simple', 'http://www.example.org'), 'simple');
ok(valid('/simple', 'http://www.example.org/uri'), 'simple uri');
ok(valid('/simple', 'http://www.example.org:' . port(8080) . '/uri'),
	'simple port uri');
ok(!valid('/simple', 'localhost'), 'simple invalid');
ok(valid('/simple', 'https://www.example.org'), 'https');
ok(!valid('/simple', 'example.com'), 'no scheme');
ok(!valid('/simple'), 'no none');
ok(valid('/none'), 'none');
ok(!valid('/none', ''), 'none empty');

ok(valid('/blocked', 'www.example.org'), 'blocked');
ok(valid('/blocked', 'www.example.com'), 'blocked 2');
ok(valid('/blocked', 'http://su'), 'blocked short');
ok(valid('/blocked', 'foobar'), 'blocked short no scheme');
ok(valid('/blocked', ''), 'blocked empty');

ok(!valid('/simple', 'foobar'), 'small');
ok(valid('/simple', 'http://www.example.org/' . 'a' x 256), 'long uri');
ok(!valid('/simple', 'http://www.example.' . 'a' x 256), 'long hostname');
ok(!valid('/wc', 'http://example.' . 'a' x 256), 'long hostname wildcard');

ok(valid('/long', 'http://' . 'a' x 255), 'long hostname 255');
ok(valid('/long', 'http://' . 'a' x 256), 'long hostname 256');
ok(!valid('/long', 'http://' . 'a' x 257), 'long hostname 257');

ok(valid('/uri', 'http://www.example.org/uri'), 'uri');
ok(valid('/uri', 'http://www.example.org/urii'), 'uri prefix');
ok(!valid('/uri', 'http://www.example.org/uRi'), 'uri case');
ok(valid('/uri', 'http://www.example.org:' . port(8080) . '/urii'), 'uri port');
ok(!valid('/uri', 'http://www.example.org/ur'), 'uri invalid len');
ok(!valid('/uri', 'http://www.example.org/urd'), 'uri invalid cmp');

ok(valid('/regex', 'http://www.example.org'), 'regex');
ok(valid('/regex', 'http://www.eXample.org'), 'regex caseless');
ok(valid('/regex', 'http://www.example.org/uri'), 'regex uri');
ok(!valid('/regex', 'http://www.example.com'), 'regex mismatch');
ok(!valid('/regex', 'http://www.eXample.net'), 'regex case mismatch');

ok(valid('/regex2', 'http://www.example.org/uri'), 'regex 2 uri');
ok(!valid('/regex2', 'http://www.example.org'), 'regex 2 no uri');
ok(valid('/regex2', 'http://www.example.org/uRI'), 'regex 2 uri caseless');
ok(valid('/regex3', 'https://www.eXample.org'), 'regex https');

ok(valid('/sn', 'http://localhost'), 'server_names');
ok(valid('/sn', 'http://localHost'), 'server_names caseless');
ok(valid('/sn', 'http://localhost/uri'), 'server_names uri');
ok(valid('/sn', 'http://foobar'), 'server_names regex');
ok(valid('/sn', 'http://foobAr'), 'server_names regex caseless');
ok(valid('/sn', 'http://foobAr/uri'), 'server_names regex caseless uri');
ok(valid('/sn', 'http://anchoredre/uri'), 'server_names regex anchored');
ok(valid('/sn', 'http://foobar/uri'), 'server_names regex uri');
ok(!valid('/sn', 'localhost'), 'server_names no scheme');
ok(!valid('/sn', 'foobar'), 'server_names regex no scheme');
ok(valid('/sn_blocked', 'localhost'), 'server_names no scheme blocked');

ok(valid('/wc', 'http://www.example.org'), 'wildcard head');
ok(valid('/wc', 'http://www.example.net'), 'wildcard tail');
ok(valid('/wc2', 'http://www.example.net/uri'), 'wildcard uri');
ok(valid('/wc2', 'http://www.example.net/urii'), 'wildcard uri prefix');
ok(!valid('/wc2', 'http://www.example.net/uRI'), 'wildcard uri case');

ok(valid('/', 'http://another', 'another'), 'server context');

# server_name below valid_referers

ok(valid('/', 'http://below', 'below'), 'server below');

###############################################################################

sub valid {
	my ($uri, $referer, $host) = @_;
	my $text;

	$host = 'localhost' unless defined $host;

	unless (defined $referer) {
		$text = http_get($uri);
	} else {
		$text = http(<<EOF);
GET $uri HTTP/1.0
Host: $host
Referer: $referer

EOF
	}

	$text =~ /value 1/ && return 0;
	$text =~ /value/ && return 1;
	fail("no valid_referers in $uri");
}

###############################################################################