Skip to content

Commit

Permalink
Auto-update vendor list when option is set
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Nov 24, 2023
1 parent 1bf8794 commit 362ab9f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,30 @@ a static database from https://standards-oui.ieee.org/oui/oui.txt and performing
queries against this file. The file is stored at `~/.config/go-lanscan/oui.txt`

```go
import (
...
"github.com/robgonnella/go-lanscan/pkg/oui"
)

vendorRepo, err := oui.GetDefaultVendorRepo()

if err != nil {
panic(err)
}

arpScanner := scanner.NewArpScanner(
targets,
netInfo,
arpResults,
arpDone,
scanner.WithVendorInfo(true)
scanner.WithVendorInfo(vendorRepo)
)

// or
arpScanner.IncludeVendorInfo(true)
arpScanner.IncludeVendorInfo(vendorRepo)

// or
option := scanner.WithVendorInfo(true)
option := scanner.WithVendorInfo(vendorRepo)
option(arpScanner)
```

Expand Down
3 changes: 3 additions & 0 deletions pkg/scanner/arpscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (s *ArpScanner) SetIdleTimeout(duration time.Duration) {

func (s *ArpScanner) IncludeVendorInfo(repo oui.VendorRepo) {
s.vendorRepo = repo
if err := s.vendorRepo.UpdateVendors(); err != nil {
panic(err)
}
}

func (s *ArpScanner) SetAccuracy(accuracy Accuracy) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/scanner/arpscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ func TestArpScanner(t *testing.T) {

resultChan := make(chan *scanner.ScanResult)

vendorRepo.EXPECT().UpdateVendors().Times(1)

arpScanner := scanner.NewArpScanner(
[]string{"172.17.1.1"},
mockNetInfo,
Expand Down Expand Up @@ -441,6 +443,8 @@ func TestArpScanner(t *testing.T) {

resultChan := make(chan *scanner.ScanResult)

vendorRepo.EXPECT().UpdateVendors().Times(1)

arpScanner := scanner.NewArpScanner(
[]string{"172.17.1.1"},
mockNetInfo,
Expand Down Expand Up @@ -492,6 +496,28 @@ func TestArpScanner(t *testing.T) {
assert.NoError(st, err)
})

t.Run("panics if fails to update static vendors", func(st *testing.T) {
cap := mock_scanner.NewMockPacketCapture(ctrl)
vendorRepo := mock_oui.NewMockVendorRepo(ctrl)
mockNetInfo := mock_network.NewMockNetwork(ctrl)

resultChan := make(chan *scanner.ScanResult)

vendorRepo.EXPECT().UpdateVendors().Return(errors.New("mock update vendors error"))

panicTestFunc := func() {
scanner.NewArpScanner(
[]string{"172.17.1.1"},
mockNetInfo,
resultChan,
scanner.WithPacketCapture(cap),
scanner.WithVendorInfo(vendorRepo),
)
}

assert.Panics(st, panicTestFunc)
})

t.Run("calls request notification callback", func(st *testing.T) {
cap := mock_scanner.NewMockPacketCapture(ctrl)
handle := mock_scanner.NewMockPacketCaptureHandle(ctrl)
Expand Down
1 change: 0 additions & 1 deletion pkg/scanner/fullscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func NewFullScanner(
targets,
netInfo,
internalScanResults,
options...,
)

scanner := &FullScanner{
Expand Down
2 changes: 2 additions & 0 deletions pkg/scanner/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func TestOptions(t *testing.T) {
testPacketCapture := mock_scanner.NewMockPacketCapture(ctrl)
vendorRepo := mock_oui.NewMockVendorRepo(ctrl)

vendorRepo.EXPECT().UpdateVendors().Times(2)

t.Run("sets options", func(st *testing.T) {
netInfo := mock_network.NewMockNetwork(ctrl)

Expand Down

0 comments on commit 362ab9f

Please sign in to comment.