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 <fcntl.h>
21#include <unistd.h>
22#include <vector>
23#include <arpa/inet.h>
24#include <gtest/gtest.h>
25#include <netinet/in.h>
26#include <sys/stat.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29
30using namespace testing::ext;
31
32class HatsSyncfsTest : public testing::Test {
33public:
34static void SetUpTestCase();
35static void TearDownTestCase();
36void SetUp();
37void TearDown();
38private:
39};
40void HatsSyncfsTest::SetUp()
41{
42}
43void HatsSyncfsTest::TearDown()
44{
45}
46void HatsSyncfsTest::SetUpTestCase()
47{
48}
49void HatsSyncfsTest::TearDownTestCase()
50{
51}
52
53static const char *SYNCFS_TEST_FILE = "/data/local/tmp/trySyncfs.txt";
54static const char *TEST_DATA = "Hello syncfs!";
55static const int TEST_DATA_LEN = strlen(TEST_DATA);
56
57/*
58 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFS_0100
59 * @tc.name   : SyncfsFileToDiskSuccess_0001
60 * @tc.desc   : syncfs write file from buffer to disk success.
61 * @tc.size   : MediumTest
62 * @tc.type   : Function
63 * @tc.level  : Level 1
64 */
65HWTEST_F(HatsSyncfsTest, SyncfsFileToDiskSuccess_0001, Function | MediumTest | Level1)
66{
67    int fd;
68    int ret;
69
70    fd = open(SYNCFS_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
71    EXPECT_TRUE(fd >= 3);
72    ret = write(fd, TEST_DATA, TEST_DATA_LEN);
73    EXPECT_TRUE(ret <= TEST_DATA_LEN);
74
75    ret = syncfs(fd);
76    EXPECT_TRUE(ret == 0);
77    close(fd);
78
79    remove(SYNCFS_TEST_FILE);
80}
81
82/*
83 * @tc.number : SUB_KERNEL_SYSCALL_SYNCFS_0200
84 * @tc.name   : SyncfsInvalidFdFail_0002
85 * @tc.desc   : syncfs write file from buffer to disk using invalid fd fail.
86 * @tc.size   : MediumTest
87 * @tc.type   : Function
88 * @tc.level  : Level 2
89 */
90HWTEST_F(HatsSyncfsTest, SyncfsInvalidFdFail_0002, Function | MediumTest | Level2)
91{
92    int fd;
93    int ret;
94
95    fd = -1;
96    errno = 0;
97    ret = syncfs(fd);
98    EXPECT_TRUE(ret == -1);
99    EXPECT_EQ(errno, EBADF);
100
101    remove(SYNCFS_TEST_FILE);
102}