• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_process.h"
32 
33 static const int TEST_COUNT = 10;
34 
ThreadFunc2(void * arg)35 static void *ThreadFunc2(void *arg)
36 {
37     exit(254); // 254, exit args
38 }
39 
ThreadFunc3(void * arg)40 static void *ThreadFunc3(void *arg)
41 {
42     while (1) {
43     }
44 }
45 
ProcessTest001(void)46 static int ProcessTest001(void)
47 {
48     int ret;
49     int status;
50     int pid;
51     int policy = 0;
52     int data;
53     int pri;
54     pthread_t newPthread, newPthread1;
55     int count = 4;
56     pthread_attr_t a = { 0 };
57     struct sched_param param = { 0 };
58     int currProcessPri = getpriority(PRIO_PROCESS, getpid());
59     ICUNIT_ASSERT_WITHIN_EQUAL(currProcessPri, 0, 31, currProcessPri); // 31, assert that function Result is equal to this.
60 
61     ret = setpriority(PRIO_PROCESS, getpid(), currProcessPri - 2); // 2, Used to calculate priorities.
62     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
63 
64     ret = pthread_getschedparam(pthread_self(), &policy, &param);
65     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
66 
67     ret = pthread_attr_init(&a);
68     param.sched_priority += 1;
69     pthread_attr_setschedparam(&a, &param);
70     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
71     ret = pthread_create(&newPthread, &a, ThreadFunc3, &data);
72     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
73 
74     data = ret;
75     ret = pthread_create(&newPthread, NULL, ThreadFunc2, &data);
76     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
77 
78     exit(255); // 255, exit args
79     return 0;
80 }
81 
Testcase(void)82 static int Testcase(void)
83 {
84     int ret;
85     int status;
86     int pid;
87     int count = TEST_COUNT;
88     int temp = GetCpuCount();
89     if (temp <= 1) {
90         return 0;
91     }
92 
93     while (count > 0) {
94         ret = fork();
95         if (ret == 0) {
96             ret = ProcessTest001();
97             exit(10); // 10, exit args
98         } else if (ret > 0) {
99             pid = ret;
100             ret = wait(&status);
101             status = WEXITSTATUS(status);
102             ICUNIT_ASSERT_EQUAL(ret, pid, ret);
103             ICUNIT_ASSERT_TWO_EQUAL(status, 255, 254, status); // 255, 254, assert that function Result is equal to this.
104         }
105 
106         ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, 100000, ret); // 100000, assert that function Result is equal to this.
107         count--;
108     }
109 
110     return 0;
111 }
112 
ItTestProcess008(void)113 void ItTestProcess008(void)
114 {
115     TEST_ADD_CASE("IT_POSIX_PROCESS_008", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
116 }
117