summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Gampe2014-09-26 00:33:01 -0500
committerAndreas Gampe2014-09-26 01:18:31 -0500
commit04054e28e24866d76034236843490829b80df40c (patch)
treedbcad630f635a73a0ed4a22aa15bbc49ebbf5795 /libnativebridge
parent9f93e297ee7df04bf605b9dc1251f7c772c48189 (diff)
downloadplatform-system-core-04054e28e24866d76034236843490829b80df40c.tar.gz
platform-system-core-04054e28e24866d76034236843490829b80df40c.tar.xz
platform-system-core-04054e28e24866d76034236843490829b80df40c.zip
LibNativeBridge: Add testing
Change PreInitializeNativeBridge to have a different path for the host to allow testing. Add a test (needs root privileges). Add a test for NeedsNativeBridge. Change error reporting to use strerror. Change-Id: Id2d488f0484ff8b0438863b48ef43770e784505f
Diffstat (limited to 'libnativebridge')
-rw-r--r--libnativebridge/native_bridge.cc17
-rw-r--r--libnativebridge/tests/Android.mk2
-rw-r--r--libnativebridge/tests/NeedsNativeBridge_test.cpp48
-rw-r--r--libnativebridge/tests/PreInitializeNativeBridge_test.cpp66
4 files changed, 129 insertions, 4 deletions
diff --git a/libnativebridge/native_bridge.cc b/libnativebridge/native_bridge.cc
index c19aba95b..d460f6f5c 100644
--- a/libnativebridge/native_bridge.cc
+++ b/libnativebridge/native_bridge.cc
@@ -205,6 +205,10 @@ static const char* kRuntimeISA = "unknown";
205 205
206 206
207bool NeedsNativeBridge(const char* instruction_set) { 207bool NeedsNativeBridge(const char* instruction_set) {
208 if (instruction_set == nullptr) {
209 ALOGE("Null instruction set in NeedsNativeBridge.");
210 return false;
211 }
208 return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0; 212 return strncmp(instruction_set, kRuntimeISA, strlen(kRuntimeISA) + 1) != 0;
209} 213}
210 214
@@ -240,11 +244,16 @@ void PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruct
240 // mount command will fail, so we safe the extra file existence check... 244 // mount command will fail, so we safe the extra file existence check...
241 char cpuinfo_path[1024]; 245 char cpuinfo_path[1024];
242 246
243 snprintf(cpuinfo_path, 1024, "/system/lib" 247#ifdef HAVE_ANDROID_OS
248 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "/system/lib"
244#ifdef __LP64__ 249#ifdef __LP64__
245 "64" 250 "64"
251#endif // __LP64__
252 "/%s/cpuinfo", instruction_set);
253#else // !HAVE_ANDROID_OS
254 // To be able to test on the host, we hardwire a relative path.
255 snprintf(cpuinfo_path, sizeof(cpuinfo_path), "./cpuinfo");
246#endif 256#endif
247 "/%s/cpuinfo", instruction_set);
248 257
249 // Bind-mount. 258 // Bind-mount.
250 if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source. 259 if (TEMP_FAILURE_RETRY(mount(cpuinfo_path, // Source.
@@ -252,7 +261,7 @@ void PreInitializeNativeBridge(const char* app_data_dir_in, const char* instruct
252 nullptr, // FS type. 261 nullptr, // FS type.
253 MS_BIND, // Mount flags: bind mount. 262 MS_BIND, // Mount flags: bind mount.
254 nullptr)) == -1) { // "Data." 263 nullptr)) == -1) { // "Data."
255 ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %d", cpuinfo_path, errno); 264 ALOGW("Failed to bind-mount %s as /proc/cpuinfo: %s", cpuinfo_path, strerror(errno));
256 } 265 }
257#else 266#else
258 UNUSED(instruction_set); 267 UNUSED(instruction_set);
diff --git a/libnativebridge/tests/Android.mk b/libnativebridge/tests/Android.mk
index f58b8f7a9..a94d2f70c 100644
--- a/libnativebridge/tests/Android.mk
+++ b/libnativebridge/tests/Android.mk
@@ -5,6 +5,8 @@ include $(CLEAR_VARS)
5# Build the unit tests. 5# Build the unit tests.
6test_src_files := \ 6test_src_files := \
7 InvalidCharsNativeBridge_test.cpp \ 7 InvalidCharsNativeBridge_test.cpp \
8 NeedsNativeBridge_test.cpp \
9 PreInitializeNativeBridge_test.cpp \
8 ReSetupNativeBridge_test.cpp \ 10 ReSetupNativeBridge_test.cpp \
9 UnavailableNativeBridge_test.cpp \ 11 UnavailableNativeBridge_test.cpp \
10 ValidNameNativeBridge_test.cpp 12 ValidNameNativeBridge_test.cpp
diff --git a/libnativebridge/tests/NeedsNativeBridge_test.cpp b/libnativebridge/tests/NeedsNativeBridge_test.cpp
new file mode 100644
index 000000000..e1c087694
--- /dev/null
+++ b/libnativebridge/tests/NeedsNativeBridge_test.cpp
@@ -0,0 +1,48 @@
1/*
2 * Copyright (C) 2014 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 "NativeBridgeTest.h"
18
19namespace android {
20
21static const char* kISAs[] = { "arm", "arm64", "mips", "x86", "x86_64", "random", "64arm", "64_x86",
22 "64_x86_64", "", "reallylongstringabcd", nullptr };
23
24#if defined(__arm__)
25static const char* kRuntimeISA = "arm";
26#elif defined(__aarch64__)
27static const char* kRuntimeISA = "arm64";
28#elif defined(__mips__)
29static const char* kRuntimeISA = "mips";
30#elif defined(__i386__)
31static const char* kRuntimeISA = "x86";
32#elif defined(__x86_64__)
33static const char* kRuntimeISA = "x86_64";
34#else
35static const char* kRuntimeISA = "unknown";
36#endif
37
38TEST_F(NativeBridgeTest, NeedsNativeBridge) {
39 EXPECT_EQ(false, NeedsNativeBridge(kRuntimeISA));
40
41 const size_t kISACount = sizeof(kISAs)/sizeof(kISAs[0]);
42 for (size_t i = 0; i < kISACount; i++) {
43 EXPECT_EQ(kISAs[i] == nullptr ? false : strcmp(kISAs[i], kRuntimeISA) != 0,
44 NeedsNativeBridge(kISAs[i]));
45 }
46}
47
48} // namespace android
diff --git a/libnativebridge/tests/PreInitializeNativeBridge_test.cpp b/libnativebridge/tests/PreInitializeNativeBridge_test.cpp
new file mode 100644
index 000000000..9b487d7c4
--- /dev/null
+++ b/libnativebridge/tests/PreInitializeNativeBridge_test.cpp
@@ -0,0 +1,66 @@
1/*
2 * Copyright (C) 2014 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 "NativeBridgeTest.h"
18
19#include <cstdio>
20#include <cstring>
21#include <cutils/log.h>
22#include <dlfcn.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <sys/mount.h>
27#include <sys/stat.h>
28
29namespace android {
30
31static constexpr const char* kTestData = "PreInitializeNativeBridge test.";
32
33TEST_F(NativeBridgeTest, PreInitializeNativeBridge) {
34#ifndef __APPLE_ // Mac OS does not support bind-mount.
35#ifndef HAVE_ANDROID_OS // Cannot write into the hard-wired location.
36 // Try to create our mount namespace.
37 if (unshare(CLONE_NEWNS) != -1) {
38 // Create a dummy file.
39 FILE* cpuinfo = fopen("./cpuinfo", "w");
40 ASSERT_NE(nullptr, cpuinfo) << strerror(errno);
41 fprintf(cpuinfo, kTestData);
42 fclose(cpuinfo);
43
44 // Call the setup.
45 PreInitializeNativeBridge("does not matter 1", "short 2");
46
47 // Read /proc/cpuinfo
48 FILE* proc_cpuinfo = fopen("/proc/cpuinfo", "r");
49 ASSERT_NE(nullptr, proc_cpuinfo) << strerror(errno);
50 char buf[1024];
51 EXPECT_NE(nullptr, fgets(buf, sizeof(buf), proc_cpuinfo)) << "Error reading.";
52 fclose(proc_cpuinfo);
53
54 EXPECT_EQ(0, strcmp(buf, kTestData));
55
56 // Delete the file.
57 ASSERT_EQ(0, unlink("./cpuinfo")) << "Error unlinking temporary file.";
58 // Ending the test will tear down the mount namespace.
59 } else {
60 GTEST_LOG_(WARNING) << "Could not create mount namespace. Are you running this as root?";
61 }
62#endif
63#endif
64}
65
66} // namespace android