From c6d4b964af8a3932871ca51aa1614f4a145dc5e0 Mon Sep 17 00:00:00 2001 From: Sergey Shevchenko Date: Sat, 20 Jul 2024 11:27:46 -0700 Subject: [PATCH] #Centipede Fix OSS build error due to using Google-only code PiperOrigin-RevId: 654334593 --- centipede/test_util.sh | 16 ++++++++++++++++ centipede/testing/BUILD | 1 - centipede/testing/external_target_test.sh | 9 +++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/centipede/test_util.sh b/centipede/test_util.sh index 713099efa..a506539fb 100644 --- a/centipede/test_util.sh +++ b/centipede/test_util.sh @@ -154,3 +154,19 @@ function centipede::assert_fuzzing_success() { centipede::assert_regex_in_file "centipede.*end-fuzz:" "${log}" done } + +# Returns a random free port on the local machine. +function centipede::get_random_free_port() { + # Create an array with all ports in the range [1..65535] + all ports + # currently in use. This results in used ports appearing twice in the array. + declare -a ports=( + {1..65535} + $(netstat -tan | perl -ne '/.+?:(\d{1,5}).+/; print "$1\n";') + ) + # Dedupe and shuffle the array. This leaves free ports only. + ports=( + $(echo "${ports[@]}" | tr ' ' '\n' | sort | uniq -u | shuf) + ) + # Return a random free port. + echo "${ports[0]}" +} diff --git a/centipede/testing/BUILD b/centipede/testing/BUILD index e1ec4a053..991412da8 100644 --- a/centipede/testing/BUILD +++ b/centipede/testing/BUILD @@ -408,5 +408,4 @@ sh_test( "@com_google_fuzztest//centipede", "@com_google_fuzztest//centipede:test_util_sh", ], - deps = ["//testing/shbase"], ) diff --git a/centipede/testing/external_target_test.sh b/centipede/testing/external_target_test.sh index edee6cdf8..396763ae5 100755 --- a/centipede/testing/external_target_test.sh +++ b/centipede/testing/external_target_test.sh @@ -16,20 +16,16 @@ set -euo pipefail -source googletest.sh source "$(dirname "$0")/../test_util.sh" CENTIPEDE_TEST_SRCDIR="$(centipede::get_centipede_test_srcdir)" centipede::maybe_set_var_to_executable_path \ CENTIPEDE_BINARY "${CENTIPEDE_TEST_SRCDIR}/centipede" - centipede::maybe_set_var_to_executable_path \ LLVM_SYMBOLIZER "$(centipede::get_llvm_symbolizer_path)" - centipede::maybe_set_var_to_executable_path \ SERVER_BINARY "${CENTIPEDE_TEST_SRCDIR}/testing/external_target_server" - centipede::maybe_set_var_to_executable_path \ TARGET_BINARY "${CENTIPEDE_TEST_SRCDIR}/testing/external_target" @@ -37,9 +33,10 @@ readonly WD="${TEST_TMPDIR}/WD" readonly LOG="${TEST_TMPDIR}/log" centipede::ensure_empty_dir "${WD}" -readonly TARGET_PORT="$(get_port_from_portserver)" +TARGET_PORT=$(centipede::get_random_free_port) +readonly TARGET_PORT -echo "Starting the server binary ..." +echo "Starting the server binary using port ${TARGET_PORT}..." env CENTIPEDE_RUNNER_FLAGS=":use_auto_dictionary:use_cmp_features:use_pc_features:" \ TARGET_PORT="${TARGET_PORT}" \ "${SERVER_BINARY}" &