Tests: Test setjmp/sigsetjmp LibC functions

Since there are no real users of these functions in Serenity's
userland and this is my third attempt at this... This time, the great
LibTest test suite will make sure that I do it right!
This commit is contained in:
Jean-Baptiste Boric 2021-08-25 17:13:38 +02:00 committed by Andreas Kling
parent 0248e6ae27
commit c97f7ea23b
Notes: sideshowbarker 2024-07-18 05:17:23 +09:00
2 changed files with 152 additions and 0 deletions

View File

@ -6,6 +6,7 @@ set(TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCExec.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCDirEnt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCInodeWatcher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCSetjmp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCString.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestStackSmash.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestIo.cpp

View File

@ -0,0 +1,151 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
TEST_CASE(setjmp)
{
jmp_buf env;
volatile int set = 1;
if (setjmp(env)) {
EXPECT_EQ(set, 0);
return;
}
EXPECT_EQ(set, 1);
set = 0;
longjmp(env, 1);
VERIFY_NOT_REACHED();
}
TEST_CASE(setjmp_zero)
{
jmp_buf env;
volatile int set = 1;
switch (setjmp(env)) {
case 0:
EXPECT_EQ(set, 1);
set = 0;
longjmp(env, 0);
VERIFY_NOT_REACHED();
case 1:
EXPECT_EQ(set, 0);
break;
default:
VERIFY_NOT_REACHED();
};
}
TEST_CASE(setjmp_value)
{
jmp_buf env;
volatile int set = 1;
switch (setjmp(env)) {
case 0:
EXPECT_EQ(set, 1);
set = 0;
longjmp(env, 0x789ABCDE);
VERIFY_NOT_REACHED();
case 0x789ABCDE:
EXPECT_EQ(set, 0);
break;
default:
VERIFY_NOT_REACHED();
};
}
TEST_CASE(sigsetjmp)
{
sigjmp_buf env;
volatile int set = 1;
if (sigsetjmp(env, 0)) {
EXPECT_EQ(set, 0);
return;
}
EXPECT_EQ(set, 1);
set = 0;
siglongjmp(env, 1);
VERIFY_NOT_REACHED();
}
TEST_CASE(sigsetjmp_zero)
{
sigjmp_buf env;
volatile int set = 1;
switch (sigsetjmp(env, 0)) {
case 0:
EXPECT_EQ(set, 1);
set = 0;
siglongjmp(env, 0);
VERIFY_NOT_REACHED();
case 1:
EXPECT_EQ(set, 0);
break;
default:
VERIFY_NOT_REACHED();
};
}
TEST_CASE(sigsetjmp_value)
{
sigjmp_buf env;
volatile int set = 1;
switch (sigsetjmp(env, 0)) {
case 0:
EXPECT_EQ(set, 1);
set = 0;
siglongjmp(env, 0x789ABCDE);
VERIFY_NOT_REACHED();
case 0x789ABCDE:
EXPECT_EQ(set, 0);
break;
default:
VERIFY_NOT_REACHED();
};
}
TEST_CASE(sigsetjmp_signal_mask)
{
sigjmp_buf env;
sigset_t alternative_sigset;
sigfillset(&alternative_sigset);
sigset_t initial_sigset;
sigprocmask(SIG_SETMASK, nullptr, &initial_sigset);
EXPECT_NE(memcmp(&alternative_sigset, &initial_sigset, sizeof(sigset_t)), 0);
if (sigsetjmp(env, 1)) {
sigprocmask(SIG_SETMASK, nullptr, &alternative_sigset);
EXPECT_EQ(memcmp(&alternative_sigset, &initial_sigset, sizeof(sigset_t)), 0);
return;
}
sigprocmask(SIG_SETMASK, &alternative_sigset, nullptr);
siglongjmp(env, 1);
VERIFY_NOT_REACHED();
}