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 #include <spawn.h>
33
34 static const int FILE_NAME_BYTES = 200; // 200, set test name len.
35 static const int LONG_FILE_NAME_BYTES = 500; // 500, set test name len.
36 static const int RANDOM_MAX = 127; // 127, set random max.
37 static const unsigned int INVALID_USER_VADDR = 0x1200000;
38
GetRandomNumber(int max)39 static int GetRandomNumber(int max)
40 {
41 int n = 0;
42
43 if (max > 0) {
44 n = rand() % max;
45 }
46
47 return n + 1;
48 }
49
GetRandomData(char ** buf,int bufSize)50 static void GetRandomData(char **buf, int bufSize)
51 {
52 char *p = *buf;
53 int i;
54
55 srand(static_cast<unsigned>(time(0)));
56 for (i = 0; i < bufSize - 1; ++i) {
57 int r = GetRandomNumber(RANDOM_MAX);
58 *(p + i) = static_cast<char>(r);
59 }
60 *(p + i) = static_cast<char>(0);
61 }
62
TestCase(VOID)63 static int TestCase(VOID)
64 {
65 int ret;
66 int err;
67 pid_t pid;
68 char *fileName = NULL;
69 char *childFileName = NULL;
70 char **childArgv = NULL;
71 char **childEnvp = NULL;
72
73 childArgv = reinterpret_cast<char **>(1);
74 ret = posix_spawnp(&pid, "/usr/bin/testsuits_app", NULL, NULL, childArgv, NULL);
75 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
76
77 childEnvp = reinterpret_cast<char **>(1);
78 ret = posix_spawnp(&pid, "/usr/bin/testsuits_app", NULL, NULL, NULL, childEnvp);
79 ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
80
81 ret = posix_spawnp(&pid, "/bin", NULL, NULL, NULL, NULL);
82 ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret);
83
84 fileName = static_cast<char *>(malloc(FILE_NAME_BYTES));
85 ICUNIT_ASSERT_NOT_EQUAL(fileName, NULL, fileName);
86 GetRandomData(&fileName, FILE_NAME_BYTES);
87 ret = posix_spawnp(&pid, fileName, NULL, NULL, NULL, NULL);
88 free(fileName);
89 ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret);
90
91 fileName = static_cast<char *>(malloc(LONG_FILE_NAME_BYTES));
92 ICUNIT_ASSERT_NOT_EQUAL(fileName, NULL, fileName);
93 GetRandomData(&fileName, LONG_FILE_NAME_BYTES);
94 ret = posix_spawnp(&pid, fileName, NULL, NULL, NULL, NULL);
95 free(fileName);
96 ICUNIT_ASSERT_EQUAL(ret, ENAMETOOLONG, ret);
97
98 ret = posix_spawnp(&pid, "test_spawnp", NULL, NULL, NULL, NULL);
99 ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret);
100
101 return 0;
102 }
ItTestProcess064(void)103 void ItTestProcess064(void)
104 {
105 TEST_ADD_CASE("IT_POSIX_PROCESS_064", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
106 }