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 <errno.h>
17 #include <fcntl.h>
18 #include <grp.h>
19 #include <string.h>
20 #include "test.h"
21
22 /**
23 * @tc.name : getgrgid_0100
24 * @tc.desc : Verify that the specified GID can be retrieved from the group file (parameter valid)
25 * @tc.level : Level 0
26 */
getgrgid_0100(void)27 void getgrgid_0100(void)
28 {
29 errno = 0;
30 const char *group_name = "root";
31 gid_t gid = 0;
32
33 struct group *grp = getgrgid(gid);
34 if (errno != 0) {
35 t_error("%s failed, errno is %d\n", __func__, errno);
36 }
37 if (!grp) {
38 t_error("%s getgrgid failed\n", __func__);
39 }
40 if (strcmp(group_name, grp->gr_name)) {
41 t_error("%s failed, grp->gr_name is %s\n", __func__, grp->gr_name);
42 }
43 if (grp->gr_gid != gid) {
44 t_error("%s failed, grp->gr_gid is %d\n", __func__, grp->gr_gid);
45 }
46 if (grp->gr_mem == NULL) {
47 t_error("%s failed, grp->gr_mem is NULL\n", __func__);
48 }
49 }
50
51 /**
52 * @tc.name : getgrgid_0200
53 * @tc.desc : Verify that data with the specified GID cannot be retrieved from the group file
54 * (gid argument is invalid)
55 * @tc.level : Level 2
56 */
getgrgid_0200(void)57 void getgrgid_0200(void)
58 {
59 struct group *data = getgrgid(-1);
60 if (data != NULL) {
61 t_error("%s data should be NULL", __func__);
62 }
63 }
64
main(int argc,char * argv[])65 int main(int argc, char *argv[])
66 {
67 getgrgid_0100();
68 getgrgid_0200();
69 return t_status;
70 }