Breaking: a job reads done only once the printer has confirmed that it ran it, and holds the printer's connection until then. A job written in full that the printer does not confirm within printer.write_timeout now reads uncertain and is not sent again; before, it read done. Before each job, nothing is sent until the printer answers that it can print, so with its cover open or no paper a job is retried and fails, with no sent_at, as when the printer is unreachable, where before it read done. Receipts that take longer than write_timeout to print need a larger one. With confirmation the daemon also turns the printer's automatic status off (GS a 0) before each job and before its confirmation, so another client that relies on automatic status from the same printer stops getting it. printer.confirm = false keeps the old meaning of done and sends none of this. The third paper check of the job store showed why: with the TM-T88IV's cover open, Windows took all of a 128 KB receipt into its send buffer, so the job read done; the daemon stopped, and once the cover closed only the first third printed, without its cut. The printer was then left inside the image command, waiting for the rest, until it was switched off and on. The daemon now turns on the printer client's confirmation. The queue treats an unconfirmed send as written but unknown: it records the delivery uncertain, with an error that says the printer did not confirm it and that it may have printed in full, in part or not at all, logs it at WARN with the printer, and neither retries it nor reports it as a failure. Its commit is not synced: lost, it rolls back to sending, which the next start turns uncertain too. A printer that is not ready gives a plain error before the sending mark, which the queue retries as a failed dial. The new key printer.confirm, on by default, turns confirmation off for a printer that does not answer GS r with bits 4 and 7 clear, or does not answer DLE EOT on receipt; only the TM-T88IV is checked. The key's comments in config.go and the example config say what it sends and needs. docs/api.md warns that raw bytes which make the printer answer can be taken for the confirmation, and says that /healthz answers 503 timeout while a job holds the connection. It and design.md say that each job waits about 200 ms for answers still owed, for DLE EOT 1 to say that the printer is online, and for no answer to come late. design.md says why each connection opens with DLE EOT 1, GS a 0 and GS r 1, and names what stays open: a raw job holding a command answered with bits 4 and 7 clear, such as GS r, GS I, ESC v or ESC u 0, with printing after it, and an answer taken for the ready answer that is not it, one still queued when the fence arrived or one owed from before a late DLE EOT answer that stood in for the fence, followed by a pause of more than 100 ms while the printer reads online, as might happen during the TM-m50II's feed and cut at cover close. It relies on the printer answering DLE EOT on receipt and running GS a 0 in order, unverified beyond the TM-T88IV until stage 4a's raw-byte probe, and now says a send that fails part-way can print a whole receipt twice. The unit and install.md give the stop timeout with and without state.dir: without it the job being sent runs on from its rendering, image fetches included, up to the job's 2-minute cap, so such hosts keep systemd's default or set state.dir. configuration.md says the online check can add 2 s to write_timeout. The printers plan says what Send now does before a job, and its table of what a failed send can mean gains the ready wait and the confirmation. The tests' fake printers answer DLE EOT 1 and GS r 1 as an online printer with paper does; a test runs the daemon against one that answers that it is ready but never confirms the job. On paper, a receipt read done 884 ms after it was written, when the printer answered, and a receipt whose daemon was killed while it waited, with the cover open, read uncertain after the restart and printed once, cut short. With GS a 0 and DLE EOT 1, a receipt printed once with nothing extra and read done 888 ms after it was written; after a power cycle, with /healthz polled every 250 ms, one sent with the cover open waited 15.7 s with nothing sent and printed once, cut once, when the cover closed. With the fence, a receipt printed once with nothing extra and read done 921 ms after it was written. Part of stage 1d of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
735 lines
26 KiB
Go
735 lines
26 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
// Embeds the zone database, so that IANA names load on any host.
|
|
_ "time/tzdata"
|
|
|
|
"uberbringer/internal/pkg/config"
|
|
)
|
|
|
|
// validSecret is a placeholder that satisfies the 32-byte minimum.
|
|
const validSecret = "test-only-secret-test-only-secret-0123"
|
|
|
|
func writeConfig(t *testing.T, body string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "config.toml")
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoadInstallScriptConfigGetsDefaults(t *testing.T) {
|
|
t.Setenv("SENTRY_DSN", "")
|
|
// The shape install.sh writes; every other setting comes from the
|
|
// defaults.
|
|
path := writeConfig(t, `
|
|
[webserver]
|
|
bind_ip = "127.0.0.1"
|
|
port = 8080
|
|
|
|
[api]
|
|
secret = "`+validSecret+`"
|
|
|
|
[printer]
|
|
address = "192.168.1.245"
|
|
|
|
[state]
|
|
dir = "/var/lib/uberbringer"
|
|
`)
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.State.Dir != "/var/lib/uberbringer" {
|
|
t.Errorf("state.dir = %q", cfg.State.Dir)
|
|
}
|
|
if cfg.Printer.Port != 9100 || cfg.Printer.Model != "tm-t88" || cfg.Printer.CodePage != "cp437" {
|
|
t.Errorf("printer defaults not applied: %+v", cfg.Printer)
|
|
}
|
|
if cfg.WebServer.RateWindow != time.Minute || cfg.Queue.Size != 100 {
|
|
t.Errorf("defaults not applied: %+v %+v", cfg.WebServer, cfg.Queue)
|
|
}
|
|
if cfg.Images.AllowPrivateNetworks {
|
|
t.Error("private networks must be blocked by default")
|
|
}
|
|
if cfg.Format.MaxLines != 2000 || cfg.Printer.BoxLineSpacing != 0 || cfg.Printer.CutFeed != 3 {
|
|
t.Errorf("defaults not applied: max_lines %d, box_line_spacing %d, cut_feed %d",
|
|
cfg.Format.MaxLines, cfg.Printer.BoxLineSpacing, cfg.Printer.CutFeed)
|
|
}
|
|
if !cfg.Printer.Confirm {
|
|
t.Error("printer.confirm must be on by default")
|
|
}
|
|
if cfg.Format.Timezone != "Local" || cfg.Format.TimeLayout != "02 Jan 15:04" {
|
|
t.Errorf("format defaults not applied: timezone %q, time_layout %q", cfg.Format.Timezone, cfg.Format.TimeLayout)
|
|
}
|
|
}
|
|
|
|
func TestLoadOverridesAndDurations(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[api]
|
|
secret = "`+validSecret+`"
|
|
[printer]
|
|
address = "printer.lan"
|
|
dial_timeout = "750ms"
|
|
paper_width = 384
|
|
box_line_spacing = 24
|
|
[format]
|
|
max_lines = 500
|
|
timezone = "Europe/London"
|
|
time_layout = "2006-01-02 15:04"
|
|
[images]
|
|
allowed_hosts = ["images.lan"]
|
|
[sentry]
|
|
dsn = "https://key@sentry.example/1"
|
|
[log]
|
|
format = "json"
|
|
`)
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Format.Timezone != "Europe/London" || cfg.Format.TimeLayout != "2006-01-02 15:04" {
|
|
t.Errorf("format = %+v", cfg.Format)
|
|
}
|
|
if cfg.Printer.DialTimeout != 750*time.Millisecond {
|
|
t.Errorf("DialTimeout = %v, want 750ms", cfg.Printer.DialTimeout)
|
|
}
|
|
if cfg.Printer.PaperWidth != 384 || cfg.Printer.BoxLineSpacing != 24 || cfg.Format.MaxLines != 500 || cfg.Log.Format != "json" {
|
|
t.Errorf("overrides not applied: %+v %+v %+v", cfg.Printer, cfg.Format, cfg.Log)
|
|
}
|
|
if len(cfg.Images.AllowedHosts) != 1 || cfg.Images.AllowedHosts[0] != "images.lan" {
|
|
t.Errorf("AllowedHosts = %v", cfg.Images.AllowedHosts)
|
|
}
|
|
if cfg.Sentry.DSN != "https://key@sentry.example/1" {
|
|
t.Errorf("DSN = %q", cfg.Sentry.DSN)
|
|
}
|
|
}
|
|
|
|
func TestLoadSentryDSNFromEnvironment(t *testing.T) {
|
|
t.Setenv("SENTRY_DSN", "https://env@sentry.example/2")
|
|
path := writeConfig(t, "[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"p\"\n")
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Sentry.DSN != "https://env@sentry.example/2" {
|
|
t.Errorf("DSN = %q, want value from SENTRY_DSN", cfg.Sentry.DSN)
|
|
}
|
|
}
|
|
|
|
func TestLoadErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
body string
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "missing secret",
|
|
body: "[printer]\naddress = \"p\"\n",
|
|
wantErr: "api.secret must be at least 32 bytes",
|
|
},
|
|
{
|
|
name: "short secret",
|
|
body: "[api]\nsecret = \"short\"\n[printer]\naddress = \"p\"\n",
|
|
wantErr: "api.secret must be at least 32 bytes",
|
|
},
|
|
{
|
|
name: "missing printer address",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n",
|
|
wantErr: "printer.address is required",
|
|
},
|
|
{
|
|
name: "unknown key",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nadress = \"typo\"\n",
|
|
wantErr: "unknown keys: printer.adress",
|
|
},
|
|
{
|
|
name: "bad duration",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = \"soon\"\n",
|
|
wantErr: "dial_timeout",
|
|
},
|
|
{
|
|
name: "bad trusted proxy",
|
|
body: "[webserver]\ntrusted_proxies = [\"nginx\"]\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
|
|
wantErr: `"nginx" is not an IP or CIDR`,
|
|
},
|
|
{
|
|
name: "zero max lines",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\nmax_lines = 0\n",
|
|
wantErr: "format.max_lines must be positive",
|
|
},
|
|
{
|
|
name: "negative box line spacing",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nbox_line_spacing = -1\n",
|
|
wantErr: "printer.box_line_spacing must be 0-255",
|
|
},
|
|
{
|
|
name: "box line spacing too large",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nbox_line_spacing = 256\n",
|
|
wantErr: "printer.box_line_spacing must be 0-255",
|
|
},
|
|
{
|
|
name: "negative cut feed",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ncut_feed = -1\n",
|
|
wantErr: "printer.cut_feed must be 0-255",
|
|
},
|
|
{
|
|
name: "cut feed too large",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ncut_feed = 256\n",
|
|
wantErr: "printer.cut_feed must be 0-255",
|
|
},
|
|
{
|
|
name: "unknown time zone",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"Europe/Londn\"\n",
|
|
wantErr: `format.timezone: "Europe/Londn" is not a known time zone`,
|
|
},
|
|
{
|
|
name: "time zone that is a path",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"../etc/passwd\"\n",
|
|
wantErr: "format.timezone",
|
|
},
|
|
{
|
|
name: "empty time zone",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"\"\n",
|
|
wantErr: "format.timezone is required",
|
|
},
|
|
{
|
|
name: "time layout without a time",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntime_layout = \"HH:MM\"\n",
|
|
wantErr: `format.time_layout: "HH:MM" shows no date or time`,
|
|
},
|
|
{
|
|
name: "empty time layout",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntime_layout = \"\"\n",
|
|
wantErr: "format.time_layout",
|
|
},
|
|
{
|
|
name: "unknown format key",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\nmax_line = 10\n",
|
|
wantErr: "unknown keys: format.max_line",
|
|
},
|
|
{
|
|
name: "blank state dir",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[state]\ndir = \" \"\n",
|
|
wantErr: "state.dir is blank",
|
|
},
|
|
{
|
|
name: "bad log level",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[log]\nlevel = \"loud\"\n",
|
|
wantErr: "log.level",
|
|
},
|
|
{
|
|
name: "unknown empty table",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printr]\n",
|
|
wantErr: "unknown keys: printr",
|
|
},
|
|
{
|
|
name: "unknown empty subtable",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printer.extra]\n",
|
|
wantErr: "unknown keys: printer.extra",
|
|
},
|
|
{
|
|
name: "unknown empty inline table",
|
|
body: "extra = {}\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
|
|
wantErr: "unknown keys: extra",
|
|
},
|
|
{
|
|
name: "quoted dotted key",
|
|
body: "\"log.level\" = \"debug\"\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
|
|
wantErr: `unknown keys: "log.level"`,
|
|
},
|
|
{
|
|
name: "table for a setting",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printer.port]\nnumber = 9100\n",
|
|
wantErr: "unknown keys: printer.port",
|
|
},
|
|
{
|
|
name: "value for a section",
|
|
body: "log = \"debug\"\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
|
|
wantErr: "unknown keys: log",
|
|
},
|
|
{
|
|
name: "fraction for an integer",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = 2.9\n",
|
|
wantErr: "printer.retries",
|
|
},
|
|
{
|
|
name: "fraction for a duration",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = 2.5\n",
|
|
wantErr: "printer.dial_timeout",
|
|
},
|
|
{
|
|
name: "number for a string",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nmodel = 88\n",
|
|
wantErr: "printer.model",
|
|
},
|
|
{
|
|
name: "empty string for an integer",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = \"\"\n",
|
|
wantErr: "printer.retries",
|
|
},
|
|
{
|
|
name: "number for a flag",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[images]\nallow_private_networks = 1\n",
|
|
wantErr: "images.allow_private_networks",
|
|
},
|
|
{
|
|
name: "syntax error names its line",
|
|
body: "[printer]\naddress = \"p\n\"\n",
|
|
wantErr: "line 2",
|
|
},
|
|
{
|
|
name: "tables that differ only in case",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[Printer]\nport = 9101\n",
|
|
wantErr: "keys that differ only in case: Printer and printer",
|
|
},
|
|
{
|
|
name: "settings that differ only in case",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nport = 1000\nPORT = 2000\n",
|
|
wantErr: "keys that differ only in case: printer.PORT and printer.port",
|
|
},
|
|
{
|
|
name: "bare number for a duration",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = 5\n",
|
|
wantErr: `'printer.dial_timeout' expected a duration as a string`,
|
|
},
|
|
{
|
|
name: "number string in another base",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = \"0x10\"\n",
|
|
wantErr: "'printer.retries' expected a whole number in decimal digits",
|
|
},
|
|
{
|
|
name: "flag string other than true or false",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[sentry]\ndebug = \"1\"\n",
|
|
wantErr: "'sentry.debug' expected true or false",
|
|
},
|
|
{
|
|
name: "key with a space is quoted",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n\"retry delay\" = \"1s\"\n",
|
|
wantErr: `unknown keys: printer."retry delay"`,
|
|
},
|
|
{
|
|
name: "blank printer address",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \" \"\n",
|
|
wantErr: "printer.address is required",
|
|
},
|
|
{
|
|
name: "UTF-16 file",
|
|
body: "\xff\xfe[\x00a\x00p\x00i\x00]\x00",
|
|
wantErr: "the file is UTF-16; save it as UTF-8",
|
|
},
|
|
{
|
|
name: "ntfy style",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.format]\nstyle = \"fancy\"\n",
|
|
wantErr: `ntfy.format.style: "fancy" must be one of auto, box, compact`,
|
|
},
|
|
{
|
|
name: "ntfy max lines zero",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.format]\nmax_lines = 0\n",
|
|
wantErr: "ntfy.format.max_lines must be 1-400",
|
|
},
|
|
{
|
|
name: "ntfy max lines too many",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.format]\nmax_lines = 401\n",
|
|
wantErr: "ntfy.format.max_lines must be 1-400",
|
|
},
|
|
{
|
|
name: "ntfy qr",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.format]\nqr = \"some\"\n",
|
|
wantErr: `ntfy.format.qr: "some" must be one of all, first, none`,
|
|
},
|
|
{
|
|
name: "ntfy images not a flag",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.format]\nimages = \"yes\"\n",
|
|
wantErr: "'ntfy.format.images' expected true or false",
|
|
},
|
|
{
|
|
name: "unknown ntfy key",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy]\nserver = \"https://ntfy.sh\"\n",
|
|
wantErr: "unknown keys: ntfy.server",
|
|
},
|
|
{
|
|
name: "marker too long",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.markers]\nbackup = \"[a very long marker]\"\n",
|
|
wantErr: `ntfy.markers.backup: "[a very long marker]" is longer than 16 characters`,
|
|
},
|
|
{
|
|
name: "marker that is not a string",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.markers]\nbackup = 1\n",
|
|
wantErr: "ntfy.markers",
|
|
},
|
|
{
|
|
name: "marker table",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.markers.backup]\nmarker = \"[B]\"\n",
|
|
wantErr: "unknown keys: ntfy.markers.backup",
|
|
},
|
|
{
|
|
name: "marker name with a dot",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.markers]\n\"a.b\" = \"[B]\"\n",
|
|
wantErr: `unknown keys: ntfy.markers."a.b"`,
|
|
},
|
|
{
|
|
name: "markers that differ only in case",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy.markers]\nbackup = \"[b]\"\nBackup = \"[B]\"\n",
|
|
wantErr: "keys that differ only in case: ntfy.markers.Backup and ntfy.markers.backup",
|
|
},
|
|
{
|
|
name: "markers as a value",
|
|
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy]\nmarkers = \"backup=[B]\"\n",
|
|
wantErr: "unknown keys: ntfy.markers",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := config.Load(writeConfig(t, tt.body))
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("Load error = %v, want it to contain %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The documented example must stay loadable: every key in it must exist.
|
|
func TestExampleConfigLoads(t *testing.T) {
|
|
example, err := os.ReadFile(filepath.Join("..", "..", "..", "configs", "config.example.toml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := strings.Replace(string(example), `secret = ""`, `secret = "`+validSecret+`"`, 1)
|
|
body = strings.Replace(body, `address = ""`, `address = "192.168.1.245"`, 1)
|
|
cfg, err := config.Load(writeConfig(t, body))
|
|
if err != nil {
|
|
t.Fatalf("example config: %v", err)
|
|
}
|
|
if def := config.Default(); cfg.WebServer.RateLimit != def.WebServer.RateLimit ||
|
|
cfg.Images.MaxBytes != def.Images.MaxBytes || cfg.Printer.Model != def.Printer.Model ||
|
|
cfg.Format.MaxLines != def.Format.MaxLines || cfg.Printer.BoxLineSpacing != def.Printer.BoxLineSpacing ||
|
|
cfg.Printer.CutFeed != def.Printer.CutFeed || cfg.Printer.Confirm != def.Printer.Confirm ||
|
|
cfg.Format.Timezone != def.Format.Timezone || cfg.Format.TimeLayout != def.Format.TimeLayout ||
|
|
cfg.Ntfy.Format != def.Ntfy.Format || len(cfg.Ntfy.Markers) != 0 ||
|
|
cfg.State != def.State || cfg.Queue != def.Queue || len(cfg.Ntfy.Subscriptions) != 0 {
|
|
t.Error("example config values drifted from the defaults it documents")
|
|
}
|
|
}
|
|
|
|
func TestLoadNtfy(t *testing.T) {
|
|
def := config.Default().Ntfy.Format
|
|
if def != (config.NtfyFormatConfig{Style: "auto", MaxLines: 40, QR: "all", Images: true, Markdown: true, Updates: "print"}) {
|
|
t.Errorf("defaults %+v", def)
|
|
}
|
|
|
|
cfg, err := config.Load(writeConfig(t, `
|
|
[api]
|
|
secret = "`+validSecret+`"
|
|
[printer]
|
|
address = "p"
|
|
[ntfy.format]
|
|
style = "compact"
|
|
max_lines = 400
|
|
qr = "first"
|
|
images = false
|
|
markdown = false
|
|
updates = "skip"
|
|
[ntfy.markers]
|
|
backup = "[B]"
|
|
skull = ""
|
|
"+1" = "+1"
|
|
`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := config.NtfyFormatConfig{Style: "compact", MaxLines: 400, QR: "first", Images: false, Markdown: false, Updates: "skip"}
|
|
if cfg.Ntfy.Format != want {
|
|
t.Errorf("format %+v, want %+v", cfg.Ntfy.Format, want)
|
|
}
|
|
if m := cfg.Ntfy.Markers; len(m) != 3 || m["backup"] != "[B]" || m["skull"] != "" || m["+1"] != "+1" {
|
|
t.Errorf("markers %q", m)
|
|
}
|
|
|
|
t.Run("environment", func(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
|
|
t.Setenv("UBERBRINGER_NTFY_FORMAT_STYLE", "box")
|
|
t.Setenv("UBERBRINGER_NTFY_MARKERS", " Backup = [B] , skull= ")
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Ntfy.Format.Style != "box" {
|
|
t.Errorf("style %q", cfg.Ntfy.Format.Style)
|
|
}
|
|
if m := cfg.Ntfy.Markers; len(m) != 2 || m["backup"] != "[B]" || m["skull"] != "" {
|
|
t.Errorf("markers %q", m)
|
|
}
|
|
})
|
|
|
|
t.Run("environment wins over the file", func(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_NTFY_MARKERS", "tada=\\o/")
|
|
cfg, err := config.Load(writeConfig(t, "[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"p\"\n[ntfy.markers]\nbackup = \"[B]\"\n"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m := cfg.Ntfy.Markers; len(m) != 1 || m["tada"] != `\o/` {
|
|
t.Errorf("markers %q", m)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLoadEnvironmentOverridesFile(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[api]
|
|
secret = "`+validSecret+`"
|
|
[printer]
|
|
address = "file.lan"
|
|
retries = 2
|
|
`)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "env.lan")
|
|
t.Setenv("UBERBRINGER_PRINTER_DIAL_TIMEOUT", "2s")
|
|
t.Setenv("UBERBRINGER_WEBSERVER_PORT", "9090")
|
|
t.Setenv("UBERBRINGER_IMAGES_ALLOW_PRIVATE_NETWORKS", "true")
|
|
t.Setenv("UBERBRINGER_WEBSERVER_TRUSTED_PROXIES", "10.0.0.1, 172.18.0.0/16")
|
|
t.Setenv("UBERBRINGER_NOT_A_KEY", "ignored")
|
|
t.Setenv("UBERBRINGER_PRINTER_RETRIES", "")
|
|
t.Setenv("UBERBRINGER_PRINTER_CONFIRM", "false")
|
|
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Printer.Confirm {
|
|
t.Error("printer.confirm is on, want the environment's false")
|
|
}
|
|
if cfg.Printer.Address != "env.lan" {
|
|
t.Errorf("Address = %q, want the environment's value", cfg.Printer.Address)
|
|
}
|
|
if cfg.Printer.Retries != 2 {
|
|
t.Errorf("Retries = %d, want the file's value, since an empty variable counts as unset", cfg.Printer.Retries)
|
|
}
|
|
if cfg.Printer.DialTimeout != 2*time.Second || cfg.WebServer.Port != 9090 || !cfg.Images.AllowPrivateNetworks {
|
|
t.Errorf("typed environment values not applied: %+v %+v %+v", cfg.Printer, cfg.WebServer, cfg.Images)
|
|
}
|
|
if got := strings.Join(cfg.WebServer.TrustedProxies, "|"); got != "10.0.0.1|172.18.0.0/16" {
|
|
t.Errorf("TrustedProxies = %q, want two trimmed items", got)
|
|
}
|
|
}
|
|
|
|
// A variable named after a section rather than a setting changes nothing.
|
|
func TestLoadIgnoresSectionNamedVariables(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[api]
|
|
secret = "`+validSecret+`"
|
|
[printer]
|
|
address = "file.lan"
|
|
[sentry]
|
|
environment = "staging"
|
|
`)
|
|
t.Setenv("UBERBRINGER_PRINTER", "kitchen")
|
|
t.Setenv("UBERBRINGER_SENTRY", "x")
|
|
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Printer.Address != "file.lan" || cfg.Printer.Port != 9100 {
|
|
t.Errorf("printer = %+v, want the file's address and the default port", cfg.Printer)
|
|
}
|
|
if cfg.Sentry.Environment != "staging" {
|
|
t.Errorf("sentry.environment = %q, want the file's value", cfg.Sentry.Environment)
|
|
}
|
|
}
|
|
|
|
// Some Windows editors start a UTF-8 file with a byte order mark.
|
|
func TestLoadFileWithByteOrderMark(t *testing.T) {
|
|
path := writeConfig(t, "\xef\xbb\xbf[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"bom.lan\"\n")
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Printer.Address != "bom.lan" {
|
|
t.Errorf("Address = %q", cfg.Printer.Address)
|
|
}
|
|
}
|
|
|
|
// An error from a value that may have come from the environment names the
|
|
// variables that were set, and never their values.
|
|
func TestLoadErrorNamesVariables(t *testing.T) {
|
|
path := writeConfig(t, "[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"p\"\n")
|
|
t.Setenv("UBERBRINGER_PRINTER_RETRY_DELAY", "soon")
|
|
_, err := config.Load(path)
|
|
if err == nil {
|
|
t.Fatal("Load accepted a bad retry_delay")
|
|
}
|
|
if msg := err.Error(); !strings.Contains(msg, path) || !strings.Contains(msg, "UBERBRINGER_PRINTER_RETRY_DELAY") {
|
|
t.Errorf("error = %q, want it to name the file and the variable", msg)
|
|
}
|
|
|
|
t.Setenv("UBERBRINGER_API_SECRET", "short-env-secret")
|
|
_, err = config.Load(path)
|
|
if err == nil || strings.Contains(err.Error(), "short-env-secret") {
|
|
t.Errorf("error = %v, want a failure that does not contain the secret", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvironmentOnly(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "192.168.1.245")
|
|
t.Setenv("UBERBRINGER_WEBSERVER_BIND_IP", "0.0.0.0")
|
|
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Printer.Address != "192.168.1.245" || cfg.WebServer.BindIP != "0.0.0.0" || cfg.API.Secret != validSecret {
|
|
t.Errorf("environment not applied: %+v %+v", cfg.Printer, cfg.WebServer)
|
|
}
|
|
if def := config.Default(); cfg.Queue.Size != def.Queue.Size || cfg.Printer.Model != def.Printer.Model {
|
|
t.Error("defaults not applied without a file")
|
|
}
|
|
}
|
|
|
|
// A failed load names the UBERBRINGER_* variables that set nothing, which
|
|
// are likely misspelt, but not their values or UBERBRINGER_CONFIG.
|
|
func TestLoadErrorNamesMisspeltVariables(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_API_SECRETS", validSecret)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
|
|
t.Setenv(config.PathEnvVar, "unused.toml")
|
|
_, err := config.Load("")
|
|
if err == nil {
|
|
t.Fatal("Load accepted a config without api.secret")
|
|
}
|
|
msg := err.Error()
|
|
if !strings.Contains(msg, "variables that set nothing: UBERBRINGER_API_SECRETS") {
|
|
t.Errorf("error = %q, want it to name UBERBRINGER_API_SECRETS", msg)
|
|
}
|
|
if strings.Contains(msg, validSecret) || strings.Contains(msg, config.PathEnvVar) {
|
|
t.Errorf("error = %q, want no values and no %s", msg, config.PathEnvVar)
|
|
}
|
|
}
|
|
|
|
// Strings for numbers are read as decimal, with spaces around them ignored.
|
|
func TestLoadEnvironmentNumbersAreDecimal(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
|
|
t.Setenv("UBERBRINGER_QUEUE_SIZE", "010")
|
|
t.Setenv("UBERBRINGER_WEBSERVER_PORT", " 9090 ")
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Queue.Size != 10 || cfg.WebServer.Port != 9090 {
|
|
t.Errorf("queue.size = %d, webserver.port = %d; want 10 and 9090", cfg.Queue.Size, cfg.WebServer.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoadEnvironmentErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name, key, value, wantErr string
|
|
}{
|
|
{"not a number", "UBERBRINGER_WEBSERVER_PORT", "eighty", "port"},
|
|
{"bad duration", "UBERBRINGER_PRINTER_RETRY_DELAY", "soon", "retry_delay"},
|
|
{"bare number for a duration", "UBERBRINGER_PRINTER_RETRY_DELAY", "5", "retry_delay"},
|
|
{"fails validation", "UBERBRINGER_LOG_LEVEL", "loud", "log.level"},
|
|
{"only spaces", "UBERBRINGER_IMAGES_ALLOWED_HOSTS", " ", "UBERBRINGER_IMAGES_ALLOWED_HOSTS holds only spaces"},
|
|
{"trailing comma", "UBERBRINGER_WEBSERVER_TRUSTED_PROXIES", "10.0.0.1,", "a list item is empty"},
|
|
{"number for a flag", "UBERBRINGER_SENTRY_DEBUG", "1", "expected true or false"},
|
|
{"unknown time zone", "UBERBRINGER_FORMAT_TIMEZONE", "Mars/Olympus", "format.timezone"},
|
|
{"marker without a value", "UBERBRINGER_NTFY_MARKERS", "backup", "expected entries name=value separated by commas"},
|
|
{"marker without a name", "UBERBRINGER_NTFY_MARKERS", "=[B]", "expected entries name=value separated by commas"},
|
|
{"marker given twice", "UBERBRINGER_NTFY_MARKERS", "backup=[B],Backup=[b]", `"backup" is given twice`},
|
|
{"ntfy style", "UBERBRINGER_NTFY_FORMAT_QR", "every", "ntfy.format.qr"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
|
|
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
|
|
t.Setenv(tt.key, tt.value)
|
|
_, err := config.Load("")
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("Load error = %v, want it to contain %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFile(t *testing.T) {
|
|
_, err := config.Load(filepath.Join(t.TempDir(), "nope.toml"))
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestValue(t *testing.T) {
|
|
cfg := config.Default()
|
|
cfg.Images.AllowedHosts = []string{"a.lan", "b.lan"}
|
|
cfg.Ntfy.Markers = map[string]string{"skull": "", "backup": "[B]"}
|
|
tests := []struct {
|
|
key, want, wantErr string
|
|
}{
|
|
{key: "webserver.port", want: "8080"},
|
|
{key: "WebServer.Body_Limit", want: "4194304"},
|
|
{key: "printer.dial_timeout", want: "5s"},
|
|
{key: "webserver.trusted_proxies", want: "127.0.0.1,::1"},
|
|
{key: "images.allowed_hosts", want: "a.lan,b.lan"},
|
|
{key: "images.allow_private_networks", want: "false"},
|
|
{key: "format.timezone", want: "Local"},
|
|
{key: "format.time_layout", want: "02 Jan 15:04"},
|
|
{key: "ntfy.format.style", want: "auto"},
|
|
{key: "ntfy.format.images", want: "true"},
|
|
{key: "ntfy.markers", want: "backup=[B],skull="},
|
|
{key: "ntfy.format", wantErr: `"ntfy.format" is a section`},
|
|
{key: "printer", wantErr: `"printer" is a section`},
|
|
{key: "printer.adress", wantErr: `unknown key "printer.adress"`},
|
|
{key: "printer.port.x", wantErr: `unknown key "printer.port.x"`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.key, func(t *testing.T) {
|
|
got, err := cfg.Value(tt.key)
|
|
if tt.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("Value error = %v, want it to contain %q", err, tt.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || got != tt.want {
|
|
t.Errorf("Value = %q, %v; want %q", got, err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// queue.content_bytes defaults to 64 MiB and takes 0 to 1 GiB.
|
|
func TestLoadContentBytes(t *testing.T) {
|
|
base := "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n"
|
|
cfg, err := config.Load(writeConfig(t, base))
|
|
if err != nil || cfg.Queue.ContentBytes != 64<<20 {
|
|
t.Fatalf("default content_bytes %d, %v; want 67108864", cfg.Queue.ContentBytes, err)
|
|
}
|
|
for _, tt := range []struct {
|
|
value string
|
|
want int64
|
|
ok bool
|
|
}{
|
|
{"0", 0, true},
|
|
{"1073741824", 1 << 30, true},
|
|
{"-1", 0, false},
|
|
{"1073741825", 0, false},
|
|
} {
|
|
cfg, err := config.Load(writeConfig(t, base+"[queue]\ncontent_bytes = "+tt.value+"\n"))
|
|
switch {
|
|
case tt.ok && err != nil:
|
|
t.Errorf("content_bytes = %s: %v", tt.value, err)
|
|
case tt.ok && cfg.Queue.ContentBytes != tt.want:
|
|
t.Errorf("content_bytes = %s gave %d", tt.value, cfg.Queue.ContentBytes)
|
|
case !tt.ok && (err == nil || !strings.Contains(err.Error(), "queue.content_bytes must be 0-1073741824")):
|
|
t.Errorf("content_bytes = %s: err %v, want it refused", tt.value, err)
|
|
}
|
|
}
|
|
}
|