From 8ab8ae3c7f2c1be8643801b5079d707a8cd9877c Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Mon, 18 Sep 2023 13:55:47 +0200 Subject: [PATCH] Fix PCI devices with device > 16 --- pci.go | 9 ++++----- pci_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) 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()) + }) +}