1#!/bin/sh
2
3# Copyright (C) 2016-2021 Internet Systems Consortium, Inc. ("ISC")
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9# shellcheck disable=SC1091
10# SC1091: Not following: ... was not specified as input (see shellcheck -x).
11
12# shellcheck disable=SC2039
13# SC2039: In POSIX sh, 'local' is undefined.
14
15# Exit with error if commands exit with non-zero and if undefined variables are
16# used.
17set -eu
18
19# Include common test library.
20. "@abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh"
21
22# Path to the temporary configuration file.
23CFG_FILE="@abs_top_builddir@/src/bin/agent/tests/test_config.json"
24# Path to the Control Agent log file.
25LOG_FILE="@abs_top_builddir@/src/bin/agent/tests/test.log"
26
27# Control Agent configuration to be stored in the configuration file.
28CONFIG="{
29    \"Control-agent\":
30    {
31        \"http-host\": \"127.0.0.1\",
32        \"loggers\": [
33        {
34            \"name\": \"kea-ctrl-agent\",
35            \"output_options\": [
36                {
37                    \"output\": \"$LOG_FILE\"
38                }
39            ],
40            \"severity\": \"DEBUG\"
41        }
42        ]
43    }
44}"
45
46# Invalid configuration (syntax error) to check that Kea can check syntax.
47CONFIG_BAD_SYNTAX="{
48    \"Control-agent\":
49    {
50        \"http-port\": BOGUS
51    }
52}"
53
54# Invalid configuration (out of range port) to check that Kea can check syntax.
55CONFIG_BAD_VALUE="{
56    \"Control-agent\":
57    {
58        \"http-port\": 80000
59    }
60}"
61
62# Configuration with a password.
63CONFIG_PWD="{
64    \"Control-agent\":
65    {
66        \"http-host\": \"127.0.0.1\",
67        \"authentication\":
68        {
69            \"clients\": [
70                {
71                    \"password\": \"sensitive\",
72                    \"user\": \"superadmin\"
73                }
74            ],
75            \"type\": \"basic\"
76        }
77    }
78}"
79
80bin="kea-ctrl-agent"
81bin_path="@abs_top_builddir@/src/bin/agent"
82
83# Import common test library.
84. "@abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh"
85
86# This test verifies that syntax checking works properly. This function
87# requires 3 parameters:
88# test_name
89# config - string with a content of the config (will be written to a file)
90# expected_code - expected exit code returned by kea (0 - success, 1 - failure)
91syntax_check_test() {
92    local test_name="${1}"
93    local config="${2}"
94    local expected_code="${3}"
95
96    # Log the start of the test and print test name.
97    test_start "${test_name}"
98    # Create correct configuration file.
99    create_config "${config}"
100    # Check it
101    printf 'Running command %s.\n' "\"${bin_path}/${bin} -t ${CFG_FILE}\""
102    run_command \
103        "${bin_path}/${bin}" -t "${CFG_FILE}"
104    if [ "${EXIT_CODE}" -ne "${expected_code}" ]; then
105        printf 'ERROR: expected exit code %s, got %s\n' "${expected_code}" "${EXIT_CODE}"
106        clean_exit 1
107    fi
108    test_finish 0
109}
110
111# This test verifies that Control Agent is shut down gracefully when it
112# receives a SIGINT or SIGTERM signal.
113shutdown_test() {
114    test_name=${1}  # Test name
115    signum=${2}      # Signal number
116    # Log the start of the test and print test name.
117    test_start "${test_name}"
118    # Create new configuration file.
119    create_config "${CONFIG}"
120    # Instruct Control Agent to log to the specific file.
121    set_logger
122    # Start Control Agent.
123    start_kea ${bin_path}/${bin}
124    # Wait up to 20s for Control Agent to start.
125    wait_for_kea 20
126    if [ "${_WAIT_FOR_KEA}" -eq 0 ]; then
127        printf "ERROR: timeout waiting for Control Agent to start.\n"
128        clean_exit 1
129    fi
130
131    # Check if it is still running. It could have terminated (e.g. as a result
132    # of configuration failure).
133    get_pid ${bin}
134    if [ "${_GET_PIDS_NUM}" -ne 1 ]; then
135        printf "ERROR: expected one Control Agent process to be started. Found %d processes\
136 started.\n" "${_GET_PIDS_NUM}"
137        clean_exit 1
138    fi
139
140    # Check in the log file, how many times server has been configured.
141    # It should be just once on startup.
142    get_reconfigs
143    if [ "${_GET_RECONFIGS}" -ne 1 ]; then
144        printf 'ERROR: server been configured %s time(s), but exactly 1 was expected.\n' "${_GET_RECONFIGS}"
145        clean_exit 1
146    else
147        printf "Server successfully configured.\n"
148    fi
149
150    # Send signal to Control Agent (SIGTERM, SIGINT etc.)
151    send_signal "${signum}" "${bin}"
152
153    # Now wait for process to log that it is exiting.
154    wait_for_message 10 "DCTL_SHUTDOWN" 1
155    if [ "${_WAIT_FOR_MESSAGE}" -eq 0 ]; then
156        printf "ERROR: Control Agent did not log shutdown.\n"
157        clean_exit 1
158    fi
159
160    # Make sure the server is down.
161    wait_for_server_down 5 ${bin}
162    assert_eq 1 "${_WAIT_FOR_SERVER_DOWN}" \
163        "Expected wait_for_server_down return %d, returned %d"
164
165    test_finish 0
166}
167
168server_pid_file_test "${CONFIG}" DCTL_ALREADY_RUNNING
169shutdown_test "ctrl-agent.sigterm_test" 15
170shutdown_test "ctrl-agent.sigint_test" 2
171syntax_check_test "ctrl-agent.syntax_check_success" "${CONFIG}" 0
172syntax_check_test "ctrl-agent.syntax_check_bad_syntax" "${CONFIG_BAD_SYNTAX}" 1
173syntax_check_test "ctrl-agent.syntax_check_bad_values" "${CONFIG_BAD_VALUE}" 1
174password_redact_test "ctrl-agent.password_redact_test" "${CONFIG_PWD}" 0
175