test:测试用力补齐

Signed-off-by: zwx1232718 <zhenghui23@huawei.com>
This commit is contained in:
zwx1232718 2023-06-13 16:41:52 +08:00
parent 16dae422eb
commit 4cfb20d7e9
14 changed files with 1588 additions and 1 deletions

View File

@ -48,7 +48,10 @@ kernel_module("test_init") {
"src/osTest.c",
]
include_dirs = [ "unittest/posix/src" ]
include_dirs = [
"unittest/posix/src",
"unittest/xts",
]
configs += [ ":include" ]
}
@ -69,6 +72,7 @@ group("testsuites") {
"sample/kernel/task:test_task",
"sample/posix:test_posix",
"unittest/posix:posix_test",
"unittest/xts:xts_test",
]
if (defined(LOSCFG_DYNLINK)) {
deps += [ "sample/kernel/dynlink:test_dynlink" ]

View File

@ -96,6 +96,7 @@ extern "C" {
#define LOS_KERNEL_LMK_TEST 0
#define LOS_KERNEL_SIGNAL_TEST 0
#define LOS_XTS_TEST 1
#define LOS_POSIX_TEST 1
#define LOS_CMSIS_TEST 1
#define LOS_CMSIS2_CORE_TASK_TEST 0

View File

@ -38,6 +38,9 @@
#if (LOS_POSIX_TEST == 1)
#include "posix_test.h"
#endif
#if (LOS_XTS_TEST == 1)
#include "xts_test.h"
#endif
UINT32 volatile g_testCount;
UINT32 g_testTskHandle;
@ -246,6 +249,10 @@ VOID TestTaskEntry(VOID)
CmsisFuncTestSuite();
#endif
#if(LOS_XTS_TEST == 1)
XtsTestSuite();
#endif
/* The log is used for testing entrance guard, please do not make any changes. */
PRINTF("\nfailed count:%d, success count:%d\n", g_failResult, g_passResult);
PRINTF("--- Test End ---\n");

View File

@ -0,0 +1,35 @@
# Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import("//kernel/liteos_m/liteos.gni")
static_library("xts_test") {
sources = [ "xts_test.c" ]
deps = [ "ipc:ipc_test" ]
configs += [ "$LITEOSTOPDIR/testsuites:include" ]
}

View File

@ -0,0 +1,46 @@
# Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import("//kernel/liteos_m/liteos.gni")
static_library("ipc_test") {
sources = [
"cond/pthread_cond_test.c",
"mutex/pthread_mutex_test.c",
"semaphone/sem_abn_test.c",
"semaphone/sem_test.c",
"semaphone/sem_xts_test.c",
"xts_ipc.c",
]
include_dirs = [
".",
"$LITEOSTOPDIR/testsuites/include",
"$LITEOSTOPDIR/testsuites/unittest/xts",
]
}

View File

@ -0,0 +1,192 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_ipc.h"
pthread_mutex_t g_mtx3 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t g_cond3 = PTHREAD_COND_INITIALIZER;
LITE_TEST_SUIT(FUTEX, PthreadCondApiTest, PthreadCondApiTestSuite);
static BOOL PthreadCondApiTestSuiteSetUp(void)
{
return TRUE;
}
static BOOL PthreadCondApiTestSuiteTearDown(void)
{
return TRUE;
}
/**
* @tc.number SUB_KERNEL_PTHREAD_COND_INIT_0100
* @tc.name pthread_cond_init initializes condition variables
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondInit, Function | MediumTest | Level2)
{
int ret;
pthread_condattr_t condAttr;
ret = pthread_condattr_init(&condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
pthread_cond_t cond1;
ret = pthread_cond_init(&cond1, &condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
pthread_cond_t cond2;
ret = pthread_cond_init(&cond2, NULL);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
}
/**
* @tc.number SUB_KERNEL_PTHREAD_COND_DESTROY_0100
* @tc.name pthread_cond_destroy destroy condition variables
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondDestroy, Function | MediumTest | Level3)
{
int ret;
pthread_condattr_t condAttr;
ret = pthread_condattr_init(&condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
pthread_cond_t cond1;
ret = pthread_cond_init(&cond1, &condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_cond_destroy(&cond1);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
pthread_cond_t cond2;
ret = pthread_cond_init(&cond2, NULL);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_cond_destroy(&cond2);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
ret =pthread_cond_destroy(&cond3);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
}
/**
* @tc.number SUB_KERNEL_PTHREAD_CONDATTR_INIT_0100
* @tc.name Init and destroy operations
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondattrInit, Function | MediumTest | Level2)
{
int ret;
pthread_condattr_t condAttr;
ret = pthread_condattr_init(&condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret =pthread_condattr_destroy(&condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_condattr_init(&condAttr);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
}
// pthread_cond_broadcast
void *ThreadPthreadCondBroadcast1(void *arg)
{
int ret;
int *testIntP = (int *)arg;
usleep(20); /* 20, common data for test, no special meaning */
ret = pthread_mutex_lock(&g_mtx3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
*testIntP = TEST_INTP_SIZE;
ret = pthread_cond_broadcast(&g_cond3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
ret = pthread_mutex_unlock(&g_mtx3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
return arg;
EXIT:
return NULL;
}
// pthread_cond_wait
void *ThreadPthreadCondBroadcast2(void *arg)
{
int ret;
int *testIntP = (int *)arg;
ret = pthread_mutex_lock(&g_mtx3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
ret = pthread_cond_wait(&g_cond3, &g_mtx3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
(*testIntP)++;
ret = pthread_mutex_unlock(&g_mtx3);
ICUNIT_GOTO_EQUAL(ret, POSIX_IPC_NO_ERROR, ret, EXIT);
return arg;
EXIT:
return NULL;
}
/**
* @tc.number SUB_KERNEL_PTHREAD_COND_BROADCAST_0100
* @tc.name Use pthread_cond_broadcast to release conditional semaphore
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(PthreadCondApiTestSuite, testPthreadCondBroadcast, Function | MediumTest | Level3)
{
int ret;
pthread_t tid[3]; /* 3, common data for test, no special meaning */
int testInt = 0;
ret = pthread_create(&tid[0], NULL, ThreadPthreadCondBroadcast1, (void*)&testInt);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_create(&tid[1], NULL, ThreadPthreadCondBroadcast2, (void*)&testInt); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_create(&tid[2], NULL, ThreadPthreadCondBroadcast2, (void*)&testInt); /* 2, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
usleep(100); /* 100, common data for test, no special meaning */
int index = (int)(sizeof(tid) / sizeof(tid[0]));
for (int i = 0; i < index; i++) {
ret = pthread_join(tid[i], NULL);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
}
EXIT:
ret = pthread_cond_destroy(&g_cond3);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_mutex_destroy(&g_mtx3);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ICUNIT_ASSERT_EQUAL(testInt, TEST_SEEK_SIZE, testInt);
}
RUN_TEST_SUITE(PthreadCondApiTestSuite);
void PosixFutexCondTest(void)
{
RUN_ONE_TESTCASE(testPthreadCondInit);
RUN_ONE_TESTCASE(testPthreadCondDestroy);
RUN_ONE_TESTCASE(testPthreadCondattrInit);
RUN_ONE_TESTCASE(testPthreadCondBroadcast);
}

View File

@ -0,0 +1,138 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_ipc.h"
LITE_TEST_SUIT(FUTEX, PthreadMutexApiTest, PthreadMutexApiTestSuite);
static BOOL PthreadMutexApiTestSuiteSetUp(void)
{
return TRUE;
}
static BOOL PthreadMutexApiTestSuiteTearDown(void)
{
return TRUE;
}
/********************************************* Test case dividing line ***********************************************/
struct PthreadMutexCond {
int loopNum;
int countMax;
int count;
int top;
int bottom;
pthread_cond_t notfull;
pthread_cond_t notempty;
pthread_mutex_t mutex;
};
struct PthreadMutexCond g_st1;
void *PthreadProduce(void *arg)
{
int ret;
for (int i = 0; i < g_st1.loopNum; i++) {
ret = pthread_mutex_lock(&g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
// check full
if ((g_st1.top + 1) % g_st1.countMax == g_st1.bottom) { /* 1, common data for test, no special meaning */
ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
}
// Produce
g_st1.top = (g_st1.top + 1) % g_st1.countMax; /* 1, common data for test, no special meaning */
g_st1.count++;
ret = pthread_cond_signal(&g_st1.notempty);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_mutex_unlock(&g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
usleep(10); /* 10, common data for test, no special meaning */
}
return arg;
}
void *PthreadConsume(void *arg)
{
int ret;
for (int i = 0; i < g_st1.loopNum; i++) {
ret = pthread_mutex_lock(&g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
// check empty
if (g_st1.top == g_st1.bottom) {
ret = pthread_cond_wait(&g_st1.notempty, &g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
}
// Consume
g_st1.bottom = (g_st1.bottom + 1) % g_st1.countMax;
g_st1.count--;
ret = pthread_cond_signal(&g_st1.notempty);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_mutex_unlock(&g_st1.mutex);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
usleep(10); /* 10, common data for test, no special meaning */
}
return arg;
}
/**
* @tc.number SUB_KERNEL_FUTEX_MUTEX_ALL_0300
* @tc.name test pthread_mutex with condition variable, produce and consume
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(PthreadMutexApiTestSuite, testPthreadMutexCond, Function | MediumTest | Level3)
{
int ret;
pthread_t tid[2]; /* 2, common data for test, no special meaning */
g_st1.count = 0;
memset_s(tid, sizeof(tid), POSIX_IPC_NO_ERROR, sizeof(tid));
ret = pthread_create(&tid[0], NULL, PthreadProduce, NULL);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_create(&tid[1], NULL, PthreadConsume, NULL); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_join(tid[0], NULL);
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ret = pthread_join(tid[1], NULL); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, POSIX_IPC_NO_ERROR, ret);
ICUNIT_ASSERT_EQUAL(g_st1.count, POSIX_IPC_NO_ERROR, g_st1.count);
}
RUN_TEST_SUITE(PthreadMutexApiTestSuite);
void PosixFutexMutexTest(void)
{
RUN_ONE_TESTCASE(testPthreadMutexCond);
}

View File

@ -0,0 +1,190 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_ipc.h"
LITE_TEST_SUIT(IPC, SemAbnormalTestSuite, SemAbnormalTestSuite);
static BOOL SemAbnormalTestSuiteSetUp(void)
{
return TRUE;
}
static BOOL SemAbnormalTestSuiteTearDown(void)
{
return TRUE;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0200
* @tc.name Use sem_init initialized value when value is SEM_VALUE_MAX
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalSemvaluemax, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
int semValue = 0;
ret = sem_init(&sem, 0, SEM_VALUE_MAX);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(SEM_VALUE_MAX, semValue, SEM_VALUE_MAX);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0210
* @tc.name Use sem_init initialized value when value is greater than SEM_VALUE_MAX
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalGtsemvaluemax, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
unsigned int gtSemMax = (unsigned int)SEM_VALUE_MAX + 1; /* 1, common data for test, no special meaning */
ret = sem_init(&sem, 0, gtSemMax);
ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
EXIT:
return;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0220
* @tc.name Use sem_init initialized value twice
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemInitAbnormalInitTwice, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
ret = sem_init(&sem, 0, 1); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_POST_0200
* @tc.name sem_post increases the semaphore count near the maximum value
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemPostAbnormal, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
ret = sem_init(&sem, 0, SEM_VALUE_MAX);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_post(&sem);
ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
ICUNIT_GOTO_EQUAL(errno, EOVERFLOW, errno, EXIT); /* -1, common data for test, no special meaning */
EXIT:
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_0200
* @tc.name sem_timedwait get semaphore, wait time abnormal, tv_nsec less than 0
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemTimedwaitAbnormalA, Function | MediumTest | Level3)
{
int ret;
struct timespec ts = {0};
sem_t sem;
int semValue = 0;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ts.tv_sec = time(NULL);
ts.tv_nsec = -2; /* -2, common data for test, no special meaning */
ret = sem_timedwait(&sem, &ts);
ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */
ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT);
EXIT:
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_TIMEDWAIT_0300
* @tc.name sem_timedwait get semaphore, wait time abnormal
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(SemAbnormalTestSuite, testSemTimedwaitAbnormalB, Function | MediumTest | Level3)
{
int ret;
struct timespec ts = {0};
sem_t sem;
int semValue = 0;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ts.tv_sec = time(NULL);
ts.tv_nsec = KERNEL_NS_PER_SECOND;
ret = sem_timedwait(&sem, &ts);
ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT); /* -1, common data for test, no special meaning */
ICUNIT_GOTO_EQUAL(errno, EINVAL, errno, EXIT);
EXIT:
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
RUN_TEST_SUITE(SemAbnormalTestSuite);
void PosixSemAbnFuncTest()
{
RUN_ONE_TESTCASE(testSemInitAbnormalSemvaluemax);
RUN_ONE_TESTCASE(testSemInitAbnormalInitTwice);
RUN_ONE_TESTCASE(testSemTimedwaitAbnormalA);
RUN_ONE_TESTCASE(testSemTimedwaitAbnormalB);
}

View File

@ -0,0 +1,347 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_ipc.h"
static int g_semTestStep = 0;
LITE_TEST_SUIT(IPC, SemApi, IpcSemApiTestSuite);
static BOOL IpcSemApiTestSuiteSetUp(void)
{
return TRUE;
}
static BOOL IpcSemApiTestSuiteTearDown(void)
{
return TRUE;
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemInit0100, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int testValue[3] = {0, 1, 10}; /* 3, 1, 10 common data for test, no special meaning */
int index = (int)(sizeof(testValue) / sizeof(int));
for (int i = 0; i < index; i++) {
ret = sem_init((sem_t *)&sem, 0, testValue[0]);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemPost0100, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
ret = sem_init((sem_t *)&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemWait0100, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
ret = sem_init((sem_t *)&sem, 0, 3); /* 3, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
void *ThreadChat(void *arg)
{
int ret;
sem_t *sem = (sem_t *)arg;
ICUNIT_ASSERT_EQUAL(g_semTestStep, 0, g_semTestStep);
g_semTestStep = 1; /* 1, common data for test, no special meaning */
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_semTestStep = 2; /* 2, common data for test, no special meaning */
return NULL;
}
LITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0100, Function | MediumTest | Level3)
{
pthread_t tid;
sem_t sem;
int ret;
struct timespec req;
g_semTestStep = 0;
ret = sem_init((sem_t *)&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid, NULL, ThreadChat, (void *)&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
req.tv_sec = 0;
req.tv_nsec = TEN_CONT * NANO_MS;
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
ret = pthread_join(tid, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
void *ThreadNThreadWait1(void *arg)
{
int ret;
sem_t *sem = (sem_t *)arg;
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = HUNDRED_CONT * NANO_MS;
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_semTestStep = 1; /* 1, common data for test, no special meaning */
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_semTestStep = 2; /* 2, common data for test, no special meaning */
return NULL;
}
void *ThreadNThreadWait2(void *arg)
{
int ret;
sem_t *sem = (sem_t *)arg;
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 300 * NANO_MS; /* 300, common data for test, no special meaning */
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_semTestStep = 3; /* 3, common data for test, no special meaning */
req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
g_semTestStep = 4; /* 4, common data for test, no special meaning */
return NULL;
}
LITE_TEST_CASE(IpcSemApiTestSuite, testThreadChat0400, Function | MediumTest | Level4)
{
pthread_t tid1;
pthread_t tid2;
sem_t sem;
int ret;
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
g_semTestStep = 0;
ret = sem_init((sem_t *)&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid1, NULL, ThreadNThreadWait1, (void *)&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_create(&tid2, NULL, ThreadNThreadWait2, (void *)&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 1, g_semTestStep); /* 1, common data for test, no special meaning */
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 2, g_semTestStep); /* 2, common data for test, no special meaning */
req.tv_nsec = 200 * NANO_MS; /* 200, common data for test, no special meaning */
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */
ret = sem_post(&sem);
req.tv_nsec = 20 * NANO_MS; /* 20, common data for test, no special meaning */
ret = nanosleep(&req, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 3, g_semTestStep); /* 3, common data for test, no special meaning */
ret = pthread_join(tid1, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(tid2, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(g_semTestStep, 4, g_semTestStep); /* 4, common data for test, no special meaning */
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemInitAbnormal0200, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
unsigned int gtSemMax = (unsigned int)SEM_VALUE_MAX + 1; /* 1, common data for test, no special meaning */
ret = sem_init(&sem, 0, SEM_VALUE_MAX);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_init(&sem, 0, gtSemMax);
ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
ret = sem_init(&sem, 0, 1); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemPostAbnormal, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
ret = sem_init(&sem, 0, SEM_VALUE_MAX);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalA, Function | MediumTest | Level3)
{
int ret;
struct timespec ts;
sem_t sem;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ts.tv_sec = 0;
ts.tv_nsec = -2; /* -2, common data for test, no special meaning */
ret = sem_timedwait(&sem, &ts);
ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
LITE_TEST_CASE(IpcSemApiTestSuite, testSemTimedWaitAbnormalB, Function | MediumTest | Level3)
{
int ret;
struct timespec ts;
sem_t sem;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ts.tv_sec = time(NULL);
ts.tv_nsec = NANO_S;
ret = sem_timedwait(&sem, &ts);
ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
RUN_TEST_SUITE(IpcSemApiTestSuite);
void PosixSemFuncTest()
{
RUN_ONE_TESTCASE(testSemInit0100);
RUN_ONE_TESTCASE(testSemPost0100);
RUN_ONE_TESTCASE(testSemWait0100);
RUN_ONE_TESTCASE(testThreadChat0100);
RUN_ONE_TESTCASE(testThreadChat0400);
RUN_ONE_TESTCASE(testSemInitAbnormal0200);
RUN_ONE_TESTCASE(testSemPostAbnormal);
RUN_ONE_TESTCASE(testSemTimedWaitAbnormalA);
RUN_ONE_TESTCASE(testSemTimedWaitAbnormalB);
}

View File

@ -0,0 +1,427 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_ipc.h"
LITE_TEST_SUIT(IPC, SemApi, IpcSemApiXtsTestSuite);
static BOOL IpcSemApiXtsTestSuiteSetUp(void)
{
return TRUE;
}
static BOOL IpcSemApiXtsTestSuiteTearDown(void)
{
return TRUE;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0100
* @tc.name Use sem_init initialize the semaphore with 0
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitA, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int semValue = 0;
int testValue = 0;
ret = sem_init(&sem, 0, testValue);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0110
* @tc.name Use sem_init initialize the semaphore with 1
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitB, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int semValue = 0;
int testValue = 1; /* 1, common data for test, no special meaning */
ret = sem_init(&sem, 0, testValue);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_INIT_0120
* @tc.name Use sem_init initialize the semaphore with 100
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemInitC, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int semValue = 0;
int testValue = 10; /* 10, common data for test, no special meaning */
ret = sem_init(&sem, 0, testValue);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, testValue, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_POST_0100
* @tc.name sem_post increases the semaphore count
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemPost, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int semValue = 0;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 3, semValue); /* 3, common data for test, no special meaning */
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_WAIT_0100
* @tc.name sem_wait get semaphore
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemWait, Function | MediumTest | Level2)
{
int ret;
sem_t sem;
int semValue = 0;
ret = sem_init(&sem, 0, 3); /* 3, common data for test, no special meaning */
ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 3, semValue); /* 3, common data for test, no special meaning */
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 2, semValue); /* 2, common data for test, no special meaning */
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_wait(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_DESTROY_0100
* @tc.name check sem_destroy function
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testSemdestroy, Function | MediumTest | Level3)
{
int ret;
sem_t sem;
ret = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
void *ThreadChatF01(void *arg)
{
int ret;
sem_t *sem = (sem_t*)arg;
int semValue = 0;
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return NULL;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_CHAT_0100
* @tc.name Inter-thread communication, check sem_wait function
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testThreadChat, Function | MediumTest | Level3)
{
pthread_t tid;
sem_t sem;
int reInt;
int semValue = 0;
reInt = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = pthread_create(&tid, NULL, ThreadChatF01, (void*)&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(20); /* 20, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
reInt = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(20); /* 20, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
reInt = pthread_join(tid, NULL);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
}
void *ThreadSemTimedWait(void *arg)
{
int ret;
struct timespec ts = {0};
sem_t *sem = (sem_t*)arg;
int semValue = 0;
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec = ts.tv_sec + (ts.tv_nsec + KERNEL_100MS_BY_NS) / KERNEL_NS_PER_SECOND;
ts.tv_nsec = (ts.tv_nsec + KERNEL_100MS_BY_NS) % KERNEL_NS_PER_SECOND;
usleep(50); /* 50, common data for test, no special meaning */
ret = sem_timedwait(sem, &ts);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
usleep(100); /* 100, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
return NULL;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_CHAT_0300
* @tc.name Inter-thread communication, check sem_timedwait function
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testThreadSemTimedWait, Function | MediumTest | Level3)
{
int ret;
pthread_t tid;
sem_t sem;
int reInt;
int semValue = 0;
reInt = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = pthread_create(&tid, NULL, ThreadSemTimedWait, (void*)&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(10); /* 10, common data for test, no special meaning */
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
reInt = pthread_join(tid, NULL);
ret = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ret = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
}
void *ThreadNThreadWaitF01(void *arg)
{
int ret;
sem_t *sem = (sem_t*)arg;
int semValue = 0;
usleep(100); /* 100, common data for test, no special meaning */
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return NULL;
}
void *ThreadNThreadWaitF02(void *arg)
{
int ret;
sem_t *sem = (sem_t*)arg;
int semValue = 0;
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
usleep(300); /* 300, common data for test, no special meaning */
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
usleep(200); /* 200, common data for test, no special meaning */
ret = sem_getvalue(sem, &semValue);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
ret = sem_wait(sem);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return NULL;
}
/**
* @tc.number SUB_KERNEL_IPC_SEM_CHAT_0400
* @tc.name N threads wait, main thread post
* @tc.desc [C- SOFTWARE -0200]
*/
LITE_TEST_CASE(IpcSemApiXtsTestSuite, testNThreadWait, Function | MediumTest | Level4)
{
pthread_t tid1;
pthread_t tid2;
sem_t sem;
int reInt;
int semValue = 0;
reInt = sem_init(&sem, 0, 0);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = pthread_create(&tid1, NULL, ThreadNThreadWaitF01, (void*)&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = pthread_create(&tid2, NULL, ThreadNThreadWaitF02, (void*)&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(200); /* 200, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
reInt = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(20); /* 20, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
usleep(200); /* 200, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 0, semValue);
reInt = sem_post(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
usleep(20); /* 20, common data for test, no special meaning */
reInt = sem_getvalue(&sem, &semValue);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
ICUNIT_ASSERT_EQUAL(semValue, 1, semValue); /* 1, common data for test, no special meaning */
reInt = pthread_join(tid1, NULL);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = pthread_join(tid2, NULL);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
reInt = sem_destroy(&sem);
ICUNIT_ASSERT_EQUAL(reInt, 0, reInt);
}
RUN_TEST_SUITE(IpcSemApiXtsTestSuite);
void PosixSemXtsFuncTest()
{
RUN_ONE_TESTCASE(testSemInitA);
RUN_ONE_TESTCASE(testSemInitB);
RUN_ONE_TESTCASE(testSemInitC);
RUN_ONE_TESTCASE(testSemPost);
RUN_ONE_TESTCASE(testSemWait);
RUN_ONE_TESTCASE(testSemdestroy);
RUN_ONE_TESTCASE(testThreadChat);
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_test.h"
void IpcSemApiTest(void)
{
PosixFutexCondTest();
PosixFutexMutexTest();
PosixSemFuncTest();
PosixSemAbnFuncTest();
PosixSemXtsFuncTest();
return;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef XTS_IPC_H
#define XTS_IPC_H
#include "xts_test.h"
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <securec.h>
#include <semaphore.h>
#include <sys/resource.h>
#include <errno.h>
#define NANO_S 1000000000
#define NANO_MS 1000000
#define TEN_CONT 10
#define FIFTY_CONT 50
#define HUNDRED_CONT 100
#define TEST_INTP_SIZE 10
#define TEST_SEEK_SIZE 12
#define POSIX_IPC_NO_ERROR 0
#define KERNEL_NS_PER_SECOND 1000000000
#define SEM_VALUE_MAX 0xFFFE
#define KERNEL_NS_PER_SECOND 1000000000
#define KERNEL_100MS_BY_NS 100000000
#endif

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "xts_test.h"
void XtsTestSuite(void)
{
IpcSemApiTest();
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _XTS_TEST_H
#define _XTS_TEST_H
#include <stdlib.h>
#include "iCunit.h"
#define TEST_STR(func) ItLos##func
#define TEST_TO_STR(x) #x
#define TEST_HEAD_TO_STR(x) TEST_TO_STR(x)
#define ADD_TEST_CASE(func) \
TEST_ADD_CASE(TEST_HEAD_TO_STR(TEST_STR(func)), func, TEST_LOS, TEST_TASK, TEST_LEVEL0, TEST_FUNCTION)
#define LITE_TEST_SUIT(subsystem, module, testsuit)
#define LITE_TEST_CASE(module, function, flag) static int function(void)
#define RUN_TEST_SUITE(testsuit)
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) \
ICUNIT_ASSERT_EQUAL(((expected) == (actual)) || (isnan(expected) && isnan(actual)), TRUE, 0)
#define RUN_ONE_TESTCASE(caseName) ADD_TEST_CASE(caseName)
#define AUTO_RUN_ONE_TESTCASEFUNC(func) UnityDefaultTestRun(func, __FILE__, __LINE__)
void XtsTestSuite(void);
extern void IpcSemApiTest(void);
#endif