diff --git a/pci.go b/pci.go index 10c144d..ece4cde 100644 --- a/pci.go +++ b/pci.go @@ -9,9 +9,8 @@ import ( ) const ( - pciLength uint16 = 6 - pciPrefix string = "Pci" - pciStringLength int = 12 + pciLength uint16 = 6 + pciPrefix string = "Pci" ) type PCIDevicePath struct { @@ -42,10 +41,10 @@ func (pcp *PCIDevicePath) String() string { } func (pcp *PCIDevicePath) ParseString(raw string) error { - if !checkStringFormat(raw, pciPrefix, pciStringLength) { + if !checkStringFormat(raw, pciPrefix, -1) { 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 { return errors.New("unexpected number of arguments") } diff --git a/pci_test.go b/pci_test.go index 3e8c183..7af174b 100644 --- a/pci_test.go +++ b/pci_test.go @@ -5,6 +5,7 @@ import ( "git.faercol.me/faercol/devicepath" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestToString(t *testing.T) { @@ -20,3 +21,19 @@ func TestToString(t *testing.T) { 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()) + }) +}