Add: proc comm test

This commit is contained in:
ignatella 2024-11-04 18:49:25 +01:00
parent a77a91fb46
commit 7014843161
2 changed files with 12 additions and 4 deletions

View File

@ -198,7 +198,7 @@ func NewMonitor(opts ...MonitorOption) *Monitor {
} }
func (monitor *Monitor) monitor() { func (monitor *Monitor) monitor() {
changesEvents := make([]*MonitorEvent, 0) var changesEvents []*MonitorEvent
for { for {
msgs, err := monitor.conn.Receive() msgs, err := monitor.conn.Receive()
@ -220,7 +220,7 @@ func (monitor *Monitor) monitor() {
GeneratedBy: event, GeneratedBy: event,
Changes: changesEvents, Changes: changesEvents,
} }
changesEvents = make([]*MonitorEvent, 0) changesEvents = nil
break break
} }
@ -333,7 +333,6 @@ func (monitor *Monitor) Close() error {
// handle, for now. // handle, for now.
func (cc *Conn) AddMonitor(monitor *Monitor) (chan *MonitorEvent, error) { func (cc *Conn) AddMonitor(monitor *Monitor) (chan *MonitorEvent, error) {
generationalEventCh, err := cc.AddGenerationalMonitor(monitor) generationalEventCh, err := cc.AddGenerationalMonitor(monitor)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"os"
"path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -46,7 +48,6 @@ func TestMonitor(t *testing.T) {
// Clear all rules at the beginning + end of the test. // Clear all rules at the beginning + end of the test.
c.FlushRuleset() c.FlushRuleset()
defer c.FlushRuleset() defer c.FlushRuleset()
// default to monitor all // default to monitor all
monitor := nftables.NewMonitor() monitor := nftables.NewMonitor()
events, err := c.AddGenerationalMonitor(monitor) events, err := c.AddGenerationalMonitor(monitor)
@ -69,6 +70,14 @@ func TestMonitor(t *testing.T) {
return return
} }
genMsg := event.GeneratedBy.Data.(*nftables.GenMsg)
fileName := filepath.Base(os.Args[0])
if genMsg.ProcComm != fileName {
err = fmt.Errorf("procComm: %s, want: %s", genMsg.ProcComm, fileName)
return
}
for _, change := range event.Changes { for _, change := range event.Changes {
if change.Error != nil { if change.Error != nil {
err = fmt.Errorf("monitor err: %s", change.Error) err = fmt.Errorf("monitor err: %s", change.Error)