#!/bin/bash # # Copyright 2015 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -eu case "${PLATFORM}" in darwin|freebsd) function nc_l() { nc -l $1 } ;; *) function nc_l() { nc -l -p $1 -q 1 } ;; esac # Serves $1 as a file on localhost:$nc_port. Sets the following variables: # * nc_port - the port nc is listening on. # * nc_log - the path to nc's log. # * nc_pid - the PID of nc. # * http_response - the full response nc will provide to a request. # This also creates the file $TEST_TMPDIR/http_response. function serve_file() { http_response=$TEST_TMPDIR/http_response cat > $http_response <> $http_response # Assign random_port to nc_port if not already set. echo ${nc_port:=$(pick_random_unused_tcp_port)} > /dev/null nc_log=$TEST_TMPDIR/nc.log nc_l $nc_port < $http_response >& $nc_log & nc_pid=$! } # Creates a jar carnivore.Mongoose and serves it using serve_file. function serve_jar() { make_test_jar serve_file $test_jar cd ${WORKSPACE_DIR} } function make_test_jar() { pkg_dir=$TEST_TMPDIR/carnivore rm -fr $pkg_dir mkdir -p $pkg_dir cat > $pkg_dir/Mongoose.java < AndroidManifest.xml < EOF mkdir -p com/herbivore cat > com/herbivore/Stegosaurus.java < res/layout/my_view.xml < EOF zip -0 $test_aar AndroidManifest.xml classes.jar res/layout/my_view.xml sha256=$(sha256sum $test_aar | cut -f 1 -d ' ') # OS X doesn't have sha1sum, so use openssl. sha1=$(openssl sha1 $test_aar | cut -f 2 -d ' ') cd - } # Serves a redirection from localhost:$redirect_port to $1. Sets the following variables: # * redirect_port - the port nc is listening on. # * redirect_log - the path to nc's log. # * redirect_pid - the PID of nc. function serve_redirect() { # Assign random_port to nc_port if not already set. echo ${redirect_port:=$(pick_random_unused_tcp_port)} > /dev/null redirect_log=$TEST_TMPDIR/redirect.log local response=$(cat <& $redirect_log <<<"$response" & redirect_pid=$! } # Serves a HTTP 404 Not Found response with an optional parameter for the # response body. function serve_not_found() { RESPONSE_BODY=${1:-} http_response=$TEST_TMPDIR/http_response cat > $http_response < $maven_path/$artifact_id-$version.$packaging.sha1 mv $artifact $maven_path/$artifact_id-$version.$packaging # srcjar_artifact is not created for AARs. if [ ! -z "${srcjar_artifact+x}" ]; then openssl sha1 $srcjar_artifact > $maven_path/$artifact_id-$version-sources.$packaging.sha1 mv $srcjar_artifact $maven_path/$artifact_id-$version-sources.$packaging fi } function serve_artifact() { startup_server $PWD create_artifact $1 $2 $3 ${4:-jar} } function startup_server() { fileserver_root=$1 cd $fileserver_root fileserver_port=$(pick_random_unused_tcp_port) || exit 1 python $python_server --port=$fileserver_port & fileserver_pid=$! wait_for_server_startup cd - } function startup_auth_server() { fileserver_port=$(pick_random_unused_tcp_port) || exit 1 python $python_server --port=$fileserver_port --auth=basic & fileserver_pid=$! wait_for_server_startup } function shutdown_server() { # Try to kill nc, otherwise the test will time out if Bazel has a bug and # didn't make a request to it. [ -z "${fileserver_pid:-}" ] || kill $fileserver_pid || true [ -z "${redirect_pid:-}" ] || kill $redirect_pid || true [ -z "${nc_log:-}" ] || cat $nc_log [ -z "${redirect_log:-}" ] || cat $redirect_log } function kill_nc() { shutdown_server }