1 /*
2  * Copyright (C) 2012 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 <dlfcn.h>
20 #include <libgen.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 
25 #include <string>
26 
main_global_default_serial()27 extern "C" int main_global_default_serial() {
28   return 3370318;
29 }
30 
main_global_protected_serial()31 extern "C" int main_global_protected_serial() {
32   return 2716057;
33 }
34 
35 // The following functions are defined in DT_NEEDED
36 // libdl_preempt_test.so library.
37 
38 // This one calls main_global_default_serial
39 extern "C" int main_global_default_get_serial();
40 
41 // This one calls main_global_protected_serial
42 extern "C" int main_global_protected_get_serial();
43 
44 // This one calls lib_global_default_serial
45 extern "C" int lib_global_default_get_serial();
46 
47 // This one calls lib_global_protected_serial
48 extern "C" int lib_global_protected_get_serial();
49 
50 // This test verifies that the global default function
51 // main_global_default_serial() is preempted by
52 // the function defined above.
TEST(dl,main_preempts_global_default)53 TEST(dl, main_preempts_global_default) {
54   ASSERT_EQ(3370318, main_global_default_get_serial());
55 }
56 
57 // This one makes sure that the global protected
58 // symbols do not get preempted
TEST(dl,main_does_not_preempt_global_protected)59 TEST(dl, main_does_not_preempt_global_protected) {
60   ASSERT_EQ(3370318, main_global_protected_get_serial());
61 }
62 
63 // check same things for lib
TEST(dl,lib_preempts_global_default)64 TEST(dl, lib_preempts_global_default) {
65   ASSERT_EQ(3370318, lib_global_default_get_serial());
66 }
67 
TEST(dl,lib_does_not_preempt_global_protected)68 TEST(dl, lib_does_not_preempt_global_protected) {
69   ASSERT_EQ(3370318, lib_global_protected_get_serial());
70 }
71 
72 // TODO: Add tests for LD_PRELOADs
73