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      : getgrnam_0100
24 * @tc.desc      : Verify that the specified group can be retrieved from the group file (parameter valid)
25 * @tc.level     : Level 0
26 */
27void getgrnam_0100(void)
28{
29    errno = 0;
30    const char *group_name = "root";
31    gid_t gid = 0;
32
33    struct group *grp = getgrnam(group_name);
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      : getgrnam_0200
53 * @tc.desc      : Verify that the specified group cannot be retrieved from the group file
54 *                 (the name parameter is invalid)
55 * @tc.level     : Level 2
56 */
57void getgrnam_0200(void)
58{
59    struct group *result = getgrnam("invalid_name");
60    if (result) {
61        t_error("%s getgrnam should be failed\n", __func__);
62    }
63}
64
65int main(int argc, char *argv[])
66{
67    getgrnam_0100();
68    getgrnam_0200();
69    return t_status;
70}