Skip to content

Suggestion: provide a New...() for mock/fake client #24

@nathanperkins

Description

@nathanperkins

Hey @mdlayher, thanks for this package and the others you've provided. They've been very helpful.

I want to be able to unit test code that is using arp.Client and see the packets that I'm sending by providing a mock PacketConn. The code in client_test.go is pretty close to what I want to do, but users of this package cannot set the PacketConn on the client without using New(), which requires a net.Interface. I believe creating a net.Interface for my tests would require NET_ADMIN capabilities.

I could mock out the Client as an interface, but then I cannot see the packets that go on the wire.

Is it possible that another New...() function could be created that allows you to set the net.IP, net.HardwareAddr and net.PacketConn directly for use in testing?

This might require adjustments to the Client struct. I believe you only use the net.Interface to get Addrs() during initialization and for getting the HardwareAddr during various functions.

My suggestion would look something like this and I can submit a PR if you're willing to accept it :) I'm iffy on the name, love to hear your suggestions if you don't like it.

type Client struct {
        addr net.HardwareAddr
	ip  net.IP
	p   net.PacketConn
}

func NewForTesting(addr net.HardwareAddr, ip net.IP, p net.PacketConn) *Client { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions