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 <string>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <sys/sendfile.h>
21 #include <sys/stat.h>
22 #include "securec.h"
23
24 using namespace testing::ext;
25 using namespace std;
26
27 static const char *FIRST_FILE = "/data/local/tmp/first";
28 static const char *SEC_FILE = "/data/local/tmp/second";
29 static const char* DATA = "Hello, sendfile!";
30 static const int STR_COUNT = 256;
31 static const int LOW_COUNT = -1;
32
33 class SendfileApiTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 private:
40 };
SetUp()41 void SendfileApiTest::SetUp()
42 {
43 }
TearDown()44 void SendfileApiTest::TearDown()
45 {
46 }
SetUpTestCase()47 void SendfileApiTest::SetUpTestCase()
48 {
49 }
TearDownTestCase()50 void SendfileApiTest::TearDownTestCase()
51 {
52 }
53
54 /*
55 * @tc.number : SUB_KERNEL_SYSCALL_SENDFILE_0100
56 * @tc.name : SendfileSuccess_0001
57 * @tc.desc : sendfile success.
58 * @tc.size : MediumTest
59 * @tc.type : Function
60 * @tc.level : Level 1
61 */
HWTEST_F(SendfileApiTest, SendfileSuccess_0001, Function | MediumTest | Level1)62 HWTEST_F(SendfileApiTest, SendfileSuccess_0001, Function | MediumTest | Level1)
63 {
64 int firFd = open(FIRST_FILE, O_CREAT | O_RDWR, 0644);
65 EXPECT_GE(firFd, 0);
66 write(firFd, DATA, sizeof(DATA));
67 int secFd = open(SEC_FILE, O_CREAT | O_RDWR, 0644);
68 EXPECT_GE(secFd, 0);
69
70 ssize_t size = sendfile(secFd, firFd, nullptr, STR_COUNT);
71 EXPECT_GE(size, 0);
72 size = sendfile(secFd, firFd, nullptr, LOW_COUNT);
73 EXPECT_EQ(size, 0);
74
75 unlink(FIRST_FILE);
76 unlink(SEC_FILE);
77 }
78
79 /*
80 * @tc.number : SUB_KERNEL_SYSCALL_SENDFILE_0200
81 * @tc.name : SendfileInvalidFdFailed_0002
82 * @tc.desc : sendfile -1 fd errno EBADF.
83 * @tc.size : MediumTest
84 * @tc.type : Function
85 * @tc.level : Level 2
86 */
HWTEST_F(SendfileApiTest, SendfileInvalidFdFailed_0002, Function | MediumTest | Level2)87 HWTEST_F(SendfileApiTest, SendfileInvalidFdFailed_0002, Function | MediumTest | Level2)
88 {
89 errno = 0;
90 ssize_t size = sendfile(-1, -1, nullptr, STR_COUNT);
91 EXPECT_EQ(size, -1);
92 EXPECT_EQ(errno, EBADF);
93 }