• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "It_test_IO.h"
32 
testcase(VOID)33 static UINT32 testcase(VOID)
34 {
35     struct mntent* mnt = nullptr;
36     struct mntent* mnt_new = nullptr;
37     FILE *fp = nullptr;
38     FILE *fp2 = nullptr;
39     char *argv[2] = {nullptr};
40     argv[0] = "/etc/fstab";
41     argv[1] = "errors";
42     char *opt = argv[1];
43     char *ret = nullptr;
44 
45     char fileWords[] = "/dev/disk/by-uuid/c4992556-a86e-45e8-ba5f-190b16a9073x /usr1 ext3 errors=remount-ro,nofail 0 1";
46     char *pathList[] = {"/etc/fstab"};
47     char *streamList[] = {static_cast<char *>(fileWords)};
48     int streamLen[] = {sizeof(fileWords)};
49 
50     int flag = PrepareFileEnv(pathList, streamList, streamLen, 1);
51     if (flag != 0) {
52         printf("error: need some env file, but prepare is not ok");
53         (VOID)RecoveryFileEnv(pathList, 1);
54         return -1;
55     }
56 
57     mnt_new = (struct mntent *)malloc(sizeof(struct mntent));
58     if (mnt_new == NULL) {
59         (VOID)RecoveryFileEnv(pathList, 1);
60         return LOS_NOK;
61     }
62     mnt_new->mnt_fsname = "UUID=c4992556-a86e-45e8-ba5f-190b16a9073x";
63     mnt_new->mnt_dir = "/usr1";
64     mnt_new->mnt_type = "ext3";
65     mnt_new->mnt_opts = "errors=remount-ro,nofail";
66     mnt_new->mnt_freq = 0;
67     mnt_new->mnt_passno = 1;
68 
69     fp = setmntent("/etc/fstab", "r");
70     if (!fp) {
71         printf("fp=0x%x\n", fp);
72         free(mnt_new);
73         return LOS_NOK;
74     }
75 
76     mnt = getmntent(fp);
77     if (mnt && !(feof(fp) || ferror(fp))) {
78         ret =  hasmntopt(mnt, opt);
79         printf("hasmntopt=%s\n", ret);
80         ICUNIT_ASSERT_NOT_EQUAL_NULL(ret, NULL, -1);
81         mnt = getmntent(fp);
82     }
83 
84     if (fp != NULL) {
85         endmntent(fp);
86     }
87 
88     /* test the addmntent below */
89     fp = setmntent(argv[0], "a");
90     fp2 = fopen("/lib/libc.so", "r");
91     if (fp2 != NULL) {
92         printf("aha I found you are OHOS!!!\n");
93         if (addmntent(fp, mnt_new)) {
94             printf("a new mnt is added to %s\n", argv[0]);
95         }
96     }
97 
98     if (fp != NULL) {
99         endmntent(fp);
100     }
101     if (fp2 != NULL) {
102         fclose(fp2);
103     }
104     (VOID)RecoveryFileEnv(pathList, 1);
105     return LOS_OK;
106 }
107 
IT_STDIO_HASMNTOPT_001(void)108 VOID IT_STDIO_HASMNTOPT_001(void)
109 {
110     TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
111 }
112