first-run-rules.js (3834B)
1 // Runs the polkit rules for the first run (linux/system/usr/share/polkit-1/rules.d/50-patina-first-user.rules) 2 // against a stand-in for polkit, to check who may do what and when. Usage: node tests/first-run-rules.js 3 const fs = require("fs"); 4 const path = require("path"); 5 const file = path.join(__dirname, "../../linux/system/usr/share/polkit-1/rules.d/50-patina-first-user.rules"); 6 const source = fs.readFileSync(file, "utf8"); 7 8 // `passwd` is the output of getent; `locked` says, per uid, what busctl would print for Locked. 9 function decide({ user, action, passwd, locked = {}, spawnFails = false }) { 10 let rule; 11 const polkit = { 12 Result: { YES: "YES", NO: "NO" }, 13 addRule(f) { rule = f; }, 14 spawn(argv) { 15 if (spawnFails) throw new Error("spawn failed"); 16 if (argv[0].endsWith("getent")) return passwd; 17 if (argv[0].endsWith("busctl")) { 18 const uid = argv[4].split("User").pop(); 19 return locked[uid] ? "b true\n" : "b false\n"; 20 } 21 throw new Error("unexpected " + argv.join(" ")); 22 }, 23 }; 24 new Function("polkit", source)(polkit); 25 return rule({ id: action }, { user }); 26 } 27 28 const NONE = "root:x:0:0::/root:/bin/bash\ngreetd:x:972:972::/var/lib/greetd:/sbin/nologin\nnobody:x:65534:65534::/:/sbin/nologin\n"; 29 const ALICE = NONE + "alice:x:1000:1000:Alice:/var/home/alice:/bin/bash\n"; 30 const ADMIN = "org.freedesktop.accounts.user-administration"; 31 const ZONE = "org.freedesktop.timedate1.set-timezone"; 32 const HOSTNAME = "org.freedesktop.hostname1.set-static-hostname"; 33 34 const cases = [ 35 ["greetd may administer accounts on a fresh machine", { user: "greetd", action: ADMIN, passwd: NONE }, "YES"], 36 ["greetd may set the time zone on a fresh machine", { user: "greetd", action: ZONE, passwd: NONE }, "YES"], 37 ["not once an account can log in", { user: "greetd", action: ZONE, passwd: ALICE, locked: { 1000: false } }, undefined], 38 ["still while the only account is locked (no password yet)", { user: "greetd", action: ZONE, passwd: ALICE, locked: { 1000: true } }, "YES"], 39 ["a failing check closes the window", { user: "greetd", action: ZONE, passwd: NONE, spawnFails: true }, undefined], 40 ["nobody else gets it, even on a fresh machine", { user: "divan", action: ZONE, passwd: NONE }, undefined], 41 ["greetd may set the computer's name on a fresh machine", { user: "greetd", action: HOSTNAME, passwd: NONE }, "YES"], 42 ["not the name once an account can log in", { user: "greetd", action: HOSTNAME, passwd: ALICE, locked: { 1000: false } }, undefined], 43 ...["org.freedesktop.NetworkManager.wifi.scan", "org.freedesktop.NetworkManager.network-control", "org.freedesktop.NetworkManager.settings.modify.system"] 44 .map((a) => [`greetd may use NetworkManager's ${a.split(".").slice(3).join(".")} on a fresh machine`, { user: "greetd", action: a, passwd: NONE }, "YES"]), 45 ["but not once an account can log in", { user: "greetd", action: "org.freedesktop.NetworkManager.settings.modify.system", passwd: ALICE, locked: { 1000: false } }, undefined], 46 ["and nothing else of NetworkManager (turning Wi-Fi off, say)", { user: "greetd", action: "org.freedesktop.NetworkManager.enable-disable-wifi", passwd: NONE }, undefined], 47 ["greetd gets nothing else of hostnamed (the transient name)", { user: "greetd", action: "org.freedesktop.hostname1.set-hostname", passwd: NONE }, undefined], 48 ["nor the clock's NTP setting", { user: "greetd", action: "org.freedesktop.timedate1.set-ntp", passwd: NONE }, undefined], 49 ]; 50 51 let failed = 0; 52 for (const [name, input, want] of cases) { 53 const got = decide(input); 54 const ok = got === want; 55 if (!ok) failed++; 56 console.log((ok ? "ok " : "FAIL ") + name + (ok ? "" : ` (wanted ${want}, got ${got})`)); 57 } 58 if (failed) { console.log(failed + " failed"); process.exit(1); } 59 console.log("the first-run rules do what they should");