1 /*
2  * Copyright (C) 2023 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 "gtest/gtest.h"
18 
19 #include "berberis/guest_loader/guest_loader.h"
20 
21 #include <android/dlext.h>
22 
23 #include <string>
24 
25 #include "berberis/runtime/berberis.h"
26 
27 namespace berberis {
28 
29 namespace {
30 
31 const constexpr uint64_t kNamespaceTypeIsolated = 1;
32 
TEST(guest_loader,smoke)33 TEST(guest_loader, smoke) {
34   InitBerberis();
35 
36   std::string error_msg;
37   GuestLoader* loader = GuestLoader::StartAppProcessInNewThread(&error_msg);
38   ASSERT_NE(loader, nullptr) << error_msg;
39 
40   // Reset dlerror.
41   loader->DlError();
42   ASSERT_EQ(loader->DlError(), nullptr);
43 
44   Dl_info info;
45   ASSERT_EQ(loader->DlAddr(loader, &info), 0);
46   ASSERT_EQ(loader->DlError(), nullptr);  // dladdr doesn't set dlerror.
47 
48   void* handle = loader->DlOpen("libc.so", RTLD_NOW);
49   ASSERT_NE(handle, nullptr) << loader->DlError();  // dlerror called only if assertion fails.
50   // Clear dlerror: successful dlopen(libc.so) might result in dlerror
51   // being set (because of failed dlsym("swift_demangle") during its
52   // initialization).
53   loader->DlError();
54 
55   handle = loader->DlOpen("libdl.so", RTLD_NOW);
56   const char* dlerror = loader->DlError();
57   ASSERT_NE(handle, nullptr) << dlerror;
58   ASSERT_EQ(dlerror, nullptr) << dlerror;
59 
60   android_namespace_t* ns = loader->CreateNamespace("classloader-namespace",
61                                                     nullptr,
62                                                     "/data:/mnt/expand",
63                                                     kNamespaceTypeIsolated,
64                                                     "/data:/mnt/expand",
65                                                     nullptr);
66   ASSERT_NE(ns, nullptr) << loader->DlError();  // dlerror called only if assertion fails.
67   ASSERT_EQ(loader->DlError(), nullptr);
68 }
69 
70 }  // namespace
71 
72 }  // namespace berberis
73