xref: /aosp_15_r20/art/libnativebridge/tests/PreInitializeNativeBridge_test.cpp (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include <dlfcn.h>
18*795d594fSAndroid Build Coastguard Worker #include <errno.h>
19*795d594fSAndroid Build Coastguard Worker #include <fcntl.h>
20*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
21*795d594fSAndroid Build Coastguard Worker #include <sys/mount.h>
22*795d594fSAndroid Build Coastguard Worker #include <sys/stat.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include <cstdio>
25*795d594fSAndroid Build Coastguard Worker #include <cstring>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include <android/log.h>
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker #include "NativeBridgeTest.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace android {
32*795d594fSAndroid Build Coastguard Worker 
TEST_F(NativeBridgeTest,PreInitializeNativeBridge)33*795d594fSAndroid Build Coastguard Worker TEST_F(NativeBridgeTest, PreInitializeNativeBridge) {
34*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
35*795d594fSAndroid Build Coastguard Worker #if !defined(__APPLE__)         // Mac OS does not support bind-mount.
36*795d594fSAndroid Build Coastguard Worker #if !defined(__ANDROID__)       // Cannot write into the hard-wired location.
37*795d594fSAndroid Build Coastguard Worker     static constexpr const char* kTestData = "PreInitializeNativeBridge test.";
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker     // Try to create our mount namespace.
40*795d594fSAndroid Build Coastguard Worker     if (unshare(CLONE_NEWNS) != -1) {
41*795d594fSAndroid Build Coastguard Worker         // Create a placeholder file.
42*795d594fSAndroid Build Coastguard Worker         FILE* cpuinfo = fopen("./cpuinfo", "we");
43*795d594fSAndroid Build Coastguard Worker         ASSERT_NE(nullptr, cpuinfo) << strerror(errno);
44*795d594fSAndroid Build Coastguard Worker         fprintf(cpuinfo, kTestData);
45*795d594fSAndroid Build Coastguard Worker         fclose(cpuinfo);
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker         ASSERT_TRUE(PreInitializeNativeBridge("does not matter 1", "short 2"));
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker         // Read /proc/cpuinfo
50*795d594fSAndroid Build Coastguard Worker         FILE* proc_cpuinfo = fopen("/proc/cpuinfo", "re");
51*795d594fSAndroid Build Coastguard Worker         ASSERT_NE(nullptr, proc_cpuinfo) << strerror(errno);
52*795d594fSAndroid Build Coastguard Worker         char buf[1024];
53*795d594fSAndroid Build Coastguard Worker         EXPECT_NE(nullptr, fgets(buf, sizeof(buf), proc_cpuinfo)) << "Error reading.";
54*795d594fSAndroid Build Coastguard Worker         fclose(proc_cpuinfo);
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker         EXPECT_EQ(0, strcmp(buf, kTestData));
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker         // Delete the file.
59*795d594fSAndroid Build Coastguard Worker         ASSERT_EQ(0, unlink("./cpuinfo")) << "Error unlinking temporary file.";
60*795d594fSAndroid Build Coastguard Worker         // Ending the test will tear down the mount namespace.
61*795d594fSAndroid Build Coastguard Worker     } else {
62*795d594fSAndroid Build Coastguard Worker         GTEST_LOG_(WARNING) << "Could not create mount namespace. Are you running this as root?";
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker #endif
65*795d594fSAndroid Build Coastguard Worker #endif
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker }  // namespace android
69