|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +source "$(dirname "$0")/base-test.sh" |
| 6 | + |
| 7 | +migration="$ROOT/migrations/1787946619.sh" |
| 8 | +[[ -f $migration ]] || fail "the legacy power udev rule migration exists at $migration" |
| 9 | + |
| 10 | +test_dir=$(mktemp -d) |
| 11 | +trap 'rm -rf "$test_dir"' EXIT |
| 12 | + |
| 13 | +mkdir -p "$test_dir/bin" |
| 14 | + |
| 15 | +# sudo runs the real command, so the removals act on the redirected rules |
| 16 | +# directory below and the elevated calls land in the log beside it. |
| 17 | +cat >"$test_dir/bin/sudo" <<'STUB' |
| 18 | +#!/bin/bash |
| 19 | +
|
| 20 | +printf 'sudo %s\n' "$*" >>"$CALLS" |
| 21 | +exec "$@" |
| 22 | +STUB |
| 23 | + |
| 24 | +cat >"$test_dir/bin/udevadm" <<'STUB' |
| 25 | +#!/bin/bash |
| 26 | +
|
| 27 | +printf 'udevadm %s\n' "$*" >>"$CALLS" |
| 28 | +STUB |
| 29 | + |
| 30 | +chmod +x "$test_dir/bin/"* |
| 31 | + |
| 32 | +export CALLS="$test_dir/calls" |
| 33 | + |
| 34 | +rules_dir="$test_dir/rules.d" |
| 35 | +home_dir="$test_dir/home" |
| 36 | +power_rule="$rules_dir/99-power-profile.rules" |
| 37 | +wifi_rule="$rules_dir/99-wifi-powersave.rules" |
| 38 | + |
| 39 | +reset_machine() { |
| 40 | + rm -rf "$rules_dir" "$home_dir" |
| 41 | + mkdir -p "$rules_dir" "$home_dir" |
| 42 | +} |
| 43 | + |
| 44 | +run_migration() { |
| 45 | + : >"$CALLS" |
| 46 | + |
| 47 | + HOME="$home_dir" \ |
| 48 | + OMARCHY_UDEV_RULES_DIR="$rules_dir" \ |
| 49 | + PATH="$test_dir/bin:$PATH" \ |
| 50 | + bash -euo pipefail "$migration" >/dev/null |
| 51 | +} |
| 52 | + |
| 53 | +reload_count() { |
| 54 | + grep -cx 'udevadm control --reload' "$CALLS" || true |
| 55 | +} |
| 56 | + |
| 57 | +# What Omarchy 3's unquoted heredoc actually left on disk: the installing user's |
| 58 | +# home expanded into a rule root runs on every power_supply event. |
| 59 | +write_vulnerable_power_rule() { |
| 60 | + cat >"$power_rule" <<'RULE' |
| 61 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile --property=After=power-profiles-daemon.service /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 62 | +SUBSYSTEM=="power_supply", ATTR{type}=="USB", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile --property=After=power-profiles-daemon.service /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 63 | +RULE |
| 64 | +} |
| 65 | + |
| 66 | +write_vulnerable_wifi_rule() { |
| 67 | + cat >"$wifi_rule" <<'RULE' |
| 68 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave on" |
| 69 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave off" |
| 70 | +RULE |
| 71 | +} |
| 72 | + |
| 73 | +reset_machine |
| 74 | +write_vulnerable_power_rule |
| 75 | +run_migration |
| 76 | + |
| 77 | +[[ ! -e $power_rule ]] || |
| 78 | + fail "migration removes a power profile rule that runs out of a user home" "$(cat "$power_rule")" |
| 79 | +pass "migration removes a power profile rule that runs out of a user home" |
| 80 | + |
| 81 | +grep -q '^sudo rm -f .*99-power-profile\.rules$' "$CALLS" || |
| 82 | + fail "migration removes the rule with elevated privileges" "$(cat "$CALLS")" |
| 83 | +pass "migration removes the rule with elevated privileges" |
| 84 | + |
| 85 | +reset_machine |
| 86 | +write_vulnerable_wifi_rule |
| 87 | +run_migration |
| 88 | + |
| 89 | +[[ ! -e $wifi_rule ]] || |
| 90 | + fail "migration removes a Wi-Fi power save rule that runs out of a user home" "$(cat "$wifi_rule")" |
| 91 | +pass "migration removes a Wi-Fi power save rule that runs out of a user home" |
| 92 | + |
| 93 | +# udevd keeps running the rule it already parsed, so the file being gone from |
| 94 | +# disk is only half the fix until it reloads. |
| 95 | +(( $(reload_count) == 1 )) || |
| 96 | + fail "migration reloads udev after removing a rule" "$(cat "$CALLS")" |
| 97 | +pass "migration reloads udev after removing a rule" |
| 98 | + |
| 99 | +# Both files gone is still one machine-wide reload, not one per file. |
| 100 | +reset_machine |
| 101 | +write_vulnerable_power_rule |
| 102 | +write_vulnerable_wifi_rule |
| 103 | +run_migration |
| 104 | + |
| 105 | +[[ ! -e $power_rule && ! -e $wifi_rule ]] || |
| 106 | + fail "migration removes both legacy rules in one pass" |
| 107 | +(( $(reload_count) == 1 )) || |
| 108 | + fail "migration reloads udev once for both removals" "$(cat "$CALLS")" |
| 109 | +pass "migration removes both legacy rules and reloads udev once" |
| 110 | + |
| 111 | +# The second run is what every other account on the machine does, and what a |
| 112 | +# user gets from running omarchy-migrate again. |
| 113 | +run_migration |
| 114 | + |
| 115 | +(( $(reload_count) == 0 )) || |
| 116 | + fail "migration does not reload udev on a second run" "$(cat "$CALLS")" |
| 117 | +[[ ! -s $CALLS ]] || |
| 118 | + fail "migration touches nothing on a second run" "$(cat "$CALLS")" |
| 119 | +pass "migration is a no-op on a second run" |
| 120 | + |
| 121 | +reset_machine |
| 122 | +run_migration |
| 123 | + |
| 124 | +[[ ! -s $CALLS ]] || |
| 125 | + fail "migration touches nothing when the legacy rules are absent" "$(cat "$CALLS")" |
| 126 | +pass "migration leaves a machine without the legacy rules alone" |
| 127 | + |
| 128 | +# A user who wrote their own rule under one of these names keeps it, even when |
| 129 | +# the file talks about the legacy checkout. udev never runs a comment. |
| 130 | +reset_machine |
| 131 | +cat >"$power_rule" <<'RULE' |
| 132 | +# Replaces the rule Omarchy used to install from |
| 133 | +# /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set |
| 134 | +#SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 135 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/usr/local/bin/my-own-power-hook" |
| 136 | +RULE |
| 137 | +cat >"$wifi_rule" <<'RULE' |
| 138 | +# Kept from the old local/share/omarchy setup, rewritten to my own script |
| 139 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/local/bin/my-own-wifi-hook off" |
| 140 | +RULE |
| 141 | +before=$(cat "$power_rule" "$wifi_rule") |
| 142 | +run_migration |
| 143 | + |
| 144 | +[[ -e $power_rule && -e $wifi_rule ]] || |
| 145 | + fail "migration keeps same-named rules that only mention the legacy path" |
| 146 | +[[ $(cat "$power_rule" "$wifi_rule") == "$before" ]] || |
| 147 | + fail "migration leaves the user's own rules byte for byte" |
| 148 | +[[ ! -s $CALLS ]] || |
| 149 | + fail "migration escalates nothing when it removes nothing" "$(cat "$CALLS")" |
| 150 | +pass "migration keeps same-named rules that only mention the legacy path" |
| 151 | + |
| 152 | +# udev discards a '#' line before it ever looks for a trailing backslash, so the |
| 153 | +# rule below the comment is live and root still runs it. `udevadm verify` on this |
| 154 | +# exact shape, with a bogus key on the second line, reports the error on line 2. |
| 155 | +# The file has to go. |
| 156 | +reset_machine |
| 157 | +cat >"$power_rule" <<'RULE' |
| 158 | +# Disabled while I test the packaged rule: \ |
| 159 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 160 | +RULE |
| 161 | +run_migration |
| 162 | + |
| 163 | +[[ ! -e $power_rule ]] || |
| 164 | + fail "migration removes a rule left live under a commented continuation" "$(cat "$power_rule")" |
| 165 | +pass "migration removes a rule left live under a commented continuation" |
| 166 | + |
| 167 | +# A comment that does not continue still hides nothing behind it: the file holds |
| 168 | +# no active RUN+= at all and stays. |
| 169 | +reset_machine |
| 170 | +cat >"$power_rule" <<'RULE' |
| 171 | +# SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 172 | +RULE |
| 173 | +run_migration |
| 174 | + |
| 175 | +[[ -e $power_rule ]] || |
| 176 | + fail "migration keeps a rule that is only ever mentioned in a comment" |
| 177 | +pass "migration keeps a rule that is only ever mentioned in a comment" |
| 178 | + |
| 179 | +# The May 2026 rename left an intermediate variant under the old filename that |
| 180 | +# already ran out of /usr/bin. It duplicates the packaged rule but is not the |
| 181 | +# privilege escalation this migration exists to clear, so it is not ours to take. |
| 182 | +reset_machine |
| 183 | +cat >"$power_rule" <<'RULE' |
| 184 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-ac --property=After=power-profiles-daemon.service /usr/bin/powerprofilesctl set performance" |
| 185 | +RULE |
| 186 | +run_migration |
| 187 | + |
| 188 | +[[ -e $power_rule ]] || |
| 189 | + fail "migration keeps a legacy filename already repointed at /usr/bin" |
| 190 | +pass "migration keeps a legacy filename already repointed at /usr/bin" |
| 191 | + |
| 192 | +# Homes are not all under /home, so the running user's own home counts too, and |
| 193 | +# the argument the later variants passed must not hide the path. |
| 194 | +reset_machine |
| 195 | +cat >"$wifi_rule" <<RULE |
| 196 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-wifi-powersave-on $home_dir/.local/share/omarchy/bin/omarchy-wifi-powersave on" |
| 197 | +RULE |
| 198 | +run_migration |
| 199 | + |
| 200 | +[[ ! -e $wifi_rule ]] || |
| 201 | + fail "migration removes a rule that runs out of a home outside /home" "$(cat "$wifi_rule")" |
| 202 | +pass "migration removes a rule that runs out of a home outside /home" |
| 203 | + |
| 204 | +# Nothing named the wrong binary is ours: the same path with a different command |
| 205 | +# is a rule this migration cannot claim to know anything about. |
| 206 | +reset_machine |
| 207 | +cat >"$power_rule" <<'RULE' |
| 208 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave on" |
| 209 | +RULE |
| 210 | +run_migration |
| 211 | + |
| 212 | +[[ -e $power_rule ]] || |
| 213 | + fail "migration matches the binary the filename promises, not any home path" |
| 214 | +pass "migration matches the binary the filename promises, not any home path" |
| 215 | + |
| 216 | +# udev resumes a continuation across a comment: `udevadm verify` on |
| 217 | +# 'SUBSYSTEM=="power_supply" \' + "# c" + ', RUN+="..."' reports its style warning |
| 218 | +# on line 1, so those three lines are one rule and the rule is live. |
| 219 | +reset_machine |
| 220 | +cat >"$power_rule" <<'RULE' |
| 221 | +SUBSYSTEM=="power_supply", ATTR{type}=="Mains" \ |
| 222 | +# split for readability |
| 223 | +, RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set" |
| 224 | +RULE |
| 225 | +run_migration |
| 226 | + |
| 227 | +[[ ! -e $power_rule ]] || |
| 228 | + fail "migration removes a rule that continues across a comment" "$(cat "$power_rule")" |
| 229 | +pass "migration removes a rule that continues across a comment" |
0 commit comments