b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | PRINT_PASSED=2 |
| 4 | |
| 5 | NGINX_UTIL="/usr/bin/nginx-util" |
| 6 | |
| 7 | ORIG=".original-test-nginx-util-root" |
| 8 | |
| 9 | mkdir -p /tmp/.uci/ |
| 10 | |
| 11 | uci commit nginx || { printf "Error invoking: uci commit\n Exit."; exit 2; } |
| 12 | |
| 13 | |
| 14 | pst_exit() { |
| 15 | printf "\nExit: Recovering original settings ... " |
| 16 | |
| 17 | uci revert nginx |
| 18 | |
| 19 | cd "/etc/config/" && rm "nginx" && mv "nginx.${ORIG}" "nginx" || |
| 20 | printf "\n%s: not moved %s to %s\n" "/etc/config/" "nginx${ORIG}" "nginx" |
| 21 | |
| 22 | cd "/etc/crontabs/" && rm "root" && mv "root${ORIG}" "root" || |
| 23 | printf "\n%s: not moved %s to %s\n" "/etc/crontabs/" "root${ORIG}" "root" |
| 24 | |
| 25 | cd "$(dirname "${CONF_DIR}")" && rm -r "${CONF_DIR}" && |
| 26 | mv "$(basename "${CONF_DIR}")${ORIG}" "$(basename "${CONF_DIR}")" || |
| 27 | printf "\n%s: not moved %s to %s\n" "$(dirname "${CONF_DIR}")" \ |
| 28 | "$(basename "${CONF_DIR}")${ORIG}" "$(basename "${CONF_DIR}")" |
| 29 | |
| 30 | printf "done.\n" |
| 31 | |
| 32 | exit "$1" |
| 33 | } |
| 34 | |
| 35 | |
| 36 | mkdir -p "/etc/config/" && touch "/etc/config/nginx" |
| 37 | |
| 38 | cd "/etc/config/" && [ ! -e "nginx${ORIG}" ] && cp "nginx" "nginx.${ORIG}" || { |
| 39 | printf "\n%s: not copied %s to %s\n" "/etc/config/" "nginx" "nginx${ORIG}" |
| 40 | pst_exit 3 |
| 41 | } |
| 42 | |
| 43 | uci set nginx.global.uci_enable=1 |
| 44 | |
| 45 | |
| 46 | mkdir -p "/etc/crontabs/" && touch "/etc/crontabs/root" |
| 47 | |
| 48 | cd "/etc/crontabs/" && [ ! -e "root${ORIG}" ] && mv "root" "root${ORIG}" || { |
| 49 | printf "\n%s: not moved %s to %s\n" "/etc/crontabs/" "root${ORIG}" "root" |
| 50 | pst_exit 4 |
| 51 | } |
| 52 | |
| 53 | touch "/etc/crontabs/root" |
| 54 | |
| 55 | |
| 56 | # ---------------------------------------------------------------------------- |
| 57 | |
| 58 | __esc_newlines() { |
| 59 | echo "${1}" | sed -E 's/$/\\n/' | tr -d '\n' | sed -E 's/\\n$/\n/' |
| 60 | } |
| 61 | |
| 62 | __esc_sed_rhs() { |
| 63 | __esc_newlines "${1}" | sed -E 's/[&/\]/\\&/g' |
| 64 | } |
| 65 | |
| 66 | _sed_rhs() { |
| 67 | __esc_sed_rhs "$(echo "${1}" | sed -E "s/[$]/$(__esc_sed_rhs "${2}")/g")" |
| 68 | } |
| 69 | |
| 70 | __esc_regex() { |
| 71 | __esc_newlines "${1}" | sed -E 's/[^^_a-zA-Z0-9-]/[&]/g; s/\^/\\^/g' |
| 72 | } |
| 73 | |
| 74 | _regex() { |
| 75 | __esc_regex "${1}" | sed -E -e 's/^(\[\s])*/^\\s*/' \ |
| 76 | -e 's/(\[\s])+\[[*]]/(\\s.*)?/g' \ |
| 77 | -e 's/(\[\s])+/\\s+/g' \ |
| 78 | -e 's/(\[\s])*\[[;]]/\\s*;/g' \ |
| 79 | -e "s/\[['\"]]/['\"]?/g" \ |
| 80 | -e "s/\[[$]]/$(__esc_sed_rhs "$(__esc_regex "${2}")")/g" |
| 81 | } |
| 82 | |
| 83 | _echo_sed() { |
| 84 | echo "" | sed -E "c${1}" |
| 85 | } |
| 86 | |
| 87 | |
| 88 | fileauto="# This file is re-created when Nginx starts." |
| 89 | |
| 90 | setpoint_init_lan() { |
| 91 | echo "${fileauto}" |
| 92 | |
| 93 | sed -n -E '/^\s*#UCI_HTTP_CONFIG\s*$/q;p' "${UCI_CONF}.template" |
| 94 | |
| 95 | local rhs="\t}\n\n\tserver { #see uci show 'nginx.\1'" |
| 96 | uci -n export nginx \ |
| 97 | | sed -E -e "s/'//g" \ |
| 98 | -e '/^\s*package\s+nginx\s*$/d' \ |
| 99 | -e '/^\s*config\s+main\s/d' \ |
| 100 | -e "s/^\s*config\s+server\s+(.*)$/$rhs/g" \ |
| 101 | -e 's/^\s*list\s/\t\t/g' \ |
| 102 | -e 's/^\s*option\s/\t\t/g' \ |
| 103 | -e 's/^\s*uci_listen_locally\s+/\t\tlisten 127.0.0.1:/g' \ |
| 104 | -e '/^\s*uci_/d' \ |
| 105 | -e '/^$/d' -e "s/[^'\n]$/&;/g" \ |
| 106 | | sed "1,2d" |
| 107 | printf "\t}\n\n" |
| 108 | |
| 109 | sed -E '1,/^\s*#UCI_HTTP_CONFIG\s*$/ d' "${UCI_CONF}.template" |
| 110 | } |
| 111 | |
| 112 | |
| 113 | setpoint_add_ssl() { |
| 114 | local indent="\n$1" |
| 115 | local name="$2" |
| 116 | local default="" |
| 117 | [ "${name}" = "${LAN_NAME}" ] && default=".default" |
| 118 | local prefix="${CONF_DIR}${name}" |
| 119 | |
| 120 | local ADDS="" |
| 121 | local CONF |
| 122 | CONF="$(sed -E \ |
| 123 | -e "s/$(_regex "${NGX_INCLUDE}" "${LAN_LISTEN}${default}")/$1$(\ |
| 124 | _sed_rhs "${NGX_INCLUDE}" "${LAN_SSL_LISTEN}${default}")/g" \ |
| 125 | -e "s/^(\s*listen\s+)([^:]*:|\[[^]]*\]:)?80(\s|$|;)/\1\2443 ssl\3/g" \ |
| 126 | "${prefix}.sans" 2>/dev/null)" |
| 127 | echo "${CONF}" | grep -qE "$(_regex "${NGX_SSL_CRT}" "${prefix}")" \ |
| 128 | || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_CRT}" "${prefix}")" |
| 129 | echo "${CONF}" | grep -qE "$(_regex "${NGX_SSL_KEY}" "${prefix}")" \ |
| 130 | || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_KEY}" "${prefix}")" |
| 131 | echo "${CONF}" | grep -qE "^\s*ssl_session_cache\s" \ |
| 132 | || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_SESSION_CACHE}" "${name}")" |
| 133 | echo "${CONF}" | grep -qE "^\s*ssl_session_timeout\s" \ |
| 134 | || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_SESSION_TIMEOUT}" "")" |
| 135 | |
| 136 | if [ -n "${ADDS}" ] |
| 137 | then |
| 138 | ADDS="$(echo "${ADDS}" | sed -E 's/^\\n//')" |
| 139 | echo "${CONF}" | grep -qE "$(_regex "${NGX_SERVER_NAME}" "${name}")" \ |
| 140 | && echo "${CONF}" \ |
| 141 | | sed -E "/$(_regex "${NGX_SERVER_NAME}" "${name}")/a\\${ADDS}" \ |
| 142 | > "${prefix}.with" \ |
| 143 | && _echo_sed "Added directives to ${prefix}.with:\n${ADDS}" \ |
| 144 | && return 0 \ |
| 145 | || _echo_sed "Cannot add directives to ${prefix}.sans, missing:\ |
| 146 | \n$(_sed_rhs "${NGX_SERVER_NAME}" "${name}")\n${ADDS}" |
| 147 | return 1 |
| 148 | fi |
| 149 | return 0 |
| 150 | } |
| 151 | |
| 152 | # ---------------------------------------------------------------------------- |
| 153 | |
| 154 | test_setpoint() { |
| 155 | [ "$(cat "$1")" = "$2" ] && return |
| 156 | echo "$1:"; cat "$1" |
| 157 | echo "differs from setpoint:"; echo "$2" |
| 158 | [ "${PRINT_PASSED}" -gt 1 ] && pst_exit 1 |
| 159 | } |
| 160 | |
| 161 | |
| 162 | test_existence() { |
| 163 | if [ "$2" -eq "0" ] |
| 164 | then |
| 165 | [ ! -f "$1" ] && echo "$1 missing!" && |
| 166 | [ "${PRINT_PASSED}" -gt 1 ] && pst_exit 1 |
| 167 | else |
| 168 | [ -f "$1" ] && echo "$1 existing!" && |
| 169 | [ "${PRINT_PASSED}" -gt 1 ] && pst_exit 1 |
| 170 | fi |
| 171 | } |
| 172 | |
| 173 | |
| 174 | test() { |
| 175 | eval "$1 2>/dev/null >/dev/null" |
| 176 | if [ "$?" -eq "$2" ] |
| 177 | then |
| 178 | [ "${PRINT_PASSED}" -gt 0 ] \ |
| 179 | && printf "%-72s%-1s\n" "$1" "2>/dev/null >/dev/null (-> $2?) passed." |
| 180 | else |
| 181 | printf "%-72s%-1s\n" "$1" "2>/dev/null >/dev/null (-> $2?) failed!!!" |
| 182 | [ "${PRINT_PASSED}" -gt 0 ] && printf "\n### Snip:\n" && eval "$1" |
| 183 | [ "${PRINT_PASSED}" -gt 0 ] && printf "### Snap.\n" |
| 184 | [ "${PRINT_PASSED}" -gt 1 ] && pst_exit 1 |
| 185 | fi |
| 186 | } |
| 187 | |
| 188 | |
| 189 | |
| 190 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s get_env ...\n" "${NGINX_UTIL}" |
| 191 | |
| 192 | |
| 193 | eval $("${NGINX_UTIL}" get_env) |
| 194 | test '[ -n "${UCI_CONF}" ]' 0 |
| 195 | test '[ -n "${NGINX_CONF}" ]' 0 |
| 196 | test '[ -n "${CONF_DIR}" ]' 0 |
| 197 | test '[ -n "${LAN_NAME}" ]' 0 |
| 198 | test '[ -n "${LAN_LISTEN}" ]' 0 |
| 199 | test '[ -n "${LAN_SSL_LISTEN}" ]' 0 |
| 200 | test '[ -n "${SSL_SESSION_CACHE_ARG}" ]' 0 |
| 201 | test '[ -n "${SSL_SESSION_TIMEOUT_ARG}" ]' 0 |
| 202 | test '[ -n "${ADD_SSL_FCT}" ]' 0 |
| 203 | test '[ -n "${MANAGE_SSL}" ]' 0 |
| 204 | |
| 205 | mkdir -p "$(dirname "${LAN_LISTEN}")" |
| 206 | |
| 207 | mkdir -p "${CONF_DIR}" |
| 208 | |
| 209 | cd "$(dirname "${CONF_DIR}")" && [ ! -e "$(basename "${CONF_DIR}")${ORIG}" ] && |
| 210 | mv "$(basename "${CONF_DIR}")" "$(basename "${CONF_DIR}")${ORIG}" || |
| 211 | { |
| 212 | printf "\n%s: not moved %s to %s\n" "$(dirname "${CONF_DIR}")" \ |
| 213 | "$(basename "${CONF_DIR}")" "$(basename "${CONF_DIR}")${ORIG}" |
| 214 | pst_exit 3 |
| 215 | } |
| 216 | |
| 217 | |
| 218 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nPrepare files in %s ...\n" "${CONF_DIR}" |
| 219 | |
| 220 | mkdir -p "${CONF_DIR}" |
| 221 | |
| 222 | cd "${CONF_DIR}" || pst_exit 2 |
| 223 | |
| 224 | NGX_INCLUDE="include '\$';" |
| 225 | NGX_SERVER_NAME="server_name * '\$' *;" |
| 226 | NGX_SSL_CRT="ssl_certificate '\$.crt';" |
| 227 | NGX_SSL_KEY="ssl_certificate_key '\$.key';" |
| 228 | NGX_SSL_SESSION_CACHE="ssl_session_cache '$(echo "${SSL_SESSION_CACHE_ARG}" \ |
| 229 | | sed -E "s/$(__esc_regex "${LAN_NAME}")/\$/")';" |
| 230 | NGX_SSL_SESSION_TIMEOUT="ssl_session_timeout '${SSL_SESSION_TIMEOUT_ARG}';" |
| 231 | |
| 232 | cat > "${LAN_NAME}.sans" <<EOF |
| 233 | # default_server for the LAN addresses getting the IPs by: |
| 234 | # ifstatus lan | jsonfilter -e '@["ipv4-address","ipv6-address"].*.address' |
| 235 | server { |
| 236 | include '${LAN_LISTEN}.default'; |
| 237 | server_name ${LAN_NAME}; |
| 238 | include conf.d/*.locations; |
| 239 | } |
| 240 | EOF |
| 241 | CONFS="${CONFS} ${LAN_NAME}:0" |
| 242 | |
| 243 | cat > minimal.sans <<EOF |
| 244 | server { |
| 245 | server_name minimal; |
| 246 | } |
| 247 | EOF |
| 248 | CONFS="${CONFS} minimal:0" |
| 249 | |
| 250 | cat > listens.sans <<EOF |
| 251 | server { |
| 252 | listen 80; |
| 253 | listen 81; |
| 254 | listen hostname:80; |
| 255 | listen hostname:81; |
| 256 | listen [::]:80; |
| 257 | listen [::]:81; |
| 258 | listen 1.3:80; |
| 259 | # listen 1.3:80; |
| 260 | listen 1.3:81; |
| 261 | listen [1::3]:80; |
| 262 | listen [1::3]:81; |
| 263 | server_name listens; |
| 264 | } |
| 265 | EOF |
| 266 | CONFS="${CONFS} listens:0" |
| 267 | |
| 268 | cat > normal.sans <<EOF |
| 269 | server { |
| 270 | include '${LAN_LISTEN}'; |
| 271 | server_name normal; |
| 272 | } |
| 273 | EOF |
| 274 | CONFS="${CONFS} normal:0" |
| 275 | |
| 276 | cat > acme.sans <<EOF |
| 277 | server { |
| 278 | listen 80; |
| 279 | include '${LAN_LISTEN}'; |
| 280 | server_name acme; |
| 281 | } |
| 282 | EOF |
| 283 | CONFS="${CONFS} acme:0" |
| 284 | |
| 285 | cat > more_server.sans <<EOF |
| 286 | server { |
| 287 | # include '${LAN_LISTEN}'; |
| 288 | server_name normal; |
| 289 | } |
| 290 | server { |
| 291 | include '${LAN_LISTEN}'; |
| 292 | server_name more_server; |
| 293 | } |
| 294 | EOF |
| 295 | CONFS="${CONFS} more_server:0" |
| 296 | |
| 297 | cat > more_names.sans <<EOF |
| 298 | server { |
| 299 | include '${LAN_LISTEN}'; |
| 300 | include '${LAN_LISTEN}'; |
| 301 | include '${LAN_LISTEN}'; |
| 302 | not include '${LAN_LISTEN}'; |
| 303 | server_name example.com more_names example.org; |
| 304 | } |
| 305 | EOF |
| 306 | CONFS="${CONFS} more_names:0" |
| 307 | |
| 308 | cat > different_name.sans <<EOF |
| 309 | server { |
| 310 | include '${LAN_LISTEN}'; |
| 311 | server_name minimal; |
| 312 | } |
| 313 | EOF |
| 314 | CONFS="${CONFS} different_name:1" |
| 315 | |
| 316 | cat > comments.sans <<EOF |
| 317 | server { # comment1 |
| 318 | # comment2 |
| 319 | include '${LAN_LISTEN}'; |
| 320 | server_name comments; |
| 321 | # comment3 |
| 322 | } # comment4 |
| 323 | EOF |
| 324 | CONFS="${CONFS} comments:0" |
| 325 | |
| 326 | cat > name_comment.sans <<EOF |
| 327 | server { |
| 328 | include '${LAN_LISTEN}'; |
| 329 | server_name name_comment; # comment |
| 330 | } |
| 331 | EOF |
| 332 | CONFS="${CONFS} name_comment:0" |
| 333 | |
| 334 | cat > tab.sans <<EOF |
| 335 | server { |
| 336 | include '${LAN_LISTEN}'; |
| 337 | server_name tab; |
| 338 | } |
| 339 | EOF |
| 340 | CONFS="${CONFS} tab:0" |
| 341 | |
| 342 | |
| 343 | |
| 344 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nSetup files in %s ...\n" "${CONF_DIR}" |
| 345 | |
| 346 | |
| 347 | for conf in ${CONFS} |
| 348 | do test 'setpoint_add_ssl " " '"${conf%:*}" "${conf#*:}" |
| 349 | done |
| 350 | |
| 351 | test 'setpoint_add_ssl "\t" tab' 0 # fixes wrong indentation. |
| 352 | |
| 353 | |
| 354 | |
| 355 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting Cron ... \n" |
| 356 | |
| 357 | |
| 358 | echo -n "prefix" >"/etc/crontabs/root" |
| 359 | test '"${NGINX_UTIL}" add_ssl _lan' 0 |
| 360 | echo "postfix" >>"/etc/crontabs/root" |
| 361 | test_setpoint "/etc/crontabs/root" "prefix |
| 362 | 3 3 12 12 * ${NGINX_UTIL} 'check_ssl' |
| 363 | postfix" |
| 364 | |
| 365 | test '"${NGINX_UTIL}" del_ssl _lan' 0 |
| 366 | test_setpoint "/etc/crontabs/root" "prefix |
| 367 | 3 3 12 12 * ${NGINX_UTIL} 'check_ssl' |
| 368 | postfix" |
| 369 | |
| 370 | test '"${NGINX_UTIL}" check_ssl' 0 |
| 371 | test_setpoint "/etc/crontabs/root" "prefix |
| 372 | postfix" |
| 373 | |
| 374 | test '"${NGINX_UTIL}" add_ssl _lan' 0 |
| 375 | test_setpoint "/etc/crontabs/root" "prefix |
| 376 | postfix |
| 377 | 3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 378 | |
| 379 | rm -f "/etc/crontabs/root" |
| 380 | |
| 381 | |
| 382 | [ "$PRINT_PASSED" -gt 0 ] && printf '\n\t-"-\t(legacy) ... \n' |
| 383 | |
| 384 | echo -n "prefix" >"/etc/crontabs/root" |
| 385 | cp "minimal.sans" "minimal.conf" |
| 386 | |
| 387 | test '"${NGINX_UTIL}" add_ssl minimal' 0 |
| 388 | echo "postfix" >>"/etc/crontabs/root" |
| 389 | test_setpoint "/etc/crontabs/root" "prefix |
| 390 | 3 3 12 12 * ${NGINX_UTIL} 'add_ssl' 'minimal' |
| 391 | postfix" |
| 392 | |
| 393 | test '"${NGINX_UTIL}" del_ssl minimal' 0 |
| 394 | test_setpoint "/etc/crontabs/root" "prefix |
| 395 | postfix" |
| 396 | |
| 397 | rm -f "/etc/crontabs/root" |
| 398 | |
| 399 | |
| 400 | |
| 401 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s init_lan ...\n" "${NGINX_UTIL}" |
| 402 | |
| 403 | |
| 404 | rm -f "${LAN_NAME}.conf" "_redirect2ssl.conf" "${UCI_ADDED}.conf" |
| 405 | rm -f "$(readlink "${UCI_CONF}")" |
| 406 | |
| 407 | test '"${NGINX_UTIL}" init_lan' 0 |
| 408 | test_setpoint "${UCI_CONF}" "$(setpoint_init_lan)" |
| 409 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 410 | |
| 411 | |
| 412 | [ "$PRINT_PASSED" -gt 0 ] && printf '\n\t-"-\twith temporary UCI config ... \n' |
| 413 | |
| 414 | UCI_ADDED="$(uci add nginx server)" && |
| 415 | uci set nginx.@server[-1].server_name='temp' && |
| 416 | uci add_list nginx.@server[-1].listen='81 default_server' && |
| 417 | uci add_list nginx.@server[-1].listen='80' && |
| 418 | echo "UCI: nginx.${UCI_ADDED} added." |
| 419 | |
| 420 | rm -f "${LAN_NAME}.conf" "_redirect2ssl.conf" "${UCI_ADDED}.conf" |
| 421 | rm -f "$(readlink "${UCI_CONF}")" |
| 422 | |
| 423 | test '"${NGINX_UTIL}" init_lan' 0 |
| 424 | test_setpoint "${UCI_CONF}" "$(setpoint_init_lan)" |
| 425 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 426 | |
| 427 | |
| 428 | [ "$PRINT_PASSED" -gt 0 ] && printf '\n\t-"-\t(legacy) ... \n' |
| 429 | |
| 430 | cp "${LAN_NAME}.sans" "${LAN_NAME}.conf" |
| 431 | touch "_redirect2ssl.conf" "${UCI_ADDED}.conf" |
| 432 | rm -f "$(readlink "${UCI_CONF}")" |
| 433 | test '"${NGINX_UTIL}" init_lan' 0 |
| 434 | |
| 435 | skipped() { |
| 436 | printf "\t# skipped UCI server 'nginx.%s'" "$1" |
| 437 | printf " as it could conflict with: %s%s.conf\n\n" "${CONF_DIR}" "$1" |
| 438 | } |
| 439 | rhs="$(skipped "$LAN_NAME" && skipped _redirect2ssl && skipped "${UCI_ADDED}")" |
| 440 | sed -E -e "s/^\t#UCI_HTTP_CONFIG$/$(__esc_sed_rhs "$rhs")\n/" \ |
| 441 | -e 's/\\n/\n/g' -e "1i${fileauto}" "${UCI_CONF}.template" >"uci.setpoint" |
| 442 | |
| 443 | test_setpoint "${UCI_CONF}" "$(cat "uci.setpoint")" |
| 444 | test_setpoint "/etc/crontabs/root" "" |
| 445 | |
| 446 | |
| 447 | |
| 448 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s add_ssl ...\n" "${NGINX_UTIL}" |
| 449 | |
| 450 | |
| 451 | test '[ "${ADD_SSL_FCT}" = "add_ssl" ] ' 0 |
| 452 | |
| 453 | rm -f "${LAN_NAME}.conf" "_redirect2ssl.conf" "${UCI_ADDED}.conf" |
| 454 | rm -f "$(readlink "${UCI_CONF}")" |
| 455 | test 'uci set nginx._lan.uci_manage_ssl="self-signed"' 0 |
| 456 | "${NGINX_UTIL}" del_ssl "${LAN_NAME}" 2>/dev/null |
| 457 | test_setpoint "/etc/crontabs/root" "" |
| 458 | test_existence "${LAN_NAME}.crt" 1 |
| 459 | test_existence "${LAN_NAME}.key" 1 |
| 460 | test '"${NGINX_UTIL}" add_ssl '"${UCI_ADDED}"' acme \ |
| 461 | '"${CONF_DIR}${UCI_ADDED}.crt"' '"${CONF_DIR}${UCI_ADDED}.key"' ' 0 |
| 462 | test_setpoint "/etc/crontabs/root" "" |
| 463 | test_existence "${UCI_ADDED}.crt" 1 |
| 464 | test_existence "${UCI_ADDED}.key" 1 |
| 465 | test '"${NGINX_UTIL}" add_ssl '"${LAN_NAME}" 0 |
| 466 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 467 | test_existence "${LAN_NAME}.crt" 0 |
| 468 | test_existence "${LAN_NAME}.key" 0 |
| 469 | test '"${NGINX_UTIL}" add_ssl '"${LAN_NAME}" 0 |
| 470 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 471 | test '"${NGINX_UTIL}" add_ssl inexistent' 1 |
| 472 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 473 | test '"${NGINX_UTIL}" init_lan' 0 |
| 474 | test_setpoint "${UCI_CONF}" "$(setpoint_init_lan)" |
| 475 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'check_ssl'" |
| 476 | test_existence "${UCI_ADDED}.crt" 1 |
| 477 | test_existence "${UCI_ADDED}.key" 1 |
| 478 | test_existence "${LAN_NAME}.crt" 0 |
| 479 | test_existence "${LAN_NAME}.key" 0 |
| 480 | |
| 481 | |
| 482 | [ "$PRINT_PASSED" -gt 0 ] && printf '\n\t-"-\t(legacy) ... \n' |
| 483 | |
| 484 | cp different_name.sans different_name.with |
| 485 | |
| 486 | cp "/etc/crontabs/root" "cron.setpoint" |
| 487 | for conf in ${CONFS}; do |
| 488 | name="${conf%:*}" |
| 489 | [ "${name}" = "acme" ] && continue |
| 490 | [ "${name}" = "different_name" ] || |
| 491 | echo "3 3 12 12 * ${NGINX_UTIL} 'add_ssl' '${name}'" >>"cron.setpoint" |
| 492 | cp "${name}.sans" "${name}.conf" |
| 493 | test '"${NGINX_UTIL}" add_ssl '"${name}" "${conf#*:}" |
| 494 | test_setpoint "${name}.conf" "$(cat "${name}.with")" |
| 495 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 496 | [ "${name}" = "different_name" ] || test_existence "${name}.crt" 0 |
| 497 | [ "${name}" = "different_name" ] || test_existence "${name}.key" 0 |
| 498 | done |
| 499 | |
| 500 | cp acme.sans acme.conf |
| 501 | test '"${NGINX_UTIL}" add_ssl acme acme /path/to/crt /path/to/key' 0 |
| 502 | test_setpoint "acme.conf" "$(cat "acme.with")" |
| 503 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 504 | test_existence "acme.crt" 1 |
| 505 | test_existence "acme.key" 1 |
| 506 | |
| 507 | |
| 508 | |
| 509 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s del_ssl ...\n" "${NGINX_UTIL}" |
| 510 | |
| 511 | |
| 512 | sed -E -e 's/443 ssl/80/' -e '/[^2]ssl/d' "/etc/config/nginx" >"config.setpoint" |
| 513 | |
| 514 | cp "/etc/crontabs/root" "cron.setpoint" |
| 515 | rm -f "${LAN_NAME}.conf" "_redirect2ssl.conf" "${UCI_ADDED}.conf" |
| 516 | test '"${NGINX_UTIL}" del_ssl '"${LAN_NAME}" 0 |
| 517 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 518 | test_existence "${LAN_NAME}.crt" 1 |
| 519 | test_existence "${LAN_NAME}.key" 1 |
| 520 | test '"${NGINX_UTIL}" del_ssl '"${LAN_NAME}" 1 |
| 521 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 522 | |
| 523 | rm -f "$(readlink "${UCI_CONF}")" |
| 524 | sed -E "/$(__esc_regex "'check_ssl'")/d" "/etc/crontabs/root" >"cron.setpoint" |
| 525 | test '"${NGINX_UTIL}" init_lan' 0 |
| 526 | test_setpoint "${UCI_CONF}" "$(setpoint_init_lan)" |
| 527 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 528 | |
| 529 | touch "${UCI_ADDED}.crt" "${UCI_ADDED}.key" |
| 530 | test '"${NGINX_UTIL}" del_ssl "'${UCI_ADDED}'" acme' 0 |
| 531 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 532 | test_existence "${UCI_ADDED}.crt" 0 |
| 533 | test_existence "${UCI_ADDED}.key" 0 |
| 534 | |
| 535 | test '"${NGINX_UTIL}" del_ssl inexistent' 1 |
| 536 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 537 | |
| 538 | test_setpoint "/etc/config/nginx" "$(cat "config.setpoint")" |
| 539 | test '"${NGINX_UTIL}" add_ssl "'${UCI_ADDED}'" acme \ |
| 540 | '"${CONF_DIR}${UCI_ADDED}.crt"' '"${CONF_DIR}${UCI_ADDED}.key"' ' 0 |
| 541 | test '"${NGINX_UTIL}" add_ssl "'$(uci get "nginx.${UCI_ADDED}.server_name")'"' 0 |
| 542 | test '"${NGINX_UTIL}" del_ssl "'$(uci get "nginx.${UCI_ADDED}.server_name")'"' 0 |
| 543 | rm -f "$(readlink "${UCI_CONF}")" |
| 544 | sed -E "/$(__esc_regex "'check_ssl'")/d" "/etc/crontabs/root" >"cron.setpoint" |
| 545 | test '"${NGINX_UTIL}" init_lan' 0 |
| 546 | test_setpoint "${UCI_CONF}" "$(setpoint_init_lan)" |
| 547 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 548 | test_existence "${UCI_ADDED}.crt" 1 |
| 549 | test_existence "${UCI_ADDED}.key" 1 |
| 550 | |
| 551 | |
| 552 | [ "$PRINT_PASSED" -gt 0 ] && printf '\n\t-"-\t(legacy) ... \n' |
| 553 | |
| 554 | for conf in ${CONFS}; do |
| 555 | name="${conf%:*}" |
| 556 | [ "${name}" = "acme" ] && continue |
| 557 | sed -E "/$(__esc_regex "'${name}'")/d" "/etc/crontabs/root" >"cron.setpoint" |
| 558 | touch "${name}.crt" "${name}.key" |
| 559 | cp "${name}.with" "${name}.conf" |
| 560 | test '"${NGINX_UTIL}" del_ssl '"${name}" "${conf#*:}" |
| 561 | test_setpoint "${name}.conf" "$(cat "${name}.sans")" |
| 562 | test_setpoint "/etc/crontabs/root" "$(cat "cron.setpoint")" |
| 563 | [ "${name}" = "different_name" ] && rm "${name}.crt" "${name}.key" |
| 564 | test_existence "${name}.crt" 1 |
| 565 | test_existence "${name}.key" 1 |
| 566 | done |
| 567 | test_setpoint "/etc/crontabs/root" "" |
| 568 | |
| 569 | test '"${NGINX_UTIL}" del_ssl acme acme' 0 |
| 570 | test_existence "acme.crt" 1 |
| 571 | test_existence "acme.key" 1 |
| 572 | |
| 573 | cp acme.with acme.conf |
| 574 | touch acme.crt acme.key |
| 575 | echo "3 3 12 12 * ${NGINX_UTIL} 'add_ssl' 'acme'" >>"/etc/crontabs/root" |
| 576 | test '"${NGINX_UTIL}" del_ssl acme acme' 0 |
| 577 | test_setpoint "acme.conf" "$(cat "acme.sans")" |
| 578 | test_setpoint "/etc/crontabs/root" "3 3 12 12 * ${NGINX_UTIL} 'add_ssl' 'acme'" |
| 579 | test_existence "acme.crt" 0 |
| 580 | test_existence "acme.key" 0 |
| 581 | "${NGINX_UTIL}" del_ssl acme 2>/dev/null |
| 582 | test_setpoint "/etc/crontabs/root" "" |
| 583 | test_existence "acme.crt" 1 |
| 584 | test_existence "acme.key" 1 |
| 585 | |
| 586 | |
| 587 | [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting without UCI ... \n" |
| 588 | |
| 589 | rm -f "$(readlink "${UCI_CONF}")" |
| 590 | |
| 591 | test 'uci set nginx.global.uci_enable=0' 0 |
| 592 | |
| 593 | test '"${NGINX_UTIL}" init_lan' 0 |
| 594 | |
| 595 | test '[ -e "$(readlink '"${UCI_CONF}"')" ]' 1 |
| 596 | |
| 597 | cp "${LAN_NAME}.sans" "${LAN_NAME}.conf" |
| 598 | test '"${NGINX_UTIL}" add_ssl '"${LAN_NAME}" 0 |
| 599 | test '"${NGINX_UTIL}" add_ssl '"${LAN_NAME}" 0 |
| 600 | test '"${NGINX_UTIL}" del_ssl '"${LAN_NAME}" 0 |
| 601 | test '"${NGINX_UTIL}" del_ssl '"${LAN_NAME}" 0 |
| 602 | |
| 603 | test 'rm "${LAN_NAME}.conf"' 0 |
| 604 | test '"${NGINX_UTIL}" add_ssl '"${LAN_NAME}" 1 |
| 605 | test '"${NGINX_UTIL}" del_ssl '"${LAN_NAME}" 1 |
| 606 | |
| 607 | |
| 608 | |
| 609 | pst_exit 0 |