<commit_before>#!/bin/bash
# Simple integration test. Make sure to activate virtualenv beforehand
# (source venv/bin/activate) and that you are running Boulder test
# instance (see ./boulder-fetch.sh).
#
# Environment variables:
#   SERVER: Passed as "certbot --server" argument.
#
# Note: this script is called by Boulder integration test suite!

set -eux

. ./tests/integration/_common.sh
export PATH="$PATH:/usr/sbin"  # /usr/sbin/nginx

cleanup_and_exit() {
    EXIT_STATUS=$?
    if SERVER_STILL_RUNNING=`ps -p $python_server_pid -o pid=`
    then
        echo Kill server subprocess, left running by abnormal exit
        kill $SERVER_STILL_RUNNING
    fi
    # Dump boulder logs in case they contain useful debugging information.
    : "------------------ ------------------ ------------------"
    : "------------------ begin boulder logs ------------------"
    : "------------------ ------------------ ------------------"
    docker logs boulder_boulder_1
    : "------------------ ------------------ ------------------"
    : "------------------  end boulder logs  ------------------"
    : "------------------ ------------------ ------------------"
    exit $EXIT_STATUS
}

trap cleanup_and_exit EXIT

common_no_force_renew() {
    certbot_test_no_force_renew \
        --authenticator standalone \
        --installer null \
        "$@"
}

common() {
    common_no_force_renew \
        --renew-by-default \
        "$@"
}

export HOOK_TEST="/tmp/hook$$"
CheckHooks() {
    EXPECTED="/tmp/expected$$"
    if [ $(head -n1 $HOOK_TEST) = "wtf.pre" ]; then
        echo "wtf.pre" > "$EXPECTED"
        echo "wtf2.pre" >> "$EXPECTED"
        echo "renew" >> "$EXPECTED"
        echo "renew" >> "$EXPECTED"
        echo "wtf.post" >> "$EXPECTED"
        echo "wtf2.post" >> "$EXPECTED"
    else
        echo "wtf2.pre" > "$EXPECTED"
        echo "wtf.pre" >> "$EXPECTED"
        echo "renew" >> "$EXPECTED"
        echo "renew" >> "$EXPECTED"
        echo "wtf2.post" >> "$EXPECTED"
        echo "wtf.post" >> "$EXPECTED"
    fi

    if ! cmp --quiet "$EXPECTED" "$HOOK_TEST" ; then
        echo Hooks did not run as expected\; got
        cat "$HOOK_TEST"
        echo Expected
        cat "$EXPECTED"
    fi
    rm "$HOOK_TEST"
}

# Cleanup coverage data
coverage erase

# test for regressions of #4719
get_num_tmp_files() {
    ls -1 /tmp | wc -l
}
num_tmp_files=$(get_num_tmp_files)
common --csr / && echo expected error && exit 1 || true
common --help
common --help all
common --version
if [ $(get_num_tmp_files) -ne $num_tmp_files ]; then
    echo "New files or directories created in /tmp!"
    exit 1
fi

common register
common register --update-registration --email <EMAIL>

common plugins --init --prepare | grep webroot

# We start a server listening on the port for the
# unrequested challenge to prevent regressions in #3601.
python ./tests/run_http_server.py $http_01_port &
python_server_pid=$!

common --domains le1.wtf --preferred-challenges tls-sni-01 auth \
       --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \
       --post-hook 'echo wtf.post >> "$HOOK_TEST"'\
       --renew-hook 'echo renew >> "$HOOK_TEST"'
kill $python_server_pid
python ./tests/run_http_server.py $tls_sni_01_port &
python_server_pid=$!
common --domains le2.wtf --preferred-challenges http-01 run \
       --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \
       --post-hook 'echo wtf.post >> "$HOOK_TEST"'\
       --renew-hook 'echo renew >> "$HOOK_TEST"'
kill $python_server_pid

common certonly -a manual -d le.wtf --rsa-key-size 4096 \
    --manual-auth-hook ./tests/manual-http-auth.sh \
    --manual-cleanup-hook ./tests/manual-http-cleanup.sh \
    --pre-hook 'echo wtf2.pre >> "$HOOK_TEST"' \
    --post-hook 'echo wtf2.post >> "$HOOK_TEST"'

common certonly -a manual -d dns.le.wtf --preferred-challenges dns,tls-sni \
    --manual-auth-hook ./tests/manual-dns-auth.sh

common certonly --cert-name newname -d newname.le.wtf

export CSR_PATH="${root}/csr.der" KEY_PATH="${root}/key.pem" \
       OPENSSL_CNF=examples/openssl.cnf
./examples/generate-csr.sh le3.wtf
common auth --csr "$CSR_PATH" \
       --cert-path "${root}/csr/cert.pem" \
       --chain-path "${root}/csr/chain.pem"
openssl x509 -in "${root}/csr/cert.pem" -text<commit_msg>Test renew and deploy hooks are run properly<commit_after>#!/bin/bash
# Simple integration test. Make sure to activate virtualenv beforehand
# (source venv/bin/activate) and that you are running Boulder test
# instance (see ./boulder-fetch.sh).
#
# Environment variables:
#   SERVER: Passed as "certbot --server" argument.
#
# Note: this script is called by Boulder integration test suite!

set -eux

. ./tests/integration/_common.sh
export PATH="$PATH:/usr/sbin"  # /usr/sbin/nginx

