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(nullptr, loader) << error_msg;
39
40 // Reset dlerror.
41 loader->DlError();
42 ASSERT_EQ(nullptr, loader->DlError());
43
44 Dl_info info;
45 ASSERT_EQ(0, loader->DlAddr(loader, &info));
46 ASSERT_EQ(nullptr, loader->DlError()); // dladdr doesn't set dlerror.
47
48 void* handle = loader->DlOpen("libc.so", RTLD_NOW);
49 ASSERT_NE(nullptr, handle) << loader->DlError(); // dlerror called only if assertion fails.
50 ASSERT_EQ(nullptr, loader->DlError());
51
52 android_namespace_t* ns = loader->CreateNamespace("classloader-namespace",
53 nullptr,
54 "/data:/mnt/expand",
55 kNamespaceTypeIsolated,
56 "/data:/mnt/expand",
57 nullptr);
58 ASSERT_NE(nullptr, ns) << loader->DlError(); // dlerror called only if assertion fails.
59 ASSERT_EQ(nullptr, loader->DlError());
60 }
61
62 } // namespace
63
64 } // namespace berberis