diff --git a/testsuites/sample/posix/BUILD.gn b/testsuites/sample/posix/BUILD.gn index c1b3c7c7..b92fffc1 100644 --- a/testsuites/sample/posix/BUILD.gn +++ b/testsuites/sample/posix/BUILD.gn @@ -38,6 +38,17 @@ static_library("test_posix") { "mutex/It_posix_mutex_007.c", "mutex/It_posix_mutex_008.c", "mutex/It_posix_mutex_009.c", + "mutex/It_posix_mutex_010.c", + "mutex/It_posix_mutex_011.c", + "mutex/It_posix_mutex_012.c", + "mutex/It_posix_mutex_013.c", + "mutex/It_posix_mutex_014.c", + "mutex/It_posix_mutex_015.c", + "mutex/It_posix_mutex_016.c", + "mutex/It_posix_mutex_017.c", + "mutex/It_posix_mutex_018.c", + "mutex/It_posix_mutex_019.c", + "mutex/It_posix_mutex_020.c", "pthread/It_posix_pthread.c", "pthread/It_posix_pthread_001.c", "pthread/It_posix_pthread_002.c", diff --git a/testsuites/sample/posix/mutex/It_posix_mutex.c b/testsuites/sample/posix/mutex/It_posix_mutex.c index fe1ea09c..7d705eb0 100644 --- a/testsuites/sample/posix/mutex/It_posix_mutex.c +++ b/testsuites/sample/posix/mutex/It_posix_mutex.c @@ -88,4 +88,15 @@ VOID ItSuitePosixMutex(void) ItPosixMux007(); ItPosixMux008(); ItPosixMux009(); + ItPosixMux010(); + ItPosixMux011(); + ItPosixMux012(); + ItPosixMux013(); + ItPosixMux014(); + ItPosixMux015(); + ItPosixMux016(); + ItPosixMux017(); + ItPosixMux018(); + ItPosixMux019(); + ItPosixMux020(); } diff --git a/testsuites/sample/posix/mutex/It_posix_mutex.h b/testsuites/sample/posix/mutex/It_posix_mutex.h index 362ad623..e74ad7ea 100644 --- a/testsuites/sample/posix/mutex/It_posix_mutex.h +++ b/testsuites/sample/posix/mutex/It_posix_mutex.h @@ -69,6 +69,17 @@ VOID ItPosixMux006(void); VOID ItPosixMux007(void); VOID ItPosixMux008(void); VOID ItPosixMux009(void); +VOID ItPosixMux010(void); +VOID ItPosixMux011(void); +VOID ItPosixMux012(void); +VOID ItPosixMux013(void); +VOID ItPosixMux014(void); +VOID ItPosixMux015(void); +VOID ItPosixMux016(void); +VOID ItPosixMux017(void); +VOID ItPosixMux018(void); +VOID ItPosixMux019(void); +VOID ItPosixMux020(void); VOID ItSuitePosixMutex(void); diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_010.c b/testsuites/sample/posix/mutex/It_posix_mutex_010.c new file mode 100644 index 00000000..7c574693 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_010.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + pthread_mutexattr_t mta; + pthread_mutex_t mutex = TEST_MUTEX_INIT; + int rc; + + /* Initialize a mutex attributes object */ + rc = pthread_mutexattr_init(&mta); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Initialize a mutex object with the default mutex attributes */ + rc = pthread_mutex_init(&mutex, &mta); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1); + + rc = pthread_mutexattr_destroy(&mta); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT2); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT2); + + return LOS_OK; + +EXIT2: + pthread_mutex_destroy(&mutex); + +EXIT1: + pthread_mutexattr_destroy(&mta); + + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux010 + * @tc.desc: Test interface pthread_mutexattr_destroy + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux010(void) +{ + TEST_ADD_CASE("ItPosixMux010", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_011.c b/testsuites/sample/posix/mutex/It_posix_mutex_011.c new file mode 100644 index 00000000..2865ce9b --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_011.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + int ret; + pthread_mutexattr_t mta; + pthread_mutex_t mutex = TEST_MUTEX_INIT; + + ret = pthread_mutexattr_init(&mta); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_init(&mutex, &mta); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + (void)pthread_mutexattr_destroy(&mta); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux011 + * @tc.desc: Test interface pthread_mutexattr_init + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux011(void) +{ + TEST_ADD_CASE("ItPosixMux011", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_012.c b/testsuites/sample/posix/mutex/It_posix_mutex_012.c new file mode 100644 index 00000000..d0f2b771 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_012.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + pthread_mutex_t mutex = TEST_MUTEX_INIT; + int rc; + + /* Initialize a mutex attributes object */ + rc = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Destroy the mutex attributes object */ + rc = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Initialize the mutex attributes object again. This shouldn't result in an error. */ + rc = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux012 + * @tc.desc: Test interface pthread_mutex_init + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux012(void) +{ + TEST_ADD_CASE("ItPosixMux012", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_013.c b/testsuites/sample/posix/mutex/It_posix_mutex_013.c new file mode 100644 index 00000000..38329c93 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_013.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +struct _scenar_028 { + int m_type; /* Mutex type to use */ + int m_pshared; /* 0: mutex is process-private (default) ~ !0: mutex is process-shared, if supported */ + char *descr; /* Case description */ +}; + +struct _scenar_028 g_scenarii028[] = { + {PTHREAD_MUTEX_DEFAULT, 0, "Default mutex"}, + {PTHREAD_MUTEX_NORMAL, 0, "Normal mutex"}, + {PTHREAD_MUTEX_ERRORCHECK, 0, "Errorcheck mutex"}, + {PTHREAD_MUTEX_RECURSIVE, 0, "Recursive mutex"}, + {PTHREAD_MUTEX_DEFAULT, 1, "Pshared mutex"}, + {PTHREAD_MUTEX_NORMAL, 1, "Pshared Normal mutex"}, + {PTHREAD_MUTEX_ERRORCHECK, 1, "Pshared Errorcheck mutex"}, + {PTHREAD_MUTEX_RECURSIVE, 1, "Pshared Recursive mutex"} +}; + +#define NSCENAR (sizeof(g_scenarii028) / sizeof(g_scenarii028[0])) + +static UINT32 Testcase(VOID) +{ + int ret; + int i, j; + pthread_mutex_t mtx; + pthread_mutexattr_t ma[NSCENAR + 1]; + pthread_mutexattr_t *pma[NSCENAR + 2]; // 2, The loop frequency. + + for (i = 0; i < NSCENAR; i++) { + ret = pthread_mutexattr_init(&ma[i]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + } + + ret = pthread_mutexattr_init(&ma[i]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + for (i = 0; i < NSCENAR + 2; i++) { // 2, The loop frequency. + pma[i] = NULL; + } + + for (i = 0; i < NSCENAR + 2; i++) { // 2, The loop frequency. + for (j = 0; j < NSCENAR + 2; j++) { // 2, The loop frequency. + ret = pthread_mutex_init(&mtx, pma[i]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_destroy(&mtx); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_init(&mtx, pma[j]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_destroy(&mtx); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + } + } + for (i = 0; i < NSCENAR + 1; i++) { + ret = pthread_mutexattr_destroy(&ma[i]); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + } + + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux013 + * @tc.desc: Test interface pthread_mutexattr_init + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux013(void) +{ + TEST_ADD_CASE("ItPosixMux013", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_014.c b/testsuites/sample/posix/mutex/It_posix_mutex_014.c new file mode 100644 index 00000000..f4841f82 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_014.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + pthread_mutex_t mutex = TEST_MUTEX_INIT; + int rc; + + /* Initialize a mutex object */ + rc = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + return LOS_OK; + +EXIT: + pthread_mutex_destroy(&mutex); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux014 + * @tc.desc: Test interface pthread_mutex_destroy + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux014(void) +{ + TEST_ADD_CASE("ItPosixMux014", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_015.c b/testsuites/sample/posix/mutex/It_posix_mutex_015.c new file mode 100644 index 00000000..a89e3982 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_015.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +/* pthread_mutex_destroy 4-2.c + * Test that when a pthread_mutex_destroy is called on a + * locked mutex, it fails and returns EBUSY + + * Steps: + * 1. Create a mutex + * 2. Lock the mutex + * 3. Try to destroy the mutex + * 4. Check that this may fail with EBUSY + */ + +static UINT32 Testcase(VOID) +{ + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + int rc; + + /* Lock the mutex */ + rc = pthread_mutex_lock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Try to destroy the locked mutex */ + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, EBUSY, rc, EXIT); + + rc = pthread_mutex_unlock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + return LOS_OK; + +EXIT: + return LOS_NOK; +} + +/** + * @tc.name: ItPosixMux015 + * @tc.desc: Test interface pthread_mutex_lock + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux015(void) +{ + TEST_ADD_CASE("ItPosixMux015", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_016.c b/testsuites/sample/posix/mutex/It_posix_mutex_016.c new file mode 100644 index 00000000..eca15524 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_016.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +/* pthread_mutex_destroy 5-1.c + * Test that pthread_mutex_destroy() + * It shall be safe to destroy an initialized mutex that is unlocked. + */ + +static UINT32 Testcase(VOID) +{ + int rc; + pthread_mutex_t mutex = TEST_MUTEX_INIT; + + /* Initialize mutex with the default mutex attributes */ + rc = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Lock mutex */ + rc = pthread_mutex_lock(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + sleep(1); + /* Unlock */ + rc = pthread_mutex_unlock(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1); + + /* Destroy mutex after it is unlocked */ + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + return LOS_OK; + +EXIT1: + pthread_mutex_unlock(&mutex); + +EXIT: + pthread_mutex_destroy(&mutex); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux016 + * @tc.desc: Test interface pthread_mutex_unlock + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux016(void) +{ + TEST_ADD_CASE("ItPosixMux016", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_017.c b/testsuites/sample/posix/mutex/It_posix_mutex_017.c new file mode 100644 index 00000000..aa626262 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_017.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + int ret; + pthread_mutexattr_t mta; + pthread_mutex_t mutex = TEST_MUTEX_INIT; + + sem_t sem; + int pshared; + + ret = pthread_mutexattr_init(&mta); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_init(&mutex, &mta); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_init(&sem, pshared, 2); // 2, The initial number of available semaphores. + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_lock(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_unlock(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_lock(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_post(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_wait(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_unlock(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = sem_destroy(&sem); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + (void)pthread_mutexattr_destroy(&mta); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux017 + * @tc.desc: Test interface sem_wait + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux017(void) +{ + TEST_ADD_CASE("ItPosixMux017", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_018.c b/testsuites/sample/posix/mutex/It_posix_mutex_018.c new file mode 100644 index 00000000..05331844 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_018.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + pthread_mutex_t mutex; + int rc; + + /* Initialize a mutex object with the default mutex attributes */ + rc = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Lock the mutex using pthread_mutex_lock() */ + rc = pthread_mutex_lock(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1); + + rc = pthread_mutex_unlock(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT2); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT1); + + return LOS_OK; + +EXIT2: + pthread_mutex_unlock(&mutex); +EXIT1: + pthread_mutex_destroy(&mutex); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux018 + * @tc.desc: Test interface pthread_mutex_lock + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux018(void) +{ + TEST_ADD_CASE("ItPosixMux018", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_019.c b/testsuites/sample/posix/mutex/It_posix_mutex_019.c new file mode 100644 index 00000000..df02b971 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_019.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +static UINT32 Testcase(VOID) +{ + UINT32 ret; + pthread_mutex_t mutex = TEST_MUTEX_INIT; + + ret = pthread_mutex_init(NULL, NULL); + ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); + + ret = pthread_mutex_init(&mutex, NULL); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + ret = pthread_mutex_destroy(&mutex); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux019 + * @tc.desc: Test interface pthread_mutex_init + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux019(void) +{ + TEST_ADD_CASE("ItPosixMux019", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +} diff --git a/testsuites/sample/posix/mutex/It_posix_mutex_020.c b/testsuites/sample/posix/mutex/It_posix_mutex_020.c new file mode 100644 index 00000000..35994bb7 --- /dev/null +++ b/testsuites/sample/posix/mutex/It_posix_mutex_020.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 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 "It_posix_mutex.h" + +/* pthread_mutex_unlock 1-1.c + * Test that pthread_mutex_unlock() + * shall release the mutex object 'mutex'. + + * Steps: + * -- initialize a mutex object + * -- Get the mutex using pthread_mutex_lock() + * -- Release the mutex using pthread_mutex_unlock() + * -- Try to get the mutex using pthread_mutex_trylock() + * -- Release the mutex using pthread_mutex_unlock() + * + */ + +static UINT32 Testcase(VOID) +{ + int rc; + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + + /* Get the mutex using pthread_mutex_lock() */ + rc = pthread_mutex_lock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Release the mutex using pthread_mutex_unlock() */ + rc = pthread_mutex_unlock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Get the mutex using pthread_mutex_trylock() */ + rc = pthread_mutex_trylock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + /* Release the mutex using pthread_mutex_unlock() */ + rc = pthread_mutex_unlock(&mutex); + ICUNIT_ASSERT_EQUAL(rc, 0, rc); + + rc = pthread_mutex_destroy(&mutex); + ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); + + return LOS_OK; + +EXIT: + pthread_mutex_destroy(&mutex); + return LOS_OK; +} + +/** + * @tc.name: ItPosixMux020 + * @tc.desc: Test interface pthread_mutex_trylock + * @tc.type: FUNC + * @tc.require: issueI5WZI6 + */ + +VOID ItPosixMux020(void) +{ + TEST_ADD_CASE("ItPosixMux020", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION); +}