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
29using namespace testing::ext;
30using namespace std;
31
32static const char *TEST_FILE = "/data/local/tmp/fdatasync.txt";
33mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
34
35class FdatasyncApiTest : public testing::Test {
36public:
37    static void SetUpTestCase();
38    static void TearDownTestCase();
39    void SetUp();
40    void TearDown();
41private:
42};
43void FdatasyncApiTest::SetUp()
44{
45}
46void FdatasyncApiTest::TearDown()
47{
48}
49void FdatasyncApiTest::SetUpTestCase()
50{
51}
52void 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 */
64HWTEST_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 */
89HWTEST_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