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 <cstdio>
179762338dSopenharmony_ci#include <cstdlib>
189762338dSopenharmony_ci#include <fcntl.h>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <unistd.h>
219762338dSopenharmony_ci#include <vector>
229762338dSopenharmony_ci#include <gtest/gtest.h>
239762338dSopenharmony_ci#include <sys/stat.h>
249762338dSopenharmony_ci#include <sys/types.h>
259762338dSopenharmony_ci#include <sys/xattr.h>
269762338dSopenharmony_ci#include "securec.h"
279762338dSopenharmony_ci
289762338dSopenharmony_ciusing namespace testing::ext;
299762338dSopenharmony_ciusing namespace std;
309762338dSopenharmony_ci
319762338dSopenharmony_cistatic const char *TEST_FILE = "/data/local/tmp/test_file.txt";
329762338dSopenharmony_cistatic const char *TEST_DATA = "Hello, world!";
339762338dSopenharmony_cistatic const size_t TEST_LEN = strlen(TEST_DATA);
349762338dSopenharmony_ci
359762338dSopenharmony_ciclass LseekApiTest : public testing::Test {
369762338dSopenharmony_cipublic:
379762338dSopenharmony_ci    static void SetUpTestCase();
389762338dSopenharmony_ci    static void TearDownTestCase();
399762338dSopenharmony_ci    void SetUp();
409762338dSopenharmony_ci    void TearDown();
419762338dSopenharmony_ciprivate:
429762338dSopenharmony_ci};
439762338dSopenharmony_civoid LseekApiTest::SetUp()
449762338dSopenharmony_ci{
459762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
469762338dSopenharmony_ci    write(fd, TEST_DATA, TEST_LEN);
479762338dSopenharmony_ci    close(fd);
489762338dSopenharmony_ci}
499762338dSopenharmony_civoid LseekApiTest::TearDown()
509762338dSopenharmony_ci{
519762338dSopenharmony_ci    (void)remove(TEST_FILE);
529762338dSopenharmony_ci}
539762338dSopenharmony_civoid LseekApiTest::SetUpTestCase()
549762338dSopenharmony_ci{
559762338dSopenharmony_ci}
569762338dSopenharmony_civoid LseekApiTest::TearDownTestCase()
579762338dSopenharmony_ci{
589762338dSopenharmony_ci}
599762338dSopenharmony_ci
609762338dSopenharmony_ci/*
619762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0100
629762338dSopenharmony_ci * @tc.name   : LseekSetOffsetTestSuccess_0001
639762338dSopenharmony_ci * @tc.desc   : lseek set the offset of the file test success.
649762338dSopenharmony_ci * @tc.size   : MediumTest
659762338dSopenharmony_ci * @tc.type   : Function
669762338dSopenharmony_ci * @tc.level  : Level 1
679762338dSopenharmony_ci */
689762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekSetOffsetTestSuccess_0001, Function | MediumTest | Level1)
699762338dSopenharmony_ci{
709762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
719762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
729762338dSopenharmony_ci
739762338dSopenharmony_ci    // file middle position test
749762338dSopenharmony_ci    off_t offset = TEST_LEN / 2;
759762338dSopenharmony_ci    off_t pos = lseek(fd, offset, SEEK_SET);
769762338dSopenharmony_ci    EXPECT_EQ(pos, offset);
779762338dSopenharmony_ci
789762338dSopenharmony_ci    // file start position test
799762338dSopenharmony_ci    offset = 0;
809762338dSopenharmony_ci    pos = lseek(fd, offset, SEEK_SET);
819762338dSopenharmony_ci    EXPECT_EQ(pos, offset);
829762338dSopenharmony_ci
839762338dSopenharmony_ci    // file end position test
849762338dSopenharmony_ci    offset = TEST_LEN;
859762338dSopenharmony_ci    pos = lseek(fd, offset, SEEK_SET);
869762338dSopenharmony_ci    EXPECT_EQ(pos, offset);
879762338dSopenharmony_ci
889762338dSopenharmony_ci    // position larger than file length test
899762338dSopenharmony_ci    offset = TEST_LEN * 2;
909762338dSopenharmony_ci    errno = 0;
919762338dSopenharmony_ci    pos = lseek(fd, offset, SEEK_SET);
929762338dSopenharmony_ci    EXPECT_EQ(pos, offset);
939762338dSopenharmony_ci
949762338dSopenharmony_ci    close(fd);
959762338dSopenharmony_ci}
969762338dSopenharmony_ci
979762338dSopenharmony_ci/*
989762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0200
999762338dSopenharmony_ci * @tc.name   : LseekSetNegativePositionFailed_0002
1009762338dSopenharmony_ci * @tc.desc   : lseek sets a negative position failed, errno EINVAL.
1019762338dSopenharmony_ci * @tc.size   : MediumTest
1029762338dSopenharmony_ci * @tc.type   : Function
1039762338dSopenharmony_ci * @tc.level  : Level 2
1049762338dSopenharmony_ci */
1059762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekSetNegativePositionFailed_0002, Function | MediumTest | Level2)
1069762338dSopenharmony_ci{
1079762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
1089762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1099762338dSopenharmony_ci
1109762338dSopenharmony_ci    // -1 position test failed, errno EINVAL
1119762338dSopenharmony_ci    off_t offset = -1;
1129762338dSopenharmony_ci    errno = 0;
1139762338dSopenharmony_ci    off_t pos = lseek(fd, offset, SEEK_SET);
1149762338dSopenharmony_ci    EXPECT_EQ(pos, -1);
1159762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1169762338dSopenharmony_ci    close(fd);
1179762338dSopenharmony_ci}
1189762338dSopenharmony_ci
1199762338dSopenharmony_ci/*
1209762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0300
1219762338dSopenharmony_ci * @tc.name   : LseekSEEK_CURTestSuccess_0003
1229762338dSopenharmony_ci * @tc.desc   : lseek set position from current pos success.
1239762338dSopenharmony_ci * @tc.size   : MediumTest
1249762338dSopenharmony_ci * @tc.type   : Function
1259762338dSopenharmony_ci * @tc.level  : Level 1
1269762338dSopenharmony_ci */
1279762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekSEEK_CURTestSuccess_0003, Function | MediumTest | Level1)
1289762338dSopenharmony_ci{
1299762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
1309762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1319762338dSopenharmony_ci
1329762338dSopenharmony_ci    // set pos to middle position
1339762338dSopenharmony_ci    off_t start = TEST_LEN / 2;
1349762338dSopenharmony_ci    off_t initPos = lseek(fd, start, SEEK_SET);
1359762338dSopenharmony_ci    EXPECT_EQ(initPos, start);
1369762338dSopenharmony_ci
1379762338dSopenharmony_ci    // set positive pos to offset from current position
1389762338dSopenharmony_ci    off_t offset = TEST_LEN / 2;
1399762338dSopenharmony_ci    off_t pos = lseek(fd, offset, SEEK_CUR);
1409762338dSopenharmony_ci    EXPECT_EQ(pos, initPos + offset);
1419762338dSopenharmony_ci
1429762338dSopenharmony_ci    // set negative pos to offset from current position
1439762338dSopenharmony_ci    offset = (TEST_LEN / 2) * (-1);
1449762338dSopenharmony_ci    initPos = pos;
1459762338dSopenharmony_ci    pos = lseek(fd, offset, SEEK_CUR);
1469762338dSopenharmony_ci    EXPECT_EQ(pos, initPos + offset);
1479762338dSopenharmony_ci
1489762338dSopenharmony_ci    // set positive pos to offset from current position
1499762338dSopenharmony_ci    offset = TEST_LEN * 2;
1509762338dSopenharmony_ci    initPos = pos;
1519762338dSopenharmony_ci    pos = lseek(fd, offset, SEEK_CUR);
1529762338dSopenharmony_ci    EXPECT_EQ(pos, initPos + offset);
1539762338dSopenharmony_ci
1549762338dSopenharmony_ci    close(fd);
1559762338dSopenharmony_ci}
1569762338dSopenharmony_ci
1579762338dSopenharmony_ci/*
1589762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0400
1599762338dSopenharmony_ci * @tc.name   : LseekSEEK_CURNegativeTestFailed_0004
1609762338dSopenharmony_ci * @tc.desc   : lseek set negative position from current pos failed, errno EINVAL.
1619762338dSopenharmony_ci * @tc.size   : MediumTest
1629762338dSopenharmony_ci * @tc.type   : Function
1639762338dSopenharmony_ci * @tc.level  : Level 2
1649762338dSopenharmony_ci */
1659762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekSEEK_CURNegativeTestFailed_0004, Function | MediumTest | Level2)
1669762338dSopenharmony_ci{
1679762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
1689762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1699762338dSopenharmony_ci
1709762338dSopenharmony_ci    // set negative pos to offset from current position
1719762338dSopenharmony_ci    off_t offset = -1;
1729762338dSopenharmony_ci    errno = 0;
1739762338dSopenharmony_ci    off_t pos = lseek(fd, offset, SEEK_CUR);
1749762338dSopenharmony_ci    EXPECT_EQ(pos, -1);
1759762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1769762338dSopenharmony_ci
1779762338dSopenharmony_ci    close(fd);
1789762338dSopenharmony_ci}
1799762338dSopenharmony_ci
1809762338dSopenharmony_ci/*
1819762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0500
1829762338dSopenharmony_ci * @tc.name   : LseekSEEK_ENDTestSuccess_0005
1839762338dSopenharmony_ci * @tc.desc   : lseek use SEEK_END set the position to file end success.
1849762338dSopenharmony_ci * @tc.size   : MediumTest
1859762338dSopenharmony_ci * @tc.type   : Function
1869762338dSopenharmony_ci * @tc.level  : Level 1
1879762338dSopenharmony_ci */
1889762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekSEEK_ENDTestSuccess_0005, Function | MediumTest | Level1)
1899762338dSopenharmony_ci{
1909762338dSopenharmony_ci    int fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0644);
1919762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
1929762338dSopenharmony_ci
1939762338dSopenharmony_ci    off_t pos = lseek(fd, 0, SEEK_END);
1949762338dSopenharmony_ci    EXPECT_EQ(pos, TEST_LEN);
1959762338dSopenharmony_ci
1969762338dSopenharmony_ci    close(fd);
1979762338dSopenharmony_ci}
1989762338dSopenharmony_ci
1999762338dSopenharmony_ci/*
2009762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_LSEEK_0600
2019762338dSopenharmony_ci * @tc.name   : LseekInvalidFdTestFailed_0006
2029762338dSopenharmony_ci * @tc.desc   : lseek used invalid fd test failed, errno EBADF.
2039762338dSopenharmony_ci * @tc.size   : MediumTest
2049762338dSopenharmony_ci * @tc.type   : Function
2059762338dSopenharmony_ci * @tc.level  : Level 2
2069762338dSopenharmony_ci */
2079762338dSopenharmony_ciHWTEST_F(LseekApiTest, LseekInvalidFdTestFailed_0006, Function | MediumTest | Level2)
2089762338dSopenharmony_ci{
2099762338dSopenharmony_ci    errno = 0;
2109762338dSopenharmony_ci    off_t pos = lseek(-1, 0, SEEK_SET);
2119762338dSopenharmony_ci    EXPECT_EQ(pos, -1);
2129762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
2139762338dSopenharmony_ci}