• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright 2021 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -euo pipefail
18
19source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
20
21# This should be abstracted to a unit-test library when it has more uses.
22function assert_contains_regex() {
23    local needle="$1"
24    local haystack="$2"
25    local message="${3:-Expected regexp "$needle" not found in "$haystack"}"
26    echo "${haystack}" | grep "${needle}" && return 0
27
28    echo "$message"
29    exit 1
30}
31
32# Test that a library is the expected filetype.
33function test_filetype() {
34    local filepath="$(readlink -f $1)"; shift
35    local regex="$1"; shift
36    local file_output="$(file ${filepath})"
37    assert_contains_regex "${regex}" "${file_output}"
38}
39
40# Test that the shared library contains a symbol
41function test_shared_library_symbols() {
42    local filepath="$(readlink -f $1)"; shift
43    local symbols="$1"; shift
44    local nm_output="$(nm -D "${filepath}")"
45    for symbol in "${symbols[@]}"
46    do
47        assert_contains_regex "${symbol}" "${nm_output}"
48    done
49}
50
51# Test file contents of //bionic/linker:ld-android
52function test_ld-android() {
53    local shared_library="$(rlocation __main__/bionic/linker/libld-android_bp2build_cc_library_shared.so)"
54    local static_library="$(rlocation __main__/bionic/linker/libld-android_bp2build_cc_library_static_mainlib.a)"
55
56    test_filetype "${shared_library}" "shared object.*dynamically linked"
57    test_filetype "${static_library}" "current ar archive"
58
59    symbols=(
60        __loader_add_thread_local_dtor
61        __loader_android_create_namespace
62        __loader_android_dlopen_ext
63        __loader_android_dlwarning
64        __loader_android_get_application_target_sdk_version
65        __loader_android_get_exported_namespace
66        __loader_android_get_LD_LIBRARY_PATH
67        __loader_android_init_anonymous_namespace
68        __loader_android_link_namespaces
69        __loader_android_link_namespaces_all_libs
70        __loader_android_set_application_target_sdk_version
71        __loader_android_update_LD_LIBRARY_PATH
72        __loader_cfi_fail
73        __loader_dladdr
74        __loader_dlclose
75        __loader_dlerror
76        __loader_dl_iterate_phdr
77        __loader_dlopen
78        __loader_dlsym
79        __loader_dlvsym
80        __loader_remove_thread_local_dtor
81        __loader_shared_globals
82        _db_dlactivity
83    )
84
85    test_shared_library_symbols "${shared_library}" "${symbols}"
86}
87
88function test_libdl_android() {
89    local shared_library="$(rlocation __main__/bionic/libdl/liblibdl_android_bp2build_cc_library_shared.so)"
90    local static_library="$(rlocation __main__/bionic/libdl/liblibdl_android_bp2build_cc_library_static_mainlib.a)"
91
92    test_filetype "${shared_library}" "shared object.*dynamically linked"
93    test_filetype "${static_library}" "current ar archive"
94
95    symbols=(
96        android_create_namespace
97        android_dlwarning
98        android_get_exported_namespace
99        android_get_LD_LIBRARY_PATH
100        android_init_anonymous_namespace
101        android_link_namespaces
102        android_set_application_target_sdk_version
103        android_update_LD_LIBRARY_PATH
104        __loader_android_create_namespace
105        __loader_android_dlwarning
106        __loader_android_get_exported_namespace
107        __loader_android_get_LD_LIBRARY_PATH
108        __loader_android_init_anonymous_namespace
109        __loader_android_link_namespaces
110        __loader_android_set_application_target_sdk_version
111        __loader_android_update_LD_LIBRARY_PATH
112    )
113
114    test_shared_library_symbols "${shared_library}" "${symbols}"
115}
116
117test_ld-android
118test_libdl_android
119