Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nathanperkins opened this issue Mar 30, 2021 · 0 comments
Open

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

nathanperkins opened this issue Mar 30, 2021 · 0 comments

Comments

@nathanperkins
Copy link

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 { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant