MCPcopy Index your code
hub / github.com/tailscale/tailscale / TestDeliverLoopback

Function TestDeliverLoopback

wgengine/netstack/netstack_test.go:1556–1633  ·  view source on GitHub ↗

TestDeliverLoopback verifies that DeliverLoopback correctly re-serializes an outbound packet and delivers it back into gVisor's inbound path.

(t *testing.T)

Source from the content-addressed store, hash-verified

1554// TestDeliverLoopback verifies that DeliverLoopback correctly re-serializes an
1555// outbound packet and delivers it back into gVisor's inbound path.
1556func TestDeliverLoopback(t *testing.T) {
1557 ep := newLinkEndpoint(64, 1280, "", groNotSupported)
1558
1559 // Track delivered packets via a mock dispatcher.
1560 type delivered struct {
1561 proto tcpip.NetworkProtocolNumber
1562 data []byte
1563 }
1564 deliveredCh := make(chan delivered, 4)
1565 ep.Attach(&mockDispatcher{
1566 onDeliverNetworkPacket: func(proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
1567 // Capture the raw bytes from the delivered packet. At this
1568 // point the packet is unparsed — everything is in the
1569 // payload, no headers have been consumed yet.
1570 buf := pkt.ToBuffer()
1571 raw := buf.Flatten()
1572 deliveredCh <- delivered{proto: proto, data: raw}
1573 },
1574 })
1575
1576 t.Run("ipv4", func(t *testing.T) {
1577 selfAddr := netip.MustParseAddrPort("100.64.1.2:8081")
1578 pkt := makeUDP4PacketBuffer(selfAddr, selfAddr)
1579 // Capture what the outbound bytes look like before loopback.
1580 wantLen := pkt.Size()
1581 wantProto := pkt.NetworkProtocolNumber
1582
1583 if !ep.DeliverLoopback(pkt) {
1584 t.Fatal("DeliverLoopback returned false")
1585 }
1586
1587 select {
1588 case got := <-deliveredCh:
1589 if got.proto != wantProto {
1590 t.Errorf("proto = %d, want %d", got.proto, wantProto)
1591 }
1592 if len(got.data) != wantLen {
1593 t.Errorf("data length = %d, want %d", len(got.data), wantLen)
1594 }
1595 case <-time.After(time.Second):
1596 t.Fatal("timeout waiting for loopback delivery")
1597 }
1598 })
1599
1600 t.Run("ipv6", func(t *testing.T) {
1601 selfAddr := netip.MustParseAddrPort("[fd7a:115c:a1e0::123]:8081")
1602 pkt := makeUDP6PacketBuffer(selfAddr, selfAddr)
1603 wantLen := pkt.Size()
1604 wantProto := pkt.NetworkProtocolNumber
1605
1606 if !ep.DeliverLoopback(pkt) {
1607 t.Fatal("DeliverLoopback returned false")
1608 }
1609
1610 select {
1611 case got := <-deliveredCh:
1612 if got.proto != wantProto {
1613 t.Errorf("proto = %d, want %d", got.proto, wantProto)

Callers

nothing calls this directly

Calls 13

newLinkEndpointFunction · 0.85
makeUDP4PacketBufferFunction · 0.85
makeUDP6PacketBufferFunction · 0.85
ToBufferMethod · 0.80
FlattenMethod · 0.80
DeliverLoopbackMethod · 0.80
AfterMethod · 0.80
RunMethod · 0.65
FatalMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.65
AttachMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…