19762338dSopenharmony_ci/* 29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization. 39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49762338dSopenharmony_ci * you may not use this file except in compliance with the License. 59762338dSopenharmony_ci * You may obtain a copy of the License at 69762338dSopenharmony_ci * 79762338dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89762338dSopenharmony_ci * 99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129762338dSopenharmony_ci * See the License for the specific language governing permissions and 139762338dSopenharmony_ci * limitations under the License. 149762338dSopenharmony_ci */ 159762338dSopenharmony_ci 169762338dSopenharmony_ci#include <cerrno> 179762338dSopenharmony_ci#include <cstdio> 189762338dSopenharmony_ci#include <cstdlib> 199762338dSopenharmony_ci#include <string> 209762338dSopenharmony_ci#include <vector> 219762338dSopenharmony_ci#include <fcntl.h> 229762338dSopenharmony_ci#include <unistd.h> 239762338dSopenharmony_ci#include <arpa/inet.h> 249762338dSopenharmony_ci#include <gtest/gtest.h> 259762338dSopenharmony_ci#include <netinet/in.h> 269762338dSopenharmony_ci#include <sys/stat.h> 279762338dSopenharmony_ci#include <sys/socket.h> 289762338dSopenharmony_ci#include <sys/types.h> 299762338dSopenharmony_ci 309762338dSopenharmony_ciusing namespace testing::ext; 319762338dSopenharmony_ci 329762338dSopenharmony_ciclass HatsSyncFileRangeTest : public testing::Test { 339762338dSopenharmony_cipublic: 349762338dSopenharmony_cistatic void SetUpTestCase(); 359762338dSopenharmony_cistatic void TearDownTestCase(); 369762338dSopenharmony_civoid SetUp(); 379762338dSopenharmony_civoid TearDown(); 389762338dSopenharmony_ciprivate: 399762338dSopenharmony_ci}; 409762338dSopenharmony_civoid HatsSyncFileRangeTest::SetUp() 419762338dSopenharmony_ci{ 429762338dSopenharmony_ci} 439762338dSopenharmony_civoid HatsSyncFileRangeTest::TearDown() 449762338dSopenharmony_ci{ 459762338dSopenharmony_ci} 469762338dSopenharmony_civoid HatsSyncFileRangeTest::SetUpTestCase() 479762338dSopenharmony_ci{ 489762338dSopenharmony_ci} 499762338dSopenharmony_civoid HatsSyncFileRangeTest::TearDownTestCase() 509762338dSopenharmony_ci{ 519762338dSopenharmony_ci} 529762338dSopenharmony_ci 539762338dSopenharmony_cistatic const char *SYNC_RANGE_TEST_FILE = "/data/local/tmp/trySync.txt"; 549762338dSopenharmony_cistatic const char *TEST_DATA = "Hello sync!"; 559762338dSopenharmony_cistatic const int TEST_DATA_LEN = strlen(TEST_DATA); 569762338dSopenharmony_cistatic const int FILE_OFFSET = 0; 579762338dSopenharmony_ci 589762338dSopenharmony_ci/* 599762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0100 609762338dSopenharmony_ci * @tc.name : SyncFileRangeFileToDiskSuccess_0001 619762338dSopenharmony_ci * @tc.desc : sync_file_range write file from buffer to disk success. 629762338dSopenharmony_ci * @tc.size : MediumTest 639762338dSopenharmony_ci * @tc.type : Function 649762338dSopenharmony_ci * @tc.level : Level 1 659762338dSopenharmony_ci */ 669762338dSopenharmony_ciHWTEST_F(HatsSyncFileRangeTest, SyncFileRangeFileToDiskSuccess_0001, Function | MediumTest | Level1) 679762338dSopenharmony_ci{ 689762338dSopenharmony_ci int fd; 699762338dSopenharmony_ci int ret; 709762338dSopenharmony_ci 719762338dSopenharmony_ci fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644); 729762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 739762338dSopenharmony_ci 749762338dSopenharmony_ci ret = sync_file_range(fd, FILE_OFFSET, TEST_DATA_LEN, 0); 759762338dSopenharmony_ci EXPECT_EQ(ret, 0); 769762338dSopenharmony_ci 779762338dSopenharmony_ci close(fd); 789762338dSopenharmony_ci remove(SYNC_RANGE_TEST_FILE); 799762338dSopenharmony_ci} 809762338dSopenharmony_ci 819762338dSopenharmony_ci/* 829762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0200 839762338dSopenharmony_ci * @tc.name : SyncFileRangeInvalidFdFail_0002 849762338dSopenharmony_ci * @tc.desc : sync_file_range sync invalid fd to disk fail, errno EBADF. 859762338dSopenharmony_ci * @tc.size : MediumTest 869762338dSopenharmony_ci * @tc.type : Function 879762338dSopenharmony_ci * @tc.level : Level 2 889762338dSopenharmony_ci */ 899762338dSopenharmony_ciHWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidFdFail_0002, Function | MediumTest | Level2) 909762338dSopenharmony_ci{ 919762338dSopenharmony_ci errno = 0; 929762338dSopenharmony_ci int ret = sync_file_range(-1, FILE_OFFSET, TEST_DATA_LEN, 0); 939762338dSopenharmony_ci EXPECT_EQ(ret, -1); 949762338dSopenharmony_ci EXPECT_EQ(errno, EBADF); 959762338dSopenharmony_ci} 969762338dSopenharmony_ci 979762338dSopenharmony_ci/* 989762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0300 999762338dSopenharmony_ci * @tc.name : SyncFileRangeUsingInvalidFlagFail_0003 1009762338dSopenharmony_ci * @tc.desc : sync_file_range sync file to disk using invalid flag fail. 1019762338dSopenharmony_ci * @tc.size : MediumTest 1029762338dSopenharmony_ci * @tc.type : Function 1039762338dSopenharmony_ci * @tc.level : Level 2 1049762338dSopenharmony_ci */ 1059762338dSopenharmony_ciHWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidOffsetFail_0003, Function | MediumTest | Level2) 1069762338dSopenharmony_ci{ 1079762338dSopenharmony_ci int ret; 1089762338dSopenharmony_ci int fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644); 1099762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 1109762338dSopenharmony_ci 1119762338dSopenharmony_ci errno = 0; 1129762338dSopenharmony_ci ret = sync_file_range(fd, FILE_OFFSET, TEST_DATA_LEN, -1); 1139762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1149762338dSopenharmony_ci EXPECT_EQ(errno, EINVAL); 1159762338dSopenharmony_ci 1169762338dSopenharmony_ci close(fd); 1179762338dSopenharmony_ci remove(SYNC_RANGE_TEST_FILE); 1189762338dSopenharmony_ci} 1199762338dSopenharmony_ci 1209762338dSopenharmony_ci/* 1219762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SYNCFILERANGE_0400 1229762338dSopenharmony_ci * @tc.name : SyncFileRangeInvalidNbytesFail_0004 1239762338dSopenharmony_ci * @tc.desc : sync_file_range sync file to disk when nbytes is invalid fail. 1249762338dSopenharmony_ci * @tc.size : MediumTest 1259762338dSopenharmony_ci * @tc.type : Function 1269762338dSopenharmony_ci * @tc.level : Level 2 1279762338dSopenharmony_ci */ 1289762338dSopenharmony_ciHWTEST_F(HatsSyncFileRangeTest, SyncFileRangeInvalidNbytesFail_0004, Function | MediumTest | Level2) 1299762338dSopenharmony_ci{ 1309762338dSopenharmony_ci int fd = open(SYNC_RANGE_TEST_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644); 1319762338dSopenharmony_ci EXPECT_TRUE(fd > 0); 1329762338dSopenharmony_ci ssize_t size = write(fd, TEST_DATA, TEST_DATA_LEN); 1339762338dSopenharmony_ci EXPECT_EQ(size, TEST_DATA_LEN); 1349762338dSopenharmony_ci 1359762338dSopenharmony_ci errno = 0; 1369762338dSopenharmony_ci int ret = sync_file_range(fd, FILE_OFFSET, -1, 0); 1379762338dSopenharmony_ci EXPECT_EQ(ret, -1); 1389762338dSopenharmony_ci EXPECT_EQ(errno, EINVAL); 1399762338dSopenharmony_ci 1409762338dSopenharmony_ci close(fd); 1419762338dSopenharmony_ci remove(SYNC_RANGE_TEST_FILE); 1429762338dSopenharmony_ci} 143