-
Notifications
You must be signed in to change notification settings - Fork 8k
Add unit test setup with php_network_connect_socket test #16987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
name: ./configure (unit tests) | ||
description: Configure PHP with minimal settings for unit testing | ||
runs: | ||
using: composite | ||
steps: | ||
- shell: bash | ||
run: | | ||
set -x | ||
./buildconf --force | ||
./configure --disable-all --enable-embed=static |
This file contains hidden or 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,75 @@ | ||
name: Unit Tests | ||
on: | ||
push: | ||
paths: | ||
- 'main/network.c' | ||
- 'tests/unit/**' | ||
- '.github/workflows/unit-tests.yml' | ||
branches: | ||
- master | ||
pull_request: | ||
paths: | ||
- 'main/network.c' | ||
- 'tests/unit/**' | ||
- '.github/workflows/unit-tests.yml' | ||
branches: | ||
- '**' | ||
workflow_dispatch: ~ | ||
|
||
permissions: | ||
contents: read | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.url || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
env: | ||
CC: ccache gcc | ||
CXX: ccache g++ | ||
|
||
jobs: | ||
UNIT_TESTS: | ||
if: github.repository == 'php/php-src' || github.event_name == 'pull_request' | ||
name: UNIT_TESTS_LINUX_X64 | ||
runs-on: ubuntu-24.04 | ||
timeout-minutes: 20 | ||
steps: | ||
- name: git checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: | | ||
set -x | ||
sudo apt-get update | ||
sudo apt-get install -y \ | ||
libcmocka-dev \ | ||
autoconf \ | ||
gcc \ | ||
make \ | ||
unzip \ | ||
bison \ | ||
re2c \ | ||
locales \ | ||
ccache | ||
|
||
- name: ccache | ||
uses: hendrikmuhs/[email protected] | ||
with: | ||
key: "unit-tests-${{hashFiles('main/php_version.h')}}" | ||
append-timestamp: false | ||
save: ${{ github.event_name != 'pull_request' }} | ||
|
||
- name: ./configure (minimal build) | ||
uses: ./.github/actions/configure-unit-tests | ||
|
||
- name: make libphp.a | ||
run: | | ||
set -x | ||
make -j$(/usr/bin/nproc) >/dev/null | ||
|
||
- name: Run unit tests | ||
run: | | ||
set -x | ||
cd tests/unit | ||
make test | ||
|
This file contains hidden or 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,32 @@ | ||
CC = gcc | ||
CFLAGS = -g -Wall -I../../ -I../../Zend -I../../main -I../../TSRM -I. -I.. | ||
COMMON_LDFLAGS = ../../.libs/libphp.a -lcmocka -lpthread -lm -ldl -lresolv -lutil | ||
|
||
# Update paths in .github/workflows/unit-tests.yml when adding new test to make it run in PR when such file changes | ||
TESTS = main/test_network | ||
main/test_network_SRC = main/test_network.c | ||
main/test_network_LDFLAGS = $(COMMON_LDFLAGS) -Wl,--wrap=connect,--wrap=poll,--wrap=getsockopt,--wrap=gettimeofday | ||
|
||
|
||
# Build all tests | ||
all: $(TESTS) | ||
|
||
# Build rule for each test | ||
$(TESTS): | ||
$(CC) $(CFLAGS) -o [email protected] $($(basename $@)_SRC) $($(basename $@)_LDFLAGS) | ||
|
||
# Run all tests | ||
.PHONY: test | ||
test: $(TESTS) | ||
@echo "Running all tests..." | ||
@for test in $(TESTS); do \ | ||
echo "Running $$test..."; \ | ||
$$test.out || exit 1; \ | ||
done | ||
|
||
# Clean tests | ||
.PHONY: clean | ||
clean: | ||
@for test in $(TESTS); do \ | ||
rm -f $$test.out; \ | ||
done |
This file contains hidden or 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,254 @@ | ||
#include "php.h" | ||
#include "php_network.h" | ||
#include <cmocka.h> | ||
|
||
// Mocked poll | ||
int __wrap_poll(struct pollfd *ufds, nfds_t nfds, int timeout) | ||
{ | ||
function_called(); | ||
check_expected(timeout); | ||
|
||
int n = mock_type(int); | ||
if (n > 0) { | ||
ufds->revents = 1; | ||
} else if (n < 0) { | ||
errno = -n; | ||
n = -1; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
// Mocked connect | ||
int __wrap_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | ||
{ | ||
function_called(); | ||
errno = mock_type(int); | ||
return errno != 0 ? -1 : 0; | ||
} | ||
|
||
// Mocked getsockopt | ||
int __wrap_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen) | ||
{ | ||
function_called(); | ||
int *error = (int *) optval; | ||
*error = mock_type(int); | ||
return mock_type(int); | ||
} | ||
|
||
// Mocked gettimeofday | ||
int __wrap_gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info) | ||
{ | ||
function_called(); | ||
struct timeval *now = mock_ptr_type(struct timeval *); | ||
if (now) { | ||
time_Info->tv_sec = now->tv_sec; | ||
time_Info->tv_usec = now->tv_usec; | ||
} | ||
return mock_type(int); | ||
} | ||
|
||
// Test successful connection | ||
static void test_php_network_connect_socket_immediate_success(void **state) { | ||
struct timeval timeout = { .tv_sec = 2, .tv_usec = 500000 }; | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, 0); | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout, NULL, &error_code); | ||
|
||
assert_int_equal(result, 0); | ||
assert_int_equal(error_code, 0); | ||
} | ||
|
||
// Test successful connection in progress followed by poll | ||
static void test_php_network_connect_socket_progress_success(void **state) { | ||
struct timeval timeout_tv = { .tv_sec = 2, .tv_usec = 500000 }; | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
// Mock connect setting EINPROGRESS errno | ||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, EINPROGRESS); | ||
|
||
// Mock time setting - ignored | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, NULL); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to return success | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 2500); | ||
will_return(__wrap_poll, 1); | ||
|
||
// Mock no socket error | ||
expect_function_call(__wrap_getsockopt); | ||
will_return(__wrap_getsockopt, 0); // optval saved result | ||
will_return(__wrap_getsockopt, 0); // actual return value | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout_tv, NULL, &error_code); | ||
|
||
assert_int_equal(result, 0); | ||
assert_int_equal(error_code, 0); | ||
} | ||
|
||
static void test_php_network_connect_socket_eintr_t1(void **state) { | ||
struct timeval timeout_tv = { .tv_sec = 2, .tv_usec = 500000 }; | ||
struct timeval start_time = { .tv_sec = 1000, .tv_usec = 0 }; // Initial time | ||
struct timeval retry_time = { .tv_sec = 1001, .tv_usec = 200000 }; // Time after EINTR | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
// Mock connect to set EINPROGRESS | ||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, EINPROGRESS); | ||
|
||
// Mock gettimeofday for initial call | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &start_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to return EINTR first | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 2500); | ||
will_return(__wrap_poll, -EINTR); | ||
|
||
// Mock gettimeofday after EINTR | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &retry_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to succeed on retry | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 1300); | ||
will_return(__wrap_poll, 1); | ||
|
||
// Mock no socket error | ||
expect_function_call(__wrap_getsockopt); | ||
will_return(__wrap_getsockopt, 0); | ||
will_return(__wrap_getsockopt, 0); | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout_tv, NULL, &error_code); | ||
|
||
// Ensure the function succeeds | ||
assert_int_equal(result, 0); | ||
assert_int_equal(error_code, 0); | ||
} | ||
|
||
static void test_php_network_connect_socket_eintr_t2(void **state) { | ||
struct timeval timeout_tv = { .tv_sec = 2, .tv_usec = 1500000 }; | ||
struct timeval start_time = { .tv_sec = 1000, .tv_usec = 300000 }; // Initial time | ||
struct timeval retry_time = { .tv_sec = 1001, .tv_usec = 200000 }; // Time after EINTR | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
// Mock connect to set EINPROGRESS | ||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, EINPROGRESS); | ||
|
||
// Mock gettimeofday for initial call | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &start_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to return EINTR first | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 3500); | ||
will_return(__wrap_poll, -EINTR); | ||
|
||
// Mock gettimeofday after EINTR | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &retry_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to succeed on retry | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 2600); | ||
will_return(__wrap_poll, 1); | ||
|
||
// Mock no socket error | ||
expect_function_call(__wrap_getsockopt); | ||
will_return(__wrap_getsockopt, 0); // optval saved result | ||
will_return(__wrap_getsockopt, 0); // actual return value | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout_tv, NULL, &error_code); | ||
|
||
// Ensure the function succeeds | ||
assert_int_equal(result, 0); | ||
assert_int_equal(error_code, 0); | ||
} | ||
|
||
static void test_php_network_connect_socket_eintr_t3(void **state) { | ||
struct timeval timeout_tv = { .tv_sec = 2, .tv_usec = 500000 }; | ||
struct timeval start_time = { .tv_sec = 1002, .tv_usec = 300000 }; // Initial time | ||
struct timeval retry_time = { .tv_sec = 1001, .tv_usec = 2200000 }; // Time after EINTR | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
// Mock connect to set EINPROGRESS | ||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, EINPROGRESS); | ||
|
||
// Mock gettimeofday for initial call | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &start_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to return EINTR first | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 2500); | ||
will_return(__wrap_poll, -EINTR); | ||
|
||
// Mock gettimeofday after EINTR | ||
expect_function_call(__wrap_gettimeofday); | ||
will_return(__wrap_gettimeofday, &retry_time); | ||
will_return(__wrap_gettimeofday, 0); | ||
|
||
// Mock poll to succeed on retry | ||
expect_function_call(__wrap_poll); | ||
expect_value(__wrap_poll, timeout, 1600); | ||
will_return(__wrap_poll, 1); | ||
|
||
// Mock no socket error | ||
expect_function_call(__wrap_getsockopt); | ||
will_return(__wrap_getsockopt, 0); // optval saved result | ||
will_return(__wrap_getsockopt, 0); // actual return value | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout_tv, NULL, &error_code); | ||
|
||
// Ensure the function succeeds | ||
assert_int_equal(result, 0); | ||
assert_int_equal(error_code, 0); | ||
} | ||
|
||
// Test connection error (ECONNREFUSED) | ||
static void test_php_network_connect_socket_connect_error(void **state) { | ||
struct timeval timeout = { .tv_sec = 2, .tv_usec = 500000 }; | ||
php_socket_t sockfd = 12; | ||
int error_code = 0; | ||
|
||
// Mock connect to set ECONNREFUSED | ||
expect_function_call(__wrap_connect); | ||
will_return(__wrap_connect, ECONNREFUSED); | ||
|
||
int result = php_network_connect_socket(sockfd, NULL, 0, 0, &timeout, NULL, &error_code); | ||
|
||
// Ensure the function returns an error | ||
assert_int_equal(result, -1); | ||
assert_int_equal(error_code, ECONNREFUSED); | ||
} | ||
|
||
|
||
int main(void) { | ||
const struct CMUnitTest tests[] = { | ||
cmocka_unit_test(test_php_network_connect_socket_immediate_success), | ||
cmocka_unit_test(test_php_network_connect_socket_progress_success), | ||
cmocka_unit_test(test_php_network_connect_socket_eintr_t1), | ||
cmocka_unit_test(test_php_network_connect_socket_eintr_t2), | ||
cmocka_unit_test(test_php_network_connect_socket_eintr_t3), | ||
cmocka_unit_test(test_php_network_connect_socket_connect_error), | ||
}; | ||
return cmocka_run_group_tests(tests, NULL, NULL); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.