Skip to content

Commit d0cc4e2

Browse files
authored
[k2[ add POSIX functions tests (#1591)
1 parent 96003f8 commit d0cc4e2

5 files changed

Lines changed: 89 additions & 1 deletion

File tree

runtime-light/stdlib/system/system-functions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ inline Optional<array<mixed>> f$posix_getpwuid(int64_t user_id) noexcept {
181181

182182
array<mixed> result{array_size{7, false}};
183183
result.set_value(string{kphp::posix::impl::NAME_PWUID_KEY.data(), kphp::posix::impl::NAME_PWUID_KEY.size()}, string{pwd.pw_name});
184-
result.set_value(string{kphp::posix::impl::PASSWD_PWUID_KEY.data(), kphp::posix::impl::NAME_PWUID_KEY.size()}, string{pwd.pw_passwd});
184+
result.set_value(string{kphp::posix::impl::PASSWD_PWUID_KEY.data(), kphp::posix::impl::PASSWD_PWUID_KEY.size()}, string{pwd.pw_passwd});
185185
result.set_value(string{kphp::posix::impl::UID_PWUID_KEY.data(), kphp::posix::impl::UID_PWUID_KEY.size()}, static_cast<int64_t>(pwd.pw_uid));
186186
result.set_value(string{kphp::posix::impl::GID_PWUID_KEY.data(), kphp::posix::impl::GID_PWUID_KEY.size()}, static_cast<int64_t>(pwd.pw_gid));
187187
result.set_value(string{kphp::posix::impl::GECOS_PWUID_KEY.data(), kphp::posix::impl::GECOS_PWUID_KEY.size()}, string{pwd.pw_gecos});

tests/python/tests/system_function/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
entry: script
2+
components:
3+
script:
4+
image: KPHP
5+
scope: Request
6+
args: {}
7+
links: {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
function test_posix_getpid() {
4+
echo json_encode(array(
5+
"pid" => posix_getpid()
6+
));
7+
}
8+
9+
function test_posix_getuid() {
10+
echo json_encode(array(
11+
"uid" => posix_getuid()
12+
));
13+
}
14+
15+
function test_posix_getpwuid() {
16+
echo json_encode(posix_getpwuid(posix_getuid()));
17+
}
18+
19+
function main() {
20+
switch ($_SERVER["PHP_SELF"]) {
21+
case "/test_posix_getpid": {
22+
test_posix_getpid();
23+
return;
24+
}
25+
case "/test_posix_getuid": {
26+
test_posix_getuid();
27+
return;
28+
}
29+
case "/test_posix_getpwuid": {
30+
test_posix_getpwuid();
31+
return;
32+
}
33+
}
34+
35+
critical_error("unknown test");
36+
}
37+
38+
main();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import json
2+
import os
3+
import pwd
4+
import typing
5+
from python.lib.testcase import WebServerAutoTestCase
6+
from python.lib.kphp_server import KphpServer
7+
8+
class TestErrors(WebServerAutoTestCase):
9+
@classmethod
10+
def extra_class_setup(cls):
11+
if not cls.should_use_k2():
12+
cls.web_server.update_options({
13+
"--workers-num": 1
14+
})
15+
16+
def test_posix_getpid(self):
17+
pid = 0
18+
if isinstance(self.web_server, KphpServer):
19+
pid = typing.cast(KphpServer, self.web_server).get_workers()[0].pid
20+
else:
21+
pid = self.web_server.pid
22+
23+
response = self.web_server.http_request(uri="/test_posix_getpid", method='GET')
24+
self.assertEqual(pid, json.loads(response.text)["pid"])
25+
self.assertEqual(200, response.status_code)
26+
27+
def test_posix_getuid(self):
28+
response = self.web_server.http_request(uri="/test_posix_getuid", method='GET')
29+
self.assertEqual(os.getuid(), json.loads(response.text)["uid"])
30+
self.assertEqual(200, response.status_code)
31+
32+
def test_posix_getpwuid(self):
33+
response = self.web_server.http_request(uri="/test_posix_getpwuid", method='GET')
34+
expected = pwd.getpwuid(os.getuid())
35+
got = json.loads(response.text)
36+
self.assertEqual(expected[0], got['name'])
37+
self.assertEqual(expected[1], got['passwd'])
38+
self.assertEqual(expected[2], got['uid'])
39+
self.assertEqual(expected[3], got['gid'])
40+
self.assertEqual(expected[4], got['gecos'])
41+
self.assertEqual(expected[5], got['dir'])
42+
self.assertEqual(expected[6], got['shell'])
43+
self.assertEqual(200, response.status_code)

0 commit comments

Comments
 (0)