-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2636 from niol/testsuite
fix uwsgi_regexp_match() with pcre2 and corresponding unittest
- Loading branch information
Showing
8 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/python3 | ||
|
||
|
||
import os | ||
import requests | ||
import signal | ||
import socket | ||
import subprocess | ||
import time | ||
import unittest | ||
|
||
|
||
TESTS_DIR = os.path.dirname(__file__) | ||
UWSGI_BINARY = os.getenv("UWSGI_BINARY", os.path.join(TESTS_DIR, "..", "uwsgi")) | ||
UWSGI_ADDR = "127.0.0.1" | ||
UWSGI_PORT = 8000 | ||
UWSGI_HTTP = f"{UWSGI_ADDR}:{UWSGI_PORT}" | ||
|
||
|
||
class BaseTest: | ||
""" | ||
Container class to avoid base test being run | ||
""" | ||
|
||
class UwsgiServerTest(unittest.TestCase): | ||
""" | ||
Test case with a server instance available on a socket for requests | ||
""" | ||
|
||
@classmethod | ||
def uwsgi_ready(cls): | ||
try: | ||
s = socket.socket() | ||
s.connect( | ||
( | ||
UWSGI_ADDR, | ||
UWSGI_PORT, | ||
) | ||
) | ||
except socket.error: | ||
return False | ||
else: | ||
return True | ||
finally: | ||
s.close() | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
# launch server | ||
cls.testserver = subprocess.Popen( | ||
[UWSGI_BINARY, "--http-socket", UWSGI_HTTP] + cls.ARGS | ||
) | ||
|
||
# ensure server is ready | ||
retries = 10 | ||
while not cls.uwsgi_ready() and retries > 0: | ||
time.sleep(0.1) | ||
retries = retries - 1 | ||
if retries == 0: | ||
raise RuntimeError("uwsgi test server is not available") | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.testserver.send_signal(signal.SIGTERM) | ||
cls.testserver.wait() | ||
|
||
|
||
class StaticTest(BaseTest.UwsgiServerTest): | ||
|
||
ARGS = [ | ||
"--plugin", | ||
"python3", # provide a request plugin if no embedded request plugin | ||
os.path.join(TESTS_DIR, "static", "config.ini"), | ||
] | ||
|
||
def test_static_expires(self): | ||
with requests.get(f"http://{UWSGI_HTTP}/foobar/config.ini") as r: | ||
self.assertTrue("Expires" in r.headers) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[uwsgi] | ||
need-app = False | ||
static-map = /foobar=t/static | ||
static-expires-uri = ^/foobar/ 315360000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <check.h> | ||
#include "../uwsgi.h" | ||
|
||
|
||
START_TEST(test_uwsgi_regexp_match) | ||
{ | ||
int result; | ||
uwsgi_pcre *pattern_all; | ||
uwsgi_pcre *pattern; | ||
|
||
result = uwsgi_regexp_build(".*", &pattern_all); | ||
ck_assert(result == 0); | ||
|
||
result = uwsgi_regexp_match(pattern_all, "/fooba", 6); | ||
ck_assert(result >= 0); | ||
|
||
result = uwsgi_regexp_build("/foobar/.*", &pattern); | ||
ck_assert(result == 0); | ||
|
||
result = uwsgi_regexp_match(pattern, "/fooba", 6); | ||
ck_assert(result < 0); | ||
|
||
result = uwsgi_regexp_match(pattern, "/foobar/baz", 11); | ||
ck_assert(result >= 0); | ||
|
||
pcre2_code_free(pattern_all); | ||
pcre2_code_free(pattern); | ||
} | ||
END_TEST | ||
|
||
START_TEST(test_uwsgi_regexp_match_ovec) | ||
{ | ||
int result; | ||
uwsgi_pcre *pattern; | ||
int *ovec = calloc((2+1)*2, sizeof(int)); | ||
char buf[20], sub[20]; | ||
|
||
result = uwsgi_regexp_build("^/foo/(.*)\\.jpg\\?([0-9]{2})", &pattern); | ||
ck_assert(result == 0); | ||
result = uwsgi_regexp_ovector(pattern); | ||
ck_assert(result == 2); | ||
|
||
result = uwsgi_regexp_match_ovec(pattern, "/fooba", 6, ovec, 2); | ||
ck_assert(result < 0); | ||
|
||
strcpy(buf, "/foo/bar.jpg?422"); | ||
result = uwsgi_regexp_match_ovec(pattern, buf, strlen(buf), ovec, 2); | ||
ck_assert(result >= 0); | ||
strncpy(sub, buf+ovec[0], ovec[1]-ovec[0]); | ||
sub[ovec[1]-ovec[0]] = '\0'; | ||
ck_assert_str_eq(sub, "/foo/bar.jpg?42"); | ||
strncpy(sub, buf+ovec[2], ovec[3]-ovec[2]); | ||
sub[ovec[3]-ovec[2]] = '\0'; | ||
ck_assert_str_eq(sub, "bar"); | ||
strncpy(sub, buf+ovec[4], ovec[5]-ovec[4]); | ||
sub[ovec[5]-ovec[4]] = '\0'; | ||
ck_assert_str_eq(sub, "42"); | ||
|
||
strcpy(sub, uwsgi_regexp_apply_ovec(buf, strlen(buf), "key=$1.$2.jpg", 13, ovec, 2)); | ||
ck_assert_str_eq(sub, "key=bar.42.jpg"); | ||
|
||
pcre2_code_free(pattern); | ||
free(ovec); | ||
} | ||
END_TEST | ||
|
||
Suite *check_regexp(void) | ||
{ | ||
Suite *s = suite_create("uwsgi regexp"); | ||
TCase *tc = tcase_create("regexp"); | ||
|
||
suite_add_tcase(s, tc); | ||
tcase_add_test(tc, test_uwsgi_regexp_match); | ||
tcase_add_test(tc, test_uwsgi_regexp_match_ovec); | ||
return s; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int nf; | ||
SRunner *r = srunner_create(check_regexp()); | ||
srunner_run_all(r, CK_NORMAL); | ||
nf = srunner_ntests_failed(r); | ||
srunner_free(r); | ||
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |