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)) }