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