(t *testing.T)
| 128 | } |
| 129 | |
| 130 | func Test_getSrcFromControl(t *testing.T) { |
| 131 | t.Run("IPv4", func(t *testing.T) { |
| 132 | control := make([]byte, stickyControlSize) |
| 133 | hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0])) |
| 134 | hdr.Level = unix.IPPROTO_IP |
| 135 | hdr.Type = unix.IP_PKTINFO |
| 136 | hdr.SetLen(unix.CmsgLen(int(unsafe.Sizeof(unix.Inet4Pktinfo{})))) |
| 137 | info := (*unix.Inet4Pktinfo)(unsafe.Pointer(&control[unix.CmsgLen(0)])) |
| 138 | info.Spec_dst = [4]byte{127, 0, 0, 1} |
| 139 | info.Ifindex = 5 |
| 140 | |
| 141 | ep := &StdNetEndpoint{} |
| 142 | getSrcFromControl(control, ep) |
| 143 | |
| 144 | if ep.SrcIP() != netip.MustParseAddr("127.0.0.1") { |
| 145 | t.Errorf("unexpected address: %v", ep.SrcIP()) |
| 146 | } |
| 147 | if ep.SrcIfidx() != 5 { |
| 148 | t.Errorf("unexpected ifindex: %d", ep.SrcIfidx()) |
| 149 | } |
| 150 | }) |
| 151 | t.Run("IPv6", func(t *testing.T) { |
| 152 | control := make([]byte, stickyControlSize) |
| 153 | hdr := (*unix.Cmsghdr)(unsafe.Pointer(&control[0])) |
| 154 | hdr.Level = unix.IPPROTO_IPV6 |
| 155 | hdr.Type = unix.IPV6_PKTINFO |
| 156 | hdr.SetLen(unix.CmsgLen(int(unsafe.Sizeof(unix.Inet6Pktinfo{})))) |
| 157 | info := (*unix.Inet6Pktinfo)(unsafe.Pointer(&control[unix.CmsgLen(0)])) |
| 158 | info.Addr = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} |
| 159 | info.Ifindex = 5 |
| 160 | |
| 161 | ep := &StdNetEndpoint{} |
| 162 | getSrcFromControl(control, ep) |
| 163 | |
| 164 | if ep.SrcIP() != netip.MustParseAddr("::1") { |
| 165 | t.Errorf("unexpected address: %v", ep.SrcIP()) |
| 166 | } |
| 167 | if ep.SrcIfidx() != 5 { |
| 168 | t.Errorf("unexpected ifindex: %d", ep.SrcIfidx()) |
| 169 | } |
| 170 | }) |
| 171 | t.Run("ClearOnEmpty", func(t *testing.T) { |
| 172 | var control []byte |
| 173 | ep := &StdNetEndpoint{} |
| 174 | setSrc(ep, netip.MustParseAddr("::1"), 5) |
| 175 | |
| 176 | getSrcFromControl(control, ep) |
| 177 | if ep.SrcIP().IsValid() { |
| 178 | t.Errorf("unexpected address: %v", ep.SrcIP()) |
| 179 | } |
| 180 | if ep.SrcIfidx() != 0 { |
| 181 | t.Errorf("unexpected ifindex: %d", ep.SrcIfidx()) |
| 182 | } |
| 183 | }) |
| 184 | t.Run("Multiple", func(t *testing.T) { |
| 185 | zeroControl := make([]byte, unix.CmsgSpace(0)) |
| 186 | zeroHdr := (*unix.Cmsghdr)(unsafe.Pointer(&zeroControl[0])) |
| 187 | zeroHdr.SetLen(unix.CmsgLen(0)) |
nothing calls this directly
no test coverage detected