cleanup_and_exit() {
    EXIT_STATUS=$?
    if SERVER_STILL_RUNNING=`ps -p $python_server_pid -o pid=`
    then
        echo Kill server subprocess, left running by abnormal exit
        kill $SERVER_STILL_RUNNING
    fi
    # Dump boulder logs in case they contain useful debugging information.
    : "------------------ ------------------ ------------------"
    : "------------------ begin boulder logs ------------------"
    : "------------------ ------------------ ------------------"
    docker logs boulder_boulder_1
    : "------------------ ------------------ ------------------"
    : "------------------  end boulder logs  ------------------"
    : "------------------ ------------------ ------------------"
    exit $EXIT_STATUS
}

trap cleanup_and_exit EXIT

common_no_force_renew() {
    certbot_test_no_force_renew \
        --authenticator standalone \
        --installer null \
        "$@"
}

common() {
    common_no_force_renew \
        --renew-by-default \
        "$@"
}

export HOOK_TEST="/tmp/hook$$"
CheckHooks() {
    EXPECTED="/tmp/expected$$"
    if [ $(head -n1 $HOOK_TEST) = "wtf.pre" ]; then
        echo "wtf.pre" > "$EXPECTED"
        echo "wtf2.pre" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "wtf.post" >> "$EXPECTED"
        echo "wtf2.post" >> "$EXPECTED"
    else
        echo "wtf2.pre" > "$EXPECTED"
        echo "wtf.pre" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "deploy" >> "$EXPECTED"
        echo "wtf2.post" >> "$EXPECTED"
        echo "wtf.post" >> "$EXPECTED"
    fi

    if ! cmp --quiet "$EXPECTED" "$HOOK_TEST" ; then
        echo Hooks did not run as expected\; got
        cat "$HOOK_TEST"
        echo Expected
        cat "$EXPECTED"
    fi
    rm "$HOOK_TEST"
}

# Checks if deploy was run and deletes the hook file
CheckDeployHook() {
    CONTENTS=$(cat "$HOOK_TEST")
    rm "$HOOK_TEST"
    grep deploy <(echo "$CONTENTS")
}

# Asserts the deploy hook was run and deletes the hook file
AssertDeployHook() {
    if ! CheckDeployHook; then
        echo "The deploy hook wasn't run" >&2
    fi
}

# Asserts the deploy hook wasn't run and deletes the hook file
AssertNoDeployHook() {
    if CheckDeployHook; then
        echo "The deploy hook was incorrectly run" >&2
    fi
}

# Cleanup coverage data
coverage erase

# test for regressions of #4719
get_num_tmp_files() {
    ls -1 /tmp | wc -l
}
num_tmp_files=$(get_num_tmp_files)
common --csr / && echo expected error && exit 1 || true
common --help
common --help all
common --version
if [ $(get_num_tmp_files) -ne $num_tmp_files ]; then
    echo "New files or directories created in /tmp!"
    exit 1
fi

common register
common register --update-registration --email <EMAIL>

common plugins --init --prepare | grep webroot

# We start a server listening on the port for the
# unrequested challenge to prevent regressions in #3601.
python ./tests/run_http_server.py $http_01_port &
python_server_pid=$!

common --domains le1.wtf --preferred-challenges tls-sni-01 auth \
       --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \
       --post-hook 'echo wtf.post >> "$HOOK_TEST"'\
       --deploy-hook 'echo deploy >> "$HOOK_TEST"'
kill $python_server_pid
AssertDeployHook

python ./tests/run_http_server.py $tls_sni_01_port &
python_server_pid=$!
common --domains le2.wtf --preferred-challenges http-01 run \
       --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \
       --post-hook 'echo wtf.post >> "$HOOK_TEST"'\
       --deploy-hook 'echo deploy >> "$HOOK_TEST"'
kill $python_server_pid
AssertDeployHook

common certonly -a manual -d le.wtf --rsa-key-size 4096 \
    --manual-auth-hook ./tests/manual-http-auth.sh \
    --manual-cleanup-hook ./tests/manual-http-cleanup.sh \
    --pre-hook 'echo wtf2.pre >> "$HOOK_TEST"' \
    --post-hook 'echo wtf2.post >> "$HOOK_TEST"' \
    --renew-hook 'echo deploy >> "$HOOK_TEST"'
AssertNoDeployHook

common -a manual -d dns.le.wtf --preferred-challenges dns,tls-sni run \
    --manual-auth-hook ./tests/manual-dns-auth.sh \
    --pre-hook 'echo wtf2.pre >> "$HOOK_TEST"' \
    --post-hook 'echo wtf2.post >> "$HOOK_TEST"' \
    --renew-hook 'echo deploy >> "$HOOK_TEST"'
AssertNoDeployHook

common certonly --cert-name newname -d newname.le.wtf

export CSR_PATH="${root}/csr.der" KEY_PATH="${root}/key.pem" \
       OPENSSL_CNF=examples/openssl.cnf
./examples/generate-csr.sh le3.wtf
common auth --csr "$CSR_PATH" \
       --cert-path "${root}/csr/cert.pem" \
       --chain-path "${root}/csr/chain.pem"
openssl x509 -in "${root}/csr/cert.pem" -text
{'fraction_non_alphanumeric': 0.19369178662150718, 'fraction_numerical': 0.00825571549534293, 'mean_word_length': 2.1879217273954117, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7193941', 'n_tokens_mistral': 3252, 'n_tokens_neox': 3109, 'n_words': 808}