github.com/cilium/cilium@v1.16.2/pkg/testutils/sockets/sockets.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package testsockets
     5  
     6  import (
     7  	"github.com/vishvananda/netlink"
     8  
     9  	"github.com/cilium/cilium/pkg/datapath/sockets"
    10  )
    11  
    12  type MockSockets struct {
    13  	sockets []*MockSocket
    14  }
    15  
    16  type MockSocket struct {
    17  	SockID    netlink.SocketID
    18  	Family    uint8
    19  	Protocol  uint8
    20  	Destroyed bool
    21  }
    22  
    23  func NewMockSockets(sockets []*MockSocket) *MockSockets {
    24  	mocks := &MockSockets{sockets: make([]*MockSocket, len(sockets))}
    25  	for i := range mocks.sockets {
    26  		mocks.sockets[i] = sockets[i]
    27  	}
    28  	return mocks
    29  }
    30  
    31  func (s *MockSockets) Destroy(filter sockets.SocketFilter) error {
    32  	for _, socket := range s.sockets {
    33  		if filter.MatchSocket(socket.SockID) && filter.Family == socket.Family &&
    34  			filter.Protocol == socket.Protocol {
    35  			socket.Destroyed = true
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func (s *MockSocket) Equal(s2 *MockSocket) bool {
    43  	return s.SockID.Destination.Equal(s2.SockID.Destination) &&
    44  		s.SockID.DestinationPort == s2.SockID.DestinationPort &&
    45  		s.Family == s2.Family && s.Protocol == s2.Protocol
    46  }