github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/mount/mtd/chips_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mtd 6 7 import ( 8 "errors" 9 "fmt" 10 "strings" 11 "testing" 12 ) 13 14 func TestFindVendor(t *testing.T) { 15 tests := []struct { 16 id VendorID 17 v VendorName 18 e error 19 }{ 20 {0xba, "ZETTADEVICE", nil}, 21 {0x123451234, "", fmt.Errorf("%v: not a known vendor", 0x123451234)}, 22 } 23 24 for _, tt := range tests { 25 v, err := VendorFromID(tt.id) 26 if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.e) { 27 t.Errorf("%v: got %v want %v", tt.id, err, tt.e) 28 } 29 if tt.e != nil { 30 continue 31 } 32 if v.Name() != tt.v { 33 t.Errorf("%v: got (%q) want (%q)", tt.id, v.Name(), tt.v) 34 } 35 36 } 37 } 38 39 func TestFindDevice(t *testing.T) { 40 tests := []struct { 41 v VendorName 42 id ChipID 43 d ChipName 44 e error 45 }{ 46 {"WINBOND", 0x32, "W49V002FA", nil}, 47 // Test a synonym 48 {"AMD", 0x0212, "S25FL004A", nil}, 49 {"ZETTADEVICE", 0xaa66aa44, "", fmt.Errorf("no chip with id 0xaa66aa44 for vendor [\"Zetta\"]")}, 50 } 51 52 for _, tt := range tests { 53 v, err := VendorFromName(tt.v) 54 t.Logf("vformname %v", v) 55 if err != nil { 56 t.Errorf("VendorFromName(%v): got %v, want nil", tt.v, err) 57 } 58 d, err := v.Chip(tt.id) 59 if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.e) { 60 t.Errorf("(%q,%v): got %v want %v", tt.v, tt.id, err, tt.e) 61 } 62 if tt.e != nil { 63 continue 64 } 65 t.Logf("%s", d.Name()) 66 if d.Name() != tt.d { 67 t.Errorf("(%q, %#x): got (%q) want (%q)", tt.v, tt.id, d, tt.d) 68 } 69 70 } 71 } 72 73 func TestChipFromVIDID(t *testing.T) { 74 for _, tt := range []struct { 75 vid VendorID 76 vname VendorName 77 cid ChipID 78 wantChipName ChipName 79 wantChipString string 80 wantError error 81 }{ 82 { 83 vid: 0xBA, 84 vname: "ZETTADEVICE", 85 cid: 0x2012, 86 wantChipName: "ZD25D20", 87 wantChipString: "ZETTADEVICE/ZD25D20: 0 pages, 0 pagesize, 0x0 bytes", 88 }, 89 { 90 vid: 0xDA, 91 vname: "WINBOND", 92 cid: 0x7E1D01, 93 wantChipName: "W29GL032CHL", 94 wantChipString: "WINBOND/W29GL032CHL: 0 pages, 0 pagesize, 0x0 bytes, remarks: /* Uniform Sectors, WP protects Top OR Bottom sector */", 95 }, 96 { 97 vid: 0xAA, 98 vname: "InvalidVendor", 99 cid: 0x2012, 100 wantError: fmt.Errorf("not a known vendor"), 101 }, 102 } { 103 t.Run("Lookup ChipFromVIDDID: "+fmt.Sprintf("VID:CID %d:%d", tt.vid, tt.cid), func(t *testing.T) { 104 chip, err := ChipFromVIDDID(tt.vid, tt.cid) 105 if !errors.Is(err, tt.wantError) { 106 if !strings.Contains(err.Error(), tt.wantError.Error()) { 107 t.Errorf("ChipFromVIDDID(tt.vid, tt.cid)=chip, %q, want chip, %q", err, tt.wantError) 108 } 109 } 110 if err != nil { 111 return 112 } 113 if chip.Name() != tt.wantChipName { 114 t.Errorf("chip.Name() = %s, want %s", chip.Name(), tt.wantChipName) 115 } 116 if chip.String() != tt.wantChipString { 117 t.Errorf("chip.String()=%s, want %s", chip.String(), tt.wantChipString) 118 } 119 if chip.ID() != tt.cid { 120 t.Errorf("chip.ID()= %x, want %x", chip.ID(), tt.cid) 121 } 122 123 }) 124 t.Run("Lookup VendorFromName: "+string(tt.vname), func(t *testing.T) { 125 vendor, err := VendorFromName(tt.vname) 126 if !errors.Is(err, tt.wantError) { 127 if !strings.Contains(err.Error(), tt.wantError.Error()) { 128 t.Errorf("VendorFromName(tt.vname)=vendor, %q, want vendor, %q", err, tt.wantError) 129 } 130 } 131 if err != nil { 132 return 133 } 134 if vendor.Name() != tt.vname { 135 t.Errorf("Vendor name does not match up.") 136 } 137 if vendor.ID() != tt.vid { 138 t.Errorf("vendor.ID()= %x, want %x", vendor.ID(), tt.vid) 139 } 140 }) 141 } 142 }