• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dlfcn.h>
17 #include <gtest/gtest.h>
18 #include "log.h"
19 #include "utils.h"
20 #include "libfs.h"
21 #include "KernelConstants.h"
22 
23 using namespace testing::ext;
24 
25 class DlopenApiTest : public testing::Test {
26 };
27 
28 #define RES_DIR_DYLOAD RES_DIR_KERNEL "dyload/"
29 #define DYLOAD_TEST_DIR "/storage/data/"
30 
31 /**
32  * @tc.number   SUB_KERNEL_DL_API_DLOPEN_0100
33  * @tc.name     the dlopen current main elf
34  * @tc.desc     [C- SOFTWARE -0200]
35  */
36 HWTEST_F(DlopenApiTest, testDlopenSelf, Function | MediumTest | Level1)
37 {
38     char* testELF = RES_DIR_DYLOAD "api_dlopen_self";
39 
40     int rt = RunElf(testELF, NULL, NULL);
41     EXPECT_EQ(rt, 0);
42 }
43 
44 /**
45  * @tc.number   SUB_KERNEL_DL_API_DLOPEN_0200
46  * @tc.name     dlopen error test
47  * @tc.desc     [C- SOFTWARE -0200]
48  */
49 HWTEST_F(DlopenApiTest, testDlopenError, Function | MediumTest | Level3)
50 {
51     dlerror(); // clear any old error info
52     void* h = dlopen(0, 0);
53     EXPECT_TRUE(h == NULL) << "dlopen with flag=0 succeed!";
54 
55     char* errMsg = dlerror();
56     LOG("err_msg:%s", errMsg);
57     ASSERT_TRUE(errMsg != NULL) << "dlerror message is empty";
58 
59     char *p = strcasestr(errMsg, "invalid mode");
60     ASSERT_TRUE(p != NULL) << "dlerror msg check failed";
61 }
62 
63 /**
64  * @tc.number   SUB_KERNEL_DL_API_DLSYM_0100
65  * @tc.name     find self symbol use RTLD_DEFAULT.
66  * @tc.desc     [C- SOFTWARE -0200]
67  */
68 HWTEST_F(DlopenApiTest, testDlsymSelf, Function | MediumTest | Level1)
69 {
70     char* testELF = RES_DIR_DYLOAD "api_dlsym_self";
71 
72     int rt = RunElf(testELF, NULL, NULL);
73     EXPECT_EQ(rt, 0);
74 }
75 
76 /**
77  * @tc.number   SUB_KERNEL_DL_API_DLSYM_0200
78  * @tc.name     find symbol in next so use RTLD_NEXT.
79  * @tc.desc     [C- SOFTWARE -0200]
80  */
81 HWTEST_F(DlopenApiTest, testDlsymNext, Function | MediumTest | Level1)
82 {
83     char* resSO = RES_DIR_DYLOAD "libdso1.so";
84     char* newSO = DYLOAD_TEST_DIR "libdso1.so";
85 
86     // test SetUp
87     ASSERT_EQ(CopyFile(resSO, newSO), 0);
88     LOG("SetUp ok");
89 
90     char* testELF = RES_DIR_DYLOAD "api_dlsym_next";
91 
92     int rt = RunElf(testELF, NULL, NULL);
93     EXPECT_EQ(rt, 0);
94 
95     // test TearDown
96     ASSERT_EQ(RemoveFile(newSO), 0);
97     LOG("TearDown ok ");
98 }
99