Nacre/tests/smoke/make-import-home.py
2026-11-12 11:39:41 -06:00

44 lines
3.2 KiB
Python
Executable file

#!/usr/bin/env python3
"""Build a throwaway HOME holding fake Chrome and Firefox profiles for tests/smoke/stores-import.txt.
Usage: tests/smoke/make-import-home.py <dir> (then SMOKE_HOME=<dir> tools/smoke.sh tests/smoke/stores-import.txt).
Never reads a real profile. Chrome (found first): 2 bar links, a folder with 1 link, 1 link in Other, 3 visits (one non-web),
home page and Bing. Firefox: 1 toolbar link, 1 menu link, 2 visits, home page and DuckDuckGo."""
import json
import sqlite3
import sys
from pathlib import Path
home = Path(sys.argv[1])
chrome = home / ".config/google-chrome/Default"
chrome.mkdir(parents=True)
node = lambda name, url: {"type": "url", "name": name, "url": url}
(chrome / "Bookmarks").write_text(json.dumps({"roots": {
"bookmark_bar": {"children": [node("Chrome bar one", "https://one.chrome.example/"), node("Chrome bar two", "https://two.chrome.example/"),
{"type": "folder", "name": "Chrome folder", "children": [node("Inside", "https://in.chrome.example/")]}]},
"other": {"children": [node("Chrome other", "https://other.chrome.example/"), node("Script", "javascript:alert(1)")]},
}}))
(chrome / "Preferences").write_text(json.dumps({"homepage": "https://home.chrome.example/", "default_search_provider_data": {"template_url_data": {"keyword": "bing.com"}}}))
db = sqlite3.connect(chrome / "History")
db.executescript("CREATE TABLE urls(id INTEGER PRIMARY KEY, url TEXT, title TEXT, visit_count INT, last_visit_time INT); CREATE TABLE visits(id INTEGER PRIMARY KEY, url INT, visit_time INT);")
epoch = 11644473600
for i, (url, title) in enumerate([("https://a.chrome.example/", "A"), ("https://b.chrome.example/", "B"), ("chrome://settings/", "Settings")], 1):
db.execute("INSERT INTO urls VALUES (?,?,?,1,?)", (i, url, title, (1700000000 + i * 60 + epoch) * 1_000_000))
db.execute("INSERT INTO visits VALUES (?,?,?)", (i, i, (1700000000 + i * 60 + epoch) * 1_000_000))
db.commit()
db.close()
fx_root = home / ".mozilla/firefox"
fx = fx_root / "abc.default"
fx.mkdir(parents=True)
(fx_root / "profiles.ini").write_text("[Profile0]\nName=default\nIsRelative=1\nPath=abc.default\n")
(fx / "prefs.js").write_text('user_pref("browser.startup.homepage", "https://home.firefox.example/");\nuser_pref("browser.search.defaultenginename", "DuckDuckGo");\n')
db = sqlite3.connect(fx / "places.sqlite")
db.executescript("""CREATE TABLE moz_places(id INTEGER PRIMARY KEY, url TEXT, title TEXT);
CREATE TABLE moz_bookmarks(id INTEGER PRIMARY KEY, type INT, fk INT, parent INT, position INT, title TEXT);
CREATE TABLE moz_historyvisits(id INTEGER PRIMARY KEY, place_id INT, visit_date INT);""")
places = [(1, "https://bar.firefox.example/", "Fx bar"), (2, "https://menu.firefox.example/", "Fx menu"), (3, "https://x.firefox.example/", "X"), (4, "https://y.firefox.example/", "Y")]
db.executemany("INSERT INTO moz_places VALUES (?,?,?)", places)
db.executemany("INSERT INTO moz_bookmarks VALUES (?,?,?,?,?,?)", [(2, 2, None, 1, 0, "menu"), (3, 2, None, 1, 1, "toolbar"), (10, 1, 1, 3, 0, "Fx bar"), (11, 1, 2, 2, 0, "Fx menu")])
db.executemany("INSERT INTO moz_historyvisits VALUES (?,?,?)", [(1, 3, 1700000100 * 1_000_000), (2, 4, 1700000200 * 1_000_000)])
db.commit()
db.close()