| 1 | #!/usr/bin/env bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | # |
| 15 | |
| 16 | # This script compiles s2n-tls as a shared library and compiles a test |
| 17 | # without linking to the library. This enables us to test behavior when |
| 18 | # s2n-tls is dynamically loaded. |
| 19 | |
| 20 | WORK_DIR=$1 |
| 21 | |
| 22 | if [ ! -z "$NIX_STORE" ]; then |
| 23 | OPENSSL=$(which openssl) |
| 24 | LIBCRYPTO_ROOT=$(nix-store --query $OPENSSL) |
| 25 | else |
| 26 | source codebuild/bin/s2n_setup_env.sh |
| 27 | fi |
| 28 | |
| 29 | S2N_BUILD_ARGS=(-H. -DCMAKE_PREFIX_PATH=$LIBCRYPTO_ROOT -DBUILD_TESTING=OFF) |
| 30 | |
| 31 | # create installation dir with libs2n.so |
| 32 | if [ ! -d $WORK_DIR/s2n-install-shared ]; then |
| 33 | (set -x; cmake -B$WORK_DIR/s2n-build-shared -DCMAKE_INSTALL_PREFIX=$WORK_DIR/s2n-install-shared -DBUILD_SHARED_LIBS=ON ${S2N_BUILD_ARGS[@]}) |
| 34 | (set -x; cmake --build $WORK_DIR/s2n-build-shared --target install -- -j $(nproc)) |
| 35 | fi |
| 36 | |
| 37 | # Compile the test file |
| 38 | $CC -Wl,-rpath $LIBCRYPTO_ROOT -o s2n_dynamic_load_test codebuild/bin/s2n_dynamic_load_test.c -ldl -lpthread |
| 39 | |
| 40 | LDD_OUTPUT=$(ldd s2n_dynamic_load_test) |
| 41 | |
| 42 | # Confirm executable doesn't have libs2n.so loaded |
| 43 | if echo "$LDD_OUTPUT" | grep -q libs2n; then |
| 44 | echo "test failure: libs2n should not appear in ldd output" |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | # Run the test with the path to libs2n |
| 49 | echo "Running s2n_dynamic_load_test" |
| 50 | LD_LIBRARY_PATH=$LIBCRYPTO_ROOT/lib ./s2n_dynamic_load_test $WORK_DIR/s2n-install-shared/lib/libs2n.so |
| 51 | returncode=$? |
| 52 | if [ $returncode -ne 0 ]; then |
| 53 | echo "test failure: s2n_dynamic_load_test did not succeed" |
| 54 | exit 1 |
| 55 | fi |
| 56 | echo "Passed s2n_dynamic_load_test" |