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
32 #include "It_test_sys.h"
33 #include <grp.h>
34
35 #define GROUPFILE "/etc/group"
36
TestCase0(void)37 STATIC INT32 TestCase0(void)
38 {
39 struct group getNam1 = { nullptr };
40 struct group *getNam2 = nullptr;
41 struct group getData1 = { nullptr };
42 struct group *getData2 = nullptr;
43 struct group *groupRet = nullptr;
44 CHAR buf[1000]; /* 1000, buffer for test */
45 size_t len = sizeof(buf);
46
47 INT32 ret = getgrgid_r(0, &getNam1, buf, len, &getNam2);
48 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
49
50 ICUNIT_ASSERT_NOT_EQUAL(getNam2, nullptr, getNam2);
51 ICUNIT_ASSERT_STRING_EQUAL(getNam2->gr_name, "root", getNam2->gr_name);
52 ICUNIT_ASSERT_STRING_EQUAL(getNam2->gr_passwd, "x", getNam2->gr_passwd);
53 ICUNIT_ASSERT_EQUAL(getNam2->gr_gid, 0, getNam2->gr_gid);
54
55 groupRet = getgrgid(0);
56 ICUNIT_ASSERT_NOT_EQUAL(groupRet, nullptr, groupRet);
57 ICUNIT_ASSERT_STRING_EQUAL(groupRet->gr_name, "root", groupRet->gr_name);
58 ICUNIT_ASSERT_STRING_EQUAL(groupRet->gr_passwd, "x", groupRet->gr_passwd);
59 ICUNIT_ASSERT_EQUAL(groupRet->gr_gid, 0, groupRet->gr_gid);
60
61 groupRet = getgrnam("root");
62 ICUNIT_ASSERT_NOT_EQUAL(groupRet, nullptr, groupRet);
63 ICUNIT_ASSERT_STRING_EQUAL(groupRet->gr_name, "root", groupRet->gr_name);
64 ICUNIT_ASSERT_STRING_EQUAL(groupRet->gr_passwd, "x", groupRet->gr_passwd);
65 ICUNIT_ASSERT_EQUAL(groupRet->gr_gid, 0, groupRet->gr_gid);
66
67 ret = getgrnam_r("root", &getData1, buf, len, &getData2);
68 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
69 ICUNIT_ASSERT_NOT_EQUAL(getData2, nullptr, getData2);
70 ICUNIT_ASSERT_STRING_EQUAL(getData2->gr_name, "root", getData2->gr_name);
71 ICUNIT_ASSERT_STRING_EQUAL(getData2->gr_passwd, "x", getData2->gr_passwd);
72 ICUNIT_ASSERT_EQUAL(getData2->gr_gid, 0, getData2->gr_gid);
73 return 0;
74 }
75
TestCase1(void)76 STATIC INT32 TestCase1(void)
77 {
78 INT32 len = 1000;
79 CHAR buf[1000];
80 struct group getNam1 = { nullptr };
81 struct group *getNam2 = nullptr;
82 struct group getData1 = { nullptr };
83 struct group *getData2 = nullptr;
84 struct group *groupRet = nullptr;
85
86 INT32 ret = getgrgid_r(-1, &getNam1, buf, len, &getNam2);
87 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
88 ICUNIT_ASSERT_EQUAL(errno, EAFNOSUPPORT, errno);
89 errno = 0;
90
91 groupRet = getgrgid(-1);
92 ICUNIT_ASSERT_EQUAL(groupRet, 0, groupRet);
93 ICUNIT_ASSERT_EQUAL(errno, EAFNOSUPPORT, errno);
94 errno = 0;
95
96 groupRet = getgrnam("null");
97 ICUNIT_ASSERT_EQUAL(groupRet, nullptr, groupRet);
98 ICUNIT_ASSERT_EQUAL(errno, EAFNOSUPPORT, errno);
99 errno = 0;
100
101 ret = getgrnam_r("null", &getData1, buf, len, &getData2);
102 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
103 ICUNIT_ASSERT_EQUAL(errno, EAFNOSUPPORT, errno);
104 errno = 0;
105
106 return 0;
107 }
108
TestCase(void)109 STATIC INT32 TestCase(void)
110 {
111 CHAR *pathList[] = {"/etc/group"};
112 CHAR *streamList[] = {g_groupFileStream};
113 INT32 streamLen[] = {strlen(g_groupFileStream)};
114
115 INT32 ret = PrepareFileEnv(pathList, streamList, streamLen, 1);
116 if (ret != 0) {
117 printf("error: need some env file, but prepare is not ok");
118 (VOID)RecoveryFileEnv(pathList, 1);
119 return -1;
120 }
121
122 ret = TestCase0();
123 ICUNIT_GOTO_EQUAL(ret, 0, ret, ERROUT);
124
125 ret = TestCase1();
126 ICUNIT_GOTO_EQUAL(ret, 0, re, ERROUT);
127
128 (VOID)RecoveryFileEnv(pathList, 1);
129 return 0;
130 ERROUT:
131 (VOID)RecoveryFileEnv(pathList, 1);
132 return -1;
133 }
134
ItTestSys025(VOID)135 VOID ItTestSys025(VOID)
136 {
137 TEST_ADD_CASE("IT_TEST_SYS_025", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
138 }
139
140