1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "berberis/base/raw_syscall.h"
18
19 #include <fcntl.h>
20 #include <linux/unistd.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "gtest/gtest.h"
27
28 namespace berberis {
29
30 namespace {
31
TEST(RawSyscall,SyscallWith0Args)32 TEST(RawSyscall, SyscallWith0Args) {
33 pid_t pid = getpid();
34 pid_t ret = RawSyscall(__NR_getpid);
35 EXPECT_EQ(ret, pid);
36 }
37
TEST(RawSyscall,SyscallWith2Args)38 TEST(RawSyscall, SyscallWith2Args) {
39 struct timespec ts;
40 long ret;
41 ret = clock_gettime(CLOCK_REALTIME, &ts);
42 EXPECT_EQ(ret, 0);
43
44 struct timespec ts2;
45 ret = RawSyscall(
46 __NR_clock_gettime, static_cast<long>(CLOCK_REALTIME), reinterpret_cast<long>(&ts2));
47 EXPECT_EQ(ret, 0);
48 EXPECT_LE(ts2.tv_sec - ts2.tv_sec, 1)
49 << "clib call and raw call should be within 1 second of each other";
50 }
51
TEST(RawSyscall,SyscallWith6Args)52 TEST(RawSyscall, SyscallWith6Args) {
53 FILE* file_in = popen("cat /dev/zero", "r");
54 int fd_in = fileno(file_in);
55 int fd_out = open("/dev/null", O_WRONLY);
56 // Equivalent call: splice(fd_in, NULL, fd_out, NULL, 10, 0);
57 long bytes_count = RawSyscall(__NR_splice, fd_in, 0, fd_out, 0, 10, 0);
58 pclose(file_in);
59 close(fd_out);
60 EXPECT_EQ(bytes_count, 10);
61 }
62
63 } // namespace
64
65 } // namespace berberis
66