1 /*
2 * Copyright (C) 2017 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 "perfetto/base/utils.h"
18
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <unistd.h>
23
24 #include "gtest/gtest.h"
25
26 namespace perfetto {
27 namespace base {
28 namespace {
29
TEST(UtilsTest,ArraySize)30 TEST(UtilsTest, ArraySize) {
31 char char_arr_1[1];
32 char char_arr_4[4];
33 EXPECT_EQ(1u, ArraySize(char_arr_1));
34 EXPECT_EQ(4u, ArraySize(char_arr_4));
35
36 int32_t int32_arr_1[1];
37 int32_t int32_arr_4[4];
38 EXPECT_EQ(1u, ArraySize(int32_arr_1));
39 EXPECT_EQ(4u, ArraySize(int32_arr_4));
40
41 uint64_t int64_arr_1[1];
42 uint64_t int64_arr_4[4];
43 EXPECT_EQ(1u, ArraySize(int64_arr_1));
44 EXPECT_EQ(4u, ArraySize(int64_arr_4));
45
46 char kString[] = "foo";
47 EXPECT_EQ(4u, ArraySize(kString));
48
49 struct Bar {
50 int32_t a;
51 int32_t b;
52 };
53 Bar bar_1[1];
54 Bar bar_4[4];
55 EXPECT_EQ(1u, ArraySize(bar_1));
56 EXPECT_EQ(4u, ArraySize(bar_4));
57 }
58
59 int pipe_fd[2];
60
TEST(UtilsTest,EintrWrapper)61 TEST(UtilsTest, EintrWrapper) {
62 ASSERT_EQ(0, pipe(pipe_fd));
63
64 struct sigaction sa = {};
65 struct sigaction old_sa = {};
66
67 // Glibc headers for sa_sigaction trigger this.
68 #pragma GCC diagnostic push
69 #if defined(__clang__)
70 #pragma GCC diagnostic ignored "-Wdisabled-macro-expansion"
71 #endif
72 sa.sa_sigaction = [](int, siginfo_t*, void*) {};
73 #pragma GCC diagnostic pop
74
75 ASSERT_EQ(0, sigaction(SIGUSR2, &sa, &old_sa));
76 int parent_pid = getpid();
77 pid_t pid = fork();
78 ASSERT_NE(-1, pid);
79 if (pid == 0 /* child */) {
80 usleep(5000);
81 kill(parent_pid, SIGUSR2);
82 ignore_result(write(pipe_fd[1], "foo\0", 4));
83 _exit(0);
84 }
85
86 char buf[6] = {};
87 EXPECT_EQ(4, PERFETTO_EINTR(read(pipe_fd[0], buf, sizeof(buf))));
88 EXPECT_STREQ("foo", buf);
89 EXPECT_EQ(0, PERFETTO_EINTR(close(pipe_fd[0])));
90 EXPECT_EQ(0, PERFETTO_EINTR(close(pipe_fd[1])));
91
92 // A 2nd close should fail with the proper errno.
93 int res = close(pipe_fd[0]);
94 auto err = errno;
95 EXPECT_EQ(-1, res);
96 EXPECT_EQ(EBADF, err);
97
98 // Restore the old handler.
99 sigaction(SIGUSR2, &old_sa, nullptr);
100 }
101
TEST(UtilsTest,Align)102 TEST(UtilsTest, Align) {
103 EXPECT_EQ(0u, AlignUp<4>(0));
104 EXPECT_EQ(4u, AlignUp<4>(1));
105 EXPECT_EQ(4u, AlignUp<4>(3));
106 EXPECT_EQ(4u, AlignUp<4>(4));
107 EXPECT_EQ(8u, AlignUp<4>(5));
108 EXPECT_EQ(0u, AlignUp<16>(0));
109 EXPECT_EQ(16u, AlignUp<16>(1));
110 EXPECT_EQ(16u, AlignUp<16>(15));
111 EXPECT_EQ(16u, AlignUp<16>(16));
112 EXPECT_EQ(32u, AlignUp<16>(17));
113 EXPECT_EQ(0xffffff00u, AlignUp<16>(0xffffff00 - 1));
114 }
115
116 } // namespace
117 } // namespace base
118 } // namespace perfetto
119