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 <pthread.h>
18 #include <stdbool.h>
19 #include "functionalext.h"
20
21 static pthread_barrier_t g_barrier;
22 #define WAIT() pthread_barrier_wait(&g_barrier)
23
getmntent_rOne()24 void *getmntent_rOne()
25 {
26 char c[1000];
27 char str[100];
28 FILE *fptr = NULL;
29 char strings[1024];
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 struct mntent mntbuf;
38 memset(&m, 0x00, sizeof(m));
39 memset(&strings[0], 0x00, sizeof(strings));
40 FILE *ffp = setmntent("/proc/mounts", "r");
41 EXPECT_PTRNE("getmntent_r_0100", ffp, NULL);
42
43 WAIT();
44 m = getmntent_r(ffp, &mntbuf, strings, sizeof(strings));
45 EXPECT_TRUE("getmntent_r_0100", m != NULL);
46 EXPECT_EQ("getmntent_r_0100", strcmp(m->mnt_fsname, str), 0);
47 EXPECT_EQ("getmntent_r_0100", strcmp(mntbuf.mnt_fsname, str), 0);
48
49 endmntent(ffp);
50 return m;
51 }
52
53 /**
54 * @tc.name : getmntent_r_0100
55 * @tc.desc : Multiple threads simultaneously read file information.
56 * @tc.level : Level 0
57 */
getmntent_r_0100(void)58 void getmntent_r_0100(void)
59 {
60 pthread_t srv;
61 pthread_t cli;
62 int ret = pthread_barrier_init(&g_barrier, 0, 2);
63 EXPECT_EQ("getmntent_r_0100", 0, ret);
64 ret = pthread_create(&srv, NULL, getmntent_rOne, NULL);
65 EXPECT_EQ("getmntent_r_0100", 0, ret);
66 ret = pthread_create(&cli, NULL, getmntent_rOne, NULL);
67 EXPECT_EQ("getmntent_r_0100", 0, ret);
68 ret = pthread_join(cli, NULL);
69 EXPECT_EQ("getmntent_r_0100", 0, ret);
70 ret = pthread_join(srv, NULL);
71 EXPECT_EQ("getmntent_r_0100", 0, ret);
72 ret = pthread_barrier_destroy(&g_barrier);
73 EXPECT_EQ("getmntent_r_0100", 0, ret);
74 }
75
76 /**
77 * @tc.name : getmntent_r_0200
78 * @tc.desc : The f parameter is invalid, reading the file information failed.
79 * @tc.level : Level 2
80 */
getmntent_r_0200(void)81 void getmntent_r_0200(void)
82 {
83 char c[1000];
84 char str[100];
85 char strings[1024];
86
87 FILE *fptr = fopen("/data/getmntent.txt", "w");
88 EXPECT_TRUE("getmntent_r_0200", fptr != NULL);
89 fclose(fptr);
90
91 struct mntent *m = NULL;
92 struct mntent mntbuf;
93 memset(&m, 0x00, sizeof(m));
94 memset(&strings[0], 0x00, sizeof(strings));
95 FILE *ffp = setmntent("/data/getmntent.txt", "r");
96 EXPECT_PTRNE("getmntent_r_0200", ffp, NULL);
97 m = getmntent_r(ffp, &mntbuf, strings, sizeof(strings));
98 EXPECT_EQ("getmntent_r_0200", m, NULL);
99 endmntent(ffp);
100 remove("/data/getmntent.txt");
101 }
102
main(int argc,char * argv[])103 int main(int argc, char *argv[])
104 {
105 getmntent_r_0100();
106 getmntent_r_0200();
107
108 return t_status;
109 }