• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "posix_fs_test.h"
32 
33 /* *
34  * @tc.number   SUB_KERNEL_FS_PRWRITE_OK
35  * @tc.name     pwrite
36  * @tc.desc     [C- SOFTWARE -0200]
37  */
38 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteOK, Function | MediumTest | Level1)
39 {
40     int32_t ret;
41     const char tmpFileName[TEST_BUF_SIZE] = { FILE1 };
42     char writeBuf[TEST_BUF_SIZE] = "fsPwrtiteTest";
43 
44     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH);
45     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
46 
47     ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0);   /* 0, offset distance */
48     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
49 
50 EXIT:
51     ret = close(fd);
52     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
53 
54     ret = unlink(tmpFileName);
55     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
56 
57     return POSIX_FS_NO_ERROR;
58 }
59 
60 /* *
61  * @tc.number   SUB_KERNEL_FS_PRWRITE_EFAULT
62  * @tc.name     pwrite
63  * @tc.desc     [C- SOFTWARE -0200]
64  */
65 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteEFAULT, Function | MediumTest | Level1)
66 {
67     int32_t ret;
68     const char tmpFileName[TEST_BUF_SIZE] = { FILE1 };
69     char *writeBuf = NULL;
70 
71     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH);
72     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
73 
74     ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0);   /* 0, offset distance */
75     ICUNIT_ASSERT_EQUAL(errno, EFAULT, POSIX_FS_IS_ERROR);
76     ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
77 
78 EXIT:
79     ret = close(fd);
80     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
81 
82     ret = unlink(tmpFileName);
83     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
84 
85     return POSIX_FS_NO_ERROR;
86 }
87 
88 /* *
89  * @tc.number   SUB_KERNEL_FS_PRWRITE_EACCES
90  * @tc.name     pwrite
91  * @tc.desc     [C- SOFTWARE -0200]
92  */
93 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsPwriteEACCES, Function | MediumTest | Level1)
94 {
95     int32_t ret;
96     const char tmpFileName[TEST_BUF_SIZE] = { FILE1 };
97     char writeBuf[TEST_BUF_SIZE] = "fsPwriteTest";
98 
99     int32_t fd = open(tmpFileName, O_CREAT | O_RDONLY, TEST_MODE_HIGH);
100     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
101 
102     ret = pwrite(fd, writeBuf, TEST_BUF_SIZE, 0);   /* 0, offset distance */
103     ICUNIT_ASSERT_EQUAL(errno, EACCES, POSIX_FS_IS_ERROR);
104     ICUNIT_GOTO_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
105 
106 EXIT:
107     ret = close(fd);
108     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
109 
110     ret = unlink(tmpFileName);
111     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
112 
113     return POSIX_FS_NO_ERROR;
114 }
115 
PosixFsPwriteTest(void)116 void PosixFsPwriteTest(void)
117 {
118     RUN_ONE_TESTCASE(TestFsPwriteOK);
119     RUN_ONE_TESTCASE(TestFsPwriteEFAULT);
120     RUN_ONE_TESTCASE(TestFsPwriteEACCES);
121 }
122