• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <shadow.h>
18 #include <string.h>
19 #include "functionalext.h"
20 #include "test.h"
21 
22 /**
23  * @tc.name      : getspnam_r_0100
24  * @tc.desc      : Get user database files. Test precondtions: /etc/shadow.
25  * @tc.level     : Level 0
26  */
getspnam_r_0100(void)27 void getspnam_r_0100(void)
28 {
29     errno = 0;
30     char buf[512];
31 
32     struct spwd *spwd;
33     struct spwd spwd_storage;
34     const char *spwd_name = "root";
35 
36     int result = getspnam_r(spwd_name, &spwd_storage, buf, sizeof(buf), &spwd);
37     EXPECT_EQ("getspnam_r_0100", result, 0);
38     EXPECT_PTRNE("getspnam_r_0100", spwd, NULL);
39     EXPECT_EQ("getspnam_r_0100", strcmp(spwd_name, spwd->sp_namp), 0);
40 }
41 
42 /**
43  * @tc.name      : getspnam_r_0200
44  * @tc.desc      : Buffer size not be able to hold name.(normal: buffer_size > strlen(name)+100)
45  * @tc.level     : Level 2
46  */
getspnam_r_0200(void)47 void getspnam_r_0200(void)
48 {
49     char buf[10];
50     struct spwd *spwd;
51     struct spwd spwd_storage;
52 
53     int result = getspnam_r("bin", &spwd_storage, buf, sizeof(buf), &spwd);
54     EXPECT_NE("getspnam_r_0300", result, 0);
55     EXPECT_PTREQ("getspnam_r_0300", spwd, NULL);
56 }
57 
58 /**
59  * @tc.name      : getspnam_r_0300
60  * @tc.desc      : Test the function of getspnam_r with invalid input(name).
61  * @tc.level     : Level 2
62  */
getspnam_r_0300(void)63 void getspnam_r_0300(void)
64 {
65     char buf[512];
66     struct spwd *spwd;
67     struct spwd spwd_storage;
68 
69     int result = getspnam_r("bin/", &spwd_storage, buf, sizeof(buf), &spwd);
70     EXPECT_NE("getspnam_r_0400", result, 0);
71     EXPECT_PTREQ("getspnam_r_0400", spwd, NULL);
72 }
73 
74 /**
75  * @tc.name      : getspnam_r_0400
76  * @tc.desc      : The length of name is greater than the NAME_MAX.
77  * @tc.level     : Level 2
78  */
getspnam_r_0400(void)79 void getspnam_r_0400(void)
80 {
81     char buf[512];
82     struct spwd *spwd;
83     struct spwd spwd_storage;
84 
85     char name[300]= {0};
86     memset(name, 1, 299);
87     name[299] = '\0';
88 
89     int result = getspnam_r(name, &spwd_storage, buf, sizeof(buf), &spwd);
90     EXPECT_NE("getspnam_r_0500", result, 0);
91     EXPECT_PTREQ("getspnam_r_0500", spwd, NULL);
92 }
93 
main(int argc,char * argv[])94 int main(int argc, char *argv[])
95 {
96     getspnam_r_0100();
97     getspnam_r_0200();
98     getspnam_r_0300();
99     getspnam_r_0400();
100     return t_status;
101 }