diff --git a/.gitignore b/.gitignore index 698065d..957eb72 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,10 @@ # will have compiled files and executables debug/ target/ +coverage/ # These are backup files generated by rustfmt **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information *.pdb - diff --git a/Cargo.lock b/Cargo.lock index 5d7aca7..cb69f8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,12 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 4 +version = 3 [[package]] name = "anstream" -version = "1.0.0" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" dependencies = [ "anstyle", "anstyle-parse", @@ -25,9 +25,9 @@ checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" [[package]] name = "anstyle-parse" -version = "1.0.0" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" dependencies = [ "utf8parse", ] @@ -58,22 +58,11 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" -[[package]] -name = "chacha20" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" -dependencies = [ - "cfg-if", - "cpufeatures", - "rand_core", -] - [[package]] name = "clap" -version = "4.6.7" +version = "4.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2" dependencies = [ "clap_builder", "clap_derive", @@ -81,9 +70,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.6.7" +version = "4.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb" dependencies = [ "anstream", "anstyle", @@ -93,9 +82,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.6.7" +version = "4.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" dependencies = [ "heck", "proc-macro2", @@ -105,9 +94,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "1.1.1" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" [[package]] name = "colorchoice" @@ -115,32 +104,22 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" -[[package]] -name = "cpufeatures" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" -dependencies = [ - "libc", -] - [[package]] name = "getrandom" -version = "0.4.3" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" dependencies = [ "cfg-if", "libc", - "r-efi", - "rand_core", + "wasi", ] [[package]] name = "heck" -version = "0.5.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "is_terminal_polyfill" @@ -161,45 +140,61 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] -name = "proc-macro2" -version = "1.0.107" +name = "ppv-lite86" +version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.47" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" dependencies = [ "proc-macro2", ] [[package]] -name = "r-efi" -version = "6.0.0" +name = "rand" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] [[package]] -name = "rand" -version = "0.10.2" +name = "rand_chacha" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ - "chacha20", - "getrandom", + "ppv-lite86", "rand_core", ] [[package]] name = "rand_core" -version = "0.10.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "rhexdump" @@ -229,15 +224,15 @@ dependencies = [ [[package]] name = "strsim" -version = "0.11.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "3.0.5" +version = "2.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9" +checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" dependencies = [ "proc-macro2", "quote", @@ -246,9 +241,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.24" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "utf8parse" @@ -256,6 +251,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "windows-link" version = "0.2.1" @@ -270,3 +271,23 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ "windows-link", ] + +[[package]] +name = "zerocopy" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6df92bf3d9227be3d53173901ddbffac2babc27ae50f397776ffd6dc33f800cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac4f328cf2f05d084e496c3e9c3f33ed0a183656a16e1fcec4d464d8373aec82" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 3428765..dac35e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,16 @@ [package] name = "rping" version = "0.1.0" -edition = "2024" -rust-version = "1.98" +edition = "2021" +rust-version = "1.70" description = "A small IPv4 and IPv6 ICMP packet utility" license = "MIT" [dependencies] -clap = { version = "4.6", features = ["derive"] } +clap = { version = ">=4.4, <4.5", features = ["derive"] } socket2 = { version = "0.6", features = ["all"] } rhexdump = "0.2" -rand = "0.10" +rand = "0.8" [profile.release] strip = true diff --git a/Jenkinsfile b/Jenkinsfile index 0195a29..6163299 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,9 +3,8 @@ // Add this webhook in Gitea: // http://192.168.1.37:8080/gitea-webhook/post // -// rping is a Rust ICMP ping CLI. CI validates compilation and tests only; -// it deliberately does not execute a live ping because raw ICMP sockets may -// require elevated privileges/capabilities on the Jenkins worker. +// rping is a Rust ICMP ping CLI. CI deliberately does not execute a live ping +// because raw ICMP sockets may require elevated privileges/capabilities. pipeline { agent { label 'rust_nodocker' } @@ -19,11 +18,23 @@ pipeline { environment { CARGO_TERM_COLOR = 'never' RUST_BACKTRACE = '1' + RUST_MSRV = '1.70.0' + CARGO_LLVM_COV_VERSION = '0.9.0' + BADGE_PUBLICATION_ROOT = '/mnt/assets/coverage' + BADGE_OWNER = 'Archive' + BADGE_PROJECT = 'rping' + DEFAULT_BRANCH = 'main' } stages { stage('Checkout') { steps { + sh ''' + set -eu + rm -rf coverage + mkdir -p coverage + ''' + checkout scm sh ''' @@ -49,17 +60,33 @@ pipeline { # Do not change the shared worker's default toolchain. rustup show active-toolchain + rustup toolchain install "$RUST_MSRV" --profile minimal + rustc --version cargo --version ''' } } + stage('Setup Coverage Tool') { + steps { + sh ''' + set -eu + + installed_version="$(cargo llvm-cov --version 2>/dev/null | awk '{print $2}' || true)" + if [ "$installed_version" != "$CARGO_LLVM_COV_VERSION" ]; then + cargo install --locked --version "$CARGO_LLVM_COV_VERSION" cargo-llvm-cov + fi + cargo llvm-cov --version + ''' + } + } + stage('Format') { steps { sh ''' set -eu - cargo fmt --all + cargo fmt --all -- --check ''' } } @@ -73,11 +100,21 @@ pipeline { } } + stage('Minimum Supported Rust') { + steps { + sh ''' + set -eu + cargo "+$RUST_MSRV" check --locked --all-targets --all-features + ''' + } + } + stage('Tests') { steps { sh ''' set -eu cargo test --locked --all-targets --all-features + : > "coverage/.tests-passed-$BUILD_NUMBER" ''' } } @@ -100,6 +137,43 @@ pipeline { ''' } } + + stage('Coverage') { + steps { + sh ''' + set -eu + + summary=coverage/coverage-summary.json + cargo llvm-cov \ + --locked \ + --all-features \ + --all-targets \ + --json \ + --summary-only \ + --output-path "$summary" \ + --fail-under-lines 40 + test -s "$summary" + + coverage_value="$(python3 - "$summary" <<'PY' +import json +import math +import sys + +with open(sys.argv[1], encoding="utf-8") as report: + value = json.load(report)["data"][0]["totals"]["lines"]["percent"] +if isinstance(value, bool) or not isinstance(value, (int, float)): + raise SystemExit("overall line coverage is not numeric") +if not math.isfinite(value) or not 0 <= value <= 100: + raise SystemExit("overall line coverage is outside 0 through 100") +print(value) +PY + )" + scripts/ci-coverage-badges.sh coverage --value "$coverage_value" + rm -f "$summary" + : > "coverage/.coverage-ready-$BUILD_NUMBER" + ''' + } + } } post { @@ -108,7 +182,7 @@ pipeline { conclusion: 'SUCCESS', name: 'rping CI', summary: 'rping passed CI.', - text: 'Formatting, compilation, tests, Clippy, and the release build completed successfully.', + text: 'Formatting, MSRV and current-toolchain checks, tests, Clippy, the release build, coverage floor, and badge generation completed successfully.', title: 'Build Successful' ) } @@ -118,7 +192,7 @@ pipeline { conclusion: 'FAILURE', name: 'rping CI', summary: 'rping failed CI.', - text: 'One or more Rust checks failed. See the Jenkins log.', + text: 'A Rust check, test, coverage floor, or badge step failed. Cleanup publishes badges that reflect this build.', title: 'Build Failed' ) } @@ -128,9 +202,75 @@ pipeline { conclusion: 'ACTION_REQUIRED', name: 'rping CI', summary: 'rping CI is unstable.', - text: 'The Jenkins build completed with an unstable status.', + text: 'The Jenkins build is unstable. Cleanup publishes badges that reflect this build.', title: 'Build Unstable' ) } + + cleanup { + sh ''' + set -eu + + if [ -f "coverage/.tests-passed-$BUILD_NUMBER" ]; then + scripts/ci-coverage-badges.sh test --value passing + else + scripts/ci-coverage-badges.sh test --value failing + fi + + if [ ! -f "coverage/.coverage-ready-$BUILD_NUMBER" ]; then + scripts/ci-coverage-badges.sh coverage --value - + fi + + version="$(cargo metadata --locked --no-deps --format-version 1 | python3 -c ' +import json +import sys + +metadata = json.load(sys.stdin) +root = metadata.get("resolve", {}).get("root") +packages = metadata["packages"] +if root is not None: + packages = [package for package in packages if package["id"] == root] +if len(packages) != 1: + raise SystemExit("could not resolve one authoritative package version") +print(packages[0]["version"]) +')" + scripts/ci-coverage-badges.sh version --value "$version" + + rm -f coverage/coverage-summary.json \ + "coverage/.tests-passed-$BUILD_NUMBER" \ + "coverage/.coverage-ready-$BUILD_NUMBER" + + for badge in version test coverage; do + file="coverage/$badge.svg" + test -f "$file" + test ! -L "$file" + test -s "$file" + done + count="$(find coverage -maxdepth 1 -type f -name '*.svg' | wc -l | tr -d ' ')" + test "$count" = 3 + ''' + + archiveArtifacts( + allowEmptyArchive: false, + artifacts: 'coverage/version.svg,coverage/test.svg,coverage/coverage.svg', + fingerprint: true + ) + + script { + if (!env.BRANCH_NAME) { + error('BRANCH_NAME is required; badge publication expects a Jenkins multibranch job.') + } + if (env.BRANCH_NAME == env.DEFAULT_BRANCH) { + sh ''' + scripts/publish-coverage-badges.sh \ + "$BADGE_PUBLICATION_ROOT" \ + "$BADGE_OWNER" \ + "$BADGE_PROJECT" \ + "$DEFAULT_BRANCH" \ + version test coverage + ''' + } + } + } } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index c3f67b6..3b32768 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] channel = "1.98.1" profile = "minimal" -components = ["rustfmt", "clippy"] +components = ["rustfmt", "clippy", "llvm-tools-preview"] diff --git a/scripts/ci-coverage-badges.sh b/scripts/ci-coverage-badges.sh new file mode 100755 index 0000000..c76b094 --- /dev/null +++ b/scripts/ci-coverage-badges.sh @@ -0,0 +1,270 @@ +#!/bin/sh +set -eu + +usage() { + cat >&2 <<'EOF' +usage: $0 [options] + +Render one deterministic SVG badge. Defaults: + version --text version --value unknown --color #007ec6 + test --text tests --value passing --color #4c1 + coverage --text coverage --value - --color #9f9f9f --suffix % + +Options: + --text TEXT left-hand label + --value VALUE right-hand value + --color COLOR value background (#RGB or #RRGGBB) + --size PX font size, from 6 through 24 (default: 11) + --height PX badge height, from 14 through 64 (default: 20) + --label-width PX fixed label width instead of automatic sizing + --value-width PX fixed value width instead of automatic sizing + --suffix TEXT text appended to the displayed value + --output PATH output file (default: coverage/.svg) + -h, --help show this help + +Examples: + ./ci-coverage-badges.sh version --value 1.2.3 + ./ci-coverage-badges.sh test --value failing + ./ci-coverage-badges.sh coverage --value 87.4 + ./ci-coverage-badges.sh version --text release --value rc.12 --size 12 --color '#8a2be2' +EOF + exit "${1:-2}" +} + +[ "$#" -ge 1 ] || usage +kind=$1 +shift + +case "$kind" in + version) + text=version + value=unknown + color='#007ec6' + suffix= + output=coverage/version.svg + ;; + test) + text=tests + value=passing + color= + suffix= + output=coverage/test.svg + ;; + coverage) + text=coverage + value=- + color= + suffix=% + output=coverage/coverage.svg + ;; + -h|--help) usage 0 ;; + *) + echo "unsupported badge kind: $kind" >&2 + usage + ;; +esac + +font_size=11 +height=20 +label_width= +value_width= +color_set=false + +while [ "$#" -gt 0 ]; do + option=$1 + case "$option" in + --text|--value|--color|--size|--height|--label-width|--value-width|--suffix|--output) + [ "$#" -ge 2 ] || { + echo "missing value for $option" >&2 + exit 2 + } + option_value=$2 + shift 2 + case "$option" in + --text) text=$option_value ;; + --value) value=$option_value ;; + --color) color=$option_value; color_set=true ;; + --size) font_size=$option_value ;; + --height) height=$option_value ;; + --label-width) label_width=$option_value ;; + --value-width) value_width=$option_value ;; + --suffix) suffix=$option_value ;; + --output) output=$option_value ;; + esac + ;; + -h|--help) usage 0 ;; + *) + echo "unknown option: $option" >&2 + usage + ;; + esac +done + +validate_text() { + field_name=$1 + field_value=$2 + if ! printf '%sX\n' "$field_value" | LC_ALL=C awk ' + BEGIN { valid = 1 } + NR != 1 || $0 !~ /^[ -~]+X$/ || length($0) <= 1 { valid = 0 } + END { exit(valid == 0 ? 1 : 0) } + '; then + echo "$field_name must contain printable ASCII characters: $field_value" >&2 + exit 2 + fi +} + +validate_integer() { + field_name=$1 + field_value=$2 + minimum=$3 + maximum=$4 + case "$field_value" in + ''|*[!0-9]*) + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + ;; + esac + if [ "$field_value" -lt "$minimum" ] || [ "$field_value" -gt "$maximum" ]; then + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + fi +} + +validate_text text "$text" +validate_text value "$value" +if [ -n "$suffix" ]; then + validate_text suffix "$suffix" +fi +validate_integer size "$font_size" 6 24 +validate_integer height "$height" 14 64 +if [ -n "$label_width" ]; then + validate_integer label-width "$label_width" 20 1000 +fi +if [ -n "$value_width" ]; then + validate_integer value-width "$value_width" 20 1000 +fi +case "$output" in + ''|*/) echo "output must name an SVG file: $output" >&2; exit 2 ;; +esac + +case "$kind" in + test) + if [ "$color_set" = false ]; then + case "$value" in + passing) color='#4c1' ;; + failing) color='#e05d44' ;; + *) color='#9f9f9f' ;; + esac + fi + ;; + coverage) + if [ "$value" = '-' ]; then + [ "$color_set" = true ] || color='#9f9f9f' + else + if ! awk -v candidate="$value" 'BEGIN { + valid = candidate ~ /^[0-9]+([.][0-9]+)?$/ && candidate >= 0 && candidate <= 100 + exit(valid ? 0 : 1) + }'; then + echo "coverage value must be - or a number from 0 through 100: $value" >&2 + exit 2 + fi + value=$(awk -v candidate="$value" 'BEGIN { printf "%.0f", candidate }') + if [ "$color_set" = false ]; then + if [ "$value" -ge 90 ]; then + color='#4c1' + elif [ "$value" -ge 70 ]; then + color='#dfb317' + else + color='#e05d44' + fi + fi + fi + ;; +esac + +case "$color" in + \#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]|\#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]) ;; + *) echo "color must be #RGB or #RRGGBB: $color" >&2; exit 2 ;; +esac + +display_value=$value$suffix +if [ -z "$label_width" ]; then + label_width=$(printf '%s\\n' "$text" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi +if [ -z "$value_width" ]; then + value_width=$(printf '%s\\n' "$display_value" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi + +badge_width=$((label_width + value_width)) +label_center=$(awk -v width="$label_width" 'BEGIN { printf "%.1f", width / 2 }') +value_center=$(awk -v left="$label_width" -v width="$value_width" 'BEGIN { + printf "%.1f", left + width / 2 +}') +vertical_center=$(awk -v badge_height="$height" 'BEGIN { printf "%.1f", badge_height / 2 }') + +xml_escape() { + sed \ + -e 's/&/\&/g' \ + -e 's//\>/g' \ + -e 's/"/\"/g' +} + +sed_replacement_escape() { + sed -e 's/[\\&|]/\\&/g' +} + +escaped_text=$(printf '%s' "$text" | xml_escape | sed_replacement_escape) +escaped_value=$(printf '%s' "$display_value" | xml_escape | sed_replacement_escape) +escaped_title=$(printf '%s: %s' "$text" "$display_value" | xml_escape | sed_replacement_escape) + +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi +temporary="${output}.tmp.$$" +trap 'rm -f "$temporary"' EXIT HUP INT TERM + +sed \ + -e "s/@BADGE_WIDTH@/$badge_width/g" \ + -e "s/@HEIGHT@/$height/g" \ + -e "s/@LABEL_WIDTH@/$label_width/g" \ + -e "s/@VALUE_WIDTH@/$value_width/g" \ + -e "s/@LABEL_CENTER@/$label_center/g" \ + -e "s/@VALUE_CENTER@/$value_center/g" \ + -e "s/@VERTICAL_CENTER@/$vertical_center/g" \ + -e "s/@FONT_SIZE@/$font_size/g" \ + -e "s/@COLOR@/$color/g" \ + -e "s|@TITLE@|$escaped_title|g" \ + -e "s|@TEXT@|$escaped_text|g" \ + -e "s|@VALUE@|$escaped_value|g" \ + >"$temporary" <<'EOF' + + @TITLE@ + + + + + + + + + + + + + + @TEXT@ + @VALUE@ + + +EOF + +mv "$temporary" "$output" +trap - EXIT HUP INT TERM diff --git a/scripts/publish-coverage-badges.sh b/scripts/publish-coverage-badges.sh new file mode 100755 index 0000000..77ec155 --- /dev/null +++ b/scripts/publish-coverage-badges.sh @@ -0,0 +1,84 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 ABSOLUTE_ROOT OWNER PROJECT DEFAULT_BRANCH NAME..." >&2 + exit 2 +} + +[ "$#" -ge 5 ] || usage +root=$1 +owner=$2 +project=$3 +branch=$4 +shift 4 + +case $root in /*) ;; *) echo "publication root must be absolute" >&2; exit 1 ;; esac +[ -d "$root" ] && [ ! -L "$root" ] && [ -w "$root" ] || { + echo "publication root must be an existing writable directory, not a symlink" >&2 + exit 1 +} + +for component in "$owner" "$project" "$branch"; do + case $component in + ''|*[!A-Za-z0-9._-]*) echo "invalid publication path component: $component" >&2; exit 1 ;; + esac +done + +seen=' ' +for name in "$@"; do + case $name in coverage|test|version) ;; *) echo "invalid badge name: $name" >&2; exit 1 ;; esac + case $seen in *" $name "*) echo "duplicate badge name: $name" >&2; exit 1 ;; esac + seen="$seen$name " + source_file="coverage/$name.svg" + [ -f "$source_file" ] && [ ! -L "$source_file" ] && [ -s "$source_file" ] || { + echo "invalid badge source: $source_file" >&2 + exit 1 + } +done + +owner_dir=$root/$owner +project_dir=$owner_dir/$project +destination=$project_dir/$branch + +for directory in "$owner_dir" "$project_dir"; do + [ ! -L "$directory" ] || { echo "refusing symlink directory: $directory" >&2; exit 1; } + [ ! -e "$directory" ] || [ -d "$directory" ] || { + echo "publication component is not a directory: $directory" >&2 + exit 1 + } + mkdir -p "$directory" + chmod 0755 "$directory" +done + +if [ -L "$destination" ]; then + unlink "$destination" +fi +[ ! -e "$destination" ] || [ -d "$destination" ] || { + echo "publication destination is not a directory: $destination" >&2 + exit 1 +} +mkdir -p "$destination" +chmod 0755 "$destination" + +cleanup() { + for name in "$@"; do + temporary=$destination/.$name.svg.tmp-$$ + [ ! -e "$temporary" ] || rm -f "$temporary" + done +} +trap cleanup 0 +trap 'exit 1' HUP INT TERM + +for name in "$@"; do + temporary=$destination/.$name.svg.tmp-$$ + (umask 077; set -C; : > "$temporary") || { + echo "cannot create publication temporary: $temporary" >&2 + exit 1 + } + cp "coverage/$name.svg" "$temporary" + chmod 0644 "$temporary" + mv -f "$temporary" "$destination/$name.svg" +done + +trap - 0 HUP INT TERM diff --git a/src/libs/args.rs b/src/libs/args.rs index 05435e5..16da0ef 100644 --- a/src/libs/args.rs +++ b/src/libs/args.rs @@ -104,16 +104,14 @@ impl Config { .chars() .filter(|c| !matches!(c, ' ' | ':' | '_' | '\n' | '\t')) .collect(); - if !hex.len().is_multiple_of(2) || !hex.bytes().all(|b| b.is_ascii_hexdigit()) { + if hex.len() % 2 != 0 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) { return Err(io::Error::new( io::ErrorKind::InvalidInput, "payload hex must contain complete byte pairs", )); } hex.as_bytes() - .as_chunks::<2>() - .0 - .iter() + .chunks_exact(2) .map(|pair| { u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).map_err(|_| { io::Error::new(io::ErrorKind::InvalidInput, "invalid payload hex") @@ -222,17 +220,15 @@ mod tests { ]) .unwrap(); assert!(config.packet_size(1).is_err()); - assert!( - Config::try_parse_from([ - "rping", - "127.0.0.1", - "--payload", - "a", - "--payload-hex", - "61" - ]) - .is_err() - ); + assert!(Config::try_parse_from([ + "rping", + "127.0.0.1", + "--payload", + "a", + "--payload-hex", + "61" + ]) + .is_err()); } #[test] diff --git a/src/libs/icmp.rs b/src/libs/icmp.rs index 63f75c1..f7f4290 100644 --- a/src/libs/icmp.rs +++ b/src/libs/icmp.rs @@ -1,5 +1,5 @@ use crate::libs::args::{Config, Family, Output, Padding}; -use rand::{RngExt, distr::Alphanumeric}; +use rand::{distributions::Alphanumeric, Rng}; use socket2::{Domain, Protocol, Socket, Type}; use std::{ io, @@ -135,7 +135,8 @@ pub fn generate_ping_request(config: Config) -> io::Result<()> { { Ok(()) } else { - Err(io::Error::other( + Err(io::Error::new( + io::ErrorKind::Other, "one or more probes did not receive a matching reply", )) } @@ -355,8 +356,9 @@ fn create_buffer( if matches!(padding, Padding::Random) || (matches!(padding, Padding::Auto) && payload.is_empty()) { + let mut rng = rand::thread_rng(); for byte in &mut buf[8 + payload.len()..] { - *byte = rand::rng().sample(Alphanumeric); + *byte = rng.sample(Alphanumeric); } } if version == Version::V4 {