TestDeliverLoopback verifies that DeliverLoopback correctly re-serializes an outbound packet and delivers it back into gVisor's inbound path.
(t *testing.T)
| 1554 | // TestDeliverLoopback verifies that DeliverLoopback correctly re-serializes an |
| 1555 | // outbound packet and delivers it back into gVisor's inbound path. |
| 1556 | func 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) |
nothing calls this directly
no test coverage detected
searching dependent graphs…