PreInitializeNativeBridge_test.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <dlfcn.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <sys/mount.h>
  21. #include <sys/stat.h>
  22. #include <cstdio>
  23. #include <cstring>
  24. #include <android/log.h>
  25. #include "NativeBridgeTest.h"
  26. namespace android {
  27. TEST_F(NativeBridgeTest, PreInitializeNativeBridge) {
  28. ASSERT_TRUE(LoadNativeBridge(kNativeBridgeLibrary, nullptr));
  29. #if !defined(__APPLE__) // Mac OS does not support bind-mount.
  30. #if !defined(__ANDROID__) // Cannot write into the hard-wired location.
  31. static constexpr const char* kTestData = "PreInitializeNativeBridge test.";
  32. // Try to create our mount namespace.
  33. if (unshare(CLONE_NEWNS) != -1) {
  34. // Create a dummy file.
  35. FILE* cpuinfo = fopen("./cpuinfo", "w");
  36. ASSERT_NE(nullptr, cpuinfo) << strerror(errno);
  37. fprintf(cpuinfo, kTestData);
  38. fclose(cpuinfo);
  39. ASSERT_TRUE(PreInitializeNativeBridge("does not matter 1", "short 2"));
  40. // Read /proc/cpuinfo
  41. FILE* proc_cpuinfo = fopen("/proc/cpuinfo", "r");
  42. ASSERT_NE(nullptr, proc_cpuinfo) << strerror(errno);
  43. char buf[1024];
  44. EXPECT_NE(nullptr, fgets(buf, sizeof(buf), proc_cpuinfo)) << "Error reading.";
  45. fclose(proc_cpuinfo);
  46. EXPECT_EQ(0, strcmp(buf, kTestData));
  47. // Delete the file.
  48. ASSERT_EQ(0, unlink("./cpuinfo")) << "Error unlinking temporary file.";
  49. // Ending the test will tear down the mount namespace.
  50. } else {
  51. GTEST_LOG_(WARNING) << "Could not create mount namespace. Are you running this as root?";
  52. }
  53. #endif
  54. #endif
  55. }
  56. } // namespace android