• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <procinfo/process.h>
18 
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <set>
25 #include <thread>
26 #include <vector>
27 
28 #include <gtest/gtest.h>
29 
30 #include <android-base/stringprintf.h>
31 
32 #if !defined(__BIONIC__)
33 #include <syscall.h>
gettid()34 static pid_t gettid() {
35   return syscall(__NR_gettid);
36 }
37 #endif
38 
TEST(process_info,process_info_smoke)39 TEST(process_info, process_info_smoke) {
40   android::procinfo::ProcessInfo self;
41   ASSERT_TRUE(android::procinfo::GetProcessInfo(gettid(), &self));
42   ASSERT_EQ(gettid(), self.tid);
43   ASSERT_EQ(getpid(), self.pid);
44   ASSERT_EQ(getppid(), self.ppid);
45   ASSERT_EQ(getuid(), self.uid);
46   ASSERT_EQ(getgid(), self.gid);
47 }
48 
TEST(process_info,process_info_proc_pid_fd_smoke)49 TEST(process_info, process_info_proc_pid_fd_smoke) {
50   android::procinfo::ProcessInfo self;
51   int fd = open(android::base::StringPrintf("/proc/%d", gettid()).c_str(), O_DIRECTORY | O_RDONLY);
52   ASSERT_NE(-1, fd);
53   ASSERT_TRUE(android::procinfo::GetProcessInfoFromProcPidFd(fd, &self));
54 
55   // Process name is capped at 15 bytes.
56   ASSERT_EQ("libprocinfo_tes", self.name);
57   ASSERT_EQ(gettid(), self.tid);
58   ASSERT_EQ(getpid(), self.pid);
59   ASSERT_EQ(getppid(), self.ppid);
60   ASSERT_EQ(getuid(), self.uid);
61   ASSERT_EQ(getgid(), self.gid);
62   close(fd);
63 }
64 
TEST(process_info,process_tids_smoke)65 TEST(process_info, process_tids_smoke) {
66   pid_t main_tid = gettid();
67   std::thread([main_tid]() {
68     pid_t thread_tid = gettid();
69 
70     {
71       std::vector<pid_t> vec;
72       ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &vec));
73       ASSERT_EQ(1, std::count(vec.begin(), vec.end(), main_tid));
74       ASSERT_EQ(1, std::count(vec.begin(), vec.end(), thread_tid));
75     }
76 
77     {
78       std::set<pid_t> set;
79       ASSERT_TRUE(android::procinfo::GetProcessTids(getpid(), &set));
80       ASSERT_EQ(1, std::count(set.begin(), set.end(), main_tid));
81       ASSERT_EQ(1, std::count(set.begin(), set.end(), thread_tid));
82     }
83   }).join();
84 }
85