← Back to blog

Testing the forwarding loops with fakes

The two loops that move packets between the TUN device and the UDP socket are the core of the program, and they had no tests. This pull request fixes that. It doesn’t change what the program does.

Why they weren’t tested

The loops lived inside run, and run creates everything itself: it reads the key file, creates the TUN device, and opens the UDP socket. Creating a TUN device needs root, so the only way to exercise the loops was to start the whole program as root, with real interfaces, like in the namespace lab.

That was fine while the loops were simple. But the handshake is about to change how session keys are created and what arrives on the socket, and I want tests in place before that happens.

Splitting setup from forwarding

The loops, and the shutdown logic around them, now live in their own function:

func forward(ctx context.Context, dev device, c conn, s *session.Session) error

run still does the setup, and ends with return forward(ctx, dev, t, s). The difference is in the parameter types. forward doesn’t take a *tun.Device or a *transport.Transport. It takes two small interfaces, listing only the methods it uses:

type device interface {
	Read(p []byte) (int, error)
	Write(p []byte) (int, error)
	Name() string
	Close() error
}

type conn interface {
	Send(payload []byte) error
	Recv(buf []byte) (int, error)
	Close() error
}

In Go, a type satisfies an interface just by having the right methods. There’s no implements keyword. So the real device and transport satisfy these interfaces without any change, and a test can pass in anything else with the same methods.

There was one small change in behavior inside forward. When sealing fails because the counter ran out, it used to cancel run’s context. forward doesn’t own that context, so it now creates its own and cancels that instead. The result is the same: both loops stop and the error is returned.

One fake for both

The tests use a single fake type, fakePipe, for both the device and the transport. It has three channels: in holds packets for forward to read, out collects what forward writes or sends, and done is closed when the fake is closed.

func (p *fakePipe) Read(b []byte) (int, error) {
	select {
	case packet := <-p.in:
		return copy(b, packet), nil
	case <-p.done:
		return 0, errClosed
	}
}

The done case is the important part. The real shutdown works by closing the device and the socket, which makes a blocked Read or Recv return an error. That’s how the loops find out they should stop. If the fake didn’t do the same, forward would block forever in Read, and every test would hang.

Write stores a copy of each packet, because forward reuses its buffers. Without the copy, the test would see the bytes change under it.

What the tests check

Each test starts forward with two fakes and a second Session that plays the peer, with the same keys the other way around. Then it checks one behavior:

  • A packet read from the device comes out sealed on the transport, and the peer can open it.
  • A message from the peer comes out on the device as the original packet.
  • Garbage, an empty datagram, and a tampered message are all dropped.
  • A replayed message is dropped.
  • A keepalive, an empty packet, isn’t written to the device.
  • Cancelling the context makes forward return nil and close both the device and the transport.

Testing that something was dropped is awkward, because you can’t wait for nothing to happen. So these tests send a valid message right after the bad ones. forward handles messages in order, so if the first packet on the device is the valid one, everything before it was dropped.

Every wait has a two-second timeout, so a bug makes a test fail instead of hang. The tests run with -race in CI, which matters here, since forward runs two goroutines that share the session.

What’s next

The handshake messages, using the building blocks from the previous post.