Add printer and scanner driver support

A new "driver" provider installs Windows printer and scanner drivers through
the same catalog, UI and profiles as everything else: stage the package into the
driver store with pnputil, then register it with the print spooler so it turns
up in Add Printer.

Driver names are matched by PREFIX against the names read out of the package's
own INF files. Vendors stamp the version into the name -- "HP Universal Printing
PCL 6 (v7.1.0)" -- so an exact name in a catalog goes stale at the next release,
and a wrong one is unrecoverable without knowing what the package contains. When
a prefix matches nothing, the error lists the names the package actually
provides, which turns a dead end into a one-line fix.

INF parsing handles what real files are, not what the spec suggests: Windows
ships its own printer INFs as UTF-16LE and vendor packages follow suit, so
reading them as UTF-8 finds nothing at all. Verified against every prn*.inf on
a stock Windows install -- that test is what caught the encoding bug, since the
hand-written sample parsed fine.

Scanners are TWAIN/WIA rather than spooler drivers, so there is nothing to
register: leaving driver_match unset means stage-only. Detection falls back to
the uninstall entry, because a TWAIN data source has no spooler record.

Packages come from a driver share rather than the web. Vendor URLs are
version-pinned and several are EULA-gated, so nothing stable can be shipped in a
catalog; a UNC path or local folder is used in place with no download at all,
which is faster and is what a shop wants anyway. $DEPLOYR_DRIVERS names the
share, and an unset variable is reported by name rather than surfacing three
layers down as "no such file".

Ships universal drivers for HP, Xerox, Ricoh, Sharp, Canon, Kyocera, Konica
Minolta, Lexmark, Brother, Toshiba, Epson, Muratec and Zebra, plus scanner
packages for the fi series, ScanSnap, Canon DR, Epson, Brother, Visioneer and
Kodak Alaris. Profiles: @printers, @printers-all, @scanners.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5 committed 2026-08-26 17:47:24 -05:00
1 parent 7daceac077
commit 7ee270e128
14 files changed
+1738 -11

No files matched your search

+265
View File
@@ -0,0 +1,265 @@
package provider
import (
"encoding/binary"
"os"
"path/filepath"
"runtime"
"testing"
"unicode/utf16"
)
// A cut-down but structurally faithful printer INF: %token% names, decorated
// model sections, quoted names containing the separator characters, comments,
// and a [Strings] table.
const sampleINF = `; Sample printer INF
[Version]
Signature="$Windows NT$"
Class=Printer
Provider=%VENDOR%
[Manufacturer]
%VENDOR%=Vendor,NTamd64.10.0,NTarm64.10.0
[Vendor.NTamd64.10.0]
%MODEL_UPD% = UPD_Install, VENDOR_UPD_PCL6 ; the universal driver
"Vendor LaserJet 4000 PCL 6" = LJ_Install, VENDOR_LJ4000
[Vendor.NTarm64.10.0]
%MODEL_UPD% = UPD_Install, VENDOR_UPD_PCL6
[Vendor]
%MODEL_LEGACY% = Legacy_Install, VENDOR_LEGACY
[Strings]
VENDOR = "Vendor Inc"
MODEL_UPD = "Vendor Universal Printing PCL 6 (v7.1.0)"
MODEL_LEGACY = "Vendor Legacy Driver"
`
func writeINF(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "vendor.inf")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestINFDriverNames(t *testing.T) {
models, err := infDriverNames(writeINF(t, sampleINF))
if err != nil {
t.Fatal(err)
}
got := map[string]bool{}
for _, m := range models {
got[m.Name] = true
}
for _, want := range []string{
"Vendor Universal Printing PCL 6 (v7.1.0)", // resolved from %MODEL_UPD%
"Vendor LaserJet 4000 PCL 6", // literal quoted name
"Vendor Legacy Driver", // undecorated model section
} {
if !got[want] {
t.Errorf("missing driver name %q; got %v", want, keys(got))
}
}
if len(models) != 3 {
t.Errorf("expected 3 unique models, got %d: %v", len(models), keys(got))
}
}
func keys(m map[string]bool) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
return out
}
func TestINFHandlesBOMAndComments(t *testing.T) {
// A UTF-8 BOM in front of the first section header is common in vendor
// INFs and would otherwise make [Version] unparseable.
withBOM := string([]byte{0xEF, 0xBB, 0xBF}) + sampleINF
models, err := infDriverNames(writeINF(t, withBOM))
if err != nil {
t.Fatal(err)
}
if len(models) != 3 {
t.Fatalf("BOM broke parsing: got %d models", len(models))
}
}
// Windows ships its own printer INFs as UTF-16LE and many vendor packages do
// the same. Read as UTF-8 they are NUL-separated bytes that match nothing, so
// this is the difference between the provider working and silently finding no
// drivers at all.
func TestINFDecodesUTF16(t *testing.T) {
for _, tc := range []struct {
name string
bom []byte
enc binary.ByteOrder
}{
{"utf16le", []byte{0xFF, 0xFE}, binary.LittleEndian},
{"utf16be", []byte{0xFE, 0xFF}, binary.BigEndian},
} {
t.Run(tc.name, func(t *testing.T) {
units := utf16.Encode([]rune(sampleINF))
data := append([]byte{}, tc.bom...)
for _, u := range units {
b := make([]byte, 2)
tc.enc.PutUint16(b, u)
data = append(data, b...)
}
dir := t.TempDir()
path := filepath.Join(dir, "vendor.inf")
if err := os.WriteFile(path, data, 0o644); err != nil {
t.Fatal(err)
}
models, err := infDriverNames(path)
if err != nil {
t.Fatal(err)
}
if len(models) != 3 {
t.Fatalf("got %d models from a %s INF, want 3", len(models), tc.name)
}
})
}
}
func TestMatchDriverNamesIsPrefixAndCaseInsensitive(t *testing.T) {
models := []infModel{
{Name: "HP Universal Printing PCL 6 (v7.1.0)"},
{Name: "HP Universal Printing PS (v7.1.0)"},
{Name: "HP LaserJet 4250 PCL6"},
}
pcl6 := matchDriverNames(models, "HP Universal Printing PCL 6")
if len(pcl6) != 1 || pcl6[0].Name != "HP Universal Printing PCL 6 (v7.1.0)" {
t.Errorf("prefix match returned %v", pcl6)
}
// The version suffix must not have to be typed, and case must not matter.
if got := matchDriverNames(models, "hp universal printing"); len(got) != 2 {
t.Errorf("case-insensitive prefix returned %d, want 2", len(got))
}
if got := matchDriverNames(models, "Brother"); len(got) != 0 {
t.Errorf("non-matching prefix returned %v", got)
}
if got := matchDriverNames(models, ""); len(got) != 3 {
t.Errorf("empty match should return everything, got %d", len(got))
}
}
func TestFindINFs(t *testing.T) {
dir := t.TempDir()
nested := filepath.Join(dir, "amd64")
if err := os.MkdirAll(nested, 0o755); err != nil {
t.Fatal(err)
}
for _, name := range []string{
filepath.Join(dir, "driver.inf"),
filepath.Join(dir, "driver.cat"),
filepath.Join(nested, "other.INF"), // case must not matter
} {
if err := os.WriteFile(name, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
}
found, err := findINFs(dir)
if err != nil {
t.Fatal(err)
}
if len(found) != 2 {
t.Errorf("found %v, want the two .inf files", found)
}
}
func TestResolveTokensLeavesUnknownTokens(t *testing.T) {
strs := map[string]string{"known": "Resolved"}
if got := resolveTokens("%KNOWN% Driver", strs); got != "Resolved Driver" {
t.Errorf("got %q", got)
}
// A half-resolved name is more debuggable than a blank one.
if got := resolveTokens("%MYSTERY% Driver", strs); got != "%MYSTERY% Driver" {
t.Errorf("got %q", got)
}
if got := resolveTokens("100% Compatible", strs); got != "100% Compatible" {
t.Errorf("stray percent mangled: %q", got)
}
}
func TestSplitEntryIgnoresEqualsInsideQuotes(t *testing.T) {
key, value, ok := splitEntry(`"Driver = Special" = Install_Section, HWID`)
if !ok || key != `"Driver = Special"` || value != "Install_Section, HWID" {
t.Errorf("got key=%q value=%q ok=%v", key, value, ok)
}
}
func TestLocalPathStripsFileScheme(t *testing.T) {
cases := map[string]string{
`file:///C:/drivers/hp`: `C:/drivers/hp`,
`file://server/drivers`: `server/drivers`,
`\\server\drivers\hp`: `\\server\drivers\hp`,
`C:\drivers\hp`: `C:\drivers\hp`,
`https://example.test/x`: `https://example.test/x`,
}
for in, want := range cases {
if got := localPath(in); got != want {
t.Errorf("localPath(%q) = %q, want %q", in, got, want)
}
}
}
func TestIsLocalSource(t *testing.T) {
local := []string{`\\fileserver\drivers\hp`, `C:\drivers`, `C:/drivers`, `file:///C:/d`, `/srv/drivers`}
remote := []string{"https://example.test/d.zip", "http://example.test/d.zip", "example.test/d"}
for _, s := range local {
if !isLocalSource(s) {
t.Errorf("%q should be local", s)
}
}
for _, s := range remote {
if isLocalSource(s) {
t.Errorf("%q should be remote", s)
}
}
}
// TestParseRealWindowsPrinterINFs runs the parser over the printer INFs Windows
// ships, which is the only cheap source of genuine vendor-shaped INF files.
func TestParseRealWindowsPrinterINFs(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("no Windows INF directory")
}
matches, err := filepath.Glob(`C:\Windows\INF\prn*.inf`)
if err != nil || len(matches) == 0 {
t.Skip("no printer INFs on this machine")
}
parsed := 0
for _, path := range matches {
models, err := infDriverNames(path)
if err != nil {
continue // an unreadable INF is a permissions problem, not a parser bug
}
for _, m := range models {
if m.Name == "" {
t.Errorf("%s produced an empty driver name", path)
}
}
if len(models) > 0 {
parsed++
}
}
if parsed == 0 {
t.Errorf("parsed %d printer INFs but found no driver names in any of them", len(matches))
}
t.Logf("extracted driver names from %d of %d Windows printer INFs", parsed, len(matches))
}