Fix PCI devices with device > 16

This commit is contained in:
Melora Hugues 2023-09-18 13:55:47 +02:00
parent 8e31e6f2dc
commit 8ab8ae3c7f
2 changed files with 21 additions and 5 deletions

9
pci.go
View file

@ -9,9 +9,8 @@ import (
) )
const ( const (
pciLength uint16 = 6 pciLength uint16 = 6
pciPrefix string = "Pci" pciPrefix string = "Pci"
pciStringLength int = 12
) )
type PCIDevicePath struct { type PCIDevicePath struct {
@ -42,10 +41,10 @@ func (pcp *PCIDevicePath) String() string {
} }
func (pcp *PCIDevicePath) ParseString(raw string) error { func (pcp *PCIDevicePath) ParseString(raw string) error {
if !checkStringFormat(raw, pciPrefix, pciStringLength) { if !checkStringFormat(raw, pciPrefix, -1) {
return ErrMalformedString return ErrMalformedString
} }
args := strings.Split(raw[len(pciPrefix)+1:pciStringLength-1], ",") args := strings.Split(raw[len(pciPrefix)+1:len(raw)-1], ",")
if len(args) != 2 { if len(args) != 2 {
return errors.New("unexpected number of arguments") return errors.New("unexpected number of arguments")
} }

View file

@ -5,6 +5,7 @@ import (
"git.faercol.me/faercol/devicepath" "git.faercol.me/faercol/devicepath"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestToString(t *testing.T) { func TestToString(t *testing.T) {
@ -20,3 +21,19 @@ func TestToString(t *testing.T) {
assert.Equal(t, expected, dev.String()) assert.Equal(t, expected, dev.String())
}) })
} }
func TestParsePCIString(t *testing.T) {
t.Run("OK device < 16", func(t *testing.T) {
rawStr := "Pci(0x0,0x1)"
var dev devicepath.PCIDevicePath
require.NoError(t, dev.ParseString(rawStr))
assert.Equal(t, rawStr, dev.String())
})
t.Run("OK device > 16", func(t *testing.T) {
rawStr := "Pci(0x1f,0x2)"
var dev devicepath.PCIDevicePath
require.NoError(t, dev.ParseString(rawStr))
assert.Equal(t, rawStr, dev.String())
})
}