Skip to content
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

libc/poll: add select() errno test #380

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ $(eval $(call add_test_libc,math, -lm, -ffloat-store)) # -ffloat-store - prevent
# -Wno-attribute-warning - ignore warning when using sendmsg/recvmsg, we know it's not fully supported
$(eval $(call add_test_libc_custom,socket,inet-socket,, -Wno-attribute-warning, inet-socket.c common.c))
$(eval $(call add_test_libc_custom,socket,unix-socket,, -Wno-attribute-warning, unix-socket.c common.c))
$(eval $(call add_test_libc,poll))
126 changes: 126 additions & 0 deletions libc/poll/poll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Phoenix-RTOS
*
* test-libc-poll
*
* poll tests
*
* Copyright 2024 Phoenix Systems
* Author: Adam Greloch
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <time.h>

#include "unity_fixture.h"


#define MS_BETWEEN(ts0, ts1) \
((ts1).tv_sec - (ts0).tv_sec) * 1000 + ((ts1).tv_nsec - (ts0).tv_nsec) / 1000000;


TEST_GROUP(test_poll);


TEST_SETUP(test_poll)
{
}


TEST_TEAR_DOWN(test_poll)
{
}


static int get_bad_fd(int min_num, int max_num)
{
int rv, fd;
for (fd = min_num; fd <= max_num; fd++) {
rv = fcntl(fd, F_GETFD);
if (rv < 0) {
return fd;
}
}
return -1;
}


TEST(test_poll, select_errnos)
{
fd_set rfds;
struct timeval tv;
struct timespec ts[2];
int rv, ms, bad_fd;

tv.tv_sec = 0;
tv.tv_usec = 300 * 1000;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
clock_gettime(CLOCK_REALTIME, &ts[0]);
rv = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
clock_gettime(CLOCK_REALTIME, &ts[1]);
ms = MS_BETWEEN(ts[0], ts[1]);
TEST_ASSERT(rv == 0);
TEST_ASSERT_EQUAL_INT(0, FD_ISSET(STDIN_FILENO, &rfds));
TEST_ASSERT_LESS_THAN(350, ms);
TEST_ASSERT_GREATER_THAN(290, ms);

tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
errno = 0;
rv = select(-1, &rfds, NULL, NULL, &tv);
TEST_ASSERT_EQUAL_INT(-1, rv);
TEST_ASSERT_EQUAL_INT(EINVAL, errno);

tv.tv_sec = 0;
tv.tv_usec = -1;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
errno = 0;
rv = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
TEST_ASSERT_EQUAL_INT(-1, rv);
TEST_ASSERT_EQUAL_INT(EINVAL, errno);

tv.tv_sec = 0;
tv.tv_usec = 1000;
bad_fd = get_bad_fd(0, 1024);
if (bad_fd < 0) {
FAIL("get_bad_fd");
}
FD_ZERO(&rfds);
FD_SET(bad_fd, &rfds);
errno = 0;
rv = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
TEST_ASSERT_EQUAL_INT(-1, rv);
TEST_ASSERT_EQUAL_INT(EBADF, errno);
}


TEST_GROUP_RUNNER(test_poll)
{
RUN_TEST_CASE(test_poll, select_errnos);
}


static void runner(void)
{
RUN_TEST_GROUP(test_poll);
}


int main(int argc, char *argv[])
{
int failures = UnityMain(argc, (const char **)argv, runner);

return (failures == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Loading