| 1 | echo nix/shell.sh: Entering a devShell |
| 2 | export SRC_ROOT=$(pwd) |
| 3 | export PATH=$SRC_ROOT/build/bin:$PATH |
| 4 | |
| 5 | # The GnuTlS config may restrict TLS versions in some environments, |
| 6 | # so do not use any existing config file. |
| 7 | export GNUTLS_SYSTEM_PRIORITY_FILE= |
| 8 | |
| 9 | # Function to create aliases for different libcrypto implementations |
| 10 | function libcrypto_alias { |
| 11 | local libcrypto_name=$1 |
| 12 | local libcrypto_binary_path=$2 |
| 13 | # Check both bin and lib directories for the binary |
| 14 | local binary_dir=$(dirname $libcrypto_binary_path) |
| 15 | local install_dir=$(dirname $binary_dir) |
| 16 | local lib_binary_path="$install_dir/lib/$(basename $libcrypto_binary_path)" |
| 17 | |
| 18 | if [[ -f $libcrypto_binary_path ]]; then |
| 19 | alias $libcrypto_name=$libcrypto_binary_path |
| 20 | echo "Libcrypto binary $libcrypto_binary_path available as $libcrypto_name" |
| 21 | elif [[ -f $lib_binary_path ]]; then |
| 22 | alias $libcrypto_name=$lib_binary_path |
| 23 | echo "Libcrypto binary $lib_binary_path available as $libcrypto_name" |
| 24 | else |
| 25 | echo "Could not find libcrypto binary for $libcrypto_name" |
| 26 | fi |
| 27 | } |
| 28 | libcrypto_alias openssl102 "${OPENSSL_1_0_2_INSTALL_DIR}/bin/openssl" |
| 29 | libcrypto_alias openssl111 "${OPENSSL_1_1_1_INSTALL_DIR}/bin/openssl" |
| 30 | libcrypto_alias openssl30 "${OPENSSL_3_0_INSTALL_DIR}/bin/openssl" |
| 31 | libcrypto_alias awslc "${AWSLC_INSTALL_DIR}/bin/bssl" |
| 32 | libcrypto_alias awslcfips2022 "${AWSLC_FIPS_2022_INSTALL_DIR}/bin/bssl" |
| 33 | libcrypto_alias awslcfips2024 "${AWSLC_FIPS_2024_INSTALL_DIR}/bin/bssl" |
| 34 | libcrypto_alias libressl "${LIBRESSL_INSTALL_DIR}/bin/openssl" |
| 35 | # No need to alias gnutls because it is included in common_packages (see flake.nix). |
| 36 | |
| 37 | function clean {(set -e |
| 38 | echo "Cleaning up build, s2n_head and the apache2 configs" |
| 39 | rm -rf ./build ./s2n_head |
| 40 | if [ -d "/usr/local/apache2" ]; then |
| 41 | rm -rf /usr/local/apache2 |
| 42 | fi |
| 43 | )} |
| 44 | |
| 45 | function configure {(set -e |
| 46 | echo "Configuring with cmake" |
| 47 | cmake -S . -B./build \ |
| 48 | -DBUILD_TESTING=ON \ |
| 49 | -DS2N_INTEG_TESTS=ON \ |
| 50 | -DS2N_INSTALL_S2NC_S2ND=ON \ |
| 51 | -DS2N_INTEG_NIX=ON \ |
| 52 | -DBUILD_SHARED_LIBS=ON \ |
| 53 | -DCMAKE_C_COMPILER="$CC" \ |
| 54 | -DCMAKE_CXX_COMPILER="$CXX" \ |
| 55 | -DS2N_ENFORCE_PROPER_LIBCRYPTO_FEATURE_PROBE=ON \ |
| 56 | "$S2N_CMAKE_OPTIONS" \ |
| 57 | -DCMAKE_BUILD_TYPE=RelWithDebInfo |
| 58 | )} |
| 59 | |
| 60 | function build {(set -e |
| 61 | echo "Running Build" |
| 62 | javac tests/integrationv2/bin/SSLSocketClient.java |
| 63 | cmake --build ./build -j $(nproc) |
| 64 | # Build s2n from HEAD |
| 65 | if [[ -z "${S2N_KTLS_TESTING_EXPECTED}" && -z "${S2N_NO_HEADBUILD}" ]]; then |
| 66 | $SRC_ROOT/codebuild/bin/install_s2n_head.sh $(mktemp -d) |
| 67 | fi |
| 68 | )} |
| 69 | |
| 70 | function unit {(set -e |
| 71 | if [[ -z "$1" ]]; then |
| 72 | cmake --build build -j $(nproc) |
| 73 | ctest --test-dir build -L unit -j $(nproc) --verbose |
| 74 | else |
| 75 | tests=$(ctest --test-dir build -N -L unit | grep -E "Test +#" | grep -Eo "[^ ]+_test$" | grep "$1") |
| 76 | # Split the tests string into words |
| 77 | for test in $tests |
| 78 | do |
| 79 | cmake --build build -j $(nproc) --target $test |
| 80 | done |
| 81 | ctest --test-dir build -L unit -R "$1" -j $(nproc) --verbose |
| 82 | fi |
| 83 | )} |
| 84 | |
| 85 | # Function to launch pytest with uv. |
| 86 | function uvinteg {( |
| 87 | set -eu |
| 88 | TESTS="${1:-all}" |
| 89 | apache2_start |
| 90 | PYTEST_ARGS="--provider-version $S2N_LIBCRYPTO -x -n auto --durations=10 -rpfs" |
| 91 | cd ./tests/integrationv2 |
| 92 | echo -n "Comparing the current list of integ tests against what is checked-in..." |
| 93 | PYTHONPATH="" uv run pytest --collect-only $PYTEST_ARGS | grep Module > /tmp/uvinteg_tests.txt |
| 94 | diff -q /tmp/uvinteg_tests.txt uvinteg_tests.txt |
| 95 | echo "tests to run match uvinteg_tests.txt." |
| 96 | if [[ "$TESTS" == "all" ]]; then |
| 97 | echo "Running all integ tests with uv" |
| 98 | # If the first attempt has any failures, try rerunning with only the failed tests. |
| 99 | PYTHONPATH="" uv run pytest --cache-clear $PYTEST_ARGS --junitxml=../../build/junit/uv_integ.xml || \ |
| 100 | PYTHONPATH="" uv run pytest --last-failed --last-failed-no-failures none $PYTEST_ARGS --junitxml=../../build/junit/uv_integ_retry.xml |
| 101 | else |
| 102 | # We're clearing state between test runs when individual tests are specified. |
| 103 | for test in "$@"; do |
| 104 | PYTHONPATH="" uv run pytest --cache-clear $PYTEST_ARGS --junitxml=../../build/junit/$test.xml -k $test |
| 105 | done |
| 106 | fi |
| 107 | )} |
| 108 | |
| 109 | # Wrap a command with stress to simulate a high-load environment. |
| 110 | # Not intended for CI, but local troubleshooting. |
| 111 | function highstress {( |
| 112 | set -e |
| 113 | local STRESSARGS="--cpu $(nproc) --io $(nproc) --quiet" |
| 114 | echo "Running: stress $STRESSARGS" |
| 115 | stress $STRESSARGS & |
| 116 | trap 'pkill stress' ERR EXIT |
| 117 | "$@" |
| 118 | )} |
| 119 | |
| 120 | function check-clang-format {(set -e |
| 121 | echo "Dry run of clang-format" |
| 122 | (cd $SRC_ROOT; |
| 123 | include_regex=".*\.(c|h)$"; |
| 124 | src_files=`find ./api -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 125 | src_files+=" "; |
| 126 | src_files+=`find ./bin -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 127 | src_files+=" "; |
| 128 | src_files+=`find ./crypto -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 129 | src_files+=" "; |
| 130 | src_files+=`find ./stuffer -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 131 | src_files+=" "; |
| 132 | src_files+=`find ./error -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 133 | src_files+=" "; |
| 134 | src_files+=`find ./tls -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 135 | src_files+=" "; |
| 136 | src_files+=`find ./utils -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 137 | src_files+=" "; |
| 138 | src_files+=`find ./tests/unit -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 139 | src_files+=" "; |
| 140 | src_files+=`find ./tests/testlib -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 141 | echo $src_files | xargs -n 1 -P $(nproc) clang-format --dry-run -style=file) |
| 142 | )} |
| 143 | |
| 144 | function do-clang-format {(set -e |
| 145 | echo "In place clang-format" |
| 146 | (cd $SRC_ROOT; |
| 147 | include_regex=".*\.(c|h)$"; |
| 148 | src_files=`find ./api -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 149 | src_files+=" "; |
| 150 | src_files+=`find ./bin -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 151 | src_files+=" "; |
| 152 | src_files+=`find ./crypto -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 153 | src_files+=" "; |
| 154 | src_files+=`find ./stuffer -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 155 | src_files+=" "; |
| 156 | src_files+=`find ./error -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 157 | src_files+=" "; |
| 158 | src_files+=`find ./tls -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 159 | src_files+=" "; |
| 160 | src_files+=`find ./utils -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 161 | src_files+=" "; |
| 162 | src_files+=`find ./tests/unit -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 163 | src_files+=" "; |
| 164 | src_files+=`find ./tests/testlib -name .git -prune -o -regextype posix-egrep -regex "$include_regex" -print`; |
| 165 | echo $src_files | xargs -n 1 -P $(nproc) clang-format -style=file -i) |
| 166 | )} |
| 167 | |
| 168 | function test_toolchain_counts {(set -e |
| 169 | # This is a starting point for a unit test of the devShell. |
| 170 | # The chosen S2N_LIBCRYPTO should be 2, and the others should be zero. |
| 171 | echo "Checking the CMAKE_INCLUDE_PATH for libcrypto counts" |
| 172 | echo $CMAKE_INCLUDE_PATH|gawk 'BEGIN{RS=":"; o10=0; o11=0; o3=0;awslc=0;libre=0} |
| 173 | /openssl-3.0/{o3++} |
| 174 | /openssl-1.1/{o11++} |
| 175 | /openssl-1.0/{o10++} |
| 176 | /aws-lc/{awslc++} |
| 177 | /libressl/{libre++} |
| 178 | END{print "\nOpenssl3:\t",o3,"\nOpenssl1.1:\t",o11,"\nOpenssl1.0.2:\t",o10,"\nAws-lc:\t\t",awslc,"\nLibreSSL:\t", libre}' |
| 179 | echo "Checking tooling counts (these should all be 1)" |
| 180 | echo -e "\nOpenssl integ:\t $(openssl version|grep -c '1.1.1')" |
| 181 | echo -e "Corretto 17:\t $(java -version 2>&1|grep -ce 'Runtime.*Corretto-17')" |
| 182 | echo -e "gnutls-cli:\t $(gnutls-cli --version |grep -c 'gnutls-cli 3.7')" |
| 183 | echo -e "gnutls-serv:\t $(gnutls-serv --version |grep -c 'gnutls-serv 3.7')" |
| 184 | echo -e "Nix Python:\t $(which python|grep -c '/nix/store')" |
| 185 | echo -e "Nix pytest:\t $(which pytest|grep -c '/nix/store')" |
| 186 | echo -e "Nix sslyze:\t $(which sslyze|grep -c '/nix/store')" |
| 187 | echo -e "python nassl:\t $(pip freeze|grep -c 'nassl')" |
| 188 | echo -e "valgrind:\t $(valgrind --version|grep -c 'valgrind-3.19.0')" |
| 189 | )} |
| 190 | |
| 191 | function test_nonstandard_compilation {(set -e |
| 192 | # Any script that needs to compile s2n in a non-standard way can run here |
| 193 | ./codebuild/bin/test_dynamic_load.sh $(mktemp -d) |
| 194 | )} |
| 195 | |
| 196 | function apache2_config(){ |
| 197 | export APACHE_NIX_STORE=$(dirname $(dirname $(which httpd))) |
| 198 | export APACHE2_INSTALL_DIR=/usr/local/apache2 |
| 199 | export APACHE_SERVER_ROOT="$APACHE2_INSTALL_DIR" |
| 200 | export APACHE_RUN_USER=nobody |
| 201 | # Unprivileged groupname differs |
| 202 | export APACHE_RUN_GROUP=$(awk 'BEGIN{FS=":"} /65534/{print $1}' /etc/group) |
| 203 | export APACHE_PID_FILE="${APACHE2_INSTALL_DIR}/run/apache2.pid" |
| 204 | export APACHE_RUN_DIR="${APACHE2_INSTALL_DIR}/run" |
| 205 | export APACHE_LOCK_DIR="${APACHE2_INSTALL_DIR}/lock" |
| 206 | export APACHE_LOG_DIR="${APACHE2_INSTALL_DIR}/log" |
| 207 | export APACHE_CERT_DIR="$SRC_ROOT/tests/pems" |
| 208 | } |
| 209 | |
| 210 | function apache2_start(){ |
| 211 | if [[ "$(pgrep -c httpd)" -eq "0" ]]; then |
| 212 | apache2_config |
| 213 | if [[ ! -f "$APACHE2_INSTALL_DIR/conf/apache2.conf" ]]; then |
| 214 | mkdir -p $APACHE2_INSTALL_DIR/{run,log,lock} |
| 215 | # NixOs specific base apache config |
| 216 | cp -R ./tests/integrationv2/apache2/nix/* $APACHE2_INSTALL_DIR |
| 217 | # Integrationv2::renegotiate site |
| 218 | cp -R ./codebuild/bin/apache2/{www,sites-enabled} $APACHE2_INSTALL_DIR |
| 219 | fi |
| 220 | httpd -k start -f "${APACHE2_INSTALL_DIR}/conf/apache2.conf" |
| 221 | trap 'pkill httpd' ERR EXIT |
| 222 | else |
| 223 | echo "Apache is already running. If \"$APACHE2_INSTALL_DIR\" is stale, it might be in an unknown state." |
| 224 | fi |
| 225 | } |
| 226 | |
| 227 | function rust_configure {(set -e |
| 228 | echo "rust_configure: Cleaning previous build" |
| 229 | rm -rf build/ |
| 230 | echo "rust_configure: Configuring with CMake (RelWithDebInfo, intern libcrypto)" |
| 231 | cmake -B build . \ |
| 232 | -D CMAKE_C_COMPILER=clang \ |
| 233 | -D CMAKE_BUILD_TYPE=RelWithDebInfo \ |
| 234 | -D BUILD_TESTING=OFF \ |
| 235 | -D S2N_INTERN_LIBCRYPTO=ON |
| 236 | )} |
| 237 | |
| 238 | function rust_build {(set -e |
| 239 | echo "rust_build: Building s2n-tls" |
| 240 | cmake --build ./build -j $(nproc) |
| 241 | echo "rust_build: Generating Rust bindings (bindgen)" |
| 242 | bindings/rust/extended/generate.sh --skip-tests |
| 243 | )} |
| 244 | |
| 245 | function rust_test {(set -e |
| 246 | echo "rust_test: Setting up local Rust toolchain (rustup stable)" |
| 247 | export RUSTUP_HOME=$(pwd)/.rustup |
| 248 | export CARGO_HOME=$(pwd)/.cargo |
| 249 | export PATH=$(pwd)/.cargo/bin:$PATH |
| 250 | echo "rust_test: Exporting s2n-tls headers and libs for Cargo" |
| 251 | export S2N_TLS_LIB_DIR=$(pwd)/build/lib |
| 252 | export S2N_TLS_INCLUDE_DIR=$(pwd)/api |
| 253 | echo "rust_test: Running Rust integration tests" |
| 254 | cargo test --manifest-path bindings/rust/standard/integration/Cargo.toml --features boringssl |
| 255 | )} |