1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <pwd.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20 #define TEST_ERROR_UID 99999999
21
22 /**
23 * @tc.name : getpwent_0100
24 * @tc.desc : Verify getpwent process success
25 * @tc.level : Level 0
26 */
getpwent_0100(void)27 void getpwent_0100(void)
28 {
29 struct passwd *user = getpwent();
30 EXPECT_PTRNE("getpwent_0100", user, NULL);
31 }
32
33 /**
34 * @tc.name : getpwnam_0100
35 * @tc.desc : Get account data from password file
36 * @tc.level : Level 0
37 */
getpwnam_0100(void)38 void getpwnam_0100(void)
39 {
40 struct passwd *ret = getpwnam("root");
41 EXPECT_PTRNE("getpwnam_0100", ret, NULL);
42 if (ret) {
43 EXPECT_STREQ("getpwnam_0100", ret->pw_name, "root");
44 EXPECT_EQ("getpwnam_0100", ret->pw_uid, CMPFLAG);
45 EXPECT_EQ("getpwnam_0100", ret->pw_gid, CMPFLAG);
46 }
47 }
48
49 /**
50 * @tc.name : getpwnam_0200
51 * @tc.desc : Retrieve data from password file for non-existing accounts
52 * @tc.level : Level 2
53 */
getpwnam_0200(void)54 void getpwnam_0200(void)
55 {
56 struct passwd *ret = getpwnam("abcdefg");
57 EXPECT_PTREQ("getpwnam_0200", ret, NULL);
58 }
59
60 /**
61 * @tc.name : getpwuid_0100
62 * @tc.desc : Get password file entry by uid
63 * @tc.level : Level 0
64 */
getpwuid_0100(void)65 void getpwuid_0100(void)
66 {
67 struct passwd *ret = getpwuid(getuid());
68 EXPECT_PTRNE("getpwuid_0100", ret, NULL);
69 if (ret) {
70 EXPECT_EQ("getpwuid_0100", ret->pw_uid, getuid());
71 }
72 }
73
74 /**
75 * @tc.name : getpwuid_0200
76 * @tc.desc : Provide illegal uid to get password file entry
77 * @tc.level : Level 2
78 */
getpwuid_0200(void)79 void getpwuid_0200(void)
80 {
81 struct passwd *ret = getpwuid(TEST_ERROR_UID);
82 EXPECT_PTREQ("getpwuid_0200", ret, NULL);
83 }
84
main(void)85 int main(void)
86 {
87 getpwent_0100();
88 getpwnam_0100();
89 getpwnam_0200();
90 getpwuid_0100();
91 getpwuid_0200();
92 return t_status;
93 }
94