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 <string>
179762338dSopenharmony_ci#include <fcntl.h>
189762338dSopenharmony_ci#include <unistd.h>
199762338dSopenharmony_ci#include <gtest/gtest.h>
209762338dSopenharmony_ci#include <sys/sendfile.h>
219762338dSopenharmony_ci#include <sys/stat.h>
229762338dSopenharmony_ci#include "securec.h"
239762338dSopenharmony_ci
249762338dSopenharmony_ciusing namespace testing::ext;
259762338dSopenharmony_ciusing namespace std;
269762338dSopenharmony_ci
279762338dSopenharmony_cistatic const char *FIRST_FILE = "/data/local/tmp/first";
289762338dSopenharmony_cistatic const char *SEC_FILE = "/data/local/tmp/second";
299762338dSopenharmony_cistatic const char* DATA = "Hello, sendfile!";
309762338dSopenharmony_cistatic const int STR_COUNT = 256;
319762338dSopenharmony_cistatic const int LOW_COUNT = -1;
329762338dSopenharmony_ci
339762338dSopenharmony_ciclass SendfileApiTest : public testing::Test {
349762338dSopenharmony_cipublic:
359762338dSopenharmony_ci    static void SetUpTestCase();
369762338dSopenharmony_ci    static void TearDownTestCase();
379762338dSopenharmony_ci    void SetUp();
389762338dSopenharmony_ci    void TearDown();
399762338dSopenharmony_ciprivate:
409762338dSopenharmony_ci};
419762338dSopenharmony_civoid SendfileApiTest::SetUp()
429762338dSopenharmony_ci{
439762338dSopenharmony_ci}
449762338dSopenharmony_civoid SendfileApiTest::TearDown()
459762338dSopenharmony_ci{
469762338dSopenharmony_ci}
479762338dSopenharmony_civoid SendfileApiTest::SetUpTestCase()
489762338dSopenharmony_ci{
499762338dSopenharmony_ci}
509762338dSopenharmony_civoid SendfileApiTest::TearDownTestCase()
519762338dSopenharmony_ci{
529762338dSopenharmony_ci}
539762338dSopenharmony_ci
549762338dSopenharmony_ci/*
559762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SENDFILE_0100
569762338dSopenharmony_ci * @tc.name   : SendfileSuccess_0001
579762338dSopenharmony_ci * @tc.desc   : sendfile success.
589762338dSopenharmony_ci * @tc.size   : MediumTest
599762338dSopenharmony_ci * @tc.type   : Function
609762338dSopenharmony_ci * @tc.level  : Level 1
619762338dSopenharmony_ci */
629762338dSopenharmony_ciHWTEST_F(SendfileApiTest, SendfileSuccess_0001, Function | MediumTest | Level1)
639762338dSopenharmony_ci{
649762338dSopenharmony_ci    int firFd = open(FIRST_FILE, O_CREAT | O_RDWR, 0644);
659762338dSopenharmony_ci    EXPECT_GE(firFd, 0);
669762338dSopenharmony_ci    write(firFd, DATA, sizeof(DATA));
679762338dSopenharmony_ci    int secFd = open(SEC_FILE, O_CREAT | O_RDWR, 0644);
689762338dSopenharmony_ci    EXPECT_GE(secFd, 0);
699762338dSopenharmony_ci
709762338dSopenharmony_ci    ssize_t size = sendfile(secFd, firFd, nullptr, STR_COUNT);
719762338dSopenharmony_ci    EXPECT_GE(size, 0);
729762338dSopenharmony_ci    size = sendfile(secFd, firFd, nullptr, LOW_COUNT);
739762338dSopenharmony_ci    EXPECT_EQ(size, 0);
749762338dSopenharmony_ci
759762338dSopenharmony_ci    unlink(FIRST_FILE);
769762338dSopenharmony_ci    unlink(SEC_FILE);
779762338dSopenharmony_ci}
789762338dSopenharmony_ci
799762338dSopenharmony_ci/*
809762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SENDFILE_0200
819762338dSopenharmony_ci * @tc.name   : SendfileInvalidFdFailed_0002
829762338dSopenharmony_ci * @tc.desc   : sendfile -1 fd errno EBADF.
839762338dSopenharmony_ci * @tc.size   : MediumTest
849762338dSopenharmony_ci * @tc.type   : Function
859762338dSopenharmony_ci * @tc.level  : Level 2
869762338dSopenharmony_ci */
879762338dSopenharmony_ciHWTEST_F(SendfileApiTest, SendfileInvalidFdFailed_0002, Function | MediumTest | Level2)
889762338dSopenharmony_ci{
899762338dSopenharmony_ci    errno = 0;
909762338dSopenharmony_ci    ssize_t size = sendfile(-1, -1, nullptr, STR_COUNT);
919762338dSopenharmony_ci    EXPECT_EQ(size, -1);
929762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
939762338dSopenharmony_ci}