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 <grp.h>
18#include <string.h>
19#include "test.h"
20
21/**
22 * @tc.name      : getgrnam_r_0100
23 * @tc.desc      : Get group file entry
24 * @tc.level     : Level 0
25 */
26void getgrnam_r_0100(void)
27{
28    errno = 0;
29    char buf[512];
30    gid_t gid = 0;
31
32    struct group *grp;
33    struct group grp_storage;
34    const char *group_name = "root";
35
36    int result = getgrnam_r(group_name, &grp_storage, buf, sizeof(buf), &grp);
37    if (result != 0) {
38        t_error("%s getgrnam_r failed\n", __func__);
39    }
40    if (errno != 0) {
41        t_error("%s errno should be zero\n", __func__);
42    }
43
44    if (!grp) {
45        t_error("%s failed, grp is NULL\n", __func__);
46    }
47    if (strcmp(group_name, grp->gr_name)) {
48        t_error("%s grp->gr_name is %s\n", __func__, grp->gr_name);
49    }
50    if (gid != grp->gr_gid) {
51        t_error("%s gr_gid is %d\n", __func__, grp->gr_gid);
52    }
53    if (!grp->gr_mem) {
54        t_error("%s grp->gr_mem is NULL\n", __func__);
55    }
56}
57
58/**
59 * @tc.name      : getgrnam_r_0200
60 * @tc.desc      : Invalid parameter test
61 * @tc.level     : Level 2
62 */
63void getgrnam_r_0200(void)
64{
65    char buf[512];
66    struct group *grp;
67    struct group grp_storage;
68
69    int result = getgrnam_r("invalid_name", &grp_storage, buf, sizeof(buf), &grp);
70    if (result) {
71        t_error("%s getgrnam_r should be failed\n", __func__);
72    }
73}
74
75int main(int argc, char *argv[])
76{
77    getgrnam_r_0100();
78    getgrnam_r_0200();
79    return t_status;
80}