MCPcopy Create free account
hub / github.com/encodeous/nylon / Test_setSrcControl

Function Test_setSrcControl

polyamide/conn/sticky_linux_test.go:56–128  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

54}
55
56func Test_setSrcControl(t *testing.T) {
57 t.Run("IPv4", func(t *testing.T) {
58 ep := &StdNetEndpoint{
59 AddrPort: netip.MustParseAddrPort("127.0.0.1:1234"),
60 }
61 setSrc(ep, netip.MustParseAddr("127.0.0.1"), 5)
62
63 control := make([]byte, stickyControlSize)
64
65 setSrcControl(&control, ep)
66
67 hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0]))
68 if hdr.Level != unix.IPPROTO_IP {
69 t.Errorf("unexpected level: %d", hdr.Level)
70 }
71 if hdr.Type != unix.IP_PKTINFO {
72 t.Errorf("unexpected type: %d", hdr.Type)
73 }
74 if uint(hdr.Len) != uint(unix.CmsgLen(int(unsafe.Sizeof(unix.Inet4Pktinfo{})))) {
75 t.Errorf("unexpected length: %d", hdr.Len)
76 }
77 info := (*unix.Inet4Pktinfo)(unsafe.Pointer(&control[unix.CmsgLen(0)]))
78 if info.Spec_dst[0] != 127 || info.Spec_dst[1] != 0 || info.Spec_dst[2] != 0 || info.Spec_dst[3] != 1 {
79 t.Errorf("unexpected address: %v", info.Spec_dst)
80 }
81 if info.Ifindex != 5 {
82 t.Errorf("unexpected ifindex: %d", info.Ifindex)
83 }
84 })
85
86 t.Run("IPv6", func(t *testing.T) {
87 ep := &StdNetEndpoint{
88 AddrPort: netip.MustParseAddrPort("[::1]:1234"),
89 }
90 setSrc(ep, netip.MustParseAddr("::1"), 5)
91
92 control := make([]byte, stickyControlSize)
93
94 setSrcControl(&control, ep)
95
96 hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0]))
97 if hdr.Level != unix.IPPROTO_IPV6 {
98 t.Errorf("unexpected level: %d", hdr.Level)
99 }
100 if hdr.Type != unix.IPV6_PKTINFO {
101 t.Errorf("unexpected type: %d", hdr.Type)
102 }
103 if uint(hdr.Len) != uint(unix.CmsgLen(int(unsafe.Sizeof(unix.Inet6Pktinfo{})))) {
104 t.Errorf("unexpected length: %d", hdr.Len)
105 }
106 info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&control[unix.CmsgLen(0)]))
107 if info.Addr != ep.SrcIP().As16() {
108 t.Errorf("unexpected address: %v", info.Addr)
109 }
110 if info.Ifindex != 5 {
111 t.Errorf("unexpected ifindex: %d", info.Ifindex)
112 }
113 })

Callers

nothing calls this directly

Calls 3

SrcIPMethod · 0.95
setSrcFunction · 0.85
setSrcControlFunction · 0.70

Tested by

no test coverage detected