• 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 <mntent.h>
17 #include <stdbool.h>
18 #include "functionalext.h"
19 
20 /**
21  * @tc.name      : getmntent_0100
22  * @tc.desc      : The parameters are valid and the file information can be read.
23  * @tc.level     : Level 0
24  */
getmntent_0100(void)25 void getmntent_0100(void)
26 {
27     char c[1000];
28     char str[100];
29     FILE *fptr = NULL;
30     if ((fptr = fopen("/proc/mounts", "r")) == NULL) {
31         t_error("%s Error! fopen failed\n", __func__);
32     }
33     fscanf(fptr, "%[^\n]", c);
34     fclose(fptr);
35     sscanf(c, "%[^ ]", str);
36     struct mntent *m = NULL;
37     FILE *ffp = setmntent("/proc/mounts", "r");
38     EXPECT_PTRNE("getmntent_0100", ffp, NULL);
39     m = getmntent(ffp);
40     EXPECT_TRUE("getmntent_0100", m != NULL);
41     EXPECT_EQ("getmntent_0100", strcmp(m->mnt_fsname, str), 0);
42 
43     endmntent(ffp);
44 }
45 
46 /**
47  * @tc.name      : getmntent_0200
48  * @tc.desc      : Invalid parameter, failed to read file information.
49  * @tc.level     : Level 2
50  */
getmntent_0200(void)51 void getmntent_0200(void)
52 {
53     char c[1000];
54     char *str = "This is a test";
55     FILE *fp = fopen("/data/getmntent.txt", "w");
56     EXPECT_PTRNE("getmntent_0200", fp, NULL);
57     fputs(str, fp);
58     fclose(fp);
59     struct mntent *m = NULL;
60     FILE *ffp = setmntent("/data/getmntent.txt", "r");
61     EXPECT_PTRNE("getmntent_0200", ffp, NULL);
62 
63     m = getmntent(ffp);
64     EXPECT_EQ("getmntent_0200", m, NULL);
65     endmntent(ffp);
66     remove("/data/getmntent.txt");
67 }
68 
main(void)69 int main(void)
70 {
71     getmntent_0100();
72     getmntent_0200();
73     return t_status;
74 }