From bb7bc6aabc9e62acfee80b463426748ec002ccce Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Fri, 3 Apr 2026 15:49:35 +0300
Subject: [PATCH] Add POSIX functions tests
Signed-off-by: Petr Shumilov
---
.../stdlib/system/system-functions.h | 2 +-
.../python/tests/system_function/__init__.py | 0
.../php/data/component-config.yaml | 7 +++
.../tests/system_function/php/index.php | 38 ++++++++++++++++
.../system_function/test_posix_functions.py | 43 +++++++++++++++++++
5 files changed, 89 insertions(+), 1 deletion(-)
create mode 100644 tests/python/tests/system_function/__init__.py
create mode 100644 tests/python/tests/system_function/php/data/component-config.yaml
create mode 100644 tests/python/tests/system_function/php/index.php
create mode 100644 tests/python/tests/system_function/test_posix_functions.py
diff --git a/runtime-light/stdlib/system/system-functions.h b/runtime-light/stdlib/system/system-functions.h
index 2bc407c5fc..70918d6fb8 100644
--- a/runtime-light/stdlib/system/system-functions.h
+++ b/runtime-light/stdlib/system/system-functions.h
@@ -181,7 +181,7 @@ inline Optional> f$posix_getpwuid(int64_t user_id) noexcept {
array result{array_size{7, false}};
result.set_value(string{kphp::posix::impl::NAME_PWUID_KEY.data(), kphp::posix::impl::NAME_PWUID_KEY.size()}, string{pwd.pw_name});
- result.set_value(string{kphp::posix::impl::PASSWD_PWUID_KEY.data(), kphp::posix::impl::NAME_PWUID_KEY.size()}, string{pwd.pw_passwd});
+ result.set_value(string{kphp::posix::impl::PASSWD_PWUID_KEY.data(), kphp::posix::impl::PASSWD_PWUID_KEY.size()}, string{pwd.pw_passwd});
result.set_value(string{kphp::posix::impl::UID_PWUID_KEY.data(), kphp::posix::impl::UID_PWUID_KEY.size()}, static_cast(pwd.pw_uid));
result.set_value(string{kphp::posix::impl::GID_PWUID_KEY.data(), kphp::posix::impl::GID_PWUID_KEY.size()}, static_cast(pwd.pw_gid));
result.set_value(string{kphp::posix::impl::GECOS_PWUID_KEY.data(), kphp::posix::impl::GECOS_PWUID_KEY.size()}, string{pwd.pw_gecos});
diff --git a/tests/python/tests/system_function/__init__.py b/tests/python/tests/system_function/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/python/tests/system_function/php/data/component-config.yaml b/tests/python/tests/system_function/php/data/component-config.yaml
new file mode 100644
index 0000000000..5ec7d22c4e
--- /dev/null
+++ b/tests/python/tests/system_function/php/data/component-config.yaml
@@ -0,0 +1,7 @@
+entry: script
+components:
+ script:
+ image: KPHP
+ scope: Request
+ args: {}
+ links: {}
diff --git a/tests/python/tests/system_function/php/index.php b/tests/python/tests/system_function/php/index.php
new file mode 100644
index 0000000000..6046f0678a
--- /dev/null
+++ b/tests/python/tests/system_function/php/index.php
@@ -0,0 +1,38 @@
+ posix_getpid()
+ ));
+}
+
+function test_posix_getuid() {
+ echo json_encode(array(
+ "uid" => posix_getuid()
+ ));
+}
+
+function test_posix_getpwuid() {
+ echo json_encode(posix_getpwuid(posix_getuid()));
+}
+
+function main() {
+ switch ($_SERVER["PHP_SELF"]) {
+ case "/test_posix_getpid": {
+ test_posix_getpid();
+ return;
+ }
+ case "/test_posix_getuid": {
+ test_posix_getuid();
+ return;
+ }
+ case "/test_posix_getpwuid": {
+ test_posix_getpwuid();
+ return;
+ }
+ }
+
+ critical_error("unknown test");
+}
+
+main();
diff --git a/tests/python/tests/system_function/test_posix_functions.py b/tests/python/tests/system_function/test_posix_functions.py
new file mode 100644
index 0000000000..2fb9ce1127
--- /dev/null
+++ b/tests/python/tests/system_function/test_posix_functions.py
@@ -0,0 +1,43 @@
+import json
+import os
+import pwd
+import typing
+from python.lib.testcase import WebServerAutoTestCase
+from python.lib.kphp_server import KphpServer
+
+class TestErrors(WebServerAutoTestCase):
+ @classmethod
+ def extra_class_setup(cls):
+ if not cls.should_use_k2():
+ cls.web_server.update_options({
+ "--workers-num": 1
+ })
+
+ def test_posix_getpid(self):
+ pid = 0
+ if isinstance(self.web_server, KphpServer):
+ pid = typing.cast(KphpServer, self.web_server).get_workers()[0].pid
+ else:
+ pid = self.web_server.pid
+
+ response = self.web_server.http_request(uri="/test_posix_getpid", method='GET')
+ self.assertEqual(pid, json.loads(response.text)["pid"])
+ self.assertEqual(200, response.status_code)
+
+ def test_posix_getuid(self):
+ response = self.web_server.http_request(uri="/test_posix_getuid", method='GET')
+ self.assertEqual(os.getuid(), json.loads(response.text)["uid"])
+ self.assertEqual(200, response.status_code)
+
+ def test_posix_getpwuid(self):
+ response = self.web_server.http_request(uri="/test_posix_getpwuid", method='GET')
+ expected = pwd.getpwuid(os.getuid())
+ got = json.loads(response.text)
+ self.assertEqual(expected[0], got['name'])
+ self.assertEqual(expected[1], got['passwd'])
+ self.assertEqual(expected[2], got['uid'])
+ self.assertEqual(expected[3], got['gid'])
+ self.assertEqual(expected[4], got['gecos'])
+ self.assertEqual(expected[5], got['dir'])
+ self.assertEqual(expected[6], got['shell'])
+ self.assertEqual(200, response.status_code)