1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/file.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include "securec.h"
28 
29 using namespace testing::ext;
30 using namespace std;
31 
32 static const char *TEST_FILE = "/data/local/tmp/fdatasync.txt";
33 mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
34 
35 class FdatasyncApiTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 private:
42 };
SetUp()43 void FdatasyncApiTest::SetUp()
44 {
45 }
TearDown()46 void FdatasyncApiTest::TearDown()
47 {
48 }
SetUpTestCase()49 void FdatasyncApiTest::SetUpTestCase()
50 {
51 }
TearDownTestCase()52 void FdatasyncApiTest::TearDownTestCase()
53 {
54 }
55 
56 /*
57  * @tc.number : SUB_KERNEL_SYSCALL_FDATASYNC_0100
58  * @tc.name   : FdatasyncValidFdSuccess_0001
59  * @tc.desc   : fdatasync sync valid file success.
60  * @tc.size   : MediumTest
61  * @tc.type   : Function
62  * @tc.level  : Level 1
63  */
HWTEST_F(FdatasyncApiTest, FdatasyncValidFdSuccess_0001, Function | MediumTest | Level1)64 HWTEST_F(FdatasyncApiTest, FdatasyncValidFdSuccess_0001, Function | MediumTest | Level1)
65 {
66     int ret = -1;
67     int fd = open(TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, MODE_0644);
68     EXPECT_TRUE(fd > 0);
69 
70     const char *data = "fdatasync test data.";
71     ssize_t bytesWritten = write(fd, data, strlen(data) + 1);
72     EXPECT_NE(bytesWritten, -1);
73 
74     ret = fdatasync(fd);
75     EXPECT_EQ(ret, 0);
76 
77     close(fd);
78     remove(TEST_FILE);
79 }
80 
81 /*
82  * @tc.number : SUB_KERNEL_SYSCALL_FDATASYNC_0200
83  * @tc.name   : FdatasyncInvalidFdFail_0002
84  * @tc.desc   : fdatasync sync invalid file fail, errno EBADF.
85  * @tc.size   : MediumTest
86  * @tc.type   : Function
87  * @tc.level  : Level 2
88  */
HWTEST_F(FdatasyncApiTest, FdatasyncInvalidFdFail_0002, Function | MediumTest | Level2)89 HWTEST_F(FdatasyncApiTest, FdatasyncInvalidFdFail_0002, Function | MediumTest | Level2)
90 {
91     int invalidFd = -1;
92     errno = 0;
93     int ret = fdatasync(invalidFd);
94     EXPECT_EQ(ret, -1);
95     EXPECT_EQ(errno, EBADF);
96 }
97