refactor-for-testability #3
| @@ -16,6 +16,14 @@ type controlMsg[T any] struct { | |||||||
| func parseControlMsg(srcIP byte, srcAddr netip.AddrPort, buf []byte) (any, error) { | func parseControlMsg(srcIP byte, srcAddr netip.AddrPort, buf []byte) (any, error) { | ||||||
| 	switch buf[0] { | 	switch buf[0] { | ||||||
|  |  | ||||||
|  | 	case packetTypeInit: | ||||||
|  | 		packet, err := parsePacketInit(buf) | ||||||
|  | 		return controlMsg[packetInit]{ | ||||||
|  | 			SrcIP:   srcIP, | ||||||
|  | 			SrcAddr: srcAddr, | ||||||
|  | 			Packet:  packet, | ||||||
|  | 		}, err | ||||||
|  |  | ||||||
| 	case packetTypeSyn: | 	case packetTypeSyn: | ||||||
| 		packet, err := parsePacketSyn(buf) | 		packet, err := parsePacketSyn(buf) | ||||||
| 		return controlMsg[packetSyn]{ | 		return controlMsg[packetSyn]{ | ||||||
| @@ -40,14 +48,6 @@ func parseControlMsg(srcIP byte, srcAddr netip.AddrPort, buf []byte) (any, error | |||||||
| 			Packet:  packet, | 			Packet:  packet, | ||||||
| 		}, err | 		}, err | ||||||
|  |  | ||||||
| 	case packetTypeInit: |  | ||||||
| 		packet, err := parsePacketInit(buf) |  | ||||||
| 		return controlMsg[packetInit]{ |  | ||||||
| 			SrcIP:   srcIP, |  | ||||||
| 			SrcAddr: srcAddr, |  | ||||||
| 			Packet:  packet, |  | ||||||
| 		}, err |  | ||||||
|  |  | ||||||
| 	default: | 	default: | ||||||
| 		return nil, errUnknownPacketType | 		return nil, errUnknownPacketType | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -128,7 +128,7 @@ func (s *stateClient) onProbe(msg controlMsg[packetProbe]) peerState { | |||||||
| 	s.syn.TraceID = newTraceID() | 	s.syn.TraceID = newTraceID() | ||||||
| 	s.syn.Direct = true | 	s.syn.Direct = true | ||||||
| 	s.Send(s.staged, s.syn) | 	s.Send(s.staged, s.syn) | ||||||
| 	s.logf("Successful probe.") | 	s.logf("Successful probe to %v.", sent.Addr) | ||||||
| 	return s | 	return s | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,13 +9,6 @@ func addrPort4(a, b, c, d byte, port uint16) netip.AddrPort { | |||||||
| 	return netip.AddrPortFrom(netip.AddrFrom4([4]byte{a, b, c, d}), port) | 	return netip.AddrPortFrom(netip.AddrFrom4([4]byte{a, b, c, d}), port) | ||||||
| } | } | ||||||
|  |  | ||||||
| func assertNil(t *testing.T, err error) { |  | ||||||
| 	t.Helper() |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func assertType[T any](t *testing.T, obj any) T { | func assertType[T any](t *testing.T, obj any) T { | ||||||
| 	t.Helper() | 	t.Helper() | ||||||
| 	x, ok := obj.(T) | 	x, ok := obj.(T) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